webgpu_layers.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. const sprite = new THREE.TextureLoader().load( 'textures/sprites/blossom.png' );
  40. sprite.colorSpace = THREE.SRGBColorSpace;
  41. const count = 2500;
  42. const geometry = new THREE.PlaneGeometry( 0.25, 0.25 );
  43. const colors = [ 0xD70654, 0xFFD95F, 0xB8D576 ];
  44. for ( let i = 0; i < 3; i ++ ) {
  45. const particles = new THREE.Mesh( geometry, getMaterial( count, colors[ i ], sprite ) );
  46. particles.layers.set( i );
  47. particles.count = count;
  48. scene.add( particles );
  49. }
  50. renderer = new THREE.WebGPURenderer( { antialias: true } );
  51. renderer.setPixelRatio( window.devicePixelRatio );
  52. renderer.setSize( window.innerWidth, window.innerHeight );
  53. renderer.setAnimationLoop( animate );
  54. document.body.appendChild( renderer.domElement );
  55. // GUI
  56. const layers = {
  57. 'toggle red': function () {
  58. camera.layers.toggle( 0 );
  59. },
  60. 'toggle yellow': function () {
  61. camera.layers.toggle( 1 );
  62. },
  63. 'toggle green': function () {
  64. camera.layers.toggle( 2 );
  65. },
  66. 'enable all': function () {
  67. camera.layers.enableAll();
  68. },
  69. 'disable all': function () {
  70. camera.layers.disableAll();
  71. }
  72. };
  73. const gui = new GUI();
  74. gui.add( layers, 'toggle red' );
  75. gui.add( layers, 'toggle yellow' );
  76. gui.add( layers, 'toggle green' );
  77. gui.add( layers, 'enable all' );
  78. gui.add( layers, 'disable all' );
  79. //
  80. window.addEventListener( 'resize', onWindowResize );
  81. }
  82. function getMaterial( count, color, sprite ) {
  83. // instance data
  84. const positions = [];
  85. const rotations = [];
  86. const directions = [];
  87. const timeOffsets = [];
  88. const v = new THREE.Vector3();
  89. for ( let i = 0; i < count; i ++ ) {
  90. positions.push(
  91. THREE.MathUtils.randFloat( - 25, - 20 ),
  92. THREE.MathUtils.randFloat( - 10, 50 ),
  93. THREE.MathUtils.randFloat( - 5, 5 )
  94. );
  95. v.set( THREE.MathUtils.randFloat( 0.7, 0.9 ), THREE.MathUtils.randFloat( - 0.3, - 0.15 ), 0 ).normalize();
  96. rotations.push( Math.random(), Math.random(), Math.random() );
  97. directions.push( v.x, v.y, v.z );
  98. timeOffsets.push( i / count );
  99. }
  100. const positionAttribute = new THREE.InstancedBufferAttribute( new Float32Array( positions ), 3 );
  101. const rotationAttribute = new THREE.InstancedBufferAttribute( new Float32Array( rotations ), 3 );
  102. const directionAttribute = new THREE.InstancedBufferAttribute( new Float32Array( directions ), 3 );
  103. const timeAttribute = new THREE.InstancedBufferAttribute( new Float32Array( timeOffsets ), 1 );
  104. // material
  105. const material = new THREE.MeshBasicNodeMaterial( {
  106. color: color,
  107. map: sprite,
  108. alphaMap: sprite,
  109. alphaTest: 0.1,
  110. side: THREE.DoubleSide,
  111. forceSinglePass: true
  112. } );
  113. // TSL
  114. const instancePosition = instancedBufferAttribute( positionAttribute );
  115. const instanceDirection = instancedBufferAttribute( directionAttribute );
  116. const instanceRotation = instancedBufferAttribute( rotationAttribute );
  117. const localTime = instancedBufferAttribute( timeAttribute ).add( time.mul( 0.02 ) );
  118. const modTime = mod( localTime, 1.0 );
  119. const rotatedPositon = rotate( positionLocal, instanceRotation.mul( modTime.mul( 20 ) ) );
  120. material.positionNode = rotatedPositon.add( instancePosition ).add( instanceDirection.mul( modTime.mul( 50 ) ) );
  121. return material;
  122. }
  123. function onWindowResize() {
  124. camera.aspect = window.innerWidth / window.innerHeight;
  125. camera.updateProjectionMatrix();
  126. renderer.setSize( window.innerWidth, window.innerHeight );
  127. }
  128. //
  129. function animate() {
  130. renderer.render( scene, camera );
  131. }
  132. </script>
  133. </body>
  134. </html>
粤ICP备19079148号