[英]Three.js - update Mesh Colour of Object3D
I have a Three.js Object3D
that I would like to update the colour of.我有一个 Three.js
Object3D
,我想更新它的颜色。
I originally construct the Mesh using a MeshStandardMaterial
and add it into the scene.我最初使用
MeshStandardMaterial
构建网格并将其添加到场景中。 Later, I look up this object by ID and retrieve an Object3D
from the scene.后来,我通过 ID 查找这个对象并从场景中检索一个
Object3D
。 How can I update the colour of the Mesh at this point - is it possible?此时如何更新网格的颜色 - 可能吗?
If I have to delete the 3D object and add an entirely new one - is there a way to retrieve the geometry that was originally used to construct it from the Object3D
itself?如果我必须删除 3D 对象并添加一个全新的对象 - 有没有办法从
Object3D
本身检索最初用于构造它的几何图形? I would rather not store a mapping of the original geometry to an object's ID, as this would make the code messy.我宁愿不存储原始几何图形到对象 ID 的映射,因为这会使代码变得混乱。 One option I can think of is to store the geometry on
Object3D.UserData
, but this is again suboptimal as currently the Meshes are constructed elsewhere - and then added to the scene (user data is available only once it has been added to the scene).我能想到的一种选择是将几何体存储在
Object3D.UserData
,但这又是次优的,因为当前网格是在其他地方构建的 - 然后添加到场景中(用户数据仅在添加到场景后才可用) .
I find a way to add texture
into a Object3D
.我找到了一种将
texture
添加到Object3D
。
You can map children
property to update material of meshes.您可以映射
children
属性来更新网格的材质。
function onLoad(object) {
// `children` is an array of `Mesh` than contain 1 or more Meshes
object.children.forEach((mesh) => {
if (!mesh) return;
const material = mesh.material;
material.map = ghostColorTexture;
material.normalMap = ghostNormalTexture;
});
}
const objLoader = new OBJLoader();
objLoader.load(
'/models/ghost/model/ghost.obj',
onLoad,
undefined,
function (error) {
console.error('error', error);
}
);
An Object3D
can be a Mesh
that's composed of a Geometry
and a Material
. Object3D
可以是由Geometry
和Material
组成的Mesh
。 If you want to update the color, simply select that material, and assign a new color to it:如果要更新颜色,只需选择该材质,并为其指定新颜色:
object.material.color.setHex(0xff9900); // Sets to orange
In the docs, you can see that .color
is a property of MeshStandardMaterial
and .material
is a property of Mesh
在文档中,您可以看到
.color
是MeshStandardMaterial
的属性,而.material
是Mesh
的属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.