summit/frontend/node_modules/three-stdlib/math/MeshSurfaceSampler.cjs.map

1 line
7.2 KiB
Plaintext
Raw Normal View History

2025-12-08 16:31:30 +00:00
{"version":3,"file":"MeshSurfaceSampler.cjs","sources":["../../src/math/MeshSurfaceSampler.js"],"sourcesContent":["import { Triangle, Vector3 } from 'three'\n\n/**\n * Utility class for sampling weighted random points on the surface of a mesh.\n *\n * Building the sampler is a one-time O(n) operation. Once built, any number of\n * random samples may be selected in O(logn) time. Memory usage is O(n).\n *\n * References:\n * - http://www.joesfer.com/?p=84\n * - https://stackoverflow.com/a/4322940/1314762\n */\n\nconst _face = /* @__PURE__ */ new Triangle()\nconst _color = /* @__PURE__ */ new Vector3()\n\nclass MeshSurfaceSampler {\n constructor(mesh) {\n let geometry = mesh.geometry\n\n if (geometry.index) {\n console.warn('THREE.MeshSurfaceSampler: Converting geometry to non-indexed BufferGeometry.')\n\n geometry = geometry.toNonIndexed()\n }\n\n this.geometry = geometry\n this.randomFunction = Math.random\n\n this.positionAttribute = this.geometry.getAttribute('position')\n this.colorAttribute = this.geometry.getAttribute('color')\n this.weightAttribute = null\n\n this.distribution = null\n }\n\n setWeightAttribute(name) {\n this.weightAttribute = name ? this.geometry.getAttribute(name) : null\n\n return this\n }\n\n build() {\n const positionAttribute = this.positionAttribute\n const weightAttribute = this.weightAttribute\n\n const faceWeights = new Float32Array(positionAttribute.count / 3)\n\n // Accumulate weights for each mesh face.\n\n for (let i = 0; i < positionAttribute.count; i += 3) {\n let faceWeight = 1\n\n if (weightAttribute) {\n faceWeight = weightAttribute.getX(i) + weightAttribute.getX(i + 1) + weightAttribute.getX(i + 2)\n }\n\n _face.a.fromBufferAttribute(positionAttribute, i)\n _face.b.fromBufferAttribute(positionAttribute, i + 1)\n _face.c.fromBufferAttribute(positionAttribute, i + 2)\n faceWeight *= _face.getArea()\n\n faceWeights[i / 3] = faceWeight\n }\n\n // Store cumulative total face weights in an array, where weight index\n // corresponds to face index.\n\n this.distribution = new Float32Array(positionAttribute.count / 3)\n\n let cumulativeTotal = 0\n\n for (let i = 0; i < faceWeights.length; i++) {\n cumulativeTotal += faceWeights[i]\n\n this.distribution[i] = cumulativeTotal\n }\n\n return this\n }\n\n setRandomGenerator(randomFunction) {\n this.randomFunction = randomFunction\n return this\n }\n\n sample(targetPosition, targetNormal, targetColor) {\n const faceIndex = this.sampleFaceIndex()\n return this.sampleFace(faceIndex, targetPosition, targetNormal, targetColor)\n }\n\n sampleFaceIndex() {\n const cumulativeTotal = this.distribution[this.distribution.length - 1]\n return this.binarySearch(this.randomFunction() * cumulativeTotal)\n }\n\n binarySearch(x) {\n const dist = this.distribution\n let start = 0\n let end = dist.length - 1\n\n let index = -1\n\n while (start <= end) {\n const mid = Math.ceil((start + end) / 2)\n\n if (mid === 0 || (dist[mid - 1] <= x && dist[mid] > x)) {\n index = mid\n\n break\n } else if (x < dist[mid]) {\n end = mid - 1\n } else {\n start = mid + 1\n }\n }\n\n return index\n }\n\n sampleFace(faceIndex, targetPosition, targetNormal, targetColor) {\n let u = this.randomFunction()\n let v = this.randomFunction()\n\n if (u + v > 1) {\n u = 1 - u\n v = 1 - v\n }\n\n _face.a.fromBufferAttribute(this.positionAttribute, faceIndex * 3)\n _face.b.fromBufferAttribute(this.positionAttribute, faceIndex * 3 + 1)\n _face.c.fromBufferAttribute(this.positionAttribute, faceIndex * 3 + 2)\n\n targetPosition\n .set(0, 0, 0)\n .addScaledVector(_face.a, u)\n .addScaledVector(_face.b, v)\n .addScaledVector(_face.c, 1 - (u + v))\n\n if (targetNormal !== undefined) {\n _face.getNormal(targetNormal)\n }\n\n if (targetColor !== undefined && this.colorAttribute !== undefined)