postprocessing.html 3.4 KB

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