three.js 源碼注釋(六十七)objects/PointCloud.js
來源:程序員人生 發(fā)布時(shí)間:2015-03-07 12:51:28 閱讀次數(shù):4223次
俺也是剛開始學(xué),好多地兒肯定不對(duì)還請(qǐng)見諒.
以下代碼是THREE.JS 源碼文件中objects/PointCloud.js文件的注釋.
更多更新在 : https://github.com/omni360/three.js.sourcecode
/**
* @author alteredq / http://alteredqualia.com/
*/
/*
///PointCloud點(diǎn)云對(duì)象,在場景中方便的改變大量的點(diǎn)精靈對(duì)象大小,位置等屬性.
*/
///PointCloud///Geometry對(duì)象點(diǎn)云對(duì)象里的點(diǎn)集合///PointCloudMaterial對(duì)象(點(diǎn)云材質(zhì)對(duì)象)///返回Mesh對(duì)象THREE.PointCloud = function ( geometry, material ) {
THREE.Object3D.call( this ); //調(diào)用Object3D對(duì)象的call方法,將本來屬于Object3D的方法交給當(dāng)前對(duì)象PointCloud來使用.
this.geometry = geometry !== undefined ? geometry : new THREE.Geometry(); // //將參數(shù)geometry賦值給mesh對(duì)象的geometry屬性
this.material = material !== undefined ? material : new THREE.PointCloudMaterial( { color: Math.random() * 0xffffff } ); //將參數(shù)material賦值給PointCloud對(duì)象的material屬性,如果沒有傳遞material參數(shù),將創(chuàng)建隨機(jī)色彩材質(zhì),賦值給當(dāng)前PointCloud對(duì)象
this.sortParticles = false; //設(shè)置是不是排序粒子??
//TODO:sortParticles屬性沒有弄明白,有時(shí)間回來處理.
};
/*************************************************
****下面是PointCloud對(duì)象的方法屬性定義,繼承自O(shè)bject3D
**************************************************/
THREE.PointCloud.prototype = Object.create( THREE.Object3D.prototype );
/*
///raycast方法用來取得當(dāng)前對(duì)象與射線(參數(shù)raycaster)的交點(diǎn).raycaster.intersectObject會(huì)調(diào)用這個(gè)方法。主要是用來進(jìn)行碰撞檢測,
/// 在選擇場景中的對(duì)象時(shí)常常會(huì)用到,判斷當(dāng)前鼠標(biāo)是不是與對(duì)象重適用來選擇對(duì)象.
/// NOTE:raycast方法中參數(shù)intersects參數(shù)用來存儲(chǔ)交點(diǎn)的集合,格式以下
/// intersects.push( {
///
/// distance: distance,
/// point: intersectionPoint,
/// indices: [ a, b, c ],
/// face: null,
/// faceIndex: null,
/// object: this
///
/// } );
///
*////raycast///射線對(duì)象///交點(diǎn)的屬性集合///交點(diǎn)的屬性集合THREE.PointCloud.prototype.raycast = ( function () {
var inverseMatrix = new THREE.Matrix4(); //聲明1個(gè)4x4矩陣,用來放置逆矩陣
var ray = new THREE.Ray(); //聲明全局射線對(duì)象
return function ( raycaster, intersects ) {
var object = this;
var geometry = object.geometry;
var threshold = raycaster.params.PointCloud.threshold;
inverseMatrix.getInverse( this.matrixWorld );
ray.copy( raycaster.ray ).applyMatrix4( inverseMatrix );
if ( geometry.boundingBox !== null ) {
if ( ray.isIntersectionBox( geometry.boundingBox ) === false ) {
return;
}
}
var localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
var position = new THREE.Vector3();
//檢查射線與點(diǎn)云元素是不是碰撞的具體實(shí)現(xiàn).
var testPoint = function ( point, index ) {
var rayPointDistance = ray.distanceToPoint( point );
if ( rayPointDistance < localThreshold ) { var intersectPoint = ray.closestPointToPoint( point ); intersectPoint.applyMatrix4( object.matrixWorld ); var distance = raycaster.ray.origin.distanceTo( intersectPoint ); intersects.push( { distance: distance, distanceToRay: rayPointDistance, point: intersectPoint.clone(), index: index, face: null, object: object } ); } }; //如果geometry對(duì)象是BufferGeometry對(duì)象 if ( geometry instanceof THREE.BufferGeometry ) { var attributes = geometry.attributes; var positions = attributes.position.array; //下面對(duì)3種數(shù)據(jù)格式的pointCloud對(duì)象的元素進(jìn)行檢測. if ( attributes.index !== undefined ) { var indices = attributes.index.array; var offsets = geometry.offsets; if ( offsets.length === 0 ) { var offset = { start: 0, count: indices.length, index: 0 }; offsets = [ offset ]; } for ( var oi = 0, ol = offsets.length; oi < ol; ++oi ) { var start = offsets[ oi ].start; var count = offsets[ oi ].count; var index = offsets[ oi ].index; for ( var i = start, il = start + count; i < il; i ++ ) { var a = index + indices[ i ]; position.set( positions[ a * 3 ], positions[ a * 3 + 1 ], positions[ a * 3 + 2 ] ); testPoint( position, a ); } } } else { var pointCount = positions.length / 3; for ( var i = 0; i < pointCount; i ++ ) { position.set( positions[ 3 * i ], positions[ 3 * i + 1 ], positions[ 3 * i + 2 ] ); testPoint( position, i ); } } } else { var vertices = this.geometry.vertices; for ( var i = 0; i < vertices.length; i ++ ) { testPoint( vertices[ i ], i ); } } }; }() ); /*clone方法 ///clone方法克隆1個(gè)PointCloud點(diǎn)云對(duì)象. */ ///clone///接收克隆的Object3D對(duì)象///返回PointCloud對(duì)象.THREE.PointCloud.prototype.clone = function ( object ) {
if ( object === undefined ) object = new THREE.PointCloud( this.geometry, this.material );
object.sortParticles = this.sortParticles;
THREE.Object3D.prototype.clone.call( this, object ); //繼承Object3D的clone方法
return object; //返回克隆的PointCloud對(duì)象
};
// Backwards compatibility 向后兼容,粒子系統(tǒng)唄點(diǎn)云對(duì)象替換.用法和THREE.PointCloud對(duì)象1樣.
THREE.ParticleSystem = function ( geometry, material ) {
console.warn( 'THREE.ParticleSystem has been renamed to THREE.PointCloud.' );
return new THREE.PointCloud( geometry, material );
};
商域無疆 (http://blog.csdn.net/omni360/)
本文遵守“署名-非商業(yè)用處-保持1致”創(chuàng)作公用協(xié)議
轉(zhuǎn)載請(qǐng)保存此句:商域無疆 - 本博客專注于 敏捷開發(fā)及移動(dòng)和物聯(lián)裝備研究:數(shù)據(jù)可視化、GOLANG、Html5、WEBGL、THREE.JS,否則,出自本博客的文章謝絕轉(zhuǎn)載或再轉(zhuǎn)載,謝謝合作。
以下代碼是THREE.JS 源碼文件中objects/PointCloud.js文件的注釋.
更多更新在 : https://github.com/omni360/three.js.sourcecode
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)