Geometry 几何模型

作为网格 Mesh 的第一个参数, 定义物体的几何形状. 它的子类有
平面几何模型:CircleGeometry, PlaneGeometry, RingGeometry, ShapeGeometry.

立体几何模型:BoxGeometry, ConeGeometry, CylinderGeometry,DodecahedronGeometry, ExtrudeGeometry, IcosahedronGeometry, LatheGeometry, OctahedronGeometry, ParametricGeometry, PolyhedronGeometry, SphereGeometry, TetrahedronGeometry, TorusGeometry, TubeGeometry.

描边模型:EdgesGeometry, WireframeGeometry

几何模型的三大属性是vertices(顶点)、colors(顶点颜色)和faces(面), 类型都是数组, 对它们赋值, 就能创建自己的模型. 如果你要的Object是 Line, 那么只需要前两个属性就可以了; 而如果是 Mesh 的话, 则要加上faces, 而且faces里的排序不同, 会导致它们的vertexColors获得不一样的顶点颜色, 最终展示出来的整个面的颜色都不一样.

function Geometry( ) { } // 左上方的线 var geometry = new THREE.Geometry(), p0 = new THREE.Vector3(-4, 0, 0), p1 = new THREE.Vector3(-4, 2, 0), p2 = new THREE.Vector3(-2, 2, 0), p3 = new THREE.Vector3(-2, 4, 0), p4 = new THREE.Vector3(2, 4, 0), p5 = new THREE.Vector3(2, 2, 0), p6 = new THREE.Vector3(4, 2, 0), p7 = new THREE.Vector3(4, 0, 0), pointsArr = [p0, p1, p2, p3, p4, p5, p6, p7], colorsArr = []; for(var i = 0; i < pointsArr.length; i++){ colorsArr.push( new THREE.Color(0xffffff * Math.random()) ); } geometry.vertices = pointsArr; geometry.colors = colorsArr; var material = new THREE.LineBasicMaterial({ vertexColors: THREE.VertexColors }), line = new THREE.LineLoop(geometry, material); scene.add(line); // 右上方的面 var face1 = new THREE.Face3(0, 2, 1), face2 = new THREE.Face3(0, 7, 2), face3 = new THREE.Face3(2, 7, 6), face4 = new THREE.Face3(2, 4, 3), face5 = new THREE.Face3(2, 5, 4), facesArr = [face1, face2, face3, face4, face5]; createMesh(6, 0); // 右下方的面 face1 = new THREE.Face3(0, 5, 1); face2 = new THREE.Face3(7, 6, 5); face3 = new THREE.Face3(0, 7, 5); face4 = new THREE.Face3(2, 5, 3); face5 = new THREE.Face3(3, 5, 4); facesArr = [face1, face2, face3, face4, face5]; createMesh(6, -5); function createMesh(x, y){ var geometry = new THREE.Geometry(), normal = new THREE.Vector3(0, 0, 1); for(var i = 0; i < facesArr.length; i++){ facesArr[i].normal.copy(normal); facesArr[i].vertexNormals.push(normal.clone(), normal.clone(), normal.clone()); } geometry.vertices = pointsArr; geometry.faces = facesArr; for(var j = 0; j < facesArr.length; j++){ facesArr[j].vertexColors[0] = colorsArr[facesArr[j].a]; facesArr[j].vertexColors[1] = colorsArr[facesArr[j].b]; facesArr[j].vertexColors[2] = colorsArr[facesArr[j].c]; } var meshMaterial = new THREE.MeshBasicMaterial({ vertexColors: THREE.FaceColors, side: THREE.DoubleSide }); var mesh = new THREE.Mesh(geometry, meshMaterial); scene.add(mesh); mesh.position.x = x; mesh.position.y = y; }

Attributes

name : String

Default value : ""
Description : 名称.

type : String

Default value : 'Geometry'
Description : 类型.

isGeometry : Boolean

Default value : true (只读)
Description : 类型是否为Geometry.

boundingBox : Box3

Default value : null
Description : 包围盒, 初始值为 null, 需要根据 computeBoundingBox( ) 求得.

boundingSphere : Sphere

Default value : null
Description : 包围球, 初始值为 null, 需要根据 computeBoundingSphere( ) 求得.

colors : Array

Default value : []
Description : 点的颜色 Color 数组. 用在 PointsLine 中. 数组更新时, 把 colorsNeedUpdate 设为 true.

colorsNeedUpdate : Boolean

Default value : false
Description : 颜色需要更新

elementsNeedUpdate : Boolean

Default value : false
Description : 元素是否需要更新.

faces : Array

Default value : []
Description : 面数组. 描述模型中的顶点是怎么连接成面的. 数组更新时, 把 elementsNeedUpdate 设为 true.

