Help us understand the problem. What is going on with this article?

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

(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データの取り込み
3Dデータの”○○.obj”をAndroidstudioに取り込みます。
スクリーンショット 2020-02-16 18.34.28.png

スクリーンショット 2020-02-16 19.05.25.png
成功するとrawフォルダの中にsfbファイルができます。
スクリーンショット 2020-02-16 19.07.00.png

(3)app.gradle
sceneform.asset・・・・の記述は、自動で追加される。

app.gradle
android {
    ・
   ・
  
  //java8が必要
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}

dependencies {
   ・
   ・

    //ARCore関係
    implementation 'com.google.ar:core:1.15.0'
    implementation 'com.google.ar.sceneform.ux:sceneform-ux:1.15.0'
    implementation 'com.google.ar.sceneform:core:1.15.0'
}
sceneform.asset('../../archive/ball.obj',
        'default',
        '../../archive/ball.sfa',
        'src/main/res/raw/ball')

(4)build.gradle
classpath 'com.google.ar.sceneform:plugin:1.15.0'追加

build.gradle
buildscript {
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.3'
        classpath 'com.google.ar.sceneform:plugin:1.15.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

(5)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.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();
                        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);
        }
    }
}

(6)activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <fragment
        android:id="@+id/ar_fragment"
        android:name="com.google.ar.sceneform.ux.ArFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

(7)ソースコード
https://github.com/Tetsuya922/ARCore2

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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした