CurveModifierGPU.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Original src: https://github.com/zz85/threejs-path-flow
  2. const CHANNELS = 4;
  3. const TEXTURE_WIDTH = 1024;
  4. const TEXTURE_HEIGHT = 4;
  5. import {
  6. DataTexture,
  7. DataUtils,
  8. RGBAFormat,
  9. HalfFloatType,
  10. RepeatWrapping,
  11. Mesh,
  12. InstancedMesh,
  13. LinearFilter
  14. } from 'three';
  15. import { modelWorldMatrix, normalLocal, vec2, vec3, vec4, mat3, varyingProperty, texture, reference, Fn, select, positionLocal } from 'three/tsl';
  16. /**
  17. * Make a new DataTexture to store the descriptions of the curves.
  18. *
  19. * @param { number } [numberOfCurves=1] the number of curves needed to be described by this texture.
  20. * @returns { DataTexture } The new DataTexture
  21. */
  22. export function initSplineTexture( numberOfCurves = 1 ) {
  23. const dataArray = new Uint16Array( TEXTURE_WIDTH * TEXTURE_HEIGHT * numberOfCurves * CHANNELS );
  24. const dataTexture = new DataTexture(
  25. dataArray,
  26. TEXTURE_WIDTH,
  27. TEXTURE_HEIGHT * numberOfCurves,
  28. RGBAFormat,
  29. HalfFloatType
  30. );
  31. dataTexture.wrapS = RepeatWrapping;
  32. dataTexture.wrapY = RepeatWrapping;
  33. dataTexture.magFilter = LinearFilter;
  34. dataTexture.minFilter = LinearFilter;
  35. dataTexture.needsUpdate = true;
  36. return dataTexture;
  37. }
  38. /**
  39. * Write the curve description to the data texture
  40. *
  41. * @param { DataTexture } texture The DataTexture to write to
  42. * @param { Curve } splineCurve The curve to describe
  43. * @param { number } offset Which curve slot to write to
  44. */
  45. export function updateSplineTexture( texture, splineCurve, offset = 0 ) {
  46. const numberOfPoints = Math.floor( TEXTURE_WIDTH * ( TEXTURE_HEIGHT / 4 ) );
  47. splineCurve.arcLengthDivisions = numberOfPoints / 2;
  48. splineCurve.updateArcLengths();
  49. const points = splineCurve.getSpacedPoints( numberOfPoints );
  50. const frenetFrames = splineCurve.computeFrenetFrames( numberOfPoints, true );
  51. for ( let i = 0; i < numberOfPoints; i ++ ) {
  52. const rowOffset = Math.floor( i / TEXTURE_WIDTH );
  53. const rowIndex = i % TEXTURE_WIDTH;
  54. let pt = points[ i ];
  55. setTextureValue( texture, rowIndex, pt.x, pt.y, pt.z, 0 + rowOffset + ( TEXTURE_HEIGHT * offset ) );
  56. pt = frenetFrames.tangents[ i ];
  57. setTextureValue( texture, rowIndex, pt.x, pt.y, pt.z, 1 + rowOffset + ( TEXTURE_HEIGHT * offset ) );
  58. pt = frenetFrames.normals[ i ];
  59. setTextureValue( texture, rowIndex, pt.x, pt.y, pt.z, 2 + rowOffset + ( TEXTURE_HEIGHT * offset ) );
  60. pt = frenetFrames.binormals[ i ];
  61. setTextureValue( texture, rowIndex, pt.x, pt.y, pt.z, 3 + rowOffset + ( TEXTURE_HEIGHT * offset ) );
  62. }
  63. texture.needsUpdate = true;
  64. }
  65. function setTextureValue( texture, index, x, y, z, o ) {
  66. const image = texture.image;
  67. const { data } = image;
  68. const i = CHANNELS * TEXTURE_WIDTH * o; // Row Offset
  69. data[ index * CHANNELS + i + 0 ] = DataUtils.toHalfFloat( x );
  70. data[ index * CHANNELS + i + 1 ] = DataUtils.toHalfFloat( y );
  71. data[ index * CHANNELS + i + 2 ] = DataUtils.toHalfFloat( z );
  72. data[ index * CHANNELS + i + 3 ] = DataUtils.toHalfFloat( 1 );
  73. }
  74. /**
  75. * Create a new set of uniforms for describing the curve modifier
  76. *
  77. * @param { DataTexture } splineTexture which holds the curve description
  78. * @returns { Object } The uniforms object
  79. */
  80. export function getUniforms( splineTexture ) {
  81. return {
  82. spineTexture: splineTexture,
  83. pathOffset: 0, // time of path curve
  84. pathSegment: 1, // fractional length of path
  85. spineOffset: 161,
  86. spineLength: 400,
  87. flow: 1, // int
  88. };
  89. }
  90. export function modifyShader( material, uniforms, numberOfCurves ) {
  91. const spineTexture = uniforms.spineTexture;
  92. const pathOffset = reference( 'pathOffset', 'float', uniforms );
  93. const pathSegment = reference( 'pathSegment', 'float', uniforms );
  94. const spineOffset = reference( 'spineOffset', 'float', uniforms );
  95. const spineLength = reference( 'spineLength', 'float', uniforms );
  96. const flow = reference( 'flow', 'float', uniforms );
  97. material.positionNode = Fn( () => {
  98. const textureStacks = TEXTURE_HEIGHT / 4;
  99. const textureScale = TEXTURE_HEIGHT * numberOfCurves;
  100. const worldPos = modelWorldMatrix.mul( vec4( positionLocal, 1 ) ).toVar();
  101. const bend = flow.greaterThan( 0 ).toVar();
  102. const xWeight = select( bend, 0, 1 ).toVar();
  103. const spinePortion = select( bend, worldPos.x.add( spineOffset ).div( spineLength ), 0 );
  104. const mt = spinePortion.mul( pathSegment ).add( pathOffset ).mul( textureStacks ).toVar();
  105. mt.assign( mt.mod( textureStacks ) );
  106. const rowOffset = mt.floor().toVar();
  107. const spinePos = texture( spineTexture, vec2( mt, rowOffset.add( 0.5 ).div( textureScale ) ) ).xyz;
  108. const a = texture( spineTexture, vec2( mt, rowOffset.add( 1.5 ).div( textureScale ) ) ).xyz;
  109. const b = texture( spineTexture, vec2( mt, rowOffset.add( 2.5 ).div( textureScale ) ) ).xyz;
  110. const c = texture( spineTexture, vec2( mt, rowOffset.add( 3.5 ).div( textureScale ) ) ).xyz;
  111. const basis = mat3( a, b, c ).toVar();
  112. varyingProperty( 'vec3', 'curveNormal' ).assign( basis.mul( normalLocal ) );
  113. return basis.mul( vec3( worldPos.x.mul( xWeight ), worldPos.y, worldPos.z ) ).add( spinePos );
  114. } )();
  115. material.normalNode = varyingProperty( 'vec3', 'curveNormal' );
  116. }
  117. /**
  118. * A helper class for making meshes bend around curves
  119. */
  120. export class Flow {
  121. /**
  122. * @param {Mesh} mesh The mesh to clone and modify to bend around the curve
  123. * @param {number} numberOfCurves The amount of space that should preallocated for additional curves
  124. */
  125. constructor( mesh, numberOfCurves = 1 ) {
  126. const obj3D = mesh.clone();
  127. const splineTexture = initSplineTexture( numberOfCurves );
  128. const uniforms = getUniforms( splineTexture );
  129. obj3D.traverse( function ( child ) {
  130. if (
  131. child instanceof Mesh ||
  132. child instanceof InstancedMesh
  133. ) {
  134. if ( Array.isArray( child.material ) ) {
  135. const materials = [];
  136. for ( const material of child.material ) {
  137. const newMaterial = material.clone();
  138. modifyShader( newMaterial, uniforms, numberOfCurves );
  139. materials.push( newMaterial );
  140. }
  141. child.material = materials;
  142. } else {
  143. child.material = child.material.clone();
  144. modifyShader( child.material, uniforms, numberOfCurves );
  145. }
  146. }
  147. } );
  148. this.curveArray = new Array( numberOfCurves );
  149. this.curveLengthArray = new Array( numberOfCurves );
  150. this.object3D = obj3D;
  151. this.splineTexture = splineTexture;
  152. this.uniforms = uniforms;
  153. }
  154. updateCurve( index, curve ) {
  155. if ( index >= this.curveArray.length ) throw Error( 'Index out of range for Flow' );
  156. const curveLength = curve.getLength();
  157. this.uniforms.spineLength = curveLength;
  158. this.curveLengthArray[ index ] = curveLength;
  159. this.curveArray[ index ] = curve;
  160. updateSplineTexture( this.splineTexture, curve, index );
  161. }
  162. moveAlongCurve( amount ) {
  163. this.uniforms.pathOffset += amount;
  164. }
  165. }
粤ICP备19079148号