BufferGeometry
three中的几何体,例如:长方体BoxGeometry、球体SphereGeometry等几何体都是基于BufferGeometry构建的,BufferGeometry是一个没有任何形状的空几何体,你可以通过BufferGeometry自定义任何几何形状,具体一点说就是定义顶点数据。
BufferAttribute
顶点位置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| const geometry = new THREE.BufferGeometry()
var vertices = new Float32Array([ 0, 0, 0, 50, 0, 0, 0, 100, 0, 0, 0, 10, 0, 0, 100, 50, 0, 10, ])
var attribute = new THREE.BufferAttribute(vertices, 3)
geometry.attributes.position = attribute; var material = new THREE.PointsMaterial({ color: 0x00ff00, size: 10.0 }) var points = new THREE.Points(geometry, material); scene.add(points)
|
顶点颜色
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| const colors = new Float32Array([ 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 100, 0, 0, 1, 1, 1, 0, 1, ])
geometry.attributes.color = new THREE.BufferAttribute(colors, 3)
var material = new THREE.PointsMaterial({ vertexColors: true, size: 10.0 }) var points = new THREE.Points(geometry, material); scene.add(points)
|
顶点索引
网格模型Mesh对应的几何体BufferGeometry,拆分为多个三角后,很多三角形重合的顶点位置坐标是相同的,这时候如果你想减少顶点坐标数据量,可以借助几何体顶点索引geometry.index来实现。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| const vertices = new Float32Array([ 0, 0, 0, 80, 0, 0, 80, 80, 0, 0, 0, 0, 80, 80, 0, 0, 80, 0, ]);
const vertices = new Float32Array([ 0, 0, 0, 80, 0, 0, 80, 80, 0, 0, 80, 0, ]);
|
索引的写法
1 2 3 4 5 6 7
| const indexes = new Uint16Array([ 0, 1, 2, 0, 2, 3, ])
geometry.index = new THREE.BufferAttribute(indexes, 1);
|
顶点法线
顶点的法线数据往往描述一个几何体对光照等效果的反应情况。一般不需要程序员自己设置,一般由模型进行导出。
Curve
Curve并不是几何体,它可以通过给定点绘制一条曲线,然后再通过getPoints(n)将点数据拿到,并通过setFromPoints将点数据赋值给BufferGeometry
样条曲线CatmullRomCurve3
样条曲线是穿过所有样例点的
1 2 3 4 5 6 7 8 9 10 11 12
| var curve = new THREE.CatmullRomCurve3([ new THREE.Vector3(-50, 20, 90), new THREE.Vector3(-10, 40, 40), new THREE.Vector3(0, 0, 0), new THREE.Vector3(60, -60, 0), new THREE.Vector3(70, 0, 80) ]);
var points = curve.getPoints(100);
geometry.setFromPoints(points);
|
贝塞尔曲线CubicBezierCurve3
与样条曲线不同的是,贝塞尔曲线多了一个控制点,控制点不在贝塞尔曲线上。
1 2
| var curve = new THREE.CubicBezierCurve3(p1, p2, p3, p4);
|
组合曲线CurvePath
将多个曲线组合,形成一条曲线
1 2 3 4
| var CurvePath = new THREE.CurvePath();
CurvePath.curves.push(line1, arc, line2);
|
一些特殊的几何体
曲线管道成型TubeGeometry
形成一条曲线形状的管道
1
| var geometry = new THREE.TubeGeometry(path, 40, 2, 25);
|
参数 值
path 扫描路径,基本类是Curve的路径构造函数
tubularSegments 路径方向细分数,默认64
radius 管道半径,默认1
radiusSegments 管道圆弧细分数,默认8
closed Boolean值,管道是否闭合
旋转成型LatheGeometry
1
| var geometry = new THREE.LatheGeometry(points,30);
|
轮廓填充ShapeGeometry
1 2 3 4 5
| var shape = new THREE.Shape(points);
var geometry = new THREE.ShapeGeometry(shape, 25);
|
拉伸扫描成型ExtrudeGeometry
参数 含义
amount 拉伸长度,默认100
bevelEnabled 是否使用倒角
bevelSegments 倒角细分数,默认3
bevelThickness 倒角尺寸(经向)
curveSegments 拉伸轮廓细分数
steps 拉伸方向细分数
extrudePath 扫描路径THREE.CurvePath,默认Z轴方向
material 前后面材质索引号
extrudeMaterial 拉伸面、倒角面材质索引号
bevelSize 倒角尺寸(拉伸方向)
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
|
var shape = new THREE.Shape();
shape.moveTo(0,0); shape.lineTo(0,10); shape.lineTo(10,10); shape.lineTo(10,0); shape.lineTo(0,0);
var curve = new THREE.SplineCurve3([ new THREE.Vector3( -10, -50, -50 ), new THREE.Vector3( 10, 0, 0 ), new THREE.Vector3( 8, 50, 50 ), new THREE.Vector3( -5, 0, 100) ]); var geometry = new THREE.ExtrudeGeometry( shape, { bevelEnabled:false, extrudePath:curve, steps:50 } );
|