postprocessing-custom.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 - Custom</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 {ShaderPass} from '../../examples/jsm/postprocessing/ShaderPass.js';
  38. import {GUI} from '../../examples/jsm/libs/lil-gui.module.min.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 colorShader = {
  75. uniforms: {
  76. tDiffuse: { value: null },
  77. color: { value: new THREE.Color(0x88CCFF) },
  78. },
  79. vertexShader: `
  80. varying vec2 vUv;
  81. void main() {
  82. vUv = uv;
  83. gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1);
  84. }
  85. `,
  86. fragmentShader: `
  87. uniform vec3 color;
  88. uniform sampler2D tDiffuse;
  89. varying vec2 vUv;
  90. void main() {
  91. vec4 previousPassColor = texture2D(tDiffuse, vUv);
  92. gl_FragColor = vec4(
  93. previousPassColor.rgb * color,
  94. previousPassColor.a);
  95. }
  96. `,
  97. };
  98. const colorPass = new ShaderPass(colorShader);
  99. colorPass.renderToScreen = true;
  100. composer.addPass(colorPass);
  101. function resizeRendererToDisplaySize(renderer) {
  102. const canvas = renderer.domElement;
  103. const width = canvas.clientWidth;
  104. const height = canvas.clientHeight;
  105. const needResize = canvas.width !== width || canvas.height !== height;
  106. if (needResize) {
  107. renderer.setSize(width, height, false);
  108. }
  109. return needResize;
  110. }
  111. const gui = new GUI();
  112. gui.add(colorPass.uniforms.color.value, 'r', 0, 4).name('red');
  113. gui.add(colorPass.uniforms.color.value, 'g', 0, 4).name('green');
  114. gui.add(colorPass.uniforms.color.value, 'b', 0, 4).name('blue');
  115. let then = 0;
  116. function render(now) {
  117. now *= 0.001; // convert to seconds
  118. const deltaTime = now - then;
  119. then = now;
  120. if (resizeRendererToDisplaySize(renderer)) {
  121. const canvas = renderer.domElement;
  122. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  123. camera.updateProjectionMatrix();
  124. composer.setSize(canvas.width, canvas.height);
  125. }
  126. cubes.forEach((cube, ndx) => {
  127. const speed = 1 + ndx * .1;
  128. const rot = now * speed;
  129. cube.rotation.x = rot;
  130. cube.rotation.y = rot;
  131. });
  132. composer.render(deltaTime);
  133. requestAnimationFrame(render);
  134. }
  135. requestAnimationFrame(render);
  136. }
  137. main();
  138. </script>
  139. </html>