webgpu_struct_drawindirect.html 7.1 KB

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