postprocessing-custom.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. <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 { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js';
  36. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  37. function main() {
  38. const canvas = document.querySelector( '#c' );
  39. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
  40. const fov = 75;
  41. const aspect = 2; // the canvas default
  42. const near = 0.1;
  43. const far = 5;
  44. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  45. camera.position.z = 2;
  46. const scene = new THREE.Scene();
  47. {
  48. const color = 0xFFFFFF;
  49. const intensity = 6;
  50. const light = new THREE.DirectionalLight( color, intensity );
  51. light.position.set( - 1, 2, 4 );
  52. scene.add( light );
  53. }
  54. const boxWidth = 1;
  55. const boxHeight = 1;
  56. const boxDepth = 1;
  57. const geometry = new THREE.BoxGeometry( boxWidth, boxHeight, boxDepth );
  58. function makeInstance( geometry, color, x ) {
  59. const material = new THREE.MeshPhongMaterial( { color } );
  60. const cube = new THREE.Mesh( geometry, material );
  61. scene.add( cube );
  62. cube.position.x = x;
  63. return cube;
  64. }
  65. const cubes = [
  66. makeInstance( geometry, 0x44aa88, 0 ),
  67. makeInstance( geometry, 0x8844aa, - 2 ),
  68. makeInstance( geometry, 0xaa8844, 2 ),
  69. ];
  70. const composer = new EffectComposer( renderer );
  71. composer.addPass( new RenderPass( scene, camera ) );
  72. const colorShader = {
  73. uniforms: {
  74. tDiffuse: { value: null },
  75. color: { value: new THREE.Color( 0x88CCFF ) },
  76. },
  77. vertexShader: `
  78. varying vec2 vUv;
  79. void main() {
  80. vUv = uv;
  81. gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1);
  82. }
  83. `,
  84. fragmentShader: `
  85. uniform vec3 color;
  86. uniform sampler2D tDiffuse;
  87. varying vec2 vUv;
  88. void main() {
  89. vec4 previousPassColor = texture2D(tDiffuse, vUv);
  90. gl_FragColor = vec4(
  91. previousPassColor.rgb * color,
  92. previousPassColor.a);
  93. }
  94. `,
  95. };
  96. const colorPass = new ShaderPass( colorShader );
  97. composer.addPass( colorPass );
  98. function resizeRendererToDisplaySize( renderer ) {
  99. const canvas = renderer.domElement;
  100. const width = canvas.clientWidth;
  101. const height = canvas.clientHeight;
  102. const needResize = canvas.width !== width || canvas.height !== height;
  103. if ( needResize ) {
  104. renderer.setSize( width, height, false );
  105. }
  106. return needResize;
  107. }
  108. const gui = new GUI();
  109. gui.add( colorPass.uniforms.color.value, 'r', 0, 4 ).name( 'red' );
  110. gui.add( colorPass.uniforms.color.value, 'g', 0, 4 ).name( 'green' );
  111. gui.add( colorPass.uniforms.color.value, 'b', 0, 4 ).name( 'blue' );
  112. let then = 0;
  113. function render( now ) {
  114. now *= 0.001; // convert to seconds
  115. const deltaTime = now - then;
  116. then = now;
  117. if ( resizeRendererToDisplaySize( renderer ) ) {
  118. const canvas = renderer.domElement;
  119. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  120. camera.updateProjectionMatrix();
  121. composer.setSize( canvas.width, canvas.height );
  122. }
  123. cubes.forEach( ( cube, ndx ) => {
  124. const speed = 1 + ndx * .1;
  125. const rot = now * speed;
  126. cube.rotation.x = rot;
  127. cube.rotation.y = rot;
  128. } );
  129. composer.render( deltaTime );
  130. requestAnimationFrame( render );
  131. }
  132. requestAnimationFrame( render );
  133. }
  134. main();
  135. </script>
  136. </html>