THREE.BufferGeometry - 如何手动设置面部颜色?

Tom*_*ler 3 three.js

我有一个BufferedGeometry,我想为每个脸设置颜色.但是,据我所知,color几何上的属性设置每个顶点的颜色,而不是面.

我无论如何都试着通过在材质上设置每面的着色方案,material.vertexColors = THREE.FaceColors;并在每个面上放置一个Float32Array颜色属性(RGB,每个范围从0到1)来尝试使用它.这没有所需的输出.

Wes*_*ley 9

您想在使用时指定面部颜色BufferGeometry.为此,请执行以下操作:

使用非索引BufferGeometry.

添加color属性.

geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
Run Code Online (Sandbox Code Playgroud)

color属性中,指定每个面的所有三个顶点具有相同的颜色.

如果您使用的是three.js内置材质,请在材质定义中进行设置

vertexColors: THREE.VertexColors
Run Code Online (Sandbox Code Playgroud)

如果您正在使用ShaderMaterial,则必须自己编写着色器.

three.js r.83

  • 一般情况下不会,因为索引的 BufferGeometry 共享(重用)顶点,并且顶点只能有一种颜色。您可以使用“geometry2 = Geometry.toNonIndexed()”将索引几何图形转换为非索引几何图形。 (3认同)