- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我已经成功地在 AR 场景中的两个向量之间画了一条线。
我的代码:
private void addLineBetweenPoints(Scene scene, Vector3 from, Vector3 to) {
// prepare an anchor position
Quaternion camQ = scene.getCamera().getWorldRotation();
float[] f1 = new float[]{to.x, to.y, to.z};
float[] f2 = new float[]{camQ.x, camQ.y, camQ.z, camQ.w};
Pose anchorPose = new Pose(f1, f2);
// make an ARCore Anchor
Anchor anchor = mCallback.getSession().createAnchor(anchorPose);
// Node that is automatically positioned in world space based on the ARCore Anchor.
AnchorNode anchorNode = new AnchorNode(anchor);
anchorNode.setParent(scene);
// Compute a line's length
float lineLength = Vector3.subtract(from, to).length();
// Prepare a sampler
Texture.Sampler sampler = Texture.Sampler.builder()
.setMinFilter(Texture.Sampler.MinFilter.LINEAR_MIPMAP_LINEAR)
.setMagFilter(Texture.Sampler.MagFilter.LINEAR)
.setWrapModeR(Texture.Sampler.WrapMode.REPEAT)
.setWrapModeS(Texture.Sampler.WrapMode.REPEAT)
.setWrapModeT(Texture.Sampler.WrapMode.REPEAT)
.build();
// 1. Make a texture
Texture.builder()
.setSource(() -> getContext().getAssets().open("textures/aim_line.png"))
.setSampler(sampler)
.build().thenAccept(texture -> {
// 2. make a material by the texture
MaterialFactory.makeTransparentWithTexture(getContext(), texture)
.thenAccept(material -> {
// 3. make a model by the material
ModelRenderable model = ShapeFactory.makeCylinder(0.0025f, lineLength,
new Vector3(0f, lineLength / 2, 0f), material);
model.setShadowReceiver(false);
model.setShadowCaster(false);
// make node
Node node = new Node();
node.setRenderable(model);
node.setParent(anchorNode);
// set rotation
final Vector3 difference = Vector3.subtract(to, from);
final Vector3 directionFromTopToBottom = difference.normalized();
final Quaternion rotationFromAToB =
Quaternion.lookRotation(directionFromTopToBottom, Vector3.up());
node.setWorldRotation(Quaternion.multiply(rotationFromAToB,
Quaternion.axisAngle(new Vector3(1.0f, 0.0f, 0.0f), 90)));
});
});
}
它工作得很好,但我有一个错误的纹理。在文件“textures/aim_line.png”中包含 PNG:(线的一半是透明的,另一半是橙色的。)
我目前的结果:
但我期待下一个结果:
因此,我使用了写入“WrapMode.REPEAT”的采样器,但纹理不会重复,只会被拉伸(stretch)。
如何在 Android ArCore Sceneform API 中为对象设置重复纹理?
最佳答案
看看圆柱体模型,它有一个 0 到 1 的 UV 贴图。这用于将纹理映射到网格上。 0,0 是纹理的左下角,1,1 是右上角。采样器上的环绕配置仅在模型上的 UV 坐标 > 1.0 时使用。在这种情况下,它会根据设置进行钳制或重复。由于圆柱体已被限制为 0,1,因此纹理始终被拉伸(stretch)。
解决此问题的替代方法是为您自己的圆柱体建模并根据需要设置 UV 坐标,或者使用自定义 Material 在采样前操纵 UV 坐标。
您可以使用 Blender 或 Maya 或其他 3D 建模工具制作模型。
自定义 Material 特定于 Sceneform,因此步骤如下:
我使用了我周围的飞机 OBJ 模型。模型是什么并不重要,我们只需要它来加载 Material 。在 app/sampledata/materials
中创建一个名为 dummy.obj
o Plane
v 0.500000 0.500000 0.000000
v -0.500000 0.500000 0.000000
v 0.500000 -0.500000 0.000000
v -0.500000 -0.500000 0.000000
vt 0.000000 1.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vn 0.0000 0.0000 1.0000
usemtl None
s off
f 1/1/1 2/2/1 4/3/1 3/4/1
custom material reference描述了 repeating_texture.mat
中的每个元素:
// Sample material for repeating a texture.
//
// the repeating factor is given as repeat_x,
// repeat_y as a factor multipled by the UV
// coordinate.
material {
"name" : "RepeatingTexture",
parameters : [
{
type : sampler2d,
name : texture
},
{
type: float,
name:"repeat_x"
},
{
type: float,
name: "repeat_y"
}
],
requires : [
"position",
"uv0"
],
}
fragment {
void material(inout MaterialInputs material) {
prepareMaterial(material);
vec2 uv = getUV0();
uv.x = uv.x * materialParams.repeat_x;
uv.y = uv.y * materialParams.repeat_y;
material.baseColor = texture(materialParams_texture, uv).rgba;
}
}
这增加了将模型和 Material 编译成.sfb
文件的步骤。在 app/build.gradle
添加:
apply plugin: 'com.google.ar.sceneform.plugin'
sceneform.asset('sampledata/materials/dummy.obj',
"sampledata/materials/repeating_texture.mat",
'sampledata/materials/dummy.sfa',
'src/main/res/raw/material_holder')
您还需要将 Sceneform 添加到顶级 build.gradle
中的 buildscript 类路径:
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath 'com.google.ar.sceneform:plugin:1.5.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
在 onCreate()
中调用:
ModelRenderable.builder().setSource(这个, R.raw.material_holder) .build().thenAccept( modelRenderable -> repeatingMaterial = modelRenderable.getMaterial());
这将 Material 存储在成员字段 repeatingMaterial
中。
将您的原始代码修改为:
private void addLineBetweenPoints(AnchorNode from, Vector3 to) {
// Compute a line's length
float lineLength = Vector3.subtract(from.getWorldPosition(), to).length();
// repeat the pattern every 10cm
float lengthCM = lineLength * 100;
repeatingMaterial.setFloat("repeat_x", lengthCM/10);
repeatingMaterial.setFloat("repeat_y", lengthCM/10);
// 3. make a model by the material
ModelRenderable model = ShapeFactory.makeCylinder(0.0025f, lineLength,
new Vector3(0f, lineLength / 2, 0f), repeatingMaterial);
model.setShadowReceiver(false);
model.setShadowCaster(false);
// make node
Node node = new Node();
node.setRenderable(model);
node.setParent(from);
// set rotation
final Vector3 difference = Vector3.subtract(from.getWorldPosition(), to);
final Vector3 directionFromTopToBottom = difference.normalized();
final Quaternion rotationFromAToB =
Quaternion.lookRotation(directionFromTopToBottom, Vector3.up());
node.setWorldRotation(Quaternion.multiply(rotationFromAToB,
Quaternion.axisAngle(new Vector3(1.0f, 0.0f, 0.0f), 90)));
}
关于android - 如何在 Android ArCore Sceneform API 中为对象设置重复纹理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53437128/
我正在扩展我非常有限的 ARCore 知识。 我的问题与 this question 相似(但不同) 我想知道我的设备相机节点是否与我的其他节点相交/重叠,但到目前为止我还没有任何运气 我正在尝试这样
我已经使用 SceneView 加载 3D 模型将近一年了,但我一直不明白是什么导致了这种泄漏。我会实现 LeakCanary,但只有这一次泄漏,因为我不知道如何解决这个问题。 但现在我想弄清问题的根
我想更新与每帧更新的 anchor 相关的数据。如何从 ArSceneView 获取每一帧的帧? 最佳答案 ArSceneView 在绘制场景之前更新 ARCore Frame 对象。您可以通过从使用
我使用了 SceneForm SDK 的示例,但是在这个示例中,当我们将对象放在检测到的地板上时,我们不能在墙上移动。如何解决? arFragment.setOnTapArPlaneListener(
private AnchorNode anchorNode; private void removeAnchorNode(Node nodeRemove) { //Remove an
关闭。这个问题需要更多 focused .它目前不接受答案。 想改进这个问题?更新问题,使其仅关注一个问题 editing this post . 2年前关闭。 Improve this questi
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提出有关书籍、工具、软件库等建议的问题。您可以编辑问题,以便可以用事实和引用来回答它。 9 个月
默认情况下,Arcore/Sceneform 使用房间作为 HDR 环境图像。我该如何更换它? Default HDR Room 最佳答案 目前无法设置自己的环境贴图。如果您想跟踪开发,请跟踪此功能请
基于 ARcore 开发指南 ( Lighting Estimation developer guide for Android ),我正在尝试禁用 Scenceform 中的灯光。但什么也没发生。
我在我的应用程序中使用 AR Core 作为 3D 查看器。我没有将 Sceneform 用于 AR 渲染,而是用于渲染 3D 模型。我面临的问题是如何使用滑动手势或触摸事件对模型进行 360 度旋转
我正在构建一个使用 AR 的 Android 应用程序,我正在为此使用 sceneform 包。我显示一个 3D 模型,其 ModelRenderable 是通过从外部 URI 下载相应的 .sfb
我知道 ARCore 尚不支持步行等 3D 动画,但我如何为节点的旋转设置动画? 我知道我可以设置 LocalRotation 或 WorldRotation,但如何以流畅的方式连续制作此动画? 最佳
我添加了一项基于 Google 的 Codelabs 教程 ( https://codelabs.developers.google.com/codelabs/sceneform-intro/inde
ARCore sceneform 示例项目“hellosceneform”很酷并且运行良好。 问题是需要四处移动手机以获得放置 anchor 的表面。太慢了。 我的应用程序不需要任何东西显示在垂直平面
如果我有两个单独的 Sceneform 可变换节点,它们有自己的 AnchorNodes 和 Anchors,我如何将它们“链接”在一起,以便用户的手势(捏合和拖动)对它们产生相同的效果?如果我将它们
我想通过它的置信度值对 PointCloud 进行着色。 首先,我创建了三个 FloatBuffer,我在其中放置基于阈值的单个点。 private FloatBuffer makeFLoatBuff
我一直在努力将垂直放置的 3d 模型 GLB 格式正确放置在垂直表面上。 Just to be clear, I am not referring to the difficulty of ident
我有一种在 AR 环境中构建和生成 3D Assets 的方法。 3D 资源是存储在元数据文件夹中的 .sfb 文件。我的元数据文件夹中有多个 3D 资源,我希望在调用此函数时随机选择一个资源。这是我
我的理解是有几种环境支持 ARCore,Unity 和 Sceneform SDK 是其中的一些选项。 我想知道除了一个在 Java 中,另一个在 C# 中之外,它们之间有什么不同?除了语言偏好之外,
我正在尝试开发一款 AR 应用程序,以帮助视障人士改善他们使用计算机的条件。 我正在研究 AR 如何帮助 HCI 解决视觉障碍问题,因此,该应用程序使用 WebRTC 来使用 Sceneform 在
我是一名优秀的程序员,十分优秀!