首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ARCore的Sceneform创建框方法

ARCore的Sceneform创建框方法
EN

Stack Overflow用户
提问于 2018-08-17 20:07:53
回答 2查看 2.7K关注 0票数 1

我正在尝试用ShapeFactory在场景中为Android ARCore创建一个简单的红框。我找不到这样的例子,最接近的例子就是在现有对象上改变材质。下面我想我的代码大部分都是正确的,试图为长方体创建一个节点并附加到锚节点上,实际的长方体材质、几何形状和位置也被创建并附加为节点的可渲染对象。该错误显示从未使用过"boxo“,并且在尝试使用setRenderable()时无法识别"boxo”。

代码语言:javascript
复制
package com.google.ar.sceneform.samples.hellosceneform;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.net.Uri;
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.math.Vector3;
import com.google.ar.sceneform.rendering.Color;
import com.google.ar.sceneform.rendering.MaterialFactory;
import com.google.ar.sceneform.rendering.ModelRenderable;
import com.google.ar.sceneform.rendering.Renderable;
import com.google.ar.sceneform.rendering.ShapeFactory;
import com.google.ar.sceneform.ux.ArFragment;
import com.google.ar.sceneform.ux.TransformableNode;

import static com.google.ar.sceneform.rendering.ShapeFactory.makeCube;

/**
 * This is an example activity that uses the Sceneform UX package to make common AR tasks easier.
 */
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 pipesRenderable;

  @Override
  @SuppressWarnings({"AndroidApiChecker", "FutureReturnValueIgnored"})
  // CompletableFuture requires api level 24
  // FutureReturnValueIgnored is not valid
  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, Uri.parse("pipes.sfb"))
        .build()
        .thenAccept(renderable -> pipesRenderable = renderable)
        .exceptionally(
            throwable -> {
              Toast toast =
                  Toast.makeText(this, "Unable to load pipes renderable", Toast.LENGTH_LONG);
              toast.setGravity(Gravity.CENTER, 0, 0);
              toast.show();
              return null;
            });

    arFragment.setOnTapArPlaneListener(
        (HitResult hitResult, Plane plane, MotionEvent motionEvent) -> {
          if (pipesRenderable == 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 pipes = new TransformableNode(arFragment.getTransformationSystem());
          pipes.setParent(anchorNode);
          pipes.setRenderable(pipesRenderable);
          pipes.select();

          TransformableNode cube = new TransformableNode(arFragment.getTransformationSystem());
          cube.setParent(anchorNode);
         // MaterialFactory
         // ModelRenderable box = ShapeFactory.makeCube(new Vector3(1f, 1f, 1f), new Vector3(0.0f, 0.15f, 0.0f), boxMat);
            //
            MaterialFactory.makeOpaqueWithColor(this, new Color(android.graphics.Color.RED))
                    .thenAccept(
                            material -> {
                               ModelRenderable boxo = ShapeFactory.makeCube(new Vector3(1f, 1f, 1f), new Vector3(1f, 1f, 1f), material); });
          cube.setRenderable(boxo);
          cube.select();

        });
  }

}

  */

  /**
   * Returns false and displays an error message if Sceneform can not run, true if Sceneform can run
   * on this device.
   *
   * <p>Sceneform requires Android N on the device as well as OpenGL 3.0 capabilities.
   *
   * <p>Finishes the activity if Sceneform can not run
   */
  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;
  }
}
EN

回答 2

Stack Overflow用户

发布于 2018-10-10 15:19:56

以下是执行此操作的方法:

代码语言:javascript
复制
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());


                MaterialFactory.makeTransparentWithColor(getApplicationContext(), new Color(244, 244, 244))
                        .thenAccept(
                                material -> {

                                    Vector3 vector3 = new Vector3(0.05f, 0.05f,0.05f);
                                    ModelRenderable model = ShapeFactory.makeCube(vector3,
                                            Vector3.zero(), material);
                                    model.setShadowCaster(false);
                                    model.setShadowReceiver(false);

                                    TransformableNode transformableNode = new TransformableNode(arFragment.getTransformationSystem());
                                    transformableNode.setParent(anchorNode);
                                    transformableNode.setRenderable(model);
                                    transformableNode.select();
            });
}
票数 4
EN

Stack Overflow用户

发布于 2018-08-20 13:21:54

您的变量boxo仅在lambda中有效。将其更改为:

代码语言:javascript
复制
MaterialFactory.makeOpaqueWithColor(this, new Color(android.graphics.Color.RED))
                .thenAccept(
                        material -> {
                           ModelRenderable boxo = ShapeFactory.makeCube(new Vector3(1f, 1f, 1f), new Vector3(1f, 1f, 1f), material); 
                           cube.setRenderable(boxo);
                           cube.select();
                           });

它会起作用的

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51895120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档