1 line
7.7 KiB
Plaintext
1 line
7.7 KiB
Plaintext
|
|
{"version":3,"file":"CSS2DRenderer.cjs","sources":["../../src/renderers/CSS2DRenderer.js"],"sourcesContent":["import { Matrix4, Object3D, Vector2, Vector3 } from 'three'\n\nclass CSS2DObject extends Object3D {\n constructor(element = document.createElement('div')) {\n super()\n\n this.isCSS2DObject = true\n\n this.element = element\n\n this.element.style.position = 'absolute'\n this.element.style.userSelect = 'none'\n\n this.element.setAttribute('draggable', false)\n\n this.center = new Vector2(0.5, 0.5) // ( 0, 0 ) is the lower left; ( 1, 1 ) is the top right\n\n this.addEventListener('removed', function () {\n this.traverse(function (object) {\n if (object.element instanceof Element && object.element.parentNode !== null) {\n object.element.parentNode.removeChild(object.element)\n }\n })\n })\n }\n\n copy(source, recursive) {\n super.copy(source, recursive)\n\n this.element = source.element.cloneNode(true)\n\n this.center = source.center\n\n return this\n }\n}\n\nconst _vector = /* @__PURE__ */ new Vector3()\nconst _viewMatrix = /* @__PURE__ */ new Matrix4()\nconst _viewProjectionMatrix = /* @__PURE__ */ new Matrix4()\nconst _a = /* @__PURE__ */ new Vector3()\nconst _b = /* @__PURE__ */ new Vector3()\n\nclass CSS2DRenderer {\n constructor(parameters = {}) {\n const _this = this\n\n let _width, _height\n let _widthHalf, _heightHalf\n\n const cache = {\n objects: new WeakMap(),\n }\n\n const domElement = parameters.element !== undefined ? parameters.element : document.createElement('div')\n\n domElement.style.overflow = 'hidden'\n\n this.domElement = domElement\n\n this.getSize = function () {\n return {\n width: _width,\n height: _height,\n }\n }\n\n this.render = function (scene, camera) {\n if (scene.matrixWorldAutoUpdate === true) scene.updateMatrixWorld()\n if (camera.parent === null && camera.matrixWorldAutoUpdate === true) camera.updateMatrixWorld()\n\n _viewMatrix.copy(camera.matrixWorldInverse)\n _viewProjectionMatrix.multiplyMatrices(camera.projectionMatrix, _viewMatrix)\n\n renderObject(scene, scene, camera)\n zOrder(scene)\n }\n\n this.setSize = function (width, height) {\n _width = width\n _height = height\n\n _widthHalf = _width / 2\n _heightHalf = _height / 2\n\n domElement.style.width = width + 'px'\n domElement.style.height = height + 'px'\n }\n\n function renderObject(object, scene, camera) {\n if (object.isCSS2DObject) {\n _vector.setFromMatrixPosition(object.matrixWorld)\n _vector.applyMatrix4(_viewProjectionMatrix)\n\n const visible =\n object.visible === true && _vector.z >= -1 && _vector.z <= 1 && object.layers.test(camera.layers) === true\n object.element.style.display = visible === true ? '' : 'none'\n\n if (visible === true) {\n object.onBeforeRender(_this, scene, camera)\n\n const element = object.element\n\n element.style.transform =\n 'translate(' +\n -100 * object.center.x +\n '%,' +\n -100 * object.center.y +\n '%)' +\n 'translate(' +\n (_vector.x * _widthHalf + _widthHalf) +\n 'px,' +\n (-_vector.y * _heightHalf + _heightHalf) +\n 'px)'\n\n if (element.parentNode !== domElement) {\n domElement.appendChild(element)\n }\n\n object.onAfterRender(_this, scene, camera)\n }\n\n const objectData = {\n distanceToCameraSquared: getDistanceToSquared(camera, object),\n }\n\n cache.objects.set(object, objectData)\n }\n\n for (let i = 0, l = object.children.length; i < l; i++) {\n renderObject(object.children[i], scene, camera)\n }\n }\n\n function getDistanceToSquared(object1, object2) {\n _a.setFromMatrixPosition(object1.matrixWorld)\n _b.setFromMatrixPosition(object2.matrixWorld)\n\n return _a.
|