faceVertexUvs : Array

Default value : [[]]
Description : 面顶点的纹理坐标. 数组更新时, 把 uvsNeedUpdate 设为 true.

groupsNeedUpdate : Boolean

Default value : false
Description : 群组是否需要更新.

lineDistances : Array

Default value : []
Description : 线间距离数组.

lineDistancesNeedUpdate : Boolean

Default value : false
Description : 线距是否需要更新.

morphNormals : Array

Default value : []
Description : 变形法线数组, 每个目标都是一个对象 { name: "NormalName", normals: [ new THREE.Vector3(), ... ] }

morphTargets : Array

Default value : []
Description : 变形目标数组, 每个目标都是一个对象 { name: "targetName", vertices: [ new THREE.Vector3(), ... ] }

normalsNeedUpdate : Boolean

Default value : false
Description : 法线是否需要更新.

skinIndices : Array

Default value : []
Description : 蒙皮指数数组.

skinWeights : Array

Default value : []
Description : 蒙皮权重数组.

uvsNeedUpdate : Boolean

Default value : false
Description : 纹理坐标是否需要更新.

vertices : Array

Default value : []
Description : 顶点数组. 一个元素都是 Vector3() 的数组, 要修改 y 坐标时, vertices[ i ].y = ~. 修改后 verticesNeedUpdate 要改为true. 对应 BufferGeometry, 可依据 bufferGeometry.attributes.position.array 得到. var pointsArr = [new THREE.Vector(-1, -1, 0), new THREE.Vector(0, 2, 0), new THREE.Vector(1, -1, 0)]; geometry.vertices = pointsArr; for ( var i = 0; i < vertices.length; i++ ) { vertices[ i ].y = Math.random() * 10; }

verticesNeedUpdate : Boolean

Default value : false
Description : 顶点是否需要更新. var geometry = group.children[0].geometry; geometry.vertices[1].x += 5; geometry.verticesNeedUpdate = true;

Methods

applyMatrix( matrix ) : this
matrix Matrix4 矩阵

对顶点坐标施加矩阵变换.

center( ) : this

把几何模型居中在包围盒内.

clone( ) : Geometry

创建自身的一个克隆, 从 copy( this ) 得到.

copy( geometry ) : this
geometry Geometry 几何模型

复制模型:把一个几何模型中的顶点、面和纹理坐标复制进来, 不再复制其他的任何属性.

computeFaceNormals( )

计算面法线.

computeVertexNormals( areaWeighted ) :
areaWeighted Boolean: true(default) 是否用面积加权平均值

计算顶点法线. 如果true: 面法线对顶点法线的贡献, 由面积加权平均值决定.

computeFlatVertexNormals( )

计算平面顶点法线. 让每个面的每一个顶点法线都跟面法线一致.

computeMorphNormals( )

计算变形法线.

computeBoundingBox( )

计算包围盒.

computeBoundingSphere( )

计算包围球.

dispose( )

删除对象. mesh.geometry.dispose(); mesh.geometry = new Geometry();

fromBufferGeometry( geometry ) : this
geometry BufferGeometry 缓冲区几何模型

把缓冲区几何模型 BufferGeometry 转变为一个普通的几何模型.

lookAt( vector ) : this
vector Vector3 用来被注视着的观察点

旋转几何模型来对着观察点.

merge( geometry, matrix, materialIndexOffset ) :
geometry Geometry 几何模型
matrix Matrix4 四维矩阵
materialIndexOffset Int 材质索引偏移量

合并几何模型.

mergeMesh( mesh )
mesh Mesh 网格

合并网格中的几何模型.

mergeVertices( ) : Int

合并顶点. 复制顶点时用哈希表来检验, 删除复制了的顶点, 更新顶点数据.

normalize( ) : this

标准化, 归一化: 把几何模型居中在以1为半径的包围球内.

rotateX( angle ),   rotateY( angle ),   rotateZ( angle ) : this
angle Float 角度

物体绕 x轴 / y轴 / z轴 旋转一个角度.

scale( x, y, z ) : this
x, y, z Float x方向, y方向, z方向上的缩放值

缩放.

setFromPoints( points ) : this
points Array 点数组

运用点数组来设置顶点. var path = new THREE.Path(); path.moveTo(0, 0); path.liveTo(5, 5); var points = path.getPoints(); var geometry = new THREE.Geometry().setFromPoints( points );

sortFacesByMaterialIndex( ) :

以材质索引来对面进行排序.

toJSON( ) : Object

返回 Json 格式的数据.

translate( x, y, z ) : this
x, y, z Float x方向, y方向, z方向上的距离

平移.