ARCore를 통해 3D 객체 표시

9001 단어 ARCore
(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를 안드로이드 스튜디오로 가져옵니다.


성공하면 raw 폴더에서 sfb 파일을 생성할 수 있습니다.

(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:plugen: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, 오류 63;, 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

좋은 웹페이지 즐겨찾기