我試圖製作一個函數,它將隨機選擇10個對象改變material
顏色,但我不知道如何將它連接到數組或向量中。我試過把color(selector)
放到一些像meshArrayX
這樣的數組中,但是後來,對象的位置是未定義的,並且根本不會着色任何對象。Three.js在10個隨機對象上生成顏色
var mesh, scene,
MeshBasicMaterial = new THREE.MeshBasicMaterial({
color: 0x000000,
opacity: 0.3,
transparent: true
});
var BoxGeometry = new THREE.BoxGeometry(16, 16, 16);
function random(){
var random = [0, 16, 32];
var randomValue = random[Math.floor(random.length * Math.random())];
console.log(randomValue);
}
function color(selector){
for(var v=0; v<10; v++){
selector.position.x = random();
selector.position.y = random();
selector.position.z = random();
selector.material.color.setHex(0x0000ff);
}
}
var meshArrayX = [];
for (var x=0; x<3; x++) {
var meshArrayY = [meshArrayX.push(mesh = new THREE.Mesh(BoxGeometry, MeshBasicMaterial))];
for(var y=0; y<3; y++){
var meshArrayZ = [meshArrayY.push(mesh = new THREE.Mesh(BoxGeometry, MeshBasicMaterial))];
for(var z=0; z<3; z++){
meshArrayZ.push(mesh = new THREE.Mesh(BoxGeometry, MeshBasicMaterial));
mesh.position.x = 16*x;
mesh.position.y = 16*y;
mesh.position.z = 16*z;
scene.add(mesh);
console.log(meshArrayX.length, meshArrayY.length-1, meshArrayZ.length-1, mesh.position);
}
}
}
color(mesh);
你的隨機()函數沒有返回任何值,所以你會得到position.x/Y「未定義」/Z –