Three.js-几何体

几何体

自定义几何体的原理

给不同的面设置不同的颜色/材质

https://jsfiddle.net/vptec390/2/

原理是给每个顶点设置一个颜色

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const geometry = new THREE.BoxGeometry( 0.2, 0.2, 0.2 ).toNonIndexed();

// vertexColors must be true so vertex colors can be used in the shader

const material = new THREE.MeshBasicMaterial( { vertexColors: true } );

// generate color data for each vertex

const positionAttribute = geometry.getAttribute( 'position' );

const colors = [];
const color = new THREE.Color();

for ( let i = 0; i < positionAttribute.count; i += 3 ) {

color.set( Math.random() * 0xffffff );

// define the same color for each vertex of a triangle

colors.push( color.r, color.g, color.b );
colors.push( color.r, color.g, color.b );
colors.push( color.r, color.g, color.b );

}

// define the new attribute

geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );

mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );

Three.js的面信息(face)有两种存储方式:Indexed和NonIndexed