webgpu_compute_geometry.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - compute geometry</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="example.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  12. <div class="title-wrapper">
  13. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Compute Geometry</span>
  14. </div>
  15. <small>Jelly deformation with compute shaders. Move pointer to interact.</small>
  16. </div>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.webgpu.js",
  21. "three/webgpu": "../build/three.webgpu.js",
  22. "three/tsl": "../build/three.tsl.js",
  23. "three/addons/": "./jsm/"
  24. }
  25. }
  26. </script>
  27. <script type="module">
  28. import * as THREE from 'three/webgpu';
  29. import { vec4, storage, Fn, If, uniform, instanceIndex, objectWorldMatrix, color, screenUV, attribute } from 'three/tsl';
  30. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  31. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  32. import { Inspector } from 'three/addons/inspector/Inspector.js';
  33. let camera, scene, renderer;
  34. let raycaster, pointer;
  35. let mesh;
  36. const pointerPosition = uniform( vec4( 0 ) );
  37. const elasticity = uniform( .4 ); // elasticity ( how "strong" the spring is )
  38. const damping = uniform( .94 ); // damping factor ( energy loss )
  39. const brushSize = uniform( .25 );
  40. const brushStrength = uniform( .22 );
  41. init();
  42. const jelly = Fn( ( { renderer, geometry, object } ) => {
  43. const count = geometry.attributes.position.count;
  44. // Create storage buffer attribute for modified position.
  45. const positionBaseAttribute = geometry.attributes.position;
  46. const positionStorageBufferAttribute = new THREE.StorageBufferAttribute( count, 3 );
  47. const speedBufferAttribute = new THREE.StorageBufferAttribute( count, 3 );
  48. geometry.setAttribute( 'storagePosition', positionStorageBufferAttribute );
  49. // Attributes
  50. const positionAttribute = storage( positionBaseAttribute, 'vec3', count );
  51. const positionStorageAttribute = storage( positionStorageBufferAttribute, 'vec3', count );
  52. const speedAttribute = storage( speedBufferAttribute, 'vec3', count );
  53. // Vectors
  54. // Base vec3 position of the mesh vertices.
  55. const basePosition = positionAttribute.element( instanceIndex );
  56. // Mesh vertices after compute modification.
  57. const currentPosition = positionStorageAttribute.element( instanceIndex );
  58. // Speed of each mesh vertex.
  59. const currentSpeed = speedAttribute.element( instanceIndex );
  60. //
  61. const computeInit = Fn( () => {
  62. // Modified storage position starts out the same as the base position.
  63. currentPosition.assign( basePosition );
  64. } )().compute( count );
  65. //
  66. const computeUpdate = Fn( () => {
  67. // pinch
  68. If( pointerPosition.w.equal( 1 ), () => {
  69. const worldPosition = objectWorldMatrix( object ).mul( currentPosition );
  70. const dist = worldPosition.distance( pointerPosition.xyz );
  71. const direction = pointerPosition.xyz.sub( worldPosition ).normalize();
  72. const power = brushSize.sub( dist ).max( 0 ).mul( brushStrength );
  73. currentPosition.addAssign( direction.mul( power ) );
  74. } );
  75. // compute ( jelly )
  76. const distance = basePosition.distance( currentPosition );
  77. const force = elasticity.mul( distance ).mul( basePosition.sub( currentPosition ) );
  78. currentSpeed.addAssign( force );
  79. currentSpeed.mulAssign( damping );
  80. currentPosition.addAssign( currentSpeed );
  81. } )().compute( count ).setName( 'Update Jelly' );
  82. // initialize the storage buffer with the base position
  83. computeUpdate.onInit( () => renderer.compute( computeInit ) );
  84. //
  85. return computeUpdate;
  86. } );
  87. function init() {
  88. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
  89. camera.position.set( 0, 0, 1 );
  90. scene = new THREE.Scene();
  91. raycaster = new THREE.Raycaster();
  92. pointer = new THREE.Vector2();
  93. // background
  94. const bgColor = screenUV.y.mix( color( 0x9f87f7 ), color( 0xf2cdcd ) );
  95. const bgVignette = screenUV.distance( .5 ).remapClamp( 0.3, .8 ).oneMinus();
  96. const bgIntensity = 4;
  97. scene.backgroundNode = bgColor.mul( bgVignette.mul( color( 0xa78ff6 ).mul( bgIntensity ) ) );
  98. // model
  99. new GLTFLoader().load( 'models/gltf/LeePerrySmith/LeePerrySmith.glb', function ( gltf ) {
  100. // create jelly effect material
  101. const material = new THREE.MeshNormalNodeMaterial();
  102. material.geometryNode = jelly();
  103. material.positionNode = attribute( 'storagePosition' );
  104. // apply the material to the mesh
  105. mesh = gltf.scene.children[ 0 ];
  106. mesh.scale.setScalar( .1 );
  107. mesh.material = material;
  108. scene.add( mesh );
  109. } );
  110. // renderer
  111. renderer = new THREE.WebGPURenderer( { antialias: true } );
  112. renderer.setPixelRatio( window.devicePixelRatio );
  113. renderer.setSize( window.innerWidth, window.innerHeight );
  114. renderer.setAnimationLoop( animate );
  115. renderer.inspector = new Inspector();
  116. document.body.appendChild( renderer.domElement );
  117. const controls = new OrbitControls( camera, renderer.domElement );
  118. controls.minDistance = .7;
  119. controls.maxDistance = 2;
  120. const gui = renderer.inspector.createParameters( 'Settings' );
  121. gui.add( elasticity, 'value', 0, .5 ).name( 'elasticity' );
  122. gui.add( damping, 'value', .9, .98 ).name( 'damping' );
  123. gui.add( brushSize, 'value', .1, .5 ).name( 'brush size' );
  124. gui.add( brushStrength, 'value', .1, .3 ).name( 'brush strength' );
  125. window.addEventListener( 'resize', onWindowResize );
  126. window.addEventListener( 'pointermove', onPointerMove );
  127. }
  128. function onPointerMove( event ) {
  129. pointer.set( ( event.clientX / window.innerWidth ) * 2 - 1, - ( event.clientY / window.innerHeight ) * 2 + 1 );
  130. raycaster.setFromCamera( pointer, camera );
  131. const intersects = raycaster.intersectObject( scene );
  132. if ( intersects.length > 0 ) {
  133. const intersect = intersects[ 0 ];
  134. pointerPosition.value.copy( intersect.point );
  135. pointerPosition.value.w = 1; // enable
  136. } else {
  137. pointerPosition.value.w = 0; // disable
  138. }
  139. }
  140. function onWindowResize() {
  141. camera.aspect = window.innerWidth / window.innerHeight;
  142. camera.updateProjectionMatrix();
  143. renderer.setSize( window.innerWidth, window.innerHeight );
  144. }
  145. async function animate() {
  146. renderer.render( scene, camera );
  147. }
  148. </script>
  149. </body>
  150. </html>
粤ICP备19079148号