postprocessing-gui.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../../build/three.module.js",
  27. "three/addons/": "../../examples/jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three';
  33. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  34. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  35. import { BloomPass } from 'three/addons/postprocessing/BloomPass.js';
  36. import { FilmPass } from 'three/addons/postprocessing/FilmPass.js';
  37. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  38. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  39. function main() {
  40. const canvas = document.querySelector( '#c' );
  41. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
  42. const fov = 75;
  43. const aspect = 2; // the canvas default
  44. const near = 0.1;
  45. const far = 5;
  46. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  47. camera.position.z = 2;
  48. const scene = new THREE.Scene();
  49. {
  50. const color = 0xFFFFFF;
  51. const intensity = 6;
  52. const light = new THREE.DirectionalLight( color, intensity );
  53. light.position.set( - 1, 2, 4 );
  54. scene.add( light );
  55. }
  56. const boxWidth = 1;
  57. const boxHeight = 1;
  58. const boxDepth = 1;
  59. const geometry = new THREE.BoxGeometry( boxWidth, boxHeight, boxDepth );
  60. function makeInstance( geometry, color, x ) {
  61. const material = new THREE.MeshPhongMaterial( { color } );
  62. const cube = new THREE.Mesh( geometry, material );
  63. scene.add( cube );
  64. cube.position.x = x;
  65. return cube;
  66. }
  67. const cubes = [
  68. makeInstance( geometry, 0x44aa88, 0 ),
  69. makeInstance( geometry, 0x8844aa, - 2 ),
  70. makeInstance( geometry, 0xaa8844, 2 ),
  71. ];
  72. const composer = new EffectComposer( renderer );
  73. composer.addPass( new RenderPass( scene, camera ) );
  74. const bloomPass = new BloomPass(
  75. 1, // strength
  76. 25, // kernel size
  77. 4, // sigma ?
  78. 256, // blur render target resolution
  79. );
  80. composer.addPass( bloomPass );
  81. const filmPass = new FilmPass(
  82. 0.35, // noise intensity
  83. 0.025, // scanline intensity
  84. 648, // scanline count
  85. false, // grayscale
  86. );
  87. composer.addPass( filmPass );
  88. const outputPass = new OutputPass();
  89. composer.addPass( outputPass );
  90. function resizeRendererToDisplaySize( renderer ) {
  91. const canvas = renderer.domElement;
  92. const width = canvas.clientWidth;
  93. const height = canvas.clientHeight;
  94. const needResize = canvas.width !== width || canvas.height !== height;
  95. if ( needResize ) {
  96. renderer.setSize( width, height, false );
  97. }
  98. return needResize;
  99. }
  100. const gui = new GUI();
  101. {
  102. const folder = gui.addFolder( 'BloomPass' );
  103. folder.add( bloomPass.combineUniforms.strength, 'value', 0, 2 ).name( 'strength' );
  104. folder.open();
  105. }
  106. {
  107. const folder = gui.addFolder( 'FilmPass' );
  108. folder.add( filmPass.uniforms.grayscale, 'value' ).name( 'grayscale' );
  109. folder.add( filmPass.uniforms.nIntensity, 'value', 0, 1 ).name( 'noise intensity' );
  110. folder.add( filmPass.uniforms.sIntensity, 'value', 0, 1 ).name( 'scanline intensity' );
  111. folder.add( filmPass.uniforms.sCount, 'value', 0, 1000 ).name( 'scanline count' );
  112. folder.open();
  113. }
  114. let then = 0;
  115. function render( now ) {
  116. now *= 0.001; // convert to seconds
  117. const deltaTime = now - then;
  118. then = now;
  119. if ( resizeRendererToDisplaySize( renderer ) ) {
  120. const canvas = renderer.domElement;
  121. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  122. camera.updateProjectionMatrix();
  123. composer.setSize( canvas.width, canvas.height );
  124. }
  125. cubes.forEach( ( cube, ndx ) => {
  126. const speed = 1 + ndx * .1;
  127. const rot = now * speed;
  128. cube.rotation.x = rot;
  129. cube.rotation.y = rot;
  130. } );
  131. composer.render( deltaTime );
  132. requestAnimationFrame( render );
  133. }
  134. requestAnimationFrame( render );
  135. }
  136. main();
  137. </script>
  138. </html>