three.js 源碼注釋(十六)Math/Frustum.js,three.jsfrustum.js
商域無疆 (http://blog.csdn.net/omni360/)
本文遵循“署名-非商業用途-保持一致”創作公用協議
轉載請保留此句:商域無疆 - 本部落格專註於 敏捷開發及移動和物聯裝置研究:資料視覺效果、GOLANG、Html5、WEBGL、THREE.JS,否則,出自本部落格的文章拒絕轉載或再轉載,謝謝合作。
俺也是剛開始學,好多地兒肯定不對還請見諒.
以下代碼是THREE.JS 源碼檔案中Math/Frustum.js檔案的注釋.
更多更新在 : https://github.com/omni360/three.js.sourcecode/blob/master/Three.js
// File:src/math/Frustum.js/** * @author mrdoob / http://mrdoob.com/ * @author alteredq / http://alteredqualia.com/ * @author bhouston / http://exocortex.com *//*///Frustum對象的建構函式.用來在三維空間內建立一個平截頭體對象.Frustum對象的功能函數採用///定義構造的函數原型對象來實現,平截頭體由6個平面對象構成.*////<summary>Frustum</summary>///<param name ="p0" type="THREE.Plane">組成平截頭體的面p0</param>///<param name ="p1" type="THREE.Plane">組成平截頭體的面p1</param>///<param name ="p2" type="THREE.Plane">組成平截頭體的面p2</param>///<param name ="p3" type="THREE.Plane">組成平截頭體的面p3</param>///<param name ="p4" type="THREE.Plane">組成平截頭體的面p4</param>///<param name ="p5" type="THREE.Plane">組成平截頭體的面p5</param>THREE.Frustum = function ( p0, p1, p2, p3, p4, p5 ) {this.planes = [( p0 !== undefined ) ? p0 : new THREE.Plane(),( p1 !== undefined ) ? p1 : new THREE.Plane(),( p2 !== undefined ) ? p2 : new THREE.Plane(),( p3 !== undefined ) ? p3 : new THREE.Plane(),( p4 !== undefined ) ? p4 : new THREE.Plane(),( p5 !== undefined ) ? p5 : new THREE.Plane()];};/********************************************下面是Frustum對象提供的功能函數.****************************************/THREE.Frustum.prototype = {constructor: THREE.Frustum,//構造器,返回對建立此對象的Frustum函數的引用/*///set方法用來重新設定平截頭體的起始點,結束點,p0, p1, p2, p3, p4, p5.並返回新的座標值的平截頭體.*////<summary>set</summary>///<param name ="p0" type="THREE.Plane">組成平截頭體的面p0</param>///<param name ="p1" type="THREE.Plane">組成平截頭體的面p1</param>///<param name ="p2" type="THREE.Plane">組成平截頭體的面p2</param>///<param name ="p3" type="THREE.Plane">組成平截頭體的面p3</param>///<param name ="p4" type="THREE.Plane">組成平截頭體的面p4</param>///<param name ="p5" type="THREE.Plane">組成平截頭體的面p5</param>///<returns type="Frustum">返回新的平截頭體</returns>set: function ( p0, p1, p2, p3, p4, p5 ) {var planes = this.planes;planes[ 0 ].copy( p0 );planes[ 1 ].copy( p1 );planes[ 2 ].copy( p2 );planes[ 3 ].copy( p3 );planes[ 4 ].copy( p4 );planes[ 5 ].copy( p5 );return this;//返回新的平截頭體},/*///copy方法用來複製組成平截頭體的六個面,p0, p1, p2, p3, p4, p5.並返回新的平截頭體.*////<summary>copy</summary>///<param name ="frustum" type="Frustum">平截頭體</param>///<returns type="Frustum">返回新的平截頭體</returns>copy: function ( frustum ) {var planes = this.planes;for ( var i = 0; i < 6; i ++ ) {planes[ i ].copy( frustum.planes[ i ] );}return this;//返回新的平截頭體},/*///setFromMatrix方法通過對當前平截頭體應用變換,返回新的平截頭體.*////<summary>setFromMatrix</summary>///<param name ="m" type="Matrix4">4x4矩陣</param>///<returns type="Frustum">返回新的平截頭體</returns>setFromMatrix: function ( m ) {var planes = this.planes;var me = m.elements;var me0 = me[ 0 ], me1 = me[ 1 ], me2 = me[ 2 ], me3 = me[ 3 ];var me4 = me[ 4 ], me5 = me[ 5 ], me6 = me[ 6 ], me7 = me[ 7 ];var me8 = me[ 8 ], me9 = me[ 9 ], me10 = me[ 10 ], me11 = me[ 11 ];var me12 = me[ 12 ], me13 = me[ 13 ], me14 = me[ 14 ], me15 = me[ 15 ];planes[ 0 ].setComponents( me3 - me0, me7 - me4, me11 - me8, me15 - me12 ).normalize();planes[ 1 ].setComponents( me3 + me0, me7 + me4, me11 + me8, me15 + me12 ).normalize();planes[ 2 ].setComponents( me3 + me1, me7 + me5, me11 + me9, me15 + me13 ).normalize();planes[ 3 ].setComponents( me3 - me1, me7 - me5, me11 - me9, me15 - me13 ).normalize();planes[ 4 ].setComponents( me3 - me2, me7 - me6, me11 - me10, me15 - me14 ).normalize();planes[ 5 ].setComponents( me3 + me2, me7 + me6, me11 + me10, me15 + me14 ).normalize();return this;//返回新的平截頭體},/*///intersectsObject方法擷取當前平截頭體是否與參數object對象相交,返回true 或者 false*////<summary>intersectsObject</summary>///<param name ="object" type="Object3d">3d立體對象</param>///<returns type="Boolean">返回true 或者 false</returns>intersectsObject: function () {var sphere = new THREE.Sphere();return function ( object ) {var geometry = object.geometry;if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();sphere.copy( geometry.boundingSphere );sphere.applyMatrix4( object.matrixWorld );return this.intersectsSphere( sphere );//返回true 或者 false};}(),/*///intersectsSphere方法擷取當前平截頭體是否與參數sphere球體對象相交,返回true 或者 false*////<summary>intersectsSphere</summary>///<param name ="sphere" type="Sphere">一個Sphere的球體</param>///<returns type="Boolean">返回true 或者 false</returns>intersectsSphere: function ( sphere ) {var planes = this.planes;var center = sphere.center;var negRadius = - sphere.radius;for ( var i = 0; i < 6; i ++ ) {var distance = planes[ i ].distanceToPoint( center );if ( distance < negRadius ) {return false;//不相交返回false}}return true;//相交返回true},/*///intersectsBox方法擷取當前平截頭體是否與參數box立方體對象相交,返回true 或者 false*////<summary>intersectsBox</summary>///<param name ="box" type="Box3">立方體對象</param>///<returns type="Boolean">返回true 或者 false</returns>intersectsBox: function () {var p1 = new THREE.Vector3(),p2 = new THREE.Vector3();return function ( box ) {var planes = this.planes;for ( var i = 0; i < 6 ; i ++ ) {var plane = planes[ i ];p1.x = plane.normal.x > 0 ? box.min.x : box.max.x;p2.x = plane.normal.x > 0 ? box.max.x : box.min.x;p1.y = plane.normal.y > 0 ? box.min.y : box.max.y;p2.y = plane.normal.y > 0 ? box.max.y : box.min.y;p1.z = plane.normal.z > 0 ? box.min.z : box.max.z;p2.z = plane.normal.z > 0 ? box.max.z : box.min.z;var d1 = plane.distanceToPoint( p1 );var d2 = plane.distanceToPoint( p2 );// if both outside plane, no intersection// 如果立方體的p1,p2都與平面不想交,表示立方體和平截頭體不相交.if ( d1 < 0 && d2 < 0 ) {return false;//不相交返回false}}return true;//相交返回true.};}(),/*///containsPoint方法用來獲得參數point(一個Vector3的三維點座標)是否在當前平截頭體內.*////<summary>containsPoint</summary>///<param name ="point" type="THREE.Vector3">一個Vector3的三維點座標</param>///<returns type="Boolean">返回true 或者 false</returns>containsPoint: function ( point ) {var planes = this.planes;for ( var i = 0; i < 6; i ++ ) {if ( planes[ i ].distanceToPoint( point ) < 0 ) {return false;//不在邊界內,返回false}}return true;//在邊界內,返回true},/*clone方法///clone方法複製一個平截頭體對象.*////<summary>clone</summary>///<returns type="Frustum">返回平截頭體對象</returns>clone: function () {return new THREE.Frustum().copy( this );//返回平截頭體對象}};
商域無疆 (http://blog.csdn.net/omni360/)
本文遵循“署名-非商業用途-保持一致”創作公用協議
轉載請保留此句:商域無疆 - 本部落格專註於 敏捷開發及移動和物聯裝置研究:資料視覺效果、GOLANG、Html5、WEBGL、THREE.JS,否則,出自本部落格的文章拒絕轉載或再轉載,謝謝合作。
以下代碼是THREE.JS 源碼檔案中Math/Frustum.js檔案的注釋.
更多更新在 : https://github.com/omni360/three.js.sourcecode/blob/master/Three.js