1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ARCoreで3Dオブジェクトを表示

Last updated at Posted at 2020-02-16

(1)Manifests
パーミッションとAR機能を必須とするために 
meta-data android:name="com.google.ar.core" android:value="requireed"
”requireed”は必須で、”optional”は必須にしない設定です。

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.ar" android:required="true"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data android:name="com.google.ar.core" android:value="requireed"/>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

(2)3Dデータの取り込み
assetフォルダに blenderなどで、エクスポートした〇〇.glbファイルを保存する

(3)app.gradle

app.gradle

dependencies {


    //ARCore関係
    implementation "com.gorisse.thomas.sceneform:sceneform:1.21.0"
}


(4)MainActitvity
3Dオブジェクトの大きさや向きを調整します。
①setMinScaleとsetMaxScaleをセットします。これがないと、大きさの指定ができません。理由は、まだわかりません。
model.getScaleController().setMinScale(0.01f);
model.getScaleController().setMaxScale(2.0f);
②大きさ指定 setLocalScale(width,?,Height)です。
model.setLocalScale(new Vector3(0.5f,0f,0.5f));
③座標です。ここでは、0,0,0なので、タップした場所になります
model.setLocalPosition(new Vector3(0,0.0f,0));
//y軸 縦軸 を中心に180度回転
④3Dオブジェクトを回転させます。ここでは、y軸(縦軸)を中心に120度回転させます。
x軸の時は、Vector3(1,0,0),-120));とすれば、いいです。 model.setLocalRotation(Quaternion.axisAngle(new Vector3(0,1,0),-120));
model.setParent(anchorNode);
model.select();

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.arfragment);


        ModelRenderable.builder()
                .setSource(this, Uri.parse("pin.glb"))
                .setIsFilamentGltf(true)
                .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();
                        AnchorNode anchorNode = new AnchorNode(anchor);
                        anchorNode.setParent(arFragment.getArSceneView().getScene());

                        TransformableNode model = new TransformableNode(arFragment.getTransformationSystem());
                        model.setParent(anchorNode);
                        model.setRenderable(modelRenderable);


                        //ツイストジェスチャを使用してこのノードを回転させるコントローラーを返します。
                        //model.getRotationController().setRotationRateDegrees(90);

                        //大きさや向きを調整します。
                        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);
        }
    }
}

(5)activity_main

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

 <androidx.fragment.app.FragmentContainerView
        android:id="@+id/arfragment"
        android:name="com.google.ar.sceneform.ux.ArFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?