-
-
Notifications
You must be signed in to change notification settings - Fork 36.1k
CurveModifier: Add WebGPU version. #29453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,234 @@ | ||
| // Original src: https://github.com/zz85/threejs-path-flow | ||
| const CHANNELS = 4; | ||
| const TEXTURE_WIDTH = 1024; | ||
| const TEXTURE_HEIGHT = 4; | ||
|
|
||
| import { | ||
| DataTexture, | ||
| DataUtils, | ||
| RGBAFormat, | ||
| HalfFloatType, | ||
| RepeatWrapping, | ||
| Mesh, | ||
| InstancedMesh, | ||
| LinearFilter | ||
| } from 'three'; | ||
|
|
||
| import { modelWorldMatrix, normalLocal, ivec2, vec3, vec4, mat3, varyingProperty, textureLoad, reference, Fn, select, positionLocal } from 'three/tsl'; | ||
|
|
||
| /** | ||
| * Make a new DataTexture to store the descriptions of the curves. | ||
| * | ||
| * @param { number } numberOfCurves the number of curves needed to be described by this texture. | ||
| */ | ||
| export function initSplineTexture( numberOfCurves = 1 ) { | ||
|
|
||
| const dataArray = new Uint16Array( TEXTURE_WIDTH * TEXTURE_HEIGHT * numberOfCurves * CHANNELS ); | ||
| const dataTexture = new DataTexture( | ||
| dataArray, | ||
| TEXTURE_WIDTH, | ||
| TEXTURE_HEIGHT * numberOfCurves, | ||
| RGBAFormat, | ||
| HalfFloatType | ||
| ); | ||
|
|
||
| dataTexture.wrapS = RepeatWrapping; | ||
| dataTexture.wrapY = RepeatWrapping; | ||
| dataTexture.magFilter = LinearFilter; | ||
| dataTexture.minFilter = LinearFilter; | ||
| dataTexture.needsUpdate = true; | ||
|
|
||
| return dataTexture; | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Write the curve description to the data texture | ||
| * | ||
| * @param { DataTexture } texture The DataTexture to write to | ||
| * @param { Curve } splineCurve The curve to describe | ||
| * @param { number } offset Which curve slot to write to | ||
| */ | ||
| export function updateSplineTexture( texture, splineCurve, offset = 0 ) { | ||
|
|
||
| const numberOfPoints = Math.floor( TEXTURE_WIDTH * ( TEXTURE_HEIGHT / 4 ) ); | ||
| splineCurve.arcLengthDivisions = numberOfPoints / 2; | ||
| splineCurve.updateArcLengths(); | ||
| const points = splineCurve.getSpacedPoints( numberOfPoints ); | ||
| const frenetFrames = splineCurve.computeFrenetFrames( numberOfPoints, true ); | ||
|
|
||
| for ( let i = 0; i < numberOfPoints; i ++ ) { | ||
|
|
||
| const rowOffset = Math.floor( i / TEXTURE_WIDTH ); | ||
| const rowIndex = i % TEXTURE_WIDTH; | ||
|
|
||
| let pt = points[ i ]; | ||
| setTextureValue( texture, rowIndex, pt.x, pt.y, pt.z, 0 + rowOffset + ( TEXTURE_HEIGHT * offset ) ); | ||
| pt = frenetFrames.tangents[ i ]; | ||
| setTextureValue( texture, rowIndex, pt.x, pt.y, pt.z, 1 + rowOffset + ( TEXTURE_HEIGHT * offset ) ); | ||
| pt = frenetFrames.normals[ i ]; | ||
| setTextureValue( texture, rowIndex, pt.x, pt.y, pt.z, 2 + rowOffset + ( TEXTURE_HEIGHT * offset ) ); | ||
| pt = frenetFrames.binormals[ i ]; | ||
| setTextureValue( texture, rowIndex, pt.x, pt.y, pt.z, 3 + rowOffset + ( TEXTURE_HEIGHT * offset ) ); | ||
|
|
||
| } | ||
|
|
||
| texture.needsUpdate = true; | ||
|
|
||
| } | ||
|
|
||
|
|
||
| function setTextureValue( texture, index, x, y, z, o ) { | ||
|
|
||
| const image = texture.image; | ||
| const { data } = image; | ||
| const i = CHANNELS * TEXTURE_WIDTH * o; // Row Offset | ||
|
|
||
| data[ index * CHANNELS + i + 0 ] = DataUtils.toHalfFloat( x ); | ||
| data[ index * CHANNELS + i + 1 ] = DataUtils.toHalfFloat( y ); | ||
| data[ index * CHANNELS + i + 2 ] = DataUtils.toHalfFloat( z ); | ||
| data[ index * CHANNELS + i + 3 ] = DataUtils.toHalfFloat( 1 ); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Create a new set of uniforms for describing the curve modifier | ||
| * | ||
| * @param { DataTexture } Texture which holds the curve description | ||
| */ | ||
| export function getUniforms( splineTexture ) { | ||
|
|
||
| return { | ||
| spineTexture: splineTexture, | ||
| pathOffset: 0, // time of path curve | ||
| pathSegment: 1, // fractional length of path | ||
| spineOffset: 161, | ||
| spineLength: 400, | ||
| flow: 1, // int | ||
| }; | ||
|
|
||
| } | ||
|
|
||
| export function modifyShader( material, uniforms ) { | ||
|
|
||
| const spineTexture = uniforms.spineTexture; | ||
|
|
||
| const pathOffset = reference( 'pathOffset', 'float', uniforms ); | ||
| const pathSegment = reference( 'pathSegment', 'float', uniforms ); | ||
| const spineOffset = reference( 'spineOffset', 'float', uniforms ); | ||
| const spineLength = reference( 'spineLength', 'float', uniforms ); | ||
| const flow = reference( 'flow', 'float', uniforms ); | ||
|
|
||
| material.positionNode = Fn( () => { | ||
|
|
||
| const textureStacks = TEXTURE_HEIGHT / 4; | ||
|
|
||
| const worldPos = modelWorldMatrix.mul( vec4( positionLocal, 1 ) ).toVar(); | ||
|
|
||
| const bend = flow.greaterThan( 0 ).toVar(); | ||
| const xWeight = select( bend, 0, 1 ).toVar(); | ||
|
|
||
| const spinePortion = select( bend, worldPos.x.add( spineOffset ).div( spineLength ), 0 ); | ||
| const mt = spinePortion.mul( pathSegment ).add( pathOffset ).mul( textureStacks ).toVar(); | ||
|
|
||
| mt.assign( mt.mod( textureStacks ) ); | ||
|
|
||
| const rowOffset = mt.floor().toVar(); | ||
|
|
||
| mt.mulAssign( TEXTURE_WIDTH ); | ||
|
|
||
| const spinePos = textureLoad( spineTexture, ivec2( mt, rowOffset.add( 0 ) ) ).xyz; | ||
|
|
||
| const a = textureLoad( spineTexture, ivec2( mt, rowOffset.add( 1 ) ) ).xyz; | ||
| const b = textureLoad( spineTexture, ivec2( mt, rowOffset.add( 2 ) ) ).xyz; | ||
| const c = textureLoad( spineTexture, ivec2( mt, rowOffset.add( 3 ) ) ).xyz; | ||
|
|
||
| const basis = mat3( a, b, c ).toVar(); | ||
|
|
||
| varyingProperty( 'vec3', 'curveNormal' ).assign( basis.mul( normalLocal ) ); | ||
|
|
||
| return basis.mul( vec3( worldPos.x.mul( xWeight ), worldPos.y, worldPos.z ) ).add( spinePos ); | ||
|
|
||
| } )(); | ||
|
|
||
| material.normalNode = varyingProperty( 'vec3', 'curveNormal' ); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * A helper class for making meshes bend aroudn curves | ||
| */ | ||
| export class Flow { | ||
|
|
||
| /** | ||
| * @param {Mesh} mesh The mesh to clone and modify to bend around the curve | ||
| * @param {number} numberOfCurves The amount of space that should preallocated for additional curves | ||
| */ | ||
| constructor( mesh, numberOfCurves = 1 ) { | ||
|
|
||
| const obj3D = mesh.clone(); | ||
| const splineTexure = initSplineTexture( numberOfCurves ); | ||
| const uniforms = getUniforms( splineTexure ); | ||
|
|
||
| obj3D.traverse( function ( child ) { | ||
|
|
||
| if ( | ||
| child instanceof Mesh || | ||
| child instanceof InstancedMesh | ||
| ) { | ||
|
|
||
| if ( Array.isArray( child.material ) ) { | ||
|
|
||
| const materials = []; | ||
|
|
||
| for ( const material of child.material ) { | ||
|
|
||
| const newMaterial = material.clone(); | ||
| modifyShader( newMaterial, uniforms ); | ||
| materials.push( newMaterial ); | ||
|
|
||
| } | ||
|
|
||
| child.material = materials; | ||
|
|
||
| } else { | ||
|
|
||
| child.material = child.material.clone(); | ||
| modifyShader( child.material, uniforms ); | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| } ); | ||
|
|
||
| this.curveArray = new Array( numberOfCurves ); | ||
| this.curveLengthArray = new Array( numberOfCurves ); | ||
|
|
||
| this.object3D = obj3D; | ||
| this.splineTexure = splineTexure; | ||
| this.uniforms = uniforms; | ||
|
|
||
| } | ||
|
|
||
| updateCurve( index, curve ) { | ||
|
|
||
| if ( index >= this.curveArray.length ) throw Error( 'Index out of range for Flow' ); | ||
|
|
||
| const curveLength = curve.getLength(); | ||
|
|
||
| this.uniforms.spineLength = curveLength; | ||
| this.curveLengthArray[ index ] = curveLength; | ||
| this.curveArray[ index ] = curve; | ||
|
|
||
| updateSplineTexture( this.splineTexure, curve, index ); | ||
|
|
||
| } | ||
|
|
||
| moveAlongCurve( amount ) { | ||
|
|
||
| this.uniforms.pathOffset += amount; | ||
|
|
||
| } | ||
|
|
||
| } | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.