javascript - Three.js 带顶点的 RGB 立方体

标签 javascript three.js rgb

我尝试在 Three.js 中做一个 RGB 立方体,但我必须使用顶点,没有纹理。我看了一些教程,但是我的代码不起作用,我可以求教吗?谢谢。

https://jsfiddle.net/yjru14q3/

var geom = new THREE.Geometry();

        geom.vertices = vertices;
        geom.vertexColors = colors;
        var colors = [];
        colors[0] = new THREE.Color( 0, 0, 0 );
        ....
        var vertices = [];
        vertices[0] = new THREE.Vector3( 0, 0, 0 );
        ....
        var material = new THREE.MeshBasicMaterial({ 
            vertexColors: THREE.VertexColors,
            side: THREE.DoubleSide, // in case we go inside the cube
        });

        var cube = new THREE.Mesh(geom, material);
        scene.add(cube);

最佳答案

当您为 THREE.Points() 使用几何时,geometry.colors[] 的使用与 vertexColors: THREE.VertexColors 一起使用。

enter image description here

如果要为THREE.Mesh() 的面顶点应用顶点颜色,那么最好遵循this example。 :

var geom = new THREE.BoxGeometry(1, 1, 1);
var faceIndices = ['a', 'b', 'c'];
var vertexIndex, point;
geom.faces.forEach(function(face) { // loop through faces
  for (var i = 0; i < 3; i++) {
    vertexIndex = face[ faceIndices[ i ] ]; // get the face's vertex's index
    point = geom.vertices[vertexIndex]; // knowing the index, find the vertex in array of vertices
    color = new THREE.Color( // create a color
      point.x + 0.5, //apply xyz as rgb
      point.y + 0.5,
      point.z + 0.5
    );
    face.vertexColors[ i ] = color; //store the color in the face's vertexColors array
  }
});

var mat = new THREE.MeshBasicMaterial({
  vertexColors: THREE.VertexColors
});

var cube = new THREE.Mesh(geom, mat);
scene.add(cube);

jsfiddle例子

关于javascript - Three.js 带顶点的 RGB 立方体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42843053/

相关文章:

three.js - 如何使用 PolyhedronGeometry 创建十二面体?

java - 如何获取CIELAB图像a轴和b轴上的像素值

javascript - HTML "Range"输入问题

javascript - Angular 如何避免使用 ng-view 加载许多 javascript 文件

javascript - 获取内容可编辑插入符位置

python - 在 python 中显示 rgb 矩阵图像

c - 如何将RGB更改为BGR

javascript - 尝试获取 span 元素的内部文本内容

javascript - 阴影没有出现在 Three.js 的场景中?

javascript - 动画 GIF 作为 THREE.js 中的纹理