CSS2DRenderer.js 4.3 KB

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