CSS2DRenderer.js 4.0 KB

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