2
0

postprocessing.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. <!-- 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. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three';
  35. import {EffectComposer} from '../../examples/jsm/postprocessing/EffectComposer.js';
  36. import {RenderPass} from '../../examples/jsm/postprocessing/RenderPass.js';
  37. import {BloomPass} from '../../examples/jsm/postprocessing/BloomPass.js';
  38. import {FilmPass} from '../../examples/jsm/postprocessing/FilmPass.js';
  39. function main() {
  40. const canvas = document.querySelector('#c');
  41. const renderer = new THREE.WebGLRenderer({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 = 2;
  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. filmPass.renderToScreen = true;
  88. composer.addPass(filmPass);
  89. function resizeRendererToDisplaySize(renderer) {
  90. const canvas = renderer.domElement;
  91. const width = canvas.clientWidth;
  92. const height = canvas.clientHeight;
  93. const needResize = canvas.width !== width || canvas.height !== height;
  94. if (needResize) {
  95. renderer.setSize(width, height, false);
  96. }
  97. return needResize;
  98. }
  99. let then = 0;
  100. function render(now) {
  101. now *= 0.001; // convert to seconds
  102. const deltaTime = now - then;
  103. then = now;
  104. if (resizeRendererToDisplaySize(renderer)) {
  105. const canvas = renderer.domElement;
  106. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  107. camera.updateProjectionMatrix();
  108. composer.setSize(canvas.width, canvas.height);
  109. }
  110. cubes.forEach((cube, ndx) => {
  111. const speed = 1 + ndx * .1;
  112. const rot = now * speed;
  113. cube.rotation.x = rot;
  114. cube.rotation.y = rot;
  115. });
  116. composer.render(deltaTime);
  117. requestAnimationFrame(render);
  118. }
  119. requestAnimationFrame(render);
  120. }
  121. main();
  122. </script>
  123. </html>