CSS2DRenderer.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. import {
  2. Matrix4,
  3. Object3D,
  4. Vector2,
  5. Vector3
  6. } from 'three';
  7. /**
  8. * The only type of 3D object that is supported by {@link CSS2DRenderer}.
  9. *
  10. * @augments Object3D
  11. */
  12. class CSS2DObject extends Object3D {
  13. /**
  14. * Constructs a new CSS2D object.
  15. *
  16. * @param {DOMElement} [element] - The DOM element.
  17. */
  18. constructor( element = document.createElement( 'div' ) ) {
  19. super();
  20. /**
  21. * This flag can be used for type testing.
  22. *
  23. * @type {boolean}
  24. * @readonly
  25. * @default true
  26. */
  27. this.isCSS2DObject = true;
  28. /**
  29. * The DOM element which defines the appearance of this 3D object.
  30. *
  31. * @type {DOMElement}
  32. * @readonly
  33. * @default true
  34. */
  35. this.element = element;
  36. this.element.style.position = 'absolute';
  37. this.element.style.userSelect = 'none';
  38. this.element.setAttribute( 'draggable', false );
  39. /**
  40. * The 3D objects center point.
  41. * `( 0, 0 )` is the lower left, `( 1, 1 )` is the top right.
  42. *
  43. * @type {Vector2}
  44. * @default (0.5,0.5)
  45. */
  46. this.center = new Vector2( 0.5, 0.5 );
  47. this.addEventListener( 'removed', function () {
  48. this.traverse( function ( object ) {
  49. if (
  50. object.element instanceof object.element.ownerDocument.defaultView.Element &&
  51. object.element.parentNode !== null
  52. ) {
  53. object.element.remove();
  54. }
  55. } );
  56. } );
  57. }
  58. copy( source, recursive ) {
  59. super.copy( source, recursive );
  60. this.element = source.element.cloneNode( true );
  61. this.center = source.center;
  62. return this;
  63. }
  64. }
  65. //
  66. const _vector = new Vector3();
  67. const _viewMatrix = new Matrix4();
  68. const _viewProjectionMatrix = new Matrix4();
  69. const _a = new Vector3();
  70. const _b = new Vector3();
  71. /**
  72. * This renderer is a simplified version of {@link CSS3DRenderer}. The only transformation that is
  73. * supported is translation.
  74. *
  75. * The renderer is very useful if you want to combine HTML based labels with 3D objects. Here too,
  76. * the respective DOM elements are wrapped into an instance of {@link CSS2DObject} and added to the
  77. * scene graph. All other types of renderable 3D objects (like meshes or point clouds) are ignored.
  78. *
  79. * `CSS2DRenderer` only supports 100% browser and display zoom.
  80. */
  81. class CSS2DRenderer {
  82. /**
  83. * Constructs a new CSS2D renderer.
  84. *
  85. * @param {CSS2DRenderer~Parameters} [parameters] - The parameters.
  86. */
  87. constructor( parameters = {} ) {
  88. const _this = this;
  89. let _width, _height;
  90. let _widthHalf, _heightHalf;
  91. const cache = {
  92. objects: new WeakMap()
  93. };
  94. const domElement = parameters.element !== undefined ? parameters.element : document.createElement( 'div' );
  95. domElement.style.overflow = 'hidden';
  96. /**
  97. * The DOM where the renderer appends its child-elements.
  98. *
  99. * @type {DOMElement}
  100. */
  101. this.domElement = domElement;
  102. /**
  103. * Returns an object containing the width and height of the renderer.
  104. *
  105. * @return {{width:number,height:number}} The size of the renderer.
  106. */
  107. this.getSize = function () {
  108. return {
  109. width: _width,
  110. height: _height
  111. };
  112. };
  113. /**
  114. * Renders the given scene using the given camera.
  115. *
  116. * @param {Object3D} scene - A scene or any other type of 3D object.
  117. * @param {Camera} camera - The camera.
  118. */
  119. this.render = function ( scene, camera ) {
  120. if ( scene.matrixWorldAutoUpdate === true ) scene.updateMatrixWorld();
  121. if ( camera.parent === null && camera.matrixWorldAutoUpdate === true ) camera.updateMatrixWorld();
  122. _viewMatrix.copy( camera.matrixWorldInverse );
  123. _viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, _viewMatrix );
  124. renderObject( scene, scene, camera );
  125. zOrder( scene );
  126. };
  127. /**
  128. * Resizes the renderer to the given width and height.
  129. *
  130. * @param {number} width - The width of the renderer.
  131. * @param {number} height - The height of the renderer.
  132. */
  133. this.setSize = function ( width, height ) {
  134. _width = width;
  135. _height = height;
  136. _widthHalf = _width / 2;
  137. _heightHalf = _height / 2;
  138. domElement.style.width = width + 'px';
  139. domElement.style.height = height + 'px';
  140. };
  141. function hideObject( object ) {
  142. if ( object.isCSS2DObject ) object.element.style.display = 'none';
  143. for ( let i = 0, l = object.children.length; i < l; i ++ ) {
  144. hideObject( object.children[ i ] );
  145. }
  146. }
  147. function renderObject( object, scene, camera ) {
  148. if ( object.visible === false ) {
  149. hideObject( object );
  150. return;
  151. }
  152. if ( object.isCSS2DObject ) {
  153. _vector.setFromMatrixPosition( object.matrixWorld );
  154. _vector.applyMatrix4( _viewProjectionMatrix );
  155. const visible = ( _vector.z >= - 1 && _vector.z <= 1 ) && ( object.layers.test( camera.layers ) === true );
  156. const element = object.element;
  157. element.style.display = visible === true ? '' : 'none';
  158. if ( visible === true ) {
  159. object.onBeforeRender( _this, scene, camera );
  160. element.style.transform = 'translate(' + ( - 100 * object.center.x ) + '%,' + ( - 100 * object.center.y ) + '%)' + 'translate(' + ( _vector.x * _widthHalf + _widthHalf ) + 'px,' + ( - _vector.y * _heightHalf + _heightHalf ) + 'px)';
  161. if ( element.parentNode !== domElement ) {
  162. domElement.appendChild( element );
  163. }
  164. object.onAfterRender( _this, scene, camera );
  165. }
  166. const objectData = {
  167. distanceToCameraSquared: getDistanceToSquared( camera, object )
  168. };
  169. cache.objects.set( object, objectData );
  170. }
  171. for ( let i = 0, l = object.children.length; i < l; i ++ ) {
  172. renderObject( object.children[ i ], scene, camera );
  173. }
  174. }
  175. function getDistanceToSquared( object1, object2 ) {
  176. _a.setFromMatrixPosition( object1.matrixWorld );
  177. _b.setFromMatrixPosition( object2.matrixWorld );
  178. return _a.distanceToSquared( _b );
  179. }
  180. function filterAndFlatten( scene ) {
  181. const result = [];
  182. scene.traverseVisible( function ( object ) {
  183. if ( object.isCSS2DObject ) result.push( object );
  184. } );
  185. return result;
  186. }
  187. function zOrder( scene ) {
  188. const sorted = filterAndFlatten( scene ).sort( function ( a, b ) {
  189. if ( a.renderOrder !== b.renderOrder ) {
  190. return b.renderOrder - a.renderOrder;
  191. }
  192. const distanceA = cache.objects.get( a ).distanceToCameraSquared;
  193. const distanceB = cache.objects.get( b ).distanceToCameraSquared;
  194. return distanceA - distanceB;
  195. } );
  196. const zMax = sorted.length;
  197. for ( let i = 0, l = sorted.length; i < l; i ++ ) {
  198. sorted[ i ].element.style.zIndex = zMax - i;
  199. }
  200. }
  201. }
  202. }
  203. /**
  204. * Constructor parameters of `CSS2DRenderer`.
  205. *
  206. * @typedef {Object} CSS2DRenderer~Parameters
  207. * @property {DOMElement} [element] - A DOM element where the renderer appends its child-elements.
  208. * If not passed in here, a new div element will be created.
  209. **/
  210. export { CSS2DObject, CSS2DRenderer };
粤ICP备19079148号