three.js 源碼注釋(六十八)extras/geometries/BoxGeometry.js,three.jsgeometries
商域無疆 (http://blog.csdn.net/omni360/)
本文遵循“署名-非商業用途-保持一致”創作公用協議
轉載請保留此句:商域無疆 - 本部落格專註於 敏捷開發及移動和物聯裝置研究:資料視覺效果、GOLANG、Html5、WEBGL、THREE.JS,否則,出自本部落格的文章拒絕轉載或再轉載,謝謝合作。
俺也是剛開始學,好多地兒肯定不對還請見諒.
以下代碼是THREE.JS 源碼檔案中extras/geometries/BoxGeometry.js檔案的注釋.
更多更新在 : https://github.com/omni360/three.js.sourcecode
/** * @author mrdoob / http://mrdoob.com/ * based on http://papervision3d.googlecode.com/svn/trunk/as3/trunk/src/org/papervision3d/objects/primitives/Cube.as *//*///BoxGeometry用來在三維空間內建立一個立方體盒子物件.//////用法: var geometry = new THREE.BoxGeometry(1,1,1);/// var material = new THREE.MeshBasicMaterial({color: 0x00ff00});/// var cube = new THREE.Mesh(geometry,material);/// scene.add(cube);*////<summary>BoxGeometry</summary>///<param name ="width" type="float">立方體寬度</param>///<param name ="height" type="float">立方體高度</param>///<param name ="depth" type="float">立方體深度</param>///<param name ="widthSegments" type="int">立方體寬度細分線段數</param>///<param name ="heightSegments" type="int">立方體高度細分線段數</param>///<param name ="depthSegments" type="int">立方體深度細分線段數</param>THREE.BoxGeometry = function ( width, height, depth, widthSegments, heightSegments, depthSegments ) {THREE.Geometry.call( this );//調用Geometry對象的call方法,將原本屬於Geometry的方法交給當前對象BoxGeometry來使用.this.parameters = {width: width,height: height,depth: depth,widthSegments: widthSegments,heightSegments: heightSegments,depthSegments: depthSegments};this.widthSegments = widthSegments || 1;this.heightSegments = heightSegments || 1;this.depthSegments = depthSegments || 1;var scope = this;var width_half = width / 2;var height_half = height / 2;var depth_half = depth / 2;buildPlane( 'z', 'y', - 1, - 1, depth, height, width_half, 0 ); // px buildPlane( 'z', 'y', 1, - 1, depth, height, - width_half, 1 ); // nxbuildPlane( 'x', 'z', 1, 1, width, depth, height_half, 2 ); // pybuildPlane( 'x', 'z', 1, - 1, width, depth, - height_half, 3 ); // nybuildPlane( 'x', 'y', 1, - 1, width, height, depth_half, 4 ); // pzbuildPlane( 'x', 'y', - 1, - 1, width, height, - depth_half, 5 ); // nz///<summary>buildPlane</summary>///<param name ="u" type="String">取值範圍"x,y,z",表示從構建面的u座標與xyz座標的映射</param>///<param name ="v" type="String">取值範圍"x,y,z",表示從構建面的v座標與xyz座標的映射</param>///<param name ="udir" type="float">為計算平面u方向上的頂點與原點的方向</param>///<param name ="vdir" type="float">為計算平面v方向上的頂點與原點的方向</param>///<param name ="width" type="float">立方體寬度</param>///<param name ="height" type="float">立方體高度</param>///<param name ="depth" type="float">立方體深度的一半(why?應為立方體是以中心點為原點!)</param>///<param name ="materialIndex" type="int">材質索引號</param>function buildPlane( u, v, udir, vdir, width, height, depth, materialIndex ) {var w, ix, iy,gridX = scope.widthSegments,gridY = scope.heightSegments,width_half = width / 2,height_half = height / 2,offset = scope.vertices.length;//通過參數uv確定w座標,用來確定面的方向,想象一下立方體有六個面,通過buildPlane來構建三角面.if ( ( u === 'x' && v === 'y' ) || ( u === 'y' && v === 'x' ) ) {w = 'z';} else if ( ( u === 'x' && v === 'z' ) || ( u === 'z' && v === 'x' ) ) {w = 'y';gridY = scope.depthSegments;} else if ( ( u === 'z' && v === 'y' ) || ( u === 'y' && v === 'z' ) ) {w = 'x';gridX = scope.depthSegments;}var gridX1 = gridX + 1,gridY1 = gridY + 1,segment_width = width / gridX,segment_height = height / gridY,normal = new THREE.Vector3();normal[ w ] = depth > 0 ? 1 : - 1;for ( iy = 0; iy < gridY1; iy ++ ) {//這裡在構建幾何體時非常重要,通過使用者給定的參數,計算出頂點,這些頂點的順序,要在下面使用.//如果要自己定義幾何體對象,計算頂點,和三角面是必不可少的.這裡需要使用者對頂點的儲存順序和面的構建順序,在頭腦中有清晰的幾何形象.多做練習吧.for ( ix = 0; ix < gridX1; ix ++ ) {var vector = new THREE.Vector3();vector[ u ] = ( ix * segment_width - width_half ) * udir;vector[ v ] = ( iy * segment_height - height_half ) * vdir;vector[ w ] = depth;scope.vertices.push( vector );//通過長寬高,方向,線段數計算出頂點的座標.}}for ( iy = 0; iy < gridY; iy ++ ) {//這裡是構建幾何體非常重要的一步,根據一個一個的三角面拼成立方體一個面for ( ix = 0; ix < gridX; ix ++ ) {//計算群組成立方體面的矩形的四個頂點,a,b,c,d,我經常誤理解為a,b,c,d,4條邊,這裡應該是學的幾何知識影響的我.var a = ix + gridX1 * iy;var b = ix + gridX1 * ( iy + 1 );var c = ( ix + 1 ) + gridX1 * ( iy + 1 );var d = ( ix + 1 ) + gridX1 * iy;var uva = new THREE.Vector2( ix / gridX, 1 - iy / gridY );var uvb = new THREE.Vector2( ix / gridX, 1 - ( iy + 1 ) / gridY );var uvc = new THREE.Vector2( ( ix + 1 ) / gridX, 1 - ( iy + 1 ) / gridY );var uvd = new THREE.Vector2( ( ix + 1 ) / gridX, 1 - iy / gridY );var face = new THREE.Face3( a + offset, b + offset, d + offset );//構建的a,b,c3個頂點組成的三角面face.normal.copy( normal );face.vertexNormals.push( normal.clone(), normal.clone(), normal.clone() );//計算面的法向量face.materialIndex = materialIndex;//賦值面的材質索引scope.faces.push( face );scope.faceVertexUvs[ 0 ].push( [ uva, uvb, uvd ] );face = new THREE.Face3( b + offset, c + offset, d + offset );//構建的b,c,d,3個頂點組成的三角面,一個平面有兩個三角面,都見過吧.face.normal.copy( normal );face.vertexNormals.push( normal.clone(), normal.clone(), normal.clone() );//計算面的法向量face.materialIndex = materialIndex;//賦值面的材質索引scope.faces.push( face );scope.faceVertexUvs[ 0 ].push( [ uvb.clone(), uvc, uvd.clone() ] );}}}this.mergeVertices();//mergeVertices方法用來清理幾何體中重複的頂點};/*****************************************************下面是BoxGeometry對象的方法屬性定義,繼承自Geometry對象.**************************************************/THREE.BoxGeometry.prototype = Object.create( THREE.Geometry.prototype );
商域無疆 (http://blog.csdn.net/omni360/)
本文遵循“署名-非商業用途-保持一致”創作公用協議
轉載請保留此句:商域無疆 - 本部落格專註於 敏捷開發及移動和物聯裝置研究:資料視覺效果、GOLANG、Html5、WEBGL、THREE.JS,否則,出自本部落格的文章拒絕轉載或再轉載,謝謝合作。
以下代碼是THREE.JS 源碼檔案中extras/geometries/BoxGeometry.js檔案的注釋.
更多更新在 : https://github.com/omni360/three.js.sourcecode