天天看點

three.js 源代碼凝視(十四)Math/Sphere.js

<a target="_blank" href="http://blog.csdn.net/omni360">轉載請保留此句:商域無疆 -  本部落格專注于 靈活開發及移動和物聯裝置研究:資料可視化、GOLANG、Html5、WEBGL、THREE.JS,否則。出自本部落格的文章拒絕轉載或再轉載,謝謝合作。</a>

俺也是剛開始學,好多地兒肯定不正确還請見諒.

下面代碼是THREE.JS 源代碼檔案裡Math/Sphere.js檔案的凝視.

radius : 0; //指派或者初始化radius

};

/****************************************

****以下是Sphere對象提供的功能函數.

****************************************/

THREE.Sphere.prototype = {

constructor: THREE.Sphere, //構造器,傳回對建立此對象的Sphere函數的引用

/*

///set方法用來從新設定球體的起始點,結束點,center,radius坐标值.并傳回新半徑,坐标值的球體.

*/

///&lt;summary&gt;set&lt;/summary&gt;

///&lt;param name ="center" type="Vector3"&gt;中心點坐标值&lt;/param&gt;

///&lt;param name ="radius" type="Number"&gt;Number球體半徑&lt;/param&gt;

///&lt;returns type="Sphere"&gt;傳回新半徑,坐标值的球體&lt;/returns&gt;

set: function ( center, radius ) {

this.center.copy( center );

this.radius = radius;

return this; //傳回新半徑,坐标值的球體

},

///setFromPoints方法通過獲得Vector3對象組成的points數組中的到圓心距離最大的值又一次設定球體的半徑,通過可選參數optionalCenter用來設定球體的圓心.并傳回新半徑,坐标值的球體.

/// NOTE:注意假設給setFromPoints()方法設定了optionalCenter參數,points數組中數值到圓心的距離将會改變.

///&lt;summary&gt;setFromPoints&lt;/summary&gt;

///&lt;param name ="points" type="Vector3Array"&gt;Vector3對象組成的points數組&lt;/param&gt;

///&lt;param name ="optionalCenter" type="Vector3"&gt;可選參數,接收傳回結果,球體的中心點&lt;/param&gt;

setFromPoints: function () {

var box = new THREE.Box3();

return function ( points, optionalCenter ) {

var center = this.center;

if ( optionalCenter !== undefined ) {

center.copy( optionalCenter );

} else {

box.setFromPoints( points ).center( center );

}

var maxRadiusSq = 0;

for ( var i = 0, il = points.length; i &lt; il; i ++ ) {

maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( points[ i ] ) ); //求points數組中到圓心的最大值并指派給maxRadiusSq

this.radius = Math.sqrt( maxRadiusSq );

return this; //傳回新半徑,坐标值的球體

}(),

///copy方法用來複制球體的圓心,半徑,center,radius值.傳回新半徑,坐标值的球體

///&lt;summary&gt;copy&lt;/summary&gt;

///&lt;param name ="sphere" type="Sphere"&gt;球體&lt;/param&gt;

copy: function ( sphere ) {

this.center.copy( sphere.center );

this.radius = sphere.radius;

///empty方法用來推斷球體的半徑是否小于等于0,用來推斷空間中半徑是0,或者小于0的球體.

///&lt;summary&gt;empty&lt;/summary&gt;

///&lt;returns type="Boolean"&gt;傳回true 或者 false&lt;/returns&gt;

empty: function () {

return ( this.radius &lt;= 0 ); //傳回true 或者 false

///containsPoint方法用來獲得參數point(一個Vector3的三維點坐标)是否在目前球體内.

///&lt;summary&gt;containsPoint&lt;/summary&gt;

///&lt;param name ="point" type="Vector3"&gt;一個Vector3的三維點坐标&lt;/param&gt;

containsPoint: function ( point ) {

return ( point.distanceToSquared( this.center ) &lt;= ( this.radius * this.radius ) ); //傳回true 或者 false

///distanceToPoint方法用來獲得三維空間内一點到Sphere球體對象表面的最小長度.

///&lt;summary&gt;distanceToPoint&lt;/summary&gt;

///&lt;param name ="point" type="Vector3"&gt;一個三維空間内的Vector3的三維點坐标&lt;/param&gt;

///&lt;returns type="Number"&gt;傳回三維空間内一點到Sphere球體對象表面的最小長度.&lt;/returns&gt;

distanceToPoint: function ( point ) {

return ( point.distanceTo( this.center ) - this.radius ); //傳回三維空間内一點到Sphere球體對象表面的最小長度.

///intersectsSphere方法擷取目前球體是否與參數sphere球體對象相交,傳回true 或者 false

///&lt;summary&gt;intersectsSphere&lt;/summary&gt;

///&lt;param name ="sphere" type="Sphere"&gt;一個Sphere的球體&lt;/param&gt;

intersectsSphere: function ( sphere ) {

var radiusSum = this.radius + sphere.radius;

return sphere.center.distanceToSquared( this.center ) &lt;= ( radiusSum * radiusSum ); //傳回true 或者 false

///clampPoint方法用來通過參數point收縮球體.假設point在球體外,強制将point設定到球體表面,假設point在球體内,又一次設定球體半徑為point到目前球體半徑的距離.

///&lt;summary&gt;clampPoint&lt;/summary&gt;

///&lt;param name ="optionalTarget" type="Vector3"&gt;可選參數,接收傳回結果,傳回剪裁過的邊界點&lt;/param&gt;

///&lt;returns type="Vector3"&gt;傳回剪裁過的邊界點.&lt;/returns&gt;

clampPoint: function ( point, optionalTarget ) {

var deltaLengthSq = this.center.distanceToSquared( point );

var result = optionalTarget || new THREE.Vector3();

result.copy( point );

if ( deltaLengthSq &gt; ( this.radius * this.radius ) ) {

result.sub( this.center ).normalize();

result.multiplyScalar( this.radius ).add( this.center );

return result; // 傳回剪裁過的邊界點

///getBoundingBox方法傳回目前球體的Box3立方體邊界(這裡應該外切于球體的一個立方體)

/// 與Box3類中的getBoundingSphere()方法相應.

///&lt;summary&gt;getBoundingBox&lt;/summary&gt;

///&lt;param name ="optionalTarget" type="THREE.Box3()"&gt;可選參數,THREE.Box3()立方體對象,用來接收傳回值&lt;/param&gt;

///&lt;returns type="THREE.Sphere()"&gt;傳回目前球體的Box3立方體邊界(這裡應該外切于球體的一個立方體)&lt;/returns&gt;

getBoundingBox: function ( optionalTarget ) {

var box = optionalTarget || new THREE.Box3();

box.set( this.center, this.center );

box.expandByScalar( this.radius );

return box; //傳回目前球體的Box3立方體邊界(這裡應該外切于球體的一個立方體)

///applyMatrix4方法通過傳遞matrix(旋轉,縮放,移動等變換矩陣)對目前Sphere球體對象的圓心和半徑,應用變換.

///&lt;summary&gt;applyMatrix4&lt;/summary&gt;

///&lt;param name ="matrix" type="Matrix4"&gt;(旋轉,縮放,移動等變換矩陣&lt;/param&gt;

///&lt;returns type="Boolean"&gt;傳回變換後的球體.&lt;/returns&gt;

applyMatrix4: function ( matrix ) {

this.center.applyMatrix4( matrix );

this.radius = this.radius * matrix.getMaxScaleOnAxis();

return this; //傳回變換後的球體.

///translate方法用來通過參數offset,移動目前球體的位置.

///&lt;summary&gt;translate&lt;/summary&gt;

///&lt;param name ="offset" type="Vector3"&gt;偏移量&lt;/param&gt;

///&lt;returns type="Boolean"&gt;傳回新半徑,坐标值的球體&lt;/returns&gt;

translate: function ( offset ) {

this.center.add( offset );

///equals方法用來獲得參數sphere(一個Sphere的球體)是否與目前球體全然相等,即圓心和半徑相等.

///&lt;summary&gt;equals&lt;/summary&gt;

equals: function ( sphere ) {

return sphere.center.equals( this.center ) &amp;&amp; ( sphere.radius === this.radius ); //傳回true 或者 false

/*clone方法

///clone方法克隆一個球體對象.

///&lt;summary&gt;clone&lt;/summary&gt;

///&lt;returns type="Sphere"&gt;傳回球體對象&lt;/returns&gt;

clone: function () {

return new THREE.Sphere().copy( this ); //傳回球體對象

<a target="_blank" href="http://blog.csdn.net/omni360">轉載請保留此句:商域無疆 -  本部落格專注于 靈活開發及移動和物聯裝置研究:資料可視化、GOLANG、Html5、WEBGL、THREE.JS。否則,出自本部落格的文章拒絕轉載或再轉載,謝謝合作。</a>

下面代碼是THREE.JS 源代碼檔案裡Math/Line3.js檔案的凝視.

本文轉自mfrbuaa部落格園部落格,原文連結:http://www.cnblogs.com/mfrbuaa/p/5172867.html,如需轉載請自行聯系原作者

繼續閱讀