webgpu_layers.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 { Inspector } from 'three/addons/inspector/Inspector.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. renderer.inspector = new Inspector();
  61. document.body.appendChild( renderer.domElement );
  62. // GUI
  63. const layers = {
  64. 'Red': true,
  65. 'Yellow': true,
  66. 'Green': true
  67. };
  68. const gui = renderer.inspector.createParameters( 'Layers' );
  69. gui.add( layers, 'Red' ).onChange( () => {
  70. camera.layers.toggle( 0 );
  71. } );
  72. gui.add( layers, 'Yellow' ).onChange( () => {
  73. camera.layers.toggle( 1 );
  74. } );
  75. gui.add( layers, 'Green' ).onChange( () => {
  76. camera.layers.toggle( 2 );
  77. } );
  78. //
  79. window.addEventListener( 'resize', onWindowResize );
  80. }
  81. function getMaterial( count, color, sprite ) {
  82. // instance data
  83. const positions = [];
  84. const rotations = [];
  85. const directions = [];
  86. const timeOffsets = [];
  87. const v = new THREE.Vector3();
  88. for ( let i = 0; i < count; i ++ ) {
  89. positions.push(
  90. THREE.MathUtils.randFloat( - 25, - 20 ),
  91. THREE.MathUtils.randFloat( - 10, 50 ),
  92. THREE.MathUtils.randFloat( - 5, 5 )
  93. );
  94. v.set( THREE.MathUtils.randFloat( 0.7, 0.9 ), THREE.MathUtils.randFloat( - 0.3, - 0.15 ), 0 ).normalize();
  95. rotations.push( Math.random(), Math.random(), Math.random() );
  96. directions.push( v.x, v.y, v.z );
  97. timeOffsets.push( i / count );
  98. }
  99. const positionAttribute = new THREE.InstancedBufferAttribute( new Float32Array( positions ), 3 );
  100. const rotationAttribute = new THREE.InstancedBufferAttribute( new Float32Array( rotations ), 3 );
  101. const directionAttribute = new THREE.InstancedBufferAttribute( new Float32Array( directions ), 3 );
  102. const timeAttribute = new THREE.InstancedBufferAttribute( new Float32Array( timeOffsets ), 1 );
  103. // material
  104. const material = new THREE.MeshBasicNodeMaterial( {
  105. color: color,
  106. map: sprite,
  107. alphaMap: sprite,
  108. alphaTest: 0.1,
  109. side: THREE.DoubleSide,
  110. forceSinglePass: true
  111. } );
  112. // TSL
  113. const instancePosition = instancedBufferAttribute( positionAttribute );
  114. const instanceDirection = instancedBufferAttribute( directionAttribute );
  115. const instanceRotation = instancedBufferAttribute( rotationAttribute );
  116. const localTime = instancedBufferAttribute( timeAttribute ).add( time.mul( 0.02 ) );
  117. const modTime = mod( localTime, 1.0 );
  118. const rotatedPosition = rotate( positionLocal, instanceRotation.mul( modTime.mul( 20 ) ) );
  119. material.positionNode = rotatedPosition.add( instancePosition ).add( instanceDirection.mul( modTime.mul( 50 ) ) );
  120. return material;
  121. }
  122. function onWindowResize() {
  123. camera.aspect = window.innerWidth / window.innerHeight;
  124. camera.updateProjectionMatrix();
  125. renderer.setSize( window.innerWidth, window.innerHeight );
  126. }
  127. //
  128. function animate() {
  129. renderer.render( scene, camera );
  130. }
  131. </script>
  132. </body>
  133. </html>
粤ICP备19079148号