LOD.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. this.LODs = [];
  9. };
  10. THREE.LOD.prototype = Object.create( THREE.Object3D.prototype );
  11. THREE.LOD.prototype.addLevel = function ( object3D, visibleAtDistance ) {
  12. if ( visibleAtDistance === undefined ) {
  13. visibleAtDistance = 0;
  14. }
  15. visibleAtDistance = Math.abs( visibleAtDistance );
  16. for ( var l = 0; l < this.LODs.length; l ++ ) {
  17. if ( visibleAtDistance < this.LODs[ l ].visibleAtDistance ) {
  18. break;
  19. }
  20. }
  21. this.LODs.splice( l, 0, { visibleAtDistance: visibleAtDistance, object3D: object3D } );
  22. this.add( object3D );
  23. };
  24. THREE.LOD.prototype.update = function ( camera ) {
  25. if ( this.LODs.length > 1 ) {
  26. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  27. var inverse = camera.matrixWorldInverse;
  28. var distance = -( inverse.elements[2] * this.matrixWorld.elements[12] + inverse.elements[6] * this.matrixWorld.elements[13] + inverse.elements[10] * this.matrixWorld.elements[14] + inverse.elements[14] );
  29. this.LODs[ 0 ].object3D.visible = true;
  30. for ( var l = 1; l < this.LODs.length; l ++ ) {
  31. if( distance >= this.LODs[ l ].visibleAtDistance ) {
  32. this.LODs[ l - 1 ].object3D.visible = false;
  33. this.LODs[ l ].object3D.visible = true;
  34. } else {
  35. break;
  36. }
  37. }
  38. for( ; l < this.LODs.length; l ++ ) {
  39. this.LODs[ l ].object3D.visible = false;
  40. }
  41. }
  42. };
粤ICP备19079148号