java - 如何使用 AR core 将多个 3D 对象组合为一个

标签 java android arcore

这里我在场景中显示多个 3D 对象(查看下图) enter image description here

这里我有 3 个 3D 模型对象,现在可以对 3D 模型对象进行分组。如果我移动一个 3D 对象,那么其他 2 个 3D 对象也需要移动。是否可以在不使用 Unity 和云 anchor 的情况下实现这一目标

下面是我的代码

package com.google.ar.sceneform.samples.hellosceneform;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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 HelloSceneformActivity extends AppCompatActivity {
  private static final String TAG = HelloSceneformActivity.class.getSimpleName();
  private static final double MIN_OPENGL_VERSION = 3.0;

  private ArFragment arFragment;
  private ModelRenderable andyRenderable;

  @Override
  @SuppressWarnings({"AndroidApiChecker", "FutureReturnValueIgnored"})

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (!checkIsSupportedDeviceOrFinish(this)) {
      return;
    }

    setContentView(R.layout.activity_ux);
    arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.ux_fragment);

    // When you build a Renderable, Sceneform loads its resources in the background while returning
    // a CompletableFuture. Call thenAccept(), handle(), or check isDone() before calling get().
    ModelRenderable.builder()
        .setSource(this, R.raw.andy)
        .build()
        .thenAccept(renderable -> andyRenderable = 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 (andyRenderable == null) {
            return;
          }

          // Create the Anchor.
          Anchor anchor = hitResult.createAnchor();
          AnchorNode anchorNode = new AnchorNode(anchor);
          anchorNode.setParent(arFragment.getArSceneView().getScene());

          // Create the transformable andy and add it to the anchor.
          TransformableNode andy = new TransformableNode(arFragment.getTransformationSystem());
          andy.setParent(anchorNode);
          andy.setRenderable(andyRenderable);
          andy.select();
        });
  }


  public static boolean checkIsSupportedDeviceOrFinish(final Activity activity) {
    if (Build.VERSION.SDK_INT < 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;
  }
}

最佳答案

您可以使用 .setParent() 方法将 3D 对象分组在一起。当对象发生变换(移动、旋转或缩放)时,子对象也会发生变换。

太阳系样本在 createSolarSystem() 中演示了这一点。行星成为太阳的 child ,月亮成为地球交点的 child 。

然后,您可以使用 setLocalPosition() 相对于父对象定位对象。如positioning the planet in solarActivity :

Planet planet =
    new Planet(
        this, name, planetScale, orbitDegreesPerSecond, axisTilt, renderable, solarSettings);
planet.setParent(orbit);
planet.setLocalPosition(new Vector3(auFromParent * AU_TO_METERS, 0.0f, 0.0f));

关于java - 如何使用 AR core 将多个 3D 对象组合为一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59766698/

相关文章:

java - 如何从 Volley 响应中读取数组内的数组?

java - Spark SQL 为 JDBC 查询生成错误的上限和下限

java - RuntimeException : Unable to start activity(MainActivity cannot be cast to android. View .View$OnClickListener)

android - 将图像从 URL 加载到图库中

android - java.lang.UnsatisfiedLinkError when loading native library in Android 5.0

android - Play 商店预发布 : limited to devices that support ArCore even though "com.google.ar.core" is set to "optional"

java - JOOQ Orderby 与案例陈述

java - 如何使用 fetch() 从 java servlet 以 JSON 形式发送数组以响应前端?

android - Google Sceneform – 是否已弃用?有替代品吗?

java - Android:需要在没有表面检测的情况下将 AR 中的 3D 对象放置在周围空间中