Skip to content

在淘宝小程序中,无法获取材质 #14

Discussion options

You must be logged in to vote

你这个模型完整获取材质方式如下:

const gltf = await engine.resourceManager.load<GLTFResource>(
    "https://gw.alipayobjects.com/os/OasisHub/267000040/9994/%25E5%25BD%2592%25E6%25A1%25A3.gltf"
  );
const gltfEntity = gltf.defaultSceneRoot;
rootEntity.addChild(gltfEntity);
// 获取有 MeshRenderer 组件的子节点
const meshEntity = gltfEntity.children[0];
const meshRenderer = meshEntity.getComponent(MeshRenderer);
const material = meshRenderer.getMaterial();

原因:

模型加载后,美术给出模型并不一定就是挂在根节点,这个时候最好可以打印下查看子节点,打印 gltf.defaultSceneRoot 可以查看加载模型的结构,如下:

也可以通过 API 直接查找到所有 MeshRenderer,如下:

const result: Array<MeshRenderer> = [];
gltf.defaultSceneRoot.getComponentsIncludeChildren(MeshRenderer, result);

Replies: 0 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by singlecoder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants