1
0

webgpu_layers.html 5.5 KB

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