21

我正在尝试更改网格单个面上的颜色。这是在 WebGL 上下文中。我可以更改整个网格颜色,但不能更改单个面。相关代码如下:

// Updated Per Lee!

var camera = _this.camera;      
var projector = new THREE.Projector();
var vector = new THREE.Vector3( ( event.clientX / window.innerWidth ) * 2 - 1,                                  - ( event.clientY / window.innerHeight ) * 2 + 1, 0.5 );
projector.unprojectVector( vector, camera );

var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
var intersects = ray.intersectObjects( kage.scene.children );

if ( intersects.length > 0 ) {
    face = intersects[0].face;
    var faceIndices = ['a', 'b', 'c', 'd'];         
    var numberOfSides = ( face instanceof THREE.Face3 ) ? 3 : 4;
    // assign color to each vertex of current face
    for( var j = 0; j < numberOfSides; j++ )  {
        var vertexIndex = face[ faceIndices[ j ] ];
    // initialize color variable
    var color = new THREE.Color( 0xffffff );
    color.setRGB( Math.random(), 0, 0 );
    face.vertexColors[ j ] = color;
    }
}

我还初始化了对象,在本例中为 Cube,如下所示:

// this material causes a mesh to use colors assigned to vertices
var material = new THREE.MeshBasicMaterial( { 
    color: 0xf0f0f0, 
    shading: THREE.FlatShading,
    vertexColors: THREE.VertexColors 
});

var directionalLight = new THREE.DirectionalLight(0xEEEEEE);
directionalLight.position.set(10, 1, 1).normalize();
kage.scene.add(directionalLight);

var cube = new THREE.Mesh(new THREE.CubeGeometry(300, 300, 300,1,1,1), material);
cube.dynamic = true;
kage.scene.add(cube);

更改材质会使立方体变为白色,而不管光的颜色如何。相交逻辑仍然有效,这意味着我选择了正确的面,但可惜颜色没有改变。

我是 Stackoverflow 的新手 [很好地提出一个问题,所以希望我的编辑不会令人困惑]

4

5 回答 5

26
  • 将库更新到 r53。
  • 加入vertexColors: THREE.FaceColors材料。
  • 最后使用face.color.setRGB( Math.random(), Math.random(), Math.random()).

    现在无需为 4 面 (a,b,c,d)THREE.Face4或 3 面 (a,b,c) 遍历循环THREE.Face3

    这适用于 WebGL 和 Canvas 渲染。

例子

三.js r53

于 2012-12-24T08:08:22.633 回答
7

假设“myGeometry”是包含要更改颜色的面的几何图形,而“faceIndex”是要更改颜色的特定面的索引。

// the face's indices are labeled with these characters 
var faceIndices = ['a', 'b', 'c', 'd'];  

var face = myGeometry.faces[ faceIndex ];   

// determine if face is a tri or a quad
var numberOfSides = ( face instanceof THREE.Face3 ) ? 3 : 4;

// assign color to each vertex of current face
for( var j = 0; j < numberOfSides; j++ )  
{
    var vertexIndex = face[ faceIndices[ j ] ];
    // initialize color variable
    var color = new THREE.Color( 0xffffff );
    color.setRGB( Math.random(), 0, 0 );
    face.vertexColors[ j ] = color;
}

然后,网格需要使用以下材质,以便从顶点派生面颜色:

// this material causes a mesh to use colors assigned to vertices
var cubeMaterial = new THREE.MeshBasicMaterial( 
    { color: 0xffffff, shading: THREE.FlatShading, 
    vertexColors: THREE.VertexColors } );
于 2012-06-29T15:16:07.570 回答
5

我对three.js 比较陌生,但是这些示例中的大多数似乎都过于冗长和复杂。以下代码似乎为立方体的所有 12 个三角形面着色......(WebGLRenderer r73)。

这样做时我注意到的一件事是面孔的顺序有点奇怪(至少对我来说是新手)。

var geometry = new THREE.BoxGeometry( 1, 1, 1 );
console.log(geometry.faces.length); // 12 triangles
geometry.faces[0].color = new THREE.Color(0x000000); //Right 1
geometry.faces[1].color = new THREE.Color(0xFF0000); //Right 2
geometry.faces[2].color = new THREE.Color(0xFF8C08); //Left 1
geometry.faces[3].color = new THREE.Color(0xFFF702); //Left 2
geometry.faces[4].color = new THREE.Color(0x00FF00); //Top 1
geometry.faces[5].color = new THREE.Color(0x0000FF); //Top 2
geometry.faces[6].color = new THREE.Color(0x6F00FF); //Bottom 1
geometry.faces[7].color = new THREE.Color(0x530070); //Bottom 2
geometry.faces[8].color = new THREE.Color(0x3F3F3F); //Front 1
geometry.faces[9].color = new THREE.Color(0x6C6C6C); //Front 2
geometry.faces[10].color = new THREE.Color(0xA7A7A7);//Rear 1
geometry.faces[11].color = new THREE.Color(0xFFFFFF);//Rear 2
于 2016-02-27T17:06:03.983 回答
3

根据三个 JS 几何文档,要在此面中发出更新信号,Geometry.elementsNeedUpdate需要设置为 true。

下面的代码片段改变了所有面孔的颜色。

mesh.material.vertexColors = THREE.FaceColors

var faces = mesh.geometry.faces;

for(var i = 0 ; i < faces.length; i++){
  var face = faces[i];
  var color = new THREE.Color("rgb(255, 0, 0)");
  face.color = color;
}

mesh.geometry.elementsNeedUpdate = true;

render();
于 2017-06-26T19:13:45.393 回答
1

我认为上面列出的方法只适用于WebGLRenderer. 如果您想要在两者中都有效的东西,CanvasRenderer并且WebGLRenderer它有点复杂,但我怀疑最终结果在内存使用和性能方面都更有效。

在浏览了 THREE.jS Projector.js 源代码后,我是这样做的:

// create the face materials
var material_1 = new THREE.MeshLambertMaterial(
    {color : 0xff0000, shading: THREE.FlatShading, overdraw : true}
);
var material_2 = new THREE.MeshLambertMaterial(
    {color : 0x00ff00, shading: THREE.FlatShading, overdraw : true}
);

// create a geometry (any should do)
var geom = new THREE.CubeGeometry(1,1,1);

// add the materials directly to the geometry
geom.materials.push(material_1);
geom.materials.push(material_2);

// assign the material to individual faces (note you assign the index in 
// the geometry, not the material)
for( var i in geom.faces ) {
    var face = geom.faces[i];
    face.materialIndex = i%geom.materials.length;
}

// create a special material for your mesh that tells the renderer to use the 
// face materials
var material = new THREE.MeshFaceMaterial();
var mesh = new THREE.Mesh(geom, material);

这个例子改编自我拥有的工作代码,但我不得不承认我实际上并没有运行这个确切的代码块,而且我没有很好的记录,先把一切都做好,但希望它能帮助任何人谁在挣扎

于 2012-09-23T01:27:34.583 回答