summit/frontend/node_modules/three-stdlib/geometries/DecalGeometry.cjs.map

1 line
14 KiB
Plaintext
Raw Normal View History

2025-12-08 16:31:30 +00:00
{"version":3,"file":"DecalGeometry.cjs","sources":["../../src/geometries/DecalGeometry.js"],"sourcesContent":["import { BufferGeometry, Float32BufferAttribute, Matrix4, Vector3 } from 'three'\n\n/**\n * You can use this geometry to create a decal mesh, that serves different kinds of purposes.\n * e.g. adding unique details to models, performing dynamic visual environmental changes or covering seams.\n *\n * Constructor parameter:\n *\n * mesh — Any mesh object\n * position — Position of the decal projector\n * orientation — Orientation of the decal projector\n * size — Size of the decal projector\n *\n * reference: http://blog.wolfire.com/2009/06/how-to-project-decals/\n *\n */\n\nclass DecalGeometry extends BufferGeometry {\n constructor(mesh, position, orientation, size) {\n super()\n\n // buffers\n\n const vertices = []\n const normals = []\n const uvs = []\n\n // helpers\n\n const plane = new Vector3()\n\n // this matrix represents the transformation of the decal projector\n\n const projectorMatrix = new Matrix4()\n projectorMatrix.makeRotationFromEuler(orientation)\n projectorMatrix.setPosition(position)\n\n const projectorMatrixInverse = new Matrix4()\n projectorMatrixInverse.copy(projectorMatrix).invert()\n\n // generate buffers\n\n generate()\n\n // build geometry\n\n this.setAttribute('position', new Float32BufferAttribute(vertices, 3))\n this.setAttribute('normal', new Float32BufferAttribute(normals, 3))\n this.setAttribute('uv', new Float32BufferAttribute(uvs, 2))\n\n function generate() {\n let i\n\n let decalVertices = []\n\n const vertex = new Vector3()\n const normal = new Vector3()\n\n // handle different geometry types\n\n if (mesh.geometry.isGeometry === true) {\n console.error('THREE.DecalGeometry no longer supports THREE.Geometry. Use BufferGeometry instead.')\n return\n }\n\n const geometry = mesh.geometry\n\n const positionAttribute = geometry.attributes.position\n const normalAttribute = geometry.attributes.normal\n\n // first, create an array of 'DecalVertex' objects\n // three consecutive 'DecalVertex' objects represent a single face\n //\n // this data structure will be later used to perform the clipping\n\n if (geometry.index !== null) {\n // indexed BufferGeometry\n\n const index = geometry.index\n\n for (i = 0; i < index.count; i++) {\n vertex.fromBufferAttribute(positionAttribute, index.getX(i))\n normal.fromBufferAttribute(normalAttribute, index.getX(i))\n\n pushDecalVertex(decalVertices, vertex, normal)\n }\n } else {\n // non-indexed BufferGeometry\n\n for (i = 0; i < positionAttribute.count; i++) {\n vertex.fromBufferAttribute(positionAttribute, i)\n normal.fromBufferAttribute(normalAttribute, i)\n\n pushDecalVertex(decalVertices, vertex, normal)\n }\n }\n\n // second, clip the geometry so that it doesn't extend out from the projector\n\n decalVertices = clipGeometry(decalVertices, plane.set(1, 0, 0))\n decalVertices = clipGeometry(decalVertices, plane.set(-1, 0, 0))\n decalVertices = clipGeometry(decalVertices, plane.set(0, 1, 0))\n decalVertices = clipGeometry(decalVertices, plane.set(0, -1, 0))\n decalVertices = clipGeometry(decalVertices, plane.set(0, 0, 1))\n decalVertices = clipGeometry(decalVertices, plane.set(0, 0, -1))\n\n // third, generate final vertices, normals and uvs\n\n for (i = 0; i < decalVertices.length; i++) {\n const decalVertex = decalVertices[i]\n\n // create texture coordinates (we are still in projector space)\n\n uvs.push(0.5 + decalVertex.position.x / size.x, 0.5 + decalVertex.position.y / size.y)\n\n // transform the vertex back to world space\n\n decalVertex.position.applyMatrix4(projectorMatrix)\n\n // now create vertex and normal buffer data\n\n vertices.push(decalVertex.position.x, decalVertex.pos