webgpu_materials_alphahash.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - materials - alpha hash</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. <script type="importmap">
  11. {
  12. "imports": {
  13. "three": "../build/three.webgpu.js",
  14. "three/tsl": "../build/three.webgpu.js",
  15. "three/addons/": "./jsm/"
  16. }
  17. }
  18. </script>
  19. <script type="module">
  20. import * as THREE from 'three';
  21. import Stats from 'three/addons/libs/stats.module.js';
  22. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  23. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  24. import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
  25. import { ssaaPass } from 'three/addons/tsl/display/SSAAPassNode.js';
  26. let camera, scene, renderer, controls, stats, mesh, material, postProcessing;
  27. const amount = parseInt( window.location.search.slice( 1 ) ) || 3;
  28. const count = Math.pow( amount, 3 );
  29. const color = new THREE.Color();
  30. const params = {
  31. alpha: 0.5,
  32. alphaHash: true
  33. };
  34. init();
  35. async function init() {
  36. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  37. camera.position.set( amount, amount, amount );
  38. camera.lookAt( 0, 0, 0 );
  39. scene = new THREE.Scene();
  40. const geometry = new THREE.IcosahedronGeometry( 0.5, 3 );
  41. material = new THREE.MeshStandardMaterial( {
  42. color: 0xffffff,
  43. alphaHash: params.alphaHash,
  44. opacity: params.alpha
  45. } );
  46. mesh = new THREE.InstancedMesh( geometry, material, count );
  47. let i = 0;
  48. const offset = ( amount - 1 ) / 2;
  49. const matrix = new THREE.Matrix4();
  50. for ( let x = 0; x < amount; x ++ ) {
  51. for ( let y = 0; y < amount; y ++ ) {
  52. for ( let z = 0; z < amount; z ++ ) {
  53. matrix.setPosition( offset - x, offset - y, offset - z );
  54. mesh.setMatrixAt( i, matrix );
  55. mesh.setColorAt( i, color.setHex( Math.random() * 0xffffff ) );
  56. i ++;
  57. }
  58. }
  59. }
  60. scene.add( mesh );
  61. //
  62. renderer = new THREE.WebGPURenderer();
  63. renderer.setPixelRatio( window.devicePixelRatio );
  64. renderer.setSize( window.innerWidth, window.innerHeight );
  65. renderer.setAnimationLoop( animate );
  66. document.body.appendChild( renderer.domElement );
  67. await renderer.init();
  68. //
  69. const environment = new RoomEnvironment();
  70. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  71. scene.environment = pmremGenerator.fromScene( environment ).texture;
  72. environment.dispose();
  73. //
  74. // postprocessing
  75. postProcessing = new THREE.PostProcessing( renderer );
  76. const scenePass = ssaaPass( scene, camera );
  77. scenePass.sampleLevel = 3;
  78. postProcessing.outputNode = scenePass;
  79. //
  80. controls = new OrbitControls( camera, renderer.domElement );
  81. controls.enableZoom = false;
  82. controls.enablePan = false;
  83. //
  84. const gui = new GUI();
  85. gui.add( params, 'alpha', 0, 1 ).onChange( onMaterialUpdate );
  86. gui.add( params, 'alphaHash' ).onChange( onMaterialUpdate );
  87. const ssaaFolder = gui.addFolder( 'SSAA' );
  88. ssaaFolder.add( scenePass, 'sampleLevel', 0, 4, 1 );
  89. //
  90. stats = new Stats();
  91. document.body.appendChild( stats.dom );
  92. window.addEventListener( 'resize', onWindowResize );
  93. }
  94. function onWindowResize() {
  95. camera.aspect = window.innerWidth / window.innerHeight;
  96. camera.updateProjectionMatrix();
  97. renderer.setSize( window.innerWidth, window.innerHeight );
  98. }
  99. function onMaterialUpdate() {
  100. material.opacity = params.alpha;
  101. material.alphaHash = params.alphaHash;
  102. material.transparent = ! params.alphaHash;
  103. material.depthWrite = params.alphaHash;
  104. material.needsUpdate = true;
  105. }
  106. function animate() {
  107. postProcessing.render();
  108. stats.update();
  109. }
  110. </script>
  111. </body>
  112. </html>
粤ICP备19079148号