1
0

webgpu_tonemapping.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - tone mapping</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. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu - tone mapping<br />
  12. Venice Mask by
  13. <a href="https://sketchfab.com/D.art" target="_blank" rel="noopener">DailyArt</a> is licensed under <a href="https://creativecommons.org/licenses/by-nc/4.0/" target="_blank" rel="noopener">CC Attribution-NonCommercial</a><br />
  14. <a href="https://hdrihaven.com/hdri/?h=venice_sunset" target="_blank" rel="noopener">Venice Sunset</a> from <a href="https://hdrihaven.com/" target="_blank" rel="noopener">HDRI Haven</a>
  15. </div>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.webgpu.js",
  20. "three/webgpu": "../build/three.webgpu.js",
  21. "three/tsl": "../build/three.tsl.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three/webgpu';
  28. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  29. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  30. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  31. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  32. import { HDRLoader } from 'three/addons/loaders/HDRLoader.js';
  33. let renderer, scene, camera, controls;
  34. let gui, guiExposure = null;
  35. const params = {
  36. exposure: 1.0,
  37. toneMapping: 'Neutral',
  38. blurriness: 0.3,
  39. intensity: 1.0,
  40. };
  41. const toneMappingOptions = {
  42. None: THREE.NoToneMapping,
  43. Linear: THREE.LinearToneMapping,
  44. Reinhard: THREE.ReinhardToneMapping,
  45. Cineon: THREE.CineonToneMapping,
  46. ACESFilmic: THREE.ACESFilmicToneMapping,
  47. AgX: THREE.AgXToneMapping,
  48. Neutral: THREE.NeutralToneMapping
  49. };
  50. init().catch( function ( err ) {
  51. console.error( err );
  52. } );
  53. async function init() {
  54. renderer = new THREE.WebGPURenderer( { antialias: true } );
  55. renderer.setPixelRatio( window.devicePixelRatio );
  56. renderer.setSize( window.innerWidth, window.innerHeight );
  57. renderer.setAnimationLoop( animate );
  58. document.body.appendChild( renderer.domElement );
  59. renderer.toneMapping = toneMappingOptions[ params.toneMapping ];
  60. renderer.toneMappingExposure = params.exposure;
  61. scene = new THREE.Scene();
  62. scene.backgroundBlurriness = params.blurriness;
  63. const light = new THREE.DirectionalLight( 0xfff3ee, 3 ); // simulate sun
  64. light.position.set( 1, 0.05, 0.7 );
  65. scene.add( light );
  66. // scene.add( new THREE.DirectionalLightHelper( light, 1, 0x000000 ) );
  67. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.01, 10 );
  68. camera.position.set( - 0.02, 0.03, 0.05 );
  69. controls = new OrbitControls( camera, renderer.domElement );
  70. controls.enablePan = false;
  71. controls.enableDamping = true;
  72. controls.minDistance = 0.03;
  73. controls.maxDistance = 0.2;
  74. controls.target.set( 0, 0.03, 0 );
  75. controls.update();
  76. const hdrLoader = new HDRLoader()
  77. .setPath( 'textures/equirectangular/' );
  78. const dracoLoader = new DRACOLoader();
  79. dracoLoader.setDecoderPath( 'jsm/libs/draco/gltf/' );
  80. const gltfLoader = new GLTFLoader();
  81. gltfLoader.setDRACOLoader( dracoLoader );
  82. gltfLoader.setPath( 'models/gltf/' );
  83. const [ texture, gltf ] = await Promise.all( [
  84. hdrLoader.loadAsync( 'venice_sunset_1k.hdr' ),
  85. gltfLoader.loadAsync( 'venice_mask.glb' ),
  86. ] );
  87. // environment
  88. texture.mapping = THREE.EquirectangularReflectionMapping;
  89. scene.background = texture;
  90. scene.environment = texture;
  91. // model
  92. scene.add( gltf.scene );
  93. window.addEventListener( 'resize', onWindowResize );
  94. //
  95. gui = new GUI();
  96. const toneMappingFolder = gui.addFolder( 'Tone Mapping' );
  97. toneMappingFolder.add( params, 'toneMapping', Object.keys( toneMappingOptions ) )
  98. .name( 'type' )
  99. .onChange( function () {
  100. updateGUI( toneMappingFolder );
  101. renderer.toneMapping = toneMappingOptions[ params.toneMapping ];
  102. } );
  103. guiExposure = toneMappingFolder.add( params, 'exposure', 0, 2 )
  104. .onChange( function ( value ) {
  105. renderer.toneMappingExposure = value;
  106. } );
  107. const backgroundFolder = gui.addFolder( 'Background' );
  108. backgroundFolder.add( params, 'blurriness', 0, 1 )
  109. .onChange( function ( value ) {
  110. scene.backgroundBlurriness = value;
  111. } );
  112. backgroundFolder.add( params, 'intensity', 0, 1 )
  113. .onChange( function ( value ) {
  114. scene.backgroundIntensity = value;
  115. } );
  116. updateGUI( toneMappingFolder );
  117. gui.open();
  118. }
  119. function updateGUI() {
  120. if ( params.toneMapping === 'None' ) {
  121. guiExposure.hide();
  122. } else {
  123. guiExposure.show();
  124. }
  125. }
  126. function onWindowResize() {
  127. camera.aspect = window.innerWidth / window.innerHeight;
  128. camera.updateProjectionMatrix();
  129. renderer.setSize( window.innerWidth, window.innerHeight );
  130. }
  131. function animate() {
  132. controls.update();
  133. renderer.render( scene, camera );
  134. }
  135. </script>
  136. </body>
  137. </html>
粤ICP备19079148号