webgpu_layers.html 5.3 KB

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