webgpu_struct_drawindirect.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - struct drawIndirect</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 - struct drawIndirect">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_struct_drawindirect.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_struct_drawindirect.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>Draw Indirect</span>
  18. </div>
  19. <small>
  20. Struct drawIndirect example.
  21. </small>
  22. </div>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../build/three.webgpu.js",
  27. "three/webgpu": "../build/three.webgpu.js",
  28. "three/tsl": "../build/three.tsl.js",
  29. "three/addons/": "./jsm/"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three/webgpu';
  35. import { struct, storage, sin, cross, normalize, abs, mix, Fn, vec4, max, pow, time, varyingProperty, attribute, uint, atomicStore } from 'three/tsl';
  36. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  37. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  38. if ( WebGPU.isAvailable() === false ) {
  39. document.body.appendChild( WebGPU.getErrorMessage() );
  40. throw new Error( 'No WebGPU support' );
  41. }
  42. const renderer = new THREE.WebGPURenderer( { antialias: true } );
  43. renderer.outputColorSpace = THREE.SRGBColorSpace;
  44. renderer.setPixelRatio( window.devicePixelRatio );
  45. renderer.setSize( window.innerWidth, window.innerHeight );
  46. renderer.setClearColor( 0x000000 );
  47. renderer.setClearAlpha( 0 );
  48. document.body.appendChild( renderer.domElement );
  49. const aspect = window.innerWidth / window.innerHeight;
  50. const camera = new THREE.PerspectiveCamera( 50.0, aspect, 0.1, 10000 );
  51. const scene = new THREE.Scene();
  52. scene.background = new THREE.Color( 0x00001f );
  53. camera.position.set( 1, 1, 1 );
  54. const controls = new OrbitControls( camera, renderer.domElement );
  55. let computeDrawBuffer, computeInitDrawBuffer;
  56. init();
  57. async function init() {
  58. await renderer.init();
  59. // geometry
  60. const vector = new THREE.Vector4();
  61. const instances = 100000;
  62. const positions = [];
  63. const offsets = [];
  64. const colors = [];
  65. const orientationsStart = [];
  66. const orientationsEnd = [];
  67. positions.push( 0.025, - 0.025, 0 );
  68. positions.push( - 0.025, 0.025, 0 );
  69. positions.push( 0, 0, 0.025 );
  70. // instanced attributes
  71. for ( let i = 0; i < instances; i ++ ) {
  72. // offsets
  73. offsets.push( Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 );
  74. // colors
  75. colors.push( Math.random(), Math.random(), Math.random(), Math.random() );
  76. // orientation start
  77. vector.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  78. vector.normalize();
  79. orientationsStart.push( vector.x, vector.y, vector.z, vector.w );
  80. // orientation end
  81. vector.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  82. vector.normalize();
  83. orientationsEnd.push( vector.x, vector.y, vector.z, vector.w );
  84. }
  85. const geometry = new THREE.InstancedBufferGeometry();
  86. geometry.instanceCount = instances;
  87. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  88. geometry.setAttribute( 'offset', new THREE.InstancedBufferAttribute( new Float32Array( offsets ), 3 ) );
  89. geometry.setAttribute( 'color', new THREE.InstancedBufferAttribute( new Float32Array( colors ), 4 ) );
  90. geometry.setAttribute( 'orientationStart', new THREE.InstancedBufferAttribute( new Float32Array( orientationsStart ), 4 ) );
  91. geometry.setAttribute( 'orientationEnd', new THREE.InstancedBufferAttribute( new Float32Array( orientationsEnd ), 4 ) );
  92. const drawBuffer = new THREE.IndirectStorageBufferAttribute( new Uint32Array( 5 ), 5 );
  93. geometry.setIndirect( drawBuffer );
  94. const drawBufferStruct = struct( {
  95. vertexCount: 'uint',
  96. instanceCount: { type: 'uint', atomic: true },
  97. firstVertex: 'uint',
  98. firstInstance: 'uint',
  99. offset: 'uint'
  100. }, 'DrawBuffer' );
  101. const drawStorage = storage( drawBuffer, drawBufferStruct, drawBuffer.count );
  102. computeDrawBuffer = Fn( () => {
  103. const halfTime = sin( time.mul( 0.5 ) );
  104. const instanceCount = max( ( pow( halfTime.add( 1 ), 4.0 ) ).mul( instances ), 100 ).toVar( 'instanceCount' );
  105. atomicStore( drawStorage.get( 'instanceCount' ), instanceCount );
  106. } )().compute( instances );
  107. computeInitDrawBuffer = Fn( () => {
  108. const drawInfo = drawStorage;
  109. drawInfo.get( 'vertexCount' ).assign( 3 );
  110. atomicStore( drawInfo.get( 'instanceCount' ), uint( 0 ) );
  111. drawInfo.get( 'firstVertex' ).assign( 0 );
  112. drawInfo.get( 'firstInstance' ).assign( 0 );
  113. drawInfo.get( 'offset' ).assign( 0 );
  114. } )().compute( 1 );
  115. const vPosition = varyingProperty( 'vec3', 'vPosition' );
  116. const vColor = varyingProperty( 'vec4', 'vColor' );
  117. const positionShaderParams = {
  118. position: attribute( 'position' ),
  119. offset: attribute( 'offset' ),
  120. color: attribute( 'color' ),
  121. orientationStart: attribute( 'orientationStart' ),
  122. orientationEnd: attribute( 'orientationEnd' ),
  123. time: time
  124. };
  125. const positionFn = Fn( () => {
  126. const { position, offset, color, orientationStart, orientationEnd } = positionShaderParams;
  127. const halfTime = sin( time.mul( 0.5 ) );
  128. // Convert slowed sign range of (-1 to 1) to range of (1 -> 0 / 0.5 -> 3)
  129. const oscilationRange = max( abs( halfTime.mul( 2.0 ).add( 1.0 ) ), 0.5 );
  130. const sphereOscilation = offset.mul( oscilationRange ).add( position ).toVar();
  131. const orientation = normalize( mix( orientationStart, orientationEnd, halfTime ) );
  132. const vcV = cross( orientation.xyz, sphereOscilation );
  133. const crossvcV = cross( orientation.xyz, vcV );
  134. vPosition.assign( vcV.mul( orientation.w.mul( 2.0 ) ).add( crossvcV.mul( 2.0 ).add( sphereOscilation ) ) );
  135. vColor.assign( color );
  136. return vPosition;
  137. } )();
  138. const fragmentFn = Fn( () => {
  139. const color = vec4( vColor ).toVar();
  140. color.r.addAssign( sin( vPosition.x.mul( 10.0 ).add( time ) ).mul( 0.5 ) );
  141. return color;
  142. } )();
  143. const material = new THREE.MeshBasicNodeMaterial( {
  144. side: THREE.DoubleSide,
  145. forceSinglePass: true,
  146. transparent: true
  147. } );
  148. material.positionNode = positionFn;
  149. material.fragmentNode = fragmentFn;
  150. const mesh = new THREE.Mesh( geometry, material );
  151. scene.add( mesh );
  152. renderer.setAnimationLoop( render );
  153. window.addEventListener( 'resize', onWindowResize, false );
  154. }
  155. function render() {
  156. controls.update();
  157. renderer.render( scene, camera );
  158. renderer.compute( computeInitDrawBuffer );
  159. renderer.compute( computeDrawBuffer );
  160. }
  161. function onWindowResize() {
  162. camera.aspect = window.innerWidth / window.innerHeight;
  163. camera.updateProjectionMatrix();
  164. renderer.setSize( window.innerWidth, window.innerHeight );
  165. }
  166. </script>
  167. </body>
  168. </html>
粤ICP备19079148号