CSS2DRenderer.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. hideObject( scene );
  60. renderObject( scene, scene, camera );
  61. zOrder( scene );
  62. };
  63. this.setSize = function ( width, height ) {
  64. _width = width;
  65. _height = height;
  66. _widthHalf = _width / 2;
  67. _heightHalf = _height / 2;
  68. domElement.style.width = width + 'px';
  69. domElement.style.height = height + 'px';
  70. };
  71. function hideObject( object ) {
  72. if ( object.isCSS2DObject ) object.element.style.display = 'none';
  73. for ( let i = 0, l = object.children.length; i < l; i ++ ) {
  74. hideObject( object.children[ i ] );
  75. }
  76. }
  77. function renderObject( object, scene, camera ) {
  78. if ( object.visible === false ) return;
  79. if ( object.isCSS2DObject ) {
  80. _vector.setFromMatrixPosition( object.matrixWorld );
  81. _vector.applyMatrix4( _viewProjectionMatrix );
  82. const visible = ( _vector.z >= - 1 && _vector.z <= 1 ) && ( object.layers.test( camera.layers ) === true );
  83. if ( visible === true ) {
  84. object.onBeforeRender( _this, scene, camera );
  85. const element = object.element;
  86. element.style.display = '';
  87. element.style.transform = 'translate(' + ( - 100 * object.center.x ) + '%,' + ( - 100 * object.center.y ) + '%)' + 'translate(' + ( _vector.x * _widthHalf + _widthHalf ) + 'px,' + ( - _vector.y * _heightHalf + _heightHalf ) + 'px)';
  88. if ( element.parentNode !== domElement ) {
  89. domElement.appendChild( element );
  90. }
  91. object.onAfterRender( _this, scene, camera );
  92. }
  93. const objectData = {
  94. distanceToCameraSquared: getDistanceToSquared( camera, object )
  95. };
  96. cache.objects.set( object, objectData );
  97. }
  98. for ( let i = 0, l = object.children.length; i < l; i ++ ) {
  99. renderObject( object.children[ i ], scene, camera );
  100. }
  101. }
  102. function getDistanceToSquared( object1, object2 ) {
  103. _a.setFromMatrixPosition( object1.matrixWorld );
  104. _b.setFromMatrixPosition( object2.matrixWorld );
  105. return _a.distanceToSquared( _b );
  106. }
  107. function filterAndFlatten( scene ) {
  108. const result = [];
  109. scene.traverse( function ( object ) {
  110. if ( object.isCSS2DObject ) result.push( object );
  111. } );
  112. return result;
  113. }
  114. function zOrder( scene ) {
  115. const sorted = filterAndFlatten( scene ).sort( function ( a, b ) {
  116. if ( a.renderOrder !== b.renderOrder ) {
  117. return b.renderOrder - a.renderOrder;
  118. }
  119. const distanceA = cache.objects.get( a ).distanceToCameraSquared;
  120. const distanceB = cache.objects.get( b ).distanceToCameraSquared;
  121. return distanceA - distanceB;
  122. } );
  123. const zMax = sorted.length;
  124. for ( let i = 0, l = sorted.length; i < l; i ++ ) {
  125. sorted[ i ].element.style.zIndex = zMax - i;
  126. }
  127. }
  128. }
  129. }
  130. export { CSS2DObject, CSS2DRenderer };