如何解决Sceneform 1.16 GLTF 更改纹理
我目前正在开发一款 AR 应用,该应用应该能够显示 GLTF/GLB 模型、播放动画并更改其纹理。由于我是渲染和使用 3D 模型的新手,因此我需要一些帮助。
因为 Sceneform 1.15 / Sceneform 1.17.1 不支持动画,所以我需要使用 Sceneform 1.16,因为我在这里可以访问 filamentAsset,它允许我访问 GLB 对象的动画。
现在,如果用户愿意,应用程序应该能够更改对象的外观,这意味着我需要更改 filamentAsset 的 materialInstance 的纹理(Sceneform 1.16 中的 submeshcount 始终为 0,因此纹理更改使用场景形式功能不起作用)。
这是我目前得到的:
我在创建可渲染的 Sceneform 后从保存 filamentAsset 开始:
private void addModel(Anchor anchor,ModelRenderable modelRenderable) {
// Creating a AnchorNode with a specific anchor
AnchorNode anchorNode = new AnchorNode(anchor);
// attaching the anchorNode with the ArFragment
anchorNode.setParent(arCam.getArSceneView().getScene());
// attaching the anchorNode with the TransformableNode
TransformableNode model = new TransformableNode(arCam.getTransformationSystem());
model.setParent(anchorNode);
// attaching the 3d model with the TransformableNode
// that is already attached with the node
model.setRenderable(modelRenderable);
model.select();
// Saving the object as filament asset
// -> Possibility to run animations and change textures
filamentAsset = model.getRenderableInstance().getFilamentAsset();
}
当用户点击按钮时,调用以下方法:
public void changeMaterials() {
textureBC = loadTexture(engine,R.drawable.avocado_basecolor);
MaterialInstance[] materialInstances = filamentAsset.getMaterialInstances();
TextureSampler textureSampler = new TextureSampler();
textureSampler.setWrapModeR(TextureSampler.WrapMode.REPEAT);
textureSampler.setWrapModeS(TextureSampler.WrapMode.REPEAT);
textureSampler.setWrapModeT(TextureSampler.WrapMode.REPEAT);
for (MaterialInstance materialInstance : materialInstances) {
materialInstance.setParameter("baseColorMap",textureBC,textureSampler);
}
}
我的 loadTexture() 方法看起来像这样(基本上是从 filament github 复制的): resourceId 指向一个 PNG
private com.google.android.filament.Texture loadTexture(Engine engine,int resourceId) {
Resources resources = getResources();
Bitmap bitmap = BitmapFactory.decodeResource(resources,resourceId,null);
com.google.android.filament.Texture texture = new com.google.android.filament.Texture.Builder()
.width(bitmap.getWidth())
.height(bitmap.getHeight())
.sampler(Texture.Sampler.SAMPLER_2D)
.format(Texture.InternalFormat.SRGB8_A8) // It is crucial to use an SRGB format.
.levels(0xff) // tells Filament to figure out the number of mip levels
.build(engine);
TextureHelper.setBitmap(engine,texture,bitmap);
texture.generateMipmaps(engine);
return texture;
}
问题是我调用了changeMaterials()方法后,模型全黑了。我做错了什么?
作为例子,我使用了这个 avocado model。作为 loadTexture 方法的输入,我使用了 avocado GLTF 的 image 并更改了一些颜色以检查纹理更改是否有效。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。