Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?
@yoshikawa922

ARCoreで3Dオブジェクト2

More than 1 year has passed since last update.

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

ユーザーの前0.5メートルの位置を見つけます。

カメラの位置に対してアンカーの位置を設定します。
SceneView sceneView = arFragment.getArSceneView();
Vector3 cameraPos = sceneView.getScene().getCamera().getWorldPosition();
Vector3 cameraForward = sceneView.getScene().getCamera().getForward();
Vector3 position = Vector3.add(cameraPos, cameraForward.scaled(0.5f));

大きさを調整

model.getScaleController().setMinScale(0.01f);
model.getScaleController().setMaxScale(2.0f);
model.setLocalScale(new Vector3(0.5f,0f,0.5f));

向きを調整

y軸(縦軸) を中心に180度回転
model.setLocalRotation(Quaternion.axisAngle(new Vector3(0,1,0),-120));

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;
                        }
                        //ユーザーの前0.5メートルの位置を見つけます。
                        Session session = arFragment.getArSceneView().getSession();
                        SceneView sceneView = arFragment.getArSceneView();
                        Vector3 cameraPos = sceneView.getScene().getCamera().getWorldPosition();
                        Vector3 cameraForward = sceneView.getScene().getCamera().getForward();
                        Vector3 position = Vector3.add(cameraPos, cameraForward.scaled(0.5f));
                        Pose pose = Pose.makeTranslation(position.x, position.y, position.z);
                        Anchor anchor = session.createAnchor(pose);

                        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);
        }
    }
}
0
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
yoshikawa922
高校の教員をしています。2017年からモバイルアプリに興味をもち、Androidアプリ、iOSアプリを勉強しながら作っています。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
0
Help us understand the problem. What is going on with this article?