webgpu_compute_geometry.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 { vec3, cos, sin, mat3, storage, Fn, instanceIndex, timerLocal } from 'three/tsl';
  25. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  26. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  27. let camera, scene, renderer;
  28. let computeUpdate;
  29. init();
  30. function init() {
  31. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
  32. camera.position.set( 0, 0, 1 );
  33. scene = new THREE.Scene();
  34. scene.background = new THREE.Color( 0x333333 );
  35. new GLTFLoader().load( 'models/gltf/LeePerrySmith/LeePerrySmith.glb', function ( gltf ) {
  36. const mesh = gltf.scene.children[ 0 ];
  37. mesh.scale.setScalar( .1 );
  38. mesh.material = new THREE.MeshNormalMaterial();
  39. scene.add( mesh );
  40. //
  41. const positionBaseAttribute = mesh.geometry.attributes.position;
  42. const normalBaseAttribute = mesh.geometry.attributes.normal;
  43. // replace geometry attributes for storage buffer attributes
  44. const positionStorageBufferAttribute = new THREE.StorageBufferAttribute( positionBaseAttribute.count, 4 );
  45. const normalStorageBufferAttribute = new THREE.StorageBufferAttribute( normalBaseAttribute.count, 4 );
  46. mesh.geometry.setAttribute( 'position', positionStorageBufferAttribute );
  47. mesh.geometry.setAttribute( 'normal', normalStorageBufferAttribute );
  48. // compute shader
  49. const computeFn = Fn( () => {
  50. const positionAttribute = storage( positionBaseAttribute, 'vec3', positionBaseAttribute.count ).toReadOnly();
  51. const normalAttribute = storage( normalBaseAttribute, 'vec3', normalBaseAttribute.count ).toReadOnly();
  52. const positionStorageAttribute = storage( positionStorageBufferAttribute, 'vec4', positionStorageBufferAttribute.count );
  53. const normalStorageAttribute = storage( normalStorageBufferAttribute, 'vec4', normalStorageBufferAttribute.count );
  54. const time = timerLocal( 1 );
  55. const scale = 0.3;
  56. //
  57. const position = vec3( positionAttribute.element( instanceIndex ) );
  58. const normal = vec3( normalAttribute.element( instanceIndex ) );
  59. const theta = sin( time.add( position.y ) ).mul( scale );
  60. const c = cos( theta );
  61. const s = sin( theta );
  62. const m = mat3(
  63. c, 0, s,
  64. 0, 1, 0,
  65. s.negate(), 0, c
  66. );
  67. const transformed = position.mul( m );
  68. const transformedNormal = normal.mul( m );
  69. positionStorageAttribute.element( instanceIndex ).assign( transformed );
  70. normalStorageAttribute.element( instanceIndex ).assign( transformedNormal );
  71. } );
  72. computeUpdate = computeFn().compute( positionBaseAttribute.count );
  73. } );
  74. // renderer
  75. renderer = new THREE.WebGPURenderer( { antialias: true } );
  76. renderer.setPixelRatio( window.devicePixelRatio );
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. renderer.setAnimationLoop( animate );
  79. document.body.appendChild( renderer.domElement );
  80. const controls = new OrbitControls( camera, renderer.domElement );
  81. controls.minDistance = .7;
  82. controls.maxDistance = 2;
  83. window.addEventListener( 'resize', onWindowResize );
  84. }
  85. function onWindowResize() {
  86. camera.aspect = window.innerWidth / window.innerHeight;
  87. camera.updateProjectionMatrix();
  88. renderer.setSize( window.innerWidth, window.innerHeight );
  89. }
  90. async function animate() {
  91. if ( computeUpdate ) await renderer.computeAsync( computeUpdate );
  92. renderer.render( scene, camera );
  93. }
  94. </script>
  95. </body>
  96. </html>