java 如何在Android上的Sceneform/ARCore中处理模型缩放?

x6h2sr28  于 2023-03-16  发布在  Java
关注(0)|答案(2)|浏览(142)

我正在尝试从服务器API使用AR核心场景加载3D模型。模型位置旋转也来自API。我能够加载3D模型。但问题是,两个3D模型有不同的比例值。但看起来明智的两个模型大小是相同的。这是我的问题。我不能渲染两个模型在相同的大小。我尝试了许多方法,但没有使用。不幸的是,没有awnser在互联网上也。
这是缩放3D模型的代码
设置局部尺度(新向量3(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;
            });
jfgube3f

jfgube3f1#

试试这个:

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);
ozxc1zmp

ozxc1zmp2#

使用LocalScale属性进行放大或缩小。
Kotlin用户代码

AnchorNode(hitResult.createAnchor()).apply {
     renderable = model

     //reduce the scaling by 0.1F

     localScale = Vector3(0.1F,0.1F,0.1F)
  }

相关问题