LOD.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * @author mikael emtinger / http://gomo.se/
  3. * @author alteredq / http://alteredqualia.com/
  4. * @author mrdoob / http://mrdoob.com/
  5. */
  6. THREE.LOD = function () {
  7. THREE.Object3D.call( this );
  8. Object.defineProperties( this, {
  9. levels: {
  10. enumerable: true,
  11. value: []
  12. },
  13. objects: {
  14. get: function () {
  15. console.warn( 'THREE.LOD: .objects has been renamed to .levels.' );
  16. return this.levels;
  17. }
  18. }
  19. } );
  20. };
  21. THREE.LOD.prototype = Object.create( THREE.Object3D.prototype );
  22. THREE.LOD.prototype.constructor = THREE.LOD;
  23. THREE.LOD.prototype.addLevel = function ( object, distance ) {
  24. if ( distance === undefined ) distance = 0;
  25. distance = Math.abs( distance );
  26. var levels = this.levels;
  27. for ( var l = 0; l < levels.length; l ++ ) {
  28. if ( distance < levels[ l ].distance ) {
  29. break;
  30. }
  31. }
  32. levels.splice( l, 0, { distance: distance, object: object } );
  33. this.add( object );
  34. };
  35. THREE.LOD.prototype.getObjectForDistance = function ( distance ) {
  36. var levels = this.levels;
  37. for ( var i = 1, l = levels.length; i < l; i ++ ) {
  38. if ( distance < levels[ i ].distance ) {
  39. break;
  40. }
  41. }
  42. return levels[ i - 1 ].object;
  43. };
  44. THREE.LOD.prototype.raycast = ( function () {
  45. var matrixPosition = new THREE.Vector3();
  46. return function raycast( raycaster, intersects ) {
  47. matrixPosition.setFromMatrixPosition( this.matrixWorld );
  48. var distance = raycaster.ray.origin.distanceTo( matrixPosition );
  49. this.getObjectForDistance( distance ).raycast( raycaster, intersects );
  50. };
  51. }() );
  52. THREE.LOD.prototype.update = function () {
  53. var v1 = new THREE.Vector3();
  54. var v2 = new THREE.Vector3();
  55. return function update( camera ) {
  56. var levels = this.levels;
  57. if ( levels.length > 1 ) {
  58. v1.setFromMatrixPosition( camera.matrixWorld );
  59. v2.setFromMatrixPosition( this.matrixWorld );
  60. var distance = v1.distanceTo( v2 );
  61. levels[ 0 ].object.visible = true;
  62. for ( var i = 1, l = levels.length; i < l; i ++ ) {
  63. if ( distance >= levels[ i ].distance ) {
  64. levels[ i - 1 ].object.visible = false;
  65. levels[ i ].object.visible = true;
  66. } else {
  67. break;
  68. }
  69. }
  70. for ( ; i < l; i ++ ) {
  71. levels[ i ].object.visible = false;
  72. }
  73. }
  74. };
  75. }();
  76. THREE.LOD.prototype.clone = function () {
  77. var lod = new THREE.LOD();
  78. return lod.copy( this );
  79. };
  80. THREE.LOD.prototype.copy = function ( source ) {
  81. THREE.Object3D.prototype.copy.call( this, source, false );
  82. var levels = source.levels;
  83. for ( var i = 0, l = levels.length; i < l; i ++ ) {
  84. var level = levels[ i ];
  85. this.addLevel( level.object.clone(), level.distance );
  86. }
  87. return this;
  88. };
粤ICP备19079148号