postprocessing.html 3.5 KB

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