CurveModifierGPU.js 6.5 KB

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