CSS2DRenderer.js 4.1 KB

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