summit/frontend/node_modules/three-stdlib/modifiers/CurveModifier.cjs.map

1 line
15 KiB
Plaintext
Raw Normal View History

2025-12-08 16:31:30 +00:00
{"version":3,"file":"CurveModifier.cjs","sources":["../../src/modifiers/CurveModifier.ts"],"sourcesContent":["// Original src: https://github.com/zz85/threejs-path-flow\nconst CHANNELS = 4\nconst TEXTURE_WIDTH = 1024\nconst TEXTURE_HEIGHT = 4\n\nimport {\n DataTexture,\n RGBAFormat,\n FloatType,\n RepeatWrapping,\n Mesh,\n InstancedMesh,\n NearestFilter,\n DynamicDrawUsage,\n Matrix4,\n Material,\n Curve,\n BufferGeometry,\n} from 'three'\n\nimport type { IUniform } from 'three'\n\n/**\n * Make a new DataTexture to store the descriptions of the curves.\n *\n * @param { number } numberOfCurves the number of curves needed to be described by this texture.\n */\nexport const initSplineTexture = (numberOfCurves = 1): DataTexture => {\n const dataArray = new Float32Array(TEXTURE_WIDTH * TEXTURE_HEIGHT * numberOfCurves * CHANNELS)\n const dataTexture = new DataTexture(dataArray, TEXTURE_WIDTH, TEXTURE_HEIGHT * numberOfCurves, RGBAFormat, FloatType)\n\n dataTexture.wrapS = RepeatWrapping\n dataTexture.wrapT = RepeatWrapping\n dataTexture.magFilter = NearestFilter\n dataTexture.needsUpdate = true\n\n return dataTexture\n}\n\n/**\n * Write the curve description to the data texture\n *\n * @param { DataTexture } texture The DataTexture to write to\n * @param { Curve } splineCurve The curve to describe\n * @param { number } offset Which curve slot to write to\n */\nexport const updateSplineTexture = <TCurve extends Curve<any>>(\n texture: DataTexture,\n splineCurve: TCurve,\n offset = 0,\n): void => {\n const numberOfPoints = Math.floor(TEXTURE_WIDTH * (TEXTURE_HEIGHT / 4))\n splineCurve.arcLengthDivisions = numberOfPoints / 2\n splineCurve.updateArcLengths()\n const points = splineCurve.getSpacedPoints(numberOfPoints)\n const frenetFrames = splineCurve.computeFrenetFrames(numberOfPoints, true)\n\n for (let i = 0; i < numberOfPoints; i++) {\n const rowOffset = Math.floor(i / TEXTURE_WIDTH)\n const rowIndex = i % TEXTURE_WIDTH\n\n let pt = points[i]\n setTextureValue(texture, rowIndex, pt.x, pt.y, pt.z, 0 + rowOffset + TEXTURE_HEIGHT * offset)\n pt = frenetFrames.tangents[i]\n setTextureValue(texture, rowIndex, pt.x, pt.y, pt.z, 1 + rowOffset + TEXTURE_HEIGHT * offset)\n pt = frenetFrames.normals[i]\n setTextureValue(texture, rowIndex, pt.x, pt.y, pt.z, 2 + rowOffset + TEXTURE_HEIGHT * offset)\n pt = frenetFrames.binormals[i]\n setTextureValue(texture, rowIndex, pt.x, pt.y, pt.z, 3 + rowOffset + TEXTURE_HEIGHT * offset)\n }\n\n texture.needsUpdate = true\n}\n\nconst setTextureValue = (texture: DataTexture, index: number, x: number, y: number, z: number, o: number): void => {\n const image = texture.image\n const { data } = image\n const i = CHANNELS * TEXTURE_WIDTH * o // Row Offset\n data[index * CHANNELS + i + 0] = x\n data[index * CHANNELS + i + 1] = y\n data[index * CHANNELS + i + 2] = z\n data[index * CHANNELS + i + 3] = 1\n}\n\nexport interface INumericUniform extends IUniform {\n type: 'f' | 'i'\n value: number\n}\n\nexport type CurveModifierUniforms = {\n spineTexture: IUniform<DataTexture>\n pathOffset: INumericUniform\n pathSegment: INumericUniform\n spineOffset: INumericUniform\n spineLength: INumericUniform\n flow: INumericUniform\n}\n\n/**\n * Create a new set of uniforms for describing the curve modifier\n *\n * @param { DataTexture } Texture which holds the curve description\n */\nexport const getUniforms = (splineTexture: DataTexture): CurveModifierUniforms => ({\n spineTexture: { value: splineTexture },\n pathOffset: { type: 'f', value: 0 }, // time of path curve\n pathSegment: { type: 'f', value: 1 }, // fractional length of path\n spineOffset: { type: 'f', value: 161 },\n spineLength: { type: 'f', value: 400 },\n flow: { type: 'i', value: 1 },\n})\n\nexport type ModifiedMaterial<TMaterial extends Material> = TMaterial & {\n __ok: boolean\n}\n\nexport function modifyShader<TMaterial extends Material = Material>(\n material: ModifiedMaterial<TMaterial>,\n uniforms: CurveModifierUniforms,\n numberOfCurves = 1,\n): void {\n if (mater