LOD.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.objects = [];
  9. };
  10. THREE.LOD.prototype = Object.create( THREE.Object3D.prototype );
  11. THREE.LOD.prototype.addLevel = function ( object, distance ) {
  12. if ( distance === undefined ) distance = 0;
  13. distance = Math.abs( distance );
  14. for ( var l = 0; l < this.objects.length; l ++ ) {
  15. if ( distance < this.objects[ l ].distance ) {
  16. break;
  17. }
  18. }
  19. this.objects.splice( l, 0, { distance: distance, object: object } );
  20. this.add( object );
  21. };
  22. THREE.LOD.prototype.getObjectForDistance = function ( distance ) {
  23. for ( var i = 1, l = this.objects.length; i < l; i ++ ) {
  24. if ( distance < this.objects[ i ].distance ) {
  25. break;
  26. }
  27. }
  28. return this.objects[ i - 1 ].object;
  29. };
  30. THREE.LOD.prototype.update = function () {
  31. var v1 = new THREE.Vector3();
  32. var v2 = new THREE.Vector3();
  33. return function ( camera ) {
  34. if ( this.objects.length > 1 ) {
  35. v1.getPositionFromMatrix( camera.matrixWorld );
  36. v2.getPositionFromMatrix( this.matrixWorld );
  37. var distance = v1.distanceTo( v2 );
  38. this.objects[ 0 ].object.visible = true;
  39. for ( var i = 1, l = this.objects.length; i < l; i ++ ) {
  40. if ( distance >= this.objects[ i ].distance ) {
  41. this.objects[ i - 1 ].object.visible = false;
  42. this.objects[ i ].object.visible = true;
  43. } else {
  44. break;
  45. }
  46. }
  47. for( ; i < l; i ++ ) {
  48. this.objects[ i ].object.visible = false;
  49. }
  50. }
  51. };
  52. }();
  53. THREE.LOD.prototype.clone = function () {
  54. // TODO
  55. };
粤ICP备19079148号