我已经成功地在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”,但纹理不会重复,只会拉伸。
如何在Android ArCore Sceneform API中为对象设置重复纹理?
答案 0 :(得分:2)
查看圆柱模型,它的UV贴图为0到1。用于将纹理贴图到网格上。 0,0是纹理的左下角,而1,1是纹理的右上角。仅当模型上的UV坐标> 1.0时,才使用采样器上的包装配置。在这种情况下,它会根据设置进行钳位或重复。由于圆柱体已经被限制为0.1,所以纹理总是被拉伸。
要解决此问题,您可以选择建模自己的圆柱体并根据需要设置UV坐标,或者使用自定义材质在采样前操纵UV坐标。
您可以使用Blender或Maya或其他3D建模工具来制作模型。
自定义材质特定于Sceneform,因此步骤如下:
我使用了周围的平面OBJ模型。不管是什么模型,我们只需要它来加载材料。在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;
}
}
这增加了将模型和材料编译为.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(this,R.raw.material_holder) .build()。thenAccept( modelRenderable-> repeatingMaterial = modelRenderable.getMaterial());
这会将材料存储在成员字段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)));
}