LOD.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * @author mikael emtinger / http://gomo.se/
  3. */
  4. THREE.LOD = function() {
  5. THREE.Object3D.call( this );
  6. this.LODs = [];
  7. };
  8. THREE.LOD.prototype = new THREE.Object3D();
  9. THREE.LOD.prototype.constructor = THREE.LOD;
  10. THREE.LOD.prototype.supr = THREE.Object3D.prototype;
  11. /*
  12. * Add
  13. */
  14. THREE.LOD.prototype.add = function ( object3D, visibleAtDistance ) {
  15. if ( visibleAtDistance === undefined ) {
  16. visibleAtDistance = 0;
  17. }
  18. visibleAtDistance = Math.abs( visibleAtDistance );
  19. for ( var l = 0; l < this.LODs.length; l++ ) {
  20. if ( visibleAtDistance < this.LODs[ l ].visibleAtDistance ) {
  21. break;
  22. }
  23. }
  24. this.LODs.splice( l, 0, { visibleAtDistance: visibleAtDistance, object3D: object3D } );
  25. this.addChild( object3D );
  26. };
  27. /*
  28. * Update
  29. */
  30. THREE.LOD.prototype.update = function ( parentMatrixWorld, forceUpdate, camera ) {
  31. // update local
  32. if ( this.matrixAutoUpdate ) {
  33. forceUpdate |= this.updateMatrix();
  34. }
  35. // update global
  36. if ( forceUpdate || this.matrixNeedsUpdate ) {
  37. if ( parentMatrixWorld ) {
  38. this.matrixWorld.multiply( parentMatrixWorld, this.matrix );
  39. } else {
  40. this.matrixWorld.copy( this.matrix );
  41. }
  42. this.matrixNeedsUpdate = false;
  43. forceUpdate = true;
  44. }
  45. // update LODs
  46. if ( this.LODs.length > 1 ) {
  47. var inverse = camera.matrixWorldInverse;
  48. var radius = this.boundRadius * this.boundRadiusScale;
  49. var distance = -( inverse.n31 * this.position.x + inverse.n32 * this.position.y + inverse.n33 * this.position.z + inverse.n34 );
  50. this.LODs[ 0 ].object3D.visible = true;
  51. for ( var l = 1; l < this.LODs.length; l++ ) {
  52. if( distance >= this.LODs[ l ].visibleAtDistance ) {
  53. this.LODs[ l - 1 ].object3D.visible = false;
  54. this.LODs[ l ].object3D.visible = true;
  55. } else {
  56. break;
  57. }
  58. }
  59. for( ; l < this.LODs.length; l++ ) {
  60. this.LODs[ l ].object3D.visible = false;
  61. }
  62. }
  63. // update children
  64. for ( var c = 0; c < this.children.length; c++ ) {
  65. this.children[ c ].update( this.matrixWorld, forceUpdate, camera );
  66. }
  67. };