0
忍受我,我是一个总的JavaScript和三个newby。我有这段代码在这儿,我想推动网格的对象的列表:推三网格时的怪异行为
geometry = new THREE.BoxGeometry(1, 50, 1);
for (var i = 0, l = geometry.faces.length; i < l; i ++) {
var face = geometry.faces[ i ];
face.vertexColors[ 0 ] = new THREE.Color().setHSL(Math.random() * 0.3 + 0.5, 0.75, Math.random() * 0.25 + 0.75);
face.vertexColors[ 1 ] = new THREE.Color().setHSL(Math.random() * 0.3 + 0.5, 0.75, Math.random() * 0.25 + 0.75);
face.vertexColors[ 2 ] = new THREE.Color().setHSL(Math.random() * 0.3 + 0.5, 0.75, Math.random() * 0.25 + 0.75);
}
for (var i = 0; i < list.length; i=i+2) {
geometry.width = size_list[i];
geometry.depth = size_list[i+1];
console.log('geo');
console.log(geometry.width);
console.log(geometry.depth);
material = new THREE.MeshPhongMaterial({ specular: 0xffffff, shading: THREE.FlatShading, vertexColors: THREE.VertexColors });
var mesh = new THREE.Mesh(geometry, material);
mesh.position.x = list[i];
mesh.position.y = 5;
mesh.position.z = list[i+1];
console.log('mesh');
console.log(mesh.geometry.width);
console.log(mesh.geometry.depth);
scene.add(mesh);
material.color.setHSL(Math.random() * 0.2 + 0.5, 0.75, Math.random() * 0.25 + 0.75);
objects.push(mesh);
}
所有网格是不同大小的矩形。尺寸存储在size_list
中。我只使用一个geometry
对象,因为如果我使用更多的程序崩溃。我试图通过移动这个
geometry = new THREE.BoxGeometry(1, 50, 1);
for (var i = 0, l = geometry.faces.length; i < l; i ++) {
var face = geometry.faces[ i ];
face.vertexColors[ 0 ] = new THREE.Color().setHSL(Math.random() * 0.3 + 0.5, 0.75, Math.random() * 0.25 + 0.75);
face.vertexColors[ 1 ] = new THREE.Color().setHSL(Math.random() * 0.3 + 0.5, 0.75, Math.random() * 0.25 + 0.75);
face.vertexColors[ 2 ] = new THREE.Color().setHSL(Math.random() * 0.3 + 0.5, 0.75, Math.random() * 0.25 + 0.75);
}
进入第二个for循环创建每个网格一个几何对象,但浏览器无法处理它。
因此,我在创建网格物体并将其推送到对象数组之前修改了对象geometry
的属性。我正在记录修改的值,以便我可以看到它们在打印好值时正确更改。
但是,最终,在显示场景时,所有网格物体的尺寸仍为(1,50,1),如第一行代码片段中所定义。我一直在阅读关于JavaScript,我认为问题就像它处理副本和引用的方式,但我无法真正理解这种行为。
如何有效修改网格而不必为每个网格创建新的geometry
对象?为什么这种方法不起作用?
试试这个:创建一次:'VAR几何=新THREE.BoxGeometry (1,1,1);'然后在你的循环中:'mesh.scale.set(width [i],height [i],depth [i]);' – WestLangley
这确实会改变网格尺寸:)。非常感谢。你能解释为什么第一个版本不起作用吗? – Malcolm
我可以给你的最好的答案是图书馆没有被设计成以这种方式工作。 – WestLangley