//对lod的源码进行了注释
/**
* @author mikael emtinger / http://gomo.se/
* @author alteredq / http://alteredqualia.com/
* @author mrdoob / http://mrdoob.com/
*/
// 目录src/objects/LOD.js
<script>
THREE.LOD = function ()
{
THREE.Object3D.call( this );
this.objects = [];
};
THREE.LOD.prototype = Object.create( THREE.Object3D.prototype );
//添加细节
THREE.LOD.prototype.addLevel = function ( object, distance )
{
if ( distance === undefined )
distance = 0;//未定义距离时为0
distance = Math.abs( distance );//取距离的绝对值
//将各个细节的距离进行从小到大的排序
for ( var l = 0; l < this.objects.length; l ++ )
{
if ( distance < this.objects[ l ].distance )
{
break;
}
}
this.objects.splice( l, 0, { distance: distance, object: object } );//?objects.splice
this.add( object );
};
//根据距离返回对应层次的对象
THREE.LOD.prototype.getObjectForDistance = function ( distance )
{
//排序由小到大遍历
for ( var i = 1, l = this.objects.length; i < l; i ++ )
{
if ( distance < this.objects[ i ].distance )
{
break;
}
}
return this.objects[ i - 1 ].object;
};
//更新显示的层次
THREE.LOD.prototype.update = function ()
{
var v1 = new THREE.Vector3();//点1
var v2 = new THREE.Vector3();//点2
return function ( camera )
{
//当细节层次不为1时
if ( this.objects.length > 1 )
{
v1.setFromMatrixPosition( camera.matrixWorld );//相机的矩阵
v2.setFromMatrixPosition( this.matrixWorld );//LOD的矩阵
var distance = v1.distanceTo( v2 );//相机到LOD的距离
this.objects[ 0 ].object.visible = true;//第一层(细节最详细)的可见性为true
//向细节不详细的层次距离遍历
for ( var i = 1, l = this.objects.length; i < l; i ++ )
{
//距离小的详细程度高的可见度为false
if ( distance >= this.objects[ i ].distance )
{
this.objects[ i - 1 ].object.visible = false;
this.objects[ i ].object.visible = true;
}
//到达对应的详细程度的距离区间后跳出
else
{
break;
}
}
//对应级别向后的可见度为false
for( ; i < l; i ++ )
{
this.objects[ i ].object.visible = false;
}
}
};
}();
THREE.LOD.prototype.clone = function ( object )
{
//未定义时声明lod
if ( object === undefined )
object = new THREE.LOD();
//将LOD复制到object中
THREE.Object3D.prototype.clone.call( this, object);
//依次添加每个细节到object中
for ( var i = 0, l = this.objects.length; i < l; i ++ )
{
var x = this.objects[i].object.clone();
x.visible = i === 0;
object.addLevel( x, this.objects[i].distance );
}
return object;
};
</script>
Three.js(二)LOD源码注释
最新推荐文章于 2024-09-27 10:00:00 发布