webgpu_layers.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - layers</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</a> - webgpu layers
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.webgpu.js",
  17. "three/webgpu": "../build/three.webgpu.js",
  18. "three/tsl": "../build/three.tsl.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  26. import { positionLocal, time, mod, instancedBufferAttribute, rotate, screenUV, color, vec2 } from 'three/tsl';
  27. let camera, scene, renderer;
  28. init();
  29. function init() {
  30. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  31. camera.layers.enable( 0 ); // enabled by default
  32. camera.layers.enable( 1 );
  33. camera.layers.enable( 2 );
  34. camera.position.z = 10;
  35. scene = new THREE.Scene();
  36. const horizontalEffect = screenUV.x.mix( color( 0xf996ae ), color( 0xf6f0a3 ) );
  37. const lightEffect = screenUV.distance( vec2( 0.5, 1.0 ) ).oneMinus().mul( color( 0xd9b6fd ) );
  38. scene.backgroundNode = horizontalEffect.add( lightEffect );
  39. scene.add( camera );
  40. const sprite = new THREE.TextureLoader().load( 'textures/sprites/blossom.png' );
  41. sprite.colorSpace = THREE.SRGBColorSpace;
  42. const count = 2500;
  43. const geometry = new THREE.PlaneGeometry( 0.25, 0.25 );
  44. const colors = [ 0xD70654, 0xFFD95F, 0xB8D576 ];
  45. for ( let i = 0; i < 3; i ++ ) {
  46. const particles = new THREE.Mesh( geometry, getMaterial( count, colors[ i ], sprite ) );
  47. particles.layers.set( i );
  48. particles.count = count;
  49. scene.add( particles );
  50. }
  51. renderer = new THREE.WebGPURenderer( { antialias: true } );
  52. renderer.setPixelRatio( window.devicePixelRatio );
  53. renderer.setSize( window.innerWidth, window.innerHeight );
  54. renderer.setAnimationLoop( animate );
  55. document.body.appendChild( renderer.domElement );
  56. // GUI
  57. const layers = {
  58. 'toggle red': function () {
  59. camera.layers.toggle( 0 );
  60. },
  61. 'toggle yellow': function () {
  62. camera.layers.toggle( 1 );
  63. },
  64. 'toggle green': function () {
  65. camera.layers.toggle( 2 );
  66. },
  67. 'enable all': function () {
  68. camera.layers.enableAll();
  69. },
  70. 'disable all': function () {
  71. camera.layers.disableAll();
  72. }
  73. };
  74. const gui = new GUI();
  75. gui.add( layers, 'toggle red' );
  76. gui.add( layers, 'toggle yellow' );
  77. gui.add( layers, 'toggle green' );
  78. gui.add( layers, 'enable all' );
  79. gui.add( layers, 'disable all' );
  80. //
  81. window.addEventListener( 'resize', onWindowResize );
  82. }
  83. function getMaterial( count, color, sprite ) {
  84. // instance data
  85. const positions = [];
  86. const rotations = [];
  87. const directions = [];
  88. const timeOffsets = [];
  89. const v = new THREE.Vector3();
  90. for ( let i = 0; i < count; i ++ ) {
  91. positions.push(
  92. THREE.MathUtils.randFloat( - 25, - 20 ),
  93. THREE.MathUtils.randFloat( - 10, 50 ),
  94. THREE.MathUtils.randFloat( - 5, 5 )
  95. );
  96. v.set( THREE.MathUtils.randFloat( 0.7, 0.9 ), THREE.MathUtils.randFloat( - 0.3, - 0.15 ), 0 ).normalize();
  97. rotations.push( Math.random(), Math.random(), Math.random() );
  98. directions.push( v.x, v.y, v.z );
  99. timeOffsets.push( i / count );
  100. }
  101. const positionAttribute = new THREE.InstancedBufferAttribute( new Float32Array( positions ), 3 );
  102. const rotationAttribute = new THREE.InstancedBufferAttribute( new Float32Array( rotations ), 3 );
  103. const directionAttribute = new THREE.InstancedBufferAttribute( new Float32Array( directions ), 3 );
  104. const timeAttribute = new THREE.InstancedBufferAttribute( new Float32Array( timeOffsets ), 1 );
  105. // material
  106. const material = new THREE.MeshBasicNodeMaterial( {
  107. color: color,
  108. map: sprite,
  109. alphaMap: sprite,
  110. alphaTest: 0.1,
  111. side: THREE.DoubleSide,
  112. forceSinglePass: true
  113. } );
  114. // TSL
  115. const instancePosition = instancedBufferAttribute( positionAttribute );
  116. const instanceDirection = instancedBufferAttribute( directionAttribute );
  117. const instanceRotation = instancedBufferAttribute( rotationAttribute );
  118. const localTime = instancedBufferAttribute( timeAttribute ).add( time.mul( 0.02 ) );
  119. const modTime = mod( localTime, 1.0 );
  120. const rotatedPositon = rotate( positionLocal, instanceRotation.mul( modTime.mul( 20 ) ) );
  121. material.positionNode = rotatedPositon.add( instancePosition ).add( instanceDirection.mul( modTime.mul( 50 ) ) );
  122. return material;
  123. }
  124. function onWindowResize() {
  125. camera.aspect = window.innerWidth / window.innerHeight;
  126. camera.updateProjectionMatrix();
  127. renderer.setSize( window.innerWidth, window.innerHeight );
  128. }
  129. //
  130. function animate() {
  131. renderer.render( scene, camera );
  132. }
  133. </script>
  134. </body>
  135. </html>
粤ICP备19079148号