postprocessing-gui.html 4.0 KB

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