CSS2DRenderer.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import {
  2. Matrix4,
  3. Object3D,
  4. Vector3
  5. } from '../../../build/three.module.js';
  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. object.onBeforeRender( _this, scene, camera );
  70. _vector.setFromMatrixPosition( object.matrixWorld );
  71. _vector.applyMatrix4( _viewProjectionMatrix );
  72. const element = object.element;
  73. if ( /apple/i.test( navigator.vendor ) ) {
  74. // https://github.com/mrdoob/three.js/issues/21415
  75. element.style.transform = 'translate(-50%,-50%) translate(' + Math.round( _vector.x * _widthHalf + _widthHalf ) + 'px,' + Math.round( - _vector.y * _heightHalf + _heightHalf ) + 'px)';
  76. } else {
  77. element.style.transform = 'translate(-50%,-50%) translate(' + ( _vector.x * _widthHalf + _widthHalf ) + 'px,' + ( - _vector.y * _heightHalf + _heightHalf ) + 'px)';
  78. }
  79. element.style.display = ( object.visible && _vector.z >= - 1 && _vector.z <= 1 ) ? '' : 'none';
  80. const objectData = {
  81. distanceToCameraSquared: getDistanceToSquared( camera, object )
  82. };
  83. cache.objects.set( object, objectData );
  84. if ( element.parentNode !== domElement ) {
  85. domElement.appendChild( element );
  86. }
  87. object.onAfterRender( _this, scene, camera );
  88. }
  89. for ( let i = 0, l = object.children.length; i < l; i ++ ) {
  90. renderObject( object.children[ i ], scene, camera );
  91. }
  92. }
  93. function getDistanceToSquared( object1, object2 ) {
  94. _a.setFromMatrixPosition( object1.matrixWorld );
  95. _b.setFromMatrixPosition( object2.matrixWorld );
  96. return _a.distanceToSquared( _b );
  97. }
  98. function filterAndFlatten( scene ) {
  99. const result = [];
  100. scene.traverse( function ( object ) {
  101. if ( object.isCSS2DObject ) result.push( object );
  102. } );
  103. return result;
  104. }
  105. function zOrder( scene ) {
  106. const sorted = filterAndFlatten( scene ).sort( function ( a, b ) {
  107. const distanceA = cache.objects.get( a ).distanceToCameraSquared;
  108. const distanceB = cache.objects.get( b ).distanceToCameraSquared;
  109. return distanceA - distanceB;
  110. } );
  111. const zMax = sorted.length;
  112. for ( let i = 0, l = sorted.length; i < l; i ++ ) {
  113. sorted[ i ].element.style.zIndex = zMax - i;
  114. }
  115. }
  116. }
  117. }
  118. export { CSS2DObject, CSS2DRenderer };