postprocessing-gui.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - PostProcessing - GUI</title>
  8. <style>
  9. html, body {
  10. height: 100%;
  11. margin: 0;
  12. }
  13. #c {
  14. width: 100%;
  15. height: 100%;
  16. display: block;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <canvas id="c"></canvas>
  22. </body>
  23. <!-- Import maps polyfill -->
  24. <!-- Remove this when import maps will be widely supported -->
  25. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  26. <script type="importmap">
  27. {
  28. "imports": {
  29. "three": "../../build/three.module.js",
  30. "three/addons/": "../../examples/jsm/"
  31. }
  32. }
  33. </script>
  34. <script type="module">
  35. import * as THREE from 'three';
  36. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  37. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  38. import { BloomPass } from 'three/addons/postprocessing/BloomPass.js';
  39. import { FilmPass } from 'three/addons/postprocessing/FilmPass.js';
  40. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  41. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  42. function main() {
  43. const canvas = document.querySelector( '#c' );
  44. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
  45. renderer.useLegacyLights = false;
  46. const fov = 75;
  47. const aspect = 2; // the canvas default
  48. const near = 0.1;
  49. const far = 5;
  50. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  51. camera.position.z = 2;
  52. const scene = new THREE.Scene();
  53. {
  54. const color = 0xFFFFFF;
  55. const intensity = 6;
  56. const light = new THREE.DirectionalLight( color, intensity );
  57. light.position.set( - 1, 2, 4 );
  58. scene.add( light );
  59. }
  60. const boxWidth = 1;
  61. const boxHeight = 1;
  62. const boxDepth = 1;
  63. const geometry = new THREE.BoxGeometry( boxWidth, boxHeight, boxDepth );
  64. function makeInstance( geometry, color, x ) {
  65. const material = new THREE.MeshPhongMaterial( { color } );
  66. const cube = new THREE.Mesh( geometry, material );
  67. scene.add( cube );
  68. cube.position.x = x;
  69. return cube;
  70. }
  71. const cubes = [
  72. makeInstance( geometry, 0x44aa88, 0 ),
  73. makeInstance( geometry, 0x8844aa, - 2 ),
  74. makeInstance( geometry, 0xaa8844, 2 ),
  75. ];
  76. const composer = new EffectComposer( renderer );
  77. composer.addPass( new RenderPass( scene, camera ) );
  78. const bloomPass = new BloomPass(
  79. 1, // strength
  80. 25, // kernel size
  81. 4, // sigma ?
  82. 256, // blur render target resolution
  83. );
  84. composer.addPass( bloomPass );
  85. const filmPass = new FilmPass(
  86. 0.35, // noise intensity
  87. 0.025, // scanline intensity
  88. 648, // scanline count
  89. false, // grayscale
  90. );
  91. composer.addPass( filmPass );
  92. const outputPass = new OutputPass();
  93. composer.addPass( outputPass );
  94. function resizeRendererToDisplaySize( renderer ) {
  95. const canvas = renderer.domElement;
  96. const width = canvas.clientWidth;
  97. const height = canvas.clientHeight;
  98. const needResize = canvas.width !== width || canvas.height !== height;
  99. if ( needResize ) {
  100. renderer.setSize( width, height, false );
  101. }
  102. return needResize;
  103. }
  104. const gui = new GUI();
  105. {
  106. const folder = gui.addFolder( 'BloomPass' );
  107. folder.add( bloomPass.combineUniforms.strength, 'value', 0, 2 ).name( 'strength' );
  108. folder.open();
  109. }
  110. {
  111. const folder = gui.addFolder( 'FilmPass' );
  112. folder.add( filmPass.uniforms.grayscale, 'value' ).name( 'grayscale' );
  113. folder.add( filmPass.uniforms.nIntensity, 'value', 0, 1 ).name( 'noise intensity' );
  114. folder.add( filmPass.uniforms.sIntensity, 'value', 0, 1 ).name( 'scanline intensity' );
  115. folder.add( filmPass.uniforms.sCount, 'value', 0, 1000 ).name( 'scanline count' );
  116. folder.open();
  117. }
  118. let then = 0;
  119. function render( now ) {
  120. now *= 0.001; // convert to seconds
  121. const deltaTime = now - then;
  122. then = now;
  123. if ( resizeRendererToDisplaySize( renderer ) ) {
  124. const canvas = renderer.domElement;
  125. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  126. camera.updateProjectionMatrix();
  127. composer.setSize( canvas.width, canvas.height );
  128. }
  129. cubes.forEach( ( cube, ndx ) => {
  130. const speed = 1 + ndx * .1;
  131. const rot = now * speed;
  132. cube.rotation.x = rot;
  133. cube.rotation.y = rot;
  134. } );
  135. composer.render( deltaTime );
  136. requestAnimationFrame( render );
  137. }
  138. requestAnimationFrame( render );
  139. }
  140. main();
  141. </script>
  142. </html>