CSS2DRenderer.js 7.2 KB

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