webgpu_struct_drawindirect.html 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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, wgslFn, instanceIndex, time, varyingProperty, attribute } 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 writeDrawBuffer = wgslFn( `
  98. fn compute(
  99. index: u32,
  100. drawBuffer: ptr<storage, DrawBuffer, read_write>,
  101. instances: f32,
  102. time: f32,
  103. ) -> void {
  104. let instanceCount = max( instances * pow( sin( time * 0.5 ) + 1, 4.0 ), 100 );
  105. atomicStore( &drawBuffer.instanceCount, u32( instanceCount ) );
  106. }
  107. ` );
  108. computeDrawBuffer = writeDrawBuffer( {
  109. drawBuffer: storage( drawBuffer, drawBufferStruct, drawBuffer.count ),
  110. instances: instances,
  111. index: instanceIndex,
  112. time: time
  113. } ).compute( instances ); // not necessary in this case but normally one wants to run through all instances
  114. const initDrawBuffer = wgslFn( `
  115. fn compute(
  116. drawBuffer: ptr< storage, DrawBuffer, read_write >,
  117. ) -> void {
  118. drawBuffer.vertexCount = 3u;
  119. atomicStore(&drawBuffer.instanceCount, 0u);
  120. drawBuffer.firstVertex = 0u;
  121. drawBuffer.firstInstance = 0u;
  122. drawBuffer.offset = 0u;
  123. }
  124. ` );
  125. computeInitDrawBuffer = initDrawBuffer( {
  126. drawBuffer: storage( drawBuffer, drawBufferStruct, drawBuffer.count ),
  127. } ).compute( 1 );
  128. const vPosition = varyingProperty( 'vec3', 'vPosition' );
  129. const vColor = varyingProperty( 'vec4', 'vColor' );
  130. const positionShaderParams = {
  131. position: attribute( 'position' ),
  132. offset: attribute( 'offset' ),
  133. color: attribute( 'color' ),
  134. orientationStart: attribute( 'orientationStart' ),
  135. orientationEnd: attribute( 'orientationEnd' ),
  136. time: time
  137. };
  138. const positionShader = wgslFn( `
  139. fn main_vertex(
  140. position: vec3<f32>,
  141. offset: vec3<f32>,
  142. color: vec4<f32>,
  143. orientationStart: vec4<f32>,
  144. orientationEnd: vec4<f32>,
  145. time: f32
  146. ) -> vec4<f32> {
  147. var vPosition = offset * max( abs( sin( time * 0.5 ) * 2.0 + 1.0 ), 0.5 ) + position;
  148. var orientation = normalize( mix( orientationStart, orientationEnd, sin( time * 0.5 ) ) );
  149. var vcV = cross( orientation.xyz, vPosition );
  150. vPosition = vcV * ( 2.0 * orientation.w ) + ( cross( orientation.xyz, vcV ) * 2.0 + vPosition );
  151. var vColor = color;
  152. var outPosition = vec4f(vPosition, 1);
  153. varyings.vPosition = vPosition;
  154. varyings.vColor = vColor;
  155. return outPosition;
  156. }
  157. `, [ vPosition, vColor ] );
  158. const fragmentShaderParams = {
  159. time: time,
  160. vPosition: vPosition,
  161. vColor: vColor
  162. };
  163. const fragmentShader = wgslFn( `
  164. fn main_fragment(
  165. time: f32,
  166. vPosition: vec3<f32>,
  167. vColor: vec4<f32>
  168. ) -> vec4<f32> {
  169. var color = vec4f( vColor );
  170. color.r += sin( vPosition.x * 10.0 + time ) * 0.5;
  171. return color;
  172. }
  173. ` );
  174. const material = new THREE.MeshBasicNodeMaterial( {
  175. side: THREE.DoubleSide,
  176. forceSinglePass: true,
  177. transparent: true
  178. } );
  179. material.positionNode = positionShader( positionShaderParams );
  180. material.fragmentNode = fragmentShader( fragmentShaderParams );
  181. const mesh = new THREE.Mesh( geometry, material );
  182. scene.add( mesh );
  183. renderer.setAnimationLoop( render );
  184. window.addEventListener( 'resize', onWindowResize, false );
  185. }
  186. function render() {
  187. controls.update();
  188. renderer.render( scene, camera );
  189. renderer.compute( computeInitDrawBuffer );
  190. renderer.compute( computeDrawBuffer );
  191. }
  192. function onWindowResize() {
  193. camera.aspect = window.innerWidth / window.innerHeight;
  194. camera.updateProjectionMatrix();
  195. renderer.setSize( window.innerWidth, window.innerHeight );
  196. }
  197. </script>
  198. </body>
  199. </html>
粤ICP备19079148号