简体   繁体   English

改变立方体一面的颜色 - THREE.js

[英]Change color of one face of the cube - THREE.js

I am learning OOP while using Three.js. 我在使用Three.js时学习OOP。 I know, a hard way to do it. 我知道,这很难做到。 So i created a box in the scene. 所以我在场景中创建了一个盒子。 Now i want to change color of one face of that cube. 现在我想改变那个立方体一面的颜色。

var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 100, window.innerWidth/window.innerHeight, 0.1, 1000 );
camera.position.set(5, 5, 10);

var geo = new THREE.BoxGeometry(5,2,5);
var mat = new THREE.MeshBasicMaterial({color:0xff0ff0, side:THREE.DoubleSide});

var mesh = new THREE.Mesh( geo, mat);
scene.add(mesh);

//Right there i want to chance color of the face. Its not working
mesh.geometry.faces[5].color.setHex( 0xffffff ); 

var edge = new THREE.WireframeHelper( mesh, 0x00ff00 );
scene.add(edge);

var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
renderer.render(scene, camera);

EDIT: the reason it did not work for me was the color i used 0xffffff . 编辑:它不适合我的原因是我使用0xffffff的颜色。 This program will work unless color is 0xffffff. 除非颜色为0xffffff,否则此程序将起作用。

In your case, if you want to change the color of one face of the cube, you will need to specify vertexColors in your material. 在您的情况下,如果要更改多维数据集的一个面的颜色,则需要在材质中指定vertexColors

var geo = new THREE.BoxGeometry( 5, 2, 5 );

var mat = new THREE.MeshBasicMaterial( { color:0xff0ff0, vertexColors: THREE.FaceColors } );

var mesh = new THREE.Mesh( geo, mat );

mesh.geometry.faces[ 5 ].color.setHex( 0x00ffff ); 

The rendered face color will be the specified face color tinted by the material color . 渲染的面部颜色将是由材质颜色着色的指定面部颜色

If you change a face color after the mesh has been rendered at least once, you will have to set: 如果在网格渲染至少一次后更改了面部颜色,则必须设置:

mesh.geometry.colorsNeedUpdate = true;

three.js r.80 three.js r.80

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM