CSS2DRenderer.js 4.3 KB

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