three_addons_renderers_CSS2DRenderer__js.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { Bs as Vector3, Vi as Object3D, ni as Matrix4, zs as Vector2 } from "./three.module-Dhi4sXJn.js";
  2. //#region node_modules/three/examples/jsm/renderers/CSS2DRenderer.js
  3. /**
  4. * The only type of 3D object that is supported by {@link CSS2DRenderer}.
  5. *
  6. * @augments Object3D
  7. * @three_import import { CSS2DObject } from 'three/addons/renderers/CSS2DRenderer.js';
  8. */
  9. var CSS2DObject = class extends Object3D {
  10. /**
  11. * Constructs a new CSS2D object.
  12. *
  13. * @param {HTMLElement} [element] - The DOM element.
  14. */
  15. constructor(element = document.createElement("div")) {
  16. super();
  17. /**
  18. * This flag can be used for type testing.
  19. *
  20. * @type {boolean}
  21. * @readonly
  22. * @default true
  23. */
  24. this.isCSS2DObject = true;
  25. /**
  26. * The DOM element which defines the appearance of this 3D object.
  27. *
  28. * @type {HTMLElement}
  29. * @readonly
  30. * @default true
  31. */
  32. this.element = element;
  33. this.element.style.position = "absolute";
  34. this.element.style.userSelect = "none";
  35. this.element.setAttribute("draggable", false);
  36. /**
  37. * The 3D objects center point.
  38. * `( 0, 0 )` is the lower left, `( 1, 1 )` is the top right.
  39. *
  40. * @type {Vector2}
  41. * @default (0.5,0.5)
  42. */
  43. this.center = new Vector2(.5, .5);
  44. this.addEventListener("removed", function() {
  45. this.traverse(function(object) {
  46. if (object.element && object.element instanceof object.element.ownerDocument.defaultView.Element && object.element.parentNode !== null) object.element.remove();
  47. });
  48. });
  49. }
  50. copy(source, recursive) {
  51. super.copy(source, recursive);
  52. this.element = source.element.cloneNode(true);
  53. this.center = source.center;
  54. return this;
  55. }
  56. };
  57. var _vector = new Vector3();
  58. var _viewMatrix = new Matrix4();
  59. var _viewProjectionMatrix = new Matrix4();
  60. var _a = new Vector3();
  61. var _b = new Vector3();
  62. /**
  63. * This renderer is a simplified version of {@link CSS3DRenderer}. The only transformation that is
  64. * supported is translation.
  65. *
  66. * The renderer is very useful if you want to combine HTML based labels with 3D objects. Here too,
  67. * the respective DOM elements are wrapped into an instance of {@link CSS2DObject} and added to the
  68. * scene graph. All other types of renderable 3D objects (like meshes or point clouds) are ignored.
  69. *
  70. * `CSS2DRenderer` only supports 100% browser and display zoom.
  71. *
  72. * @three_import import { CSS2DRenderer } from 'three/addons/renderers/CSS2DRenderer.js';
  73. */
  74. var CSS2DRenderer = class {
  75. /**
  76. * Constructs a new CSS2D renderer.
  77. *
  78. * @param {CSS2DRenderer~Parameters} [parameters] - The parameters.
  79. */
  80. constructor(parameters = {}) {
  81. const _this = this;
  82. let _width, _height;
  83. let _widthHalf, _heightHalf;
  84. const cache = { objects: /* @__PURE__ */ new WeakMap() };
  85. const domElement = parameters.element !== void 0 ? parameters.element : document.createElement("div");
  86. domElement.style.overflow = "hidden";
  87. /**
  88. * The DOM where the renderer appends its child-elements.
  89. *
  90. * @type {HTMLElement}
  91. */
  92. this.domElement = domElement;
  93. /**
  94. * Controls whether the renderer assigns `z-index` values to CSS2DObject DOM elements.
  95. * If set to `true`, z-index values are assigned first based on the `renderOrder`
  96. * and secondly - the distance to the camera. If set to `false`, no z-index values are assigned.
  97. *
  98. * @type {boolean}
  99. * @default true
  100. */
  101. this.sortObjects = true;
  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. if (this.sortObjects) 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++) hideObject(object.children[i]);
  144. }
  145. function renderObject(object, scene, camera) {
  146. if (object.visible === false) {
  147. hideObject(object);
  148. return;
  149. }
  150. if (object.isCSS2DObject) {
  151. _vector.setFromMatrixPosition(object.matrixWorld);
  152. _vector.applyMatrix4(_viewProjectionMatrix);
  153. const visible = _vector.z >= -1 && _vector.z <= 1 && object.layers.test(camera.layers) === true;
  154. const element = object.element;
  155. element.style.display = visible === true ? "" : "none";
  156. if (visible === true) {
  157. object.onBeforeRender(_this, scene, camera);
  158. element.style.transform = "translate(" + -100 * object.center.x + "%," + -100 * object.center.y + "%)translate(" + (_vector.x * _widthHalf + _widthHalf) + "px," + (-_vector.y * _heightHalf + _heightHalf) + "px)";
  159. if (element.parentNode !== domElement) domElement.appendChild(element);
  160. object.onAfterRender(_this, scene, camera);
  161. }
  162. const objectData = { distanceToCameraSquared: getDistanceToSquared(camera, object) };
  163. cache.objects.set(object, objectData);
  164. }
  165. for (let i = 0, l = object.children.length; i < l; i++) renderObject(object.children[i], scene, camera);
  166. }
  167. function getDistanceToSquared(object1, object2) {
  168. _a.setFromMatrixPosition(object1.matrixWorld);
  169. _b.setFromMatrixPosition(object2.matrixWorld);
  170. return _a.distanceToSquared(_b);
  171. }
  172. function filterAndFlatten(scene) {
  173. const result = [];
  174. scene.traverseVisible(function(object) {
  175. if (object.isCSS2DObject) result.push(object);
  176. });
  177. return result;
  178. }
  179. function zOrder(scene) {
  180. const sorted = filterAndFlatten(scene).sort(function(a, b) {
  181. if (a.renderOrder !== b.renderOrder) return b.renderOrder - a.renderOrder;
  182. return cache.objects.get(a).distanceToCameraSquared - cache.objects.get(b).distanceToCameraSquared;
  183. });
  184. const zMax = sorted.length;
  185. for (let i = 0, l = sorted.length; i < l; i++) sorted[i].element.style.zIndex = zMax - i;
  186. }
  187. }
  188. };
  189. //#endregion
  190. export { CSS2DObject, CSS2DRenderer };
  191. //# sourceMappingURL=three_addons_renderers_CSS2DRenderer__js.js.map