CSS2DRenderer.js 4.6 KB

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