1. yoshikawa922

    No comment

    yoshikawa922
Changes in body
Source | HTML | Preview

3Dオブジェクトの配置、大きさ、向きを調整する。

///カメラの位置に対してアンカーの位置を設定します。
publicSession classsession MainActivity= extendsarFragment.getArSceneView().getSession(); AppCompatActivity {
privatefloat[] staticpos final int{ CAMERA_PERMISSION_CODE0,-0.2f,-1 =}; 0;
privatefloat[] staticrotation final String{0,0,0,1}; CAMERA_PERMISSION = Manifest.permission.CAMERA;
privateAnchor ModelRenderableanchor modelRenderable;= session.createAnchor(new Pose(pos, rotation));

配置

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    ArFragment arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.ar_fragment);


    ModelRenderable.builder()
            .setSource(this, R.raw.ball)
            .build()
            .thenAccept(renderable -> modelRenderable = renderable)
            .exceptionally(
                    throwable -> {
                        Toast toast =
                                Toast.makeText(this, "読み込み失敗", Toast.LENGTH_LONG);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                        return null;
                    });



    if(arFragment != null ) {

        arFragment.setOnTapArPlaneListener(
                (HitResult hitResult, Plane plane, MotionEvent motionEvent) -> {

                    if (modelRenderable == null) {

                        return;
                    }
                   // Anchor anchor = hitResult.createAnchor();
                    //カメラのフォーカスに対してアンカーの位置を設定する
                    Session session = arFragment.getArSceneView().getSession();
                    float[] pos = { 0,-0.2f,-1 };
                    float[] rotation = {0,0,0,1};
                    Anchor anchor =  session.createAnchor(new Pose(pos, rotation));
                    AnchorNode anchorNode = new AnchorNode(anchor);
                    anchorNode.setParent(arFragment.getArSceneView().getScene());
                    TransformableNode model = new TransformableNode(arFragment.getTransformationSystem());
                    model.setRenderable(modelRenderable);

                    //大きさを指定します。
                    model.getScaleController().setMinScale(0.01f);
                    model.getScaleController().setMaxScale(2.0f);
                    //v:width v1:? v2:height
                    model.setLocalScale(new Vector3(0.5f,0f,0.5f));
                    //座標を指定します
                    //model.setLocalPosition(new Vector3(0,0.0f,0));
                    //y軸 縦軸 を中心に180度回転
                    model.setLocalRotation(Quaternion.axisAngle(new Vector3(0,1,0),-120));
                    model.setParent(anchorNode);
                    model.select();



                });
    }

}

@Override
protected void onResume() {
    super.onResume();

    if (ContextCompat.checkSelfPermission(this, CAMERA_PERMISSION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[] {CAMERA_PERMISSION}, CAMERA_PERMISSION_CODE);
    }
}

}

public class MainActivity extends AppCompatActivity {
    private static final int CAMERA_PERMISSION_CODE = 0;
    private static final String CAMERA_PERMISSION = Manifest.permission.CAMERA;
    private ModelRenderable modelRenderable;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ArFragment arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.ar_fragment);
        ModelRenderable.builder()
                .setSource(this, R.raw.ball)
                .build()
                .thenAccept(renderable -> modelRenderable = renderable)
                .exceptionally(
                        throwable -> {
                            Toast toast =
                                    Toast.makeText(this, "読み込み失敗", Toast.LENGTH_LONG);
                            toast.setGravity(Gravity.CENTER, 0, 0);
                            toast.show();
                            return null;
                        });
        if(arFragment != null ) {
            arFragment.setOnTapArPlaneListener(
                    (HitResult hitResult, Plane plane, MotionEvent motionEvent) -> {
                        if (modelRenderable == null) {
                            return;
                        }
                        //カメラの位置に対してアンカーの位置を設定する
                        Session session = arFragment.getArSceneView().getSession();
                        float[] pos = { 0,-0.2f,-1 };
                        float[] rotation = {0,0,0,1};
                        Anchor anchor =  session.createAnchor(new Pose(pos, rotation));
                        AnchorNode anchorNode = new AnchorNode(anchor);
                        anchorNode.setParent(arFragment.getArSceneView().getScene());
                        TransformableNode model = new TransformableNode(arFragment.getTransformationSystem());
                        model.setRenderable(modelRenderable);

                        //大きさを指定します。
                        model.getScaleController().setMinScale(0.01f);
                        model.getScaleController().setMaxScale(2.0f);
                        //v:width v1:? v2:height
                        model.setLocalScale(new Vector3(0.5f,0f,0.5f));
                        //y軸 縦軸 を中心に180度回転
                        model.setLocalRotation(Quaternion.axisAngle(new Vector3(0,1,0),-120));
                        model.setParent(anchorNode);
                        model.select();
                    });
        }
    }
    @Override
    protected void onResume() {
        super.onResume();

        if (ContextCompat.checkSelfPermission(this, CAMERA_PERMISSION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[] {CAMERA_PERMISSION}, CAMERA_PERMISSION_CODE);
        }
    }
}