webgpu_materials_alphahash.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - 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="example.css">
  8. </head>
  9. <body>
  10. <script type="importmap">
  11. {
  12. "imports": {
  13. "three": "../build/three.webgpu.js",
  14. "three/webgpu": "../build/three.webgpu.js",
  15. "three/tsl": "../build/three.tsl.js",
  16. "three/addons/": "./jsm/"
  17. }
  18. }
  19. </script>
  20. <script type="module">
  21. import * as THREE from 'three/webgpu';
  22. import { Inspector } from 'three/addons/inspector/Inspector.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, 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. renderer.inspector = new Inspector();
  67. document.body.appendChild( renderer.domElement );
  68. await renderer.init();
  69. //
  70. const environment = new RoomEnvironment();
  71. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  72. scene.environment = pmremGenerator.fromScene( environment, 0.04 ).texture;
  73. environment.dispose();
  74. //
  75. // postprocessing
  76. postProcessing = new THREE.PostProcessing( renderer );
  77. const scenePass = ssaaPass( scene, camera );
  78. scenePass.sampleLevel = 3;
  79. postProcessing.outputNode = scenePass;
  80. //
  81. controls = new OrbitControls( camera, renderer.domElement );
  82. controls.enableZoom = false;
  83. controls.enablePan = false;
  84. //
  85. const gui = renderer.inspector.createParameters( 'Parameters' );
  86. gui.add( params, 'alpha', 0, 1 ).onChange( onMaterialUpdate );
  87. gui.add( params, 'alphaHash' ).onChange( onMaterialUpdate );
  88. const ssaaFolder = gui.addFolder( 'SSAA' );
  89. ssaaFolder.add( scenePass, 'sampleLevel', 0, 4, 1 );
  90. //
  91. window.addEventListener( 'resize', onWindowResize );
  92. }
  93. function onWindowResize() {
  94. camera.aspect = window.innerWidth / window.innerHeight;
  95. camera.updateProjectionMatrix();
  96. renderer.setSize( window.innerWidth, window.innerHeight );
  97. }
  98. function onMaterialUpdate() {
  99. material.opacity = params.alpha;
  100. material.alphaHash = params.alphaHash;
  101. material.transparent = ! params.alphaHash;
  102. material.depthWrite = params.alphaHash;
  103. material.needsUpdate = true;
  104. }
  105. function animate() {
  106. postProcessing.render();
  107. }
  108. </script>
  109. </body>
  110. </html>
粤ICP备19079148号