这是我的主要代码
package com.example.ar;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.widget.Toast;
import com.google.ar.core.Anchor;
import com.google.ar.core.HitResult;
import com.google.ar.core.Plane;
import com.google.ar.sceneform.AnchorNode;
import com.google.ar.sceneform.rendering.ModelRenderable;
import com.google.ar.sceneform.ux.ArFragment;
import com.google.ar.sceneform.ux.TransformableNode;
public class MainActivity extends AppCompatActivity
{
private static final String TAG = MainActivity.class.getSimpleName();
private static final double MIN_OPENGL_VERSION = 3.0;
ArFragment arFragment;
ModelRenderable lampPostRenderable;
@Override
@SuppressWarnings({"AndroidApiChecker", "FutureReturnValueIgnored"})
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!checkIsSupportedDeviceOrFinish(this))
{
return;
}
setContentView(R.layout.activity_main);
arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.ux_fragment);
ModelRenderable.builder().setSource(this, Uri.parse("lamppost.sfb")).build()
.thenAccept(renderable -> lampPostRenderable = renderable)
.exceptionally(throwable ->
{
Toast toast = Toast.makeText(this, "Unable to load andy renderable",
Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return null;
}
);
arFragment.setOnTapArPlaneListener((HitResult hitresult, Plane plane, MotionEvent motionevent) -> {
if (lampPostRenderable == null){
return; }
Anchor anchor = hitresult.createAnchor();
AnchorNode anchorNode = new AnchorNode(anchor);
anchorNode.setParent(arFragment.getArSceneView().getScene());
TransformableNode lamp = new TransformableNode(arFragment.getTransformationSystem());
lamp.setParent(anchorNode);
lamp.setRenderable(lampPostRenderable);
lamp.select();
});
}
public static boolean checkIsSupportedDeviceOrFinish(final Activity activity)
{
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N)
{
Log.e(TAG, "Sceneform requires Android N or later");
Toast.makeText(activity, "Sceneform requires Android N or later", Toast.LENGTH_LONG).show();
activity.finish();
return false;
}
String openGlVersionString =
((ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE))
.getDeviceConfigurationInfo().getGlEsVersion();
if (Double.parseDouble(openGlVersionString) < MIN_OPENGL_VERSION)
{
Log.e(TAG, "Sceneform requires OpenGL ES 3.0 later");
Toast.makeText(activity, "Sceneform requires OpenGL ES 3.0 or later", Toast.LENGTH_LONG).show();
activity.finish();
return false;
}
return true;
}
}
我也已经包含了
<meta-data
android:name="com.google.ar.core"
android:value="required" />
在Android清单文件中。
Gradle模块:app拥有
implementation 'com.google.ar.sceneform.ux:sceneform-ux:1.15.0'
Gradle项目具备以下特点:
classpath 'com.google.ar.sceneform:plugin:1.5.0'
在依赖项中。
我的sampledata文件夹中有一个命名为lamppost的obj和mtl文件。
打开应用程序时,出现错误显示为“无法创建AR会话”,出现黑屏幕和手持设备旋转的动画。 这是它的外观。