CSS2DRenderer.js 4.4 KB

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