1 line
96 KiB
Plaintext
1 line
96 KiB
Plaintext
|
|
{"version":3,"file":"MarchingCubes.cjs","sources":["../../src/objects/MarchingCubes.js"],"sourcesContent":["import { BufferAttribute, BufferGeometry, Color, DynamicDrawUsage, Mesh, Sphere, Vector3 } from 'three'\n\n/**\n * Port of http://webglsamples.org/blob/blob.html\n */\n\nclass MarchingCubes extends Mesh {\n constructor(resolution, material, enableUvs = false, enableColors = false, maxPolyCount = 10000) {\n const geometry = new BufferGeometry()\n\n super(geometry, material)\n\n this.isMarchingCubes = true\n\n const scope = this\n\n // temp buffers used in polygonize\n\n const vlist = new Float32Array(12 * 3)\n const nlist = new Float32Array(12 * 3)\n const clist = new Float32Array(12 * 3)\n\n this.enableUvs = enableUvs\n this.enableColors = enableColors\n\n // functions have to be object properties\n // prototype functions kill performance\n // (tested and it was 4x slower !!!)\n\n this.init = function (resolution) {\n this.resolution = resolution\n\n // parameters\n\n this.isolation = 80.0\n\n // size of field, 32 is pushing it in Javascript :)\n\n this.size = resolution\n this.size2 = this.size * this.size\n this.size3 = this.size2 * this.size\n this.halfsize = this.size / 2.0\n\n // deltas\n\n this.delta = 2.0 / this.size\n this.yd = this.size\n this.zd = this.size2\n\n this.field = new Float32Array(this.size3)\n this.normal_cache = new Float32Array(this.size3 * 3)\n this.palette = new Float32Array(this.size3 * 3)\n\n //\n\n this.count = 0\n\n const maxVertexCount = maxPolyCount * 3\n\n this.positionArray = new Float32Array(maxVertexCount * 3)\n const positionAttribute = new BufferAttribute(this.positionArray, 3)\n positionAttribute.setUsage(DynamicDrawUsage)\n geometry.setAttribute('position', positionAttribute)\n\n this.normalArray = new Float32Array(maxVertexCount * 3)\n const normalAttribute = new BufferAttribute(this.normalArray, 3)\n normalAttribute.setUsage(DynamicDrawUsage)\n geometry.setAttribute('normal', normalAttribute)\n\n if (this.enableUvs) {\n this.uvArray = new Float32Array(maxVertexCount * 2)\n const uvAttribute = new BufferAttribute(this.uvArray, 2)\n uvAttribute.setUsage(DynamicDrawUsage)\n geometry.setAttribute('uv', uvAttribute)\n }\n\n if (this.enableColors) {\n this.colorArray = new Float32Array(maxVertexCount * 3)\n const colorAttribute = new BufferAttribute(this.colorArray, 3)\n colorAttribute.setUsage(DynamicDrawUsage)\n geometry.setAttribute('color', colorAttribute)\n }\n\n geometry.boundingSphere = new Sphere(new Vector3(), 1)\n }\n\n ///////////////////////\n // Polygonization\n ///////////////////////\n\n function lerp(a, b, t) {\n return a + (b - a) * t\n }\n\n function VIntX(q, offset, isol, x, y, z, valp1, valp2, c_offset1, c_offset2) {\n const mu = (isol - valp1) / (valp2 - valp1),\n nc = scope.normal_cache\n\n vlist[offset + 0] = x + mu * scope.delta\n vlist[offset + 1] = y\n vlist[offset + 2] = z\n\n nlist[offset + 0] = lerp(nc[q + 0], nc[q + 3], mu)\n nlist[offset + 1] = lerp(nc[q + 1], nc[q + 4], mu)\n nlist[offset + 2] = lerp(nc[q + 2], nc[q + 5], mu)\n\n clist[offset + 0] = lerp(scope.palette[c_offset1 * 3 + 0], scope.palette[c_offset2 * 3 + 0], mu)\n clist[offset + 1] = lerp(scope.palette[c_offset1 * 3 + 1], scope.palette[c_offset2 * 3 + 1], mu)\n clist[offset + 2] = lerp(scope.palette[c_offset1 * 3 + 2], scope.palette[c_offset2 * 3 + 2], mu)\n }\n\n function VIntY(q, offset, isol, x, y, z, valp1, valp2, c_offset1, c_offset2) {\n const mu = (isol - valp1) / (valp2 - valp1),\n nc = scope.normal_cache\n\n vlist[offset + 0] = x\n vlist[offset + 1] = y + mu * scope.delta\n vlist[offset + 2] = z\n\n const q2 = q + scope.yd * 3\n\n nlist[offset + 0] = lerp(nc[q + 0], nc[q2 + 0], mu)\n nlist[offset +
|