CSS2DRenderer.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. ( function () {
  2. class CSS2DObject extends THREE.Object3D {
  3. constructor( element = document.createElement( 'div' ) ) {
  4. super();
  5. this.element = element;
  6. this.element.style.position = 'absolute';
  7. this.element.style.userSelect = 'none';
  8. this.element.setAttribute( 'draggable', false );
  9. this.addEventListener( 'removed', function () {
  10. this.traverse( function ( object ) {
  11. if ( object.element instanceof Element && object.element.parentNode !== null ) {
  12. object.element.parentNode.removeChild( object.element );
  13. }
  14. } );
  15. } );
  16. }
  17. copy( source, recursive ) {
  18. super.copy( source, recursive );
  19. this.element = source.element.cloneNode( true );
  20. return this;
  21. }
  22. }
  23. CSS2DObject.prototype.isCSS2DObject = true; //
  24. const _vector = new THREE.Vector3();
  25. const _viewMatrix = new THREE.Matrix4();
  26. const _viewProjectionMatrix = new THREE.Matrix4();
  27. const _a = new THREE.Vector3();
  28. const _b = new THREE.Vector3();
  29. class CSS2DRenderer {
  30. constructor( parameters = {} ) {
  31. const _this = this;
  32. let _width, _height;
  33. let _widthHalf, _heightHalf;
  34. const cache = {
  35. objects: new WeakMap()
  36. };
  37. const domElement = parameters.element !== undefined ? parameters.element : document.createElement( 'div' );
  38. domElement.style.overflow = 'hidden';
  39. this.domElement = domElement;
  40. this.getSize = function () {
  41. return {
  42. width: _width,
  43. height: _height
  44. };
  45. };
  46. this.render = function ( scene, camera ) {
  47. if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
  48. if ( camera.parent === null ) camera.updateMatrixWorld();
  49. _viewMatrix.copy( camera.matrixWorldInverse );
  50. _viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, _viewMatrix );
  51. renderObject( scene, scene, camera );
  52. zOrder( scene );
  53. };
  54. this.setSize = function ( width, height ) {
  55. _width = width;
  56. _height = height;
  57. _widthHalf = _width / 2;
  58. _heightHalf = _height / 2;
  59. domElement.style.width = width + 'px';
  60. domElement.style.height = height + 'px';
  61. };
  62. function renderObject( object, scene, camera ) {
  63. if ( object.isCSS2DObject ) {
  64. const visible = object.visible && _vector.z >= - 1 && _vector.z <= 1 && object.layers.test( camera.layers );
  65. object.element.style.display = visible ? '' : 'none';
  66. if ( visible ) {
  67. object.onBeforeRender( _this, scene, camera );
  68. _vector.setFromMatrixPosition( object.matrixWorld );
  69. _vector.applyMatrix4( _viewProjectionMatrix );
  70. const element = object.element;
  71. if ( /apple/i.test( navigator.vendor ) ) {
  72. // https://github.com/mrdoob/three.js/issues/21415
  73. element.style.transform = 'translate(-50%,-50%) translate(' + Math.round( _vector.x * _widthHalf + _widthHalf ) + 'px,' + Math.round( - _vector.y * _heightHalf + _heightHalf ) + 'px)';
  74. } else {
  75. element.style.transform = 'translate(-50%,-50%) translate(' + ( _vector.x * _widthHalf + _widthHalf ) + 'px,' + ( - _vector.y * _heightHalf + _heightHalf ) + 'px)';
  76. }
  77. if ( element.parentNode !== domElement ) {
  78. domElement.appendChild( element );
  79. }
  80. object.onAfterRender( _this, scene, camera );
  81. }
  82. const objectData = {
  83. distanceToCameraSquared: getDistanceToSquared( camera, object )
  84. };
  85. cache.objects.set( object, objectData );
  86. }
  87. for ( let i = 0, l = object.children.length; i < l; i ++ ) {
  88. renderObject( object.children[ i ], scene, camera );
  89. }
  90. }
  91. function getDistanceToSquared( object1, object2 ) {
  92. _a.setFromMatrixPosition( object1.matrixWorld );
  93. _b.setFromMatrixPosition( object2.matrixWorld );
  94. return _a.distanceToSquared( _b );
  95. }
  96. function filterAndFlatten( scene ) {
  97. const result = [];
  98. scene.traverse( function ( object ) {
  99. if ( object.isCSS2DObject ) result.push( object );
  100. } );
  101. return result;
  102. }
  103. function zOrder( scene ) {
  104. const sorted = filterAndFlatten( scene ).sort( function ( a, b ) {
  105. if ( a.renderOrder !== b.renderOrder ) {
  106. return b.renderOrder - a.renderOrder;
  107. }
  108. const distanceA = cache.objects.get( a ).distanceToCameraSquared;
  109. const distanceB = cache.objects.get( b ).distanceToCameraSquared;
  110. return distanceA - distanceB;
  111. } );
  112. const zMax = sorted.length;
  113. for ( let i = 0, l = sorted.length; i < l; i ++ ) {
  114. sorted[ i ].element.style.zIndex = zMax - i;
  115. }
  116. }
  117. }
  118. }
  119. THREE.CSS2DObject = CSS2DObject;
  120. THREE.CSS2DRenderer = CSS2DRenderer;
  121. } )();