webgpu_compute_geometry.html 7.0 KB

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