webgpu_compute_geometry.html 6.3 KB

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