簡體   English   中英

如何在 Android 上的 Sceneform/ARCore 中處理 model 縮放?

[英]How to handle model scaling in Sceneform/ARCore on Android?

我正在嘗試使用來自服務器 Api 的 AR Core Sceneform 加載 3d 模型。 和 model 位置旋轉也來自 API。 我能夠加載 3D model。 但問題是,3D 型號都有不同的比例值。 但看起來很明智,兩種型號的尺寸都是一樣的。 這是我的問題。 我無法以相同大小渲染兩個模型。 我嘗試了很多方法,但沒有用。 不幸的是,互聯網上也沒有遮陽篷。

這是縮放 3D model 的代碼

modelNode.setLocalScale(new Vector3(0.0005f,0.0005f,0.0005));

ModelRenderable.builder()
    .setSource(
            this,
            Uri.fromFile(getModelPath(modelPathArray.get(finalI),jsonObjectDuplicate)))
    .setIsFilamentGltf(true)
    .build()
    .thenAccept(
            modelRenderable -> {
                OfflineARActivity activity = weakActivity.get();
                if (activity != null) {
                    activity.renderable = modelRenderable;

                    // Create the Anchor.
                    Anchor anchor = hitResult.createAnchor();
                    AnchorNode anchorNode = new AnchorNode(anchor);
                    anchorNode.setParent(arFragment.getArSceneView().getScene());

                    TransformableNode modelNode = new TransformableNode(arFragment.getTransformationSystem());
                    modelNode.setParent(anchorNode);


                    //Get Positions From Saved Data
                   // TinyDB tinyDB = new TinyDB(OfflineARActivity.this);
                    String position = tinyDB.getString(modeNameArray.get(finalI)+"Position");
                    String scale = tinyDB.getString(modeNameArray.get(finalI)+"Scale");
                    String rotate = tinyDB.getString(modeNameArray.get(finalI)+"Rotation");
                    String anim = tinyDB.getString(modeNameArray.get(finalI)+"Animation");

                   // Log.v("Plugxr","Scale : "+scale);
                   // Log.v("Plugxr","Model Name Inside : "+modeNameArray.get(finalI));





                    // Positions
                    try {
                        JSONObject jsonObject = new JSONObject(position);

                        String px = jsonObject.getString("x");
                        String py = jsonObject.getString("y");
                        String pz = jsonObject.getString("z");
                        Vector3 positions = new Vector3(Float.parseFloat(px),Float.parseFloat(py),Float.parseFloat(pz));

                        modelNode.setLocalPosition(positions);

                        Log.v("Plugxr","Positions : "+positions.toString());
                        Log.v("Plugxr","Positions : "+modelNode.getLocalPosition());
                        Log.v("Plugxr","Positions : "+modeNameArray.get(finalI));


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }



                    // Scaling
                    try {
                        JSONObject jsonObject = new JSONObject(scale);

                        String sx = jsonObject.getString("x");
                        String sy = jsonObject.getString("y");
                        String sz = jsonObject.getString("z");
                        Vector3 scaling = new Vector3(Float.parseFloat(sx),Float.parseFloat(sy),Float.parseFloat(sz));

                       // Log.v("Plugxr","Scale : "+scaling);
                       // Log.v("Plugxr","Scale : "+modeNameArray.get(finalI));

                        // Bounding Box





                        /*Vector3 finalSize =
                                    new Vector3(
                                            (float) (Math.round((size.x * transformableNodeScale.x * 100) * 10) / 10.0),
                                            (float) (Math.round((size.y * transformableNodeScale.y * 100) * 10) / 10.0),
                                            (float) (Math.round((size.z * transformableNodeScale.z * 100) * 10) / 10.0));*/

                        ScaleController scaleController = modelNode.getScaleController();
                        scaleController.setMaxScale((scaling.x+scaling.y+scaling.z)/3);
                        scaleController.setMinScale(((scaling.x+scaling.y+scaling.z)/3)-0.00001f);


                        /*Box boundingBox = (Box) modelRenderable.getCollisionShape();
                        if (boundingBox != null) {
                            Vector3 boundingBoxSize = boundingBox.getSize();
                            //float maxExtent = Math.max(boundingBoxSize.x, Math.max(boundingBoxSize.y, boundingBoxSize.z));
                            //float targetSize = 0.1f; // Whatever size you want.
                           // float scaleBounfing = targetSize / maxExtent;


                            Vector3 finalSize =
                                    new Vector3(
                                            (float) (Math.round((boundingBoxSize.x * modelNode.getWorldScale().x * 100) * 10) / 10.0),
                                            (float) (Math.round((boundingBoxSize.y * modelNode.getWorldScale().y * 100) * 10) / 10.0),
                                            (float) (Math.round((boundingBoxSize.z * modelNode.getWorldScale().z * 100) * 10) / 10.0));

                            modelNode.setLocalScale(new Vector3(finalSize.x,finalSize.y,finalSize.z));




                        }*/

                        //Log.v("Plugxr","Scale1 : "+modelNode.getLocalScale());

                        modelNode.setLocalScale(new Vector3(scaling.x,scaling.y,scaling.z));

                     //   Log.v("Plugxr","Scale2 : "+modelNode.getLocalScale() );
                       // modelNode.setLocalScale(scaling);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                    // Rotation
                    try {
                        JSONObject jsonObject = new JSONObject(rotate);

                        String rx = jsonObject.getString("x");
                        String ry = jsonObject.getString("y");
                        String rz = jsonObject.getString("z");

                        Quaternion qrx = Quaternion.eulerAngles(new Vector3(Float.parseFloat(rx) - 90, Float.parseFloat("0.0"), Float.parseFloat("0.0")));
                        Quaternion qry = Quaternion.eulerAngles(new Vector3(Float.parseFloat("0.0"), Float.parseFloat(ry), Float.parseFloat(rz)));
                        modelNode.setLocalRotation(Quaternion.multiply(qrx,qry));


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                    modelNode.setRenderable(modelRenderable);
                    modelNode.select();
                    // Set Model Name
                    //modelNode.setName(modelPathArray.get(finalI).getName());
                    // Add model name to modelArray
                    //modelsArray.add(modelPathArray.get(finalI).getName());



                    // Later
                    FilamentAsset filamentAsset = modelNode.getRenderableInstance().getFilamentAsset();
                    if (filamentAsset.getAnimator().getAnimationCount() > 0) {
                        animators.add(new AnimationInstance(filamentAsset.getAnimator(), 0, System.nanoTime()));
                    }

                    Color color = colors.get(nextColor);
                    nextColor++;
                    for (int j = 0; j < modelRenderable.getSubmeshCount(); ++j) {
                        Material material = modelRenderable.getMaterial(j);
                        material.setFloat4("baseColorFactor", color);
                    }


                }
            })
    .exceptionally(
            throwable -> {
                /*Toast toast =
                        Toast.makeText(this, "Unable to load model"+modeNameArray.get(finalI), Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();*/
                return null;
            });

嘗試這個:

Anchor anchor = hitResult.createAnchor();
AnchorNode anchorNode = new AnchorNode(anchor);

node.getScaleController().setMinScale(2.0f);
node.getScaleController().setMaxScale(3.0f);

node.setLocalScale(new Vector3(2.5f, 2.5f, 2.5f));
node.setParent(anchorNode);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM