webgpu_compute_geometry.html 6.5 KB

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