我目前正在玩ThreeJS decals 。我已经能够在我的球体上留下美丽的污点。
这是我用来在球体上“应用”贴花的代码片段。 (我有一些自定义类,但不用担心这个。
// Create sphere
var mainMesh = new THREE.Mesh(
new THREE.SphereGeometry(7, 16, 16),
new THREE.MeshBasicMaterial({ color: 0x00a1fd })
);
// Declare decal material
var decalMaterial = new THREE.MeshPhongMaterial({
color : 0xff0000,
specular : 0x444444,
map : TextureLoader.instance.getTexture('http://threejs.org/examples/textures/decal/decal-diffuse.png'),
normalMap : TextureLoader.instance.getTexture('http://threejs.org/examples/textures/decal/decal-normal.jpg'),
normalScale : new THREE.Vector2( 1, 1 ),
shininess : 30,
transparent : true,
depthTest : true,
depthWrite : false,
polygonOffset : true,
polygonOffsetFactor : -4,
wireframe : false
});
// Create decal itself
var decal = new THREE.Mesh(
new THREE.DecalGeometry(
mainMesh,
new THREE.Vector3(0, 2.5, 3),
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(8, 8, 8),
new THREE.Vector3(1, 1, 1)
),
decalMaterial.clone()
);
// Add mesh + decal + helpers
scene.add(
mainMesh,
new THREE.HemisphereLight(0xffffff, 0, 1),
decal,
new THREE.WireframeHelper(decal, 0xffff00)
);
decal.add(new THREE.BoxHelper(decal, 0xffff00));
现在,我想移动球体上的污点,从而更新贴花的几何形状。
不幸的是,当我调用decal.geometry.computeDecal()
时,贴花的网格不会更新。我找不到任何解决方案。
function moveDecal()
{
decal.translateX(1);
decal.geometry.computeDecal();
};
根据 DecalGeometry
类,函数 computeDecal
已设置为 true 更新顶点、颜色、UV 等所需的各种成员。
this.computeDecal = function() {
// [...]
this.verticesNeedUpdate = true;
this.elementsNeedUpdate = true;
this.morphTargetsNeedUpdate = true;
this.uvsNeedUpdate = true;
this.normalsNeedUpdate = true;
this.colorsNeedUpdate = true;
};
感谢您的帮助! :D
PS:ThreeJS r80
最佳答案
您正在尝试更新几何体的顶点。
您可以更改顶点组件的值,
geometry.vertices[ 0 ].x += 1;
但是你不能添加新的Verites
geometry.vertices.push( new THREE.Vector3( x, y, z ) ); // not allowed
或者分配一个新的顶点数组
geometry.vertices = new_array; // not allowed
在几何图形至少渲染一次之后。
对于其他属性(例如 UV)也是如此。
有关更多信息,请参阅此答案:verticesNeedUpdate in Three.js .
三.js r.80
关于javascript - 更新 DecalGeometry 顶点、UV、,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39619870/