three.js 源碼注釋(六十七)objects/PointCloud.js
來源:程序員人生 發布時間:2015-03-07 12:51:28 閱讀次數:4213次
俺也是剛開始學,好多地兒肯定不對還請見諒.
以下代碼是THREE.JS 源碼文件中objects/PointCloud.js文件的注釋.
更多更新在 : https://github.com/omni360/three.js.sourcecode
/**
* @author alteredq / http://alteredqualia.com/
*/
/*
///PointCloud點云對象,在場景中方便的改變大量的點精靈對象大小,位置等屬性.
*/
///PointCloud///Geometry對象點云對象里的點集合///PointCloudMaterial對象(點云材質對象)///返回Mesh對象THREE.PointCloud = function ( geometry, material ) {
THREE.Object3D.call( this ); //調用Object3D對象的call方法,將本來屬于Object3D的方法交給當前對象PointCloud來使用.
this.geometry = geometry !== undefined ? geometry : new THREE.Geometry(); // //將參數geometry賦值給mesh對象的geometry屬性
this.material = material !== undefined ? material : new THREE.PointCloudMaterial( { color: Math.random() * 0xffffff } ); //將參數material賦值給PointCloud對象的material屬性,如果沒有傳遞material參數,將創建隨機色彩材質,賦值給當前PointCloud對象
this.sortParticles = false; //設置是不是排序粒子??
//TODO:sortParticles屬性沒有弄明白,有時間回來處理.
};
/*************************************************
****下面是PointCloud對象的方法屬性定義,繼承自Object3D
**************************************************/
THREE.PointCloud.prototype = Object.create( THREE.Object3D.prototype );
/*
///raycast方法用來取得當前對象與射線(參數raycaster)的交點.raycaster.intersectObject會調用這個方法。主要是用來進行碰撞檢測,
/// 在選擇場景中的對象時常常會用到,判斷當前鼠標是不是與對象重適用來選擇對象.
/// NOTE:raycast方法中參數intersects參數用來存儲交點的集合,格式以下
/// intersects.push( {
///
/// distance: distance,
/// point: intersectionPoint,
/// indices: [ a, b, c ],
/// face: null,
/// faceIndex: null,
/// object: this
///
/// } );
///
*////raycast///射線對象///交點的屬性集合///交點的屬性集合THREE.PointCloud.prototype.raycast = ( function () {
var inverseMatrix = new THREE.Matrix4(); //聲明1個4x4矩陣,用來放置逆矩陣
var ray = new THREE.Ray(); //聲明全局射線對象
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();
//檢查射線與點云元素是不是碰撞的具體實現.
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對象是BufferGeometry對象 if ( geometry instanceof THREE.BufferGeometry ) { var attributes = geometry.attributes; var positions = attributes.position.array; //下面對3種數據格式的pointCloud對象的元素進行檢測. 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個PointCloud點云對象. */ ///clone///接收克隆的Object3D對象///返回PointCloud對象.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對象
};
// Backwards compatibility 向后兼容,粒子系統唄點云對象替換.用法和THREE.PointCloud對象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/)
本文遵守“署名-非商業用處-保持1致”創作公用協議
轉載請保存此句:商域無疆 - 本博客專注于 敏捷開發及移動和物聯裝備研究:數據可視化、GOLANG、Html5、WEBGL、THREE.JS,否則,出自本博客的文章謝絕轉載或再轉載,謝謝合作。
以下代碼是THREE.JS 源碼文件中objects/PointCloud.js文件的注釋.
更多更新在 : https://github.com/omni360/three.js.sourcecode
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