webgpu_compute_geometry.html 6.3 KB

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