CSS2DRenderer.js 4.6 KB

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