webgl_postprocessing_rgb_halftone.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - postprocessing RGB Halftone</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> - RGB Halftone post-processing by
  12. <a href="https://github.com/meatbags" target="_blank">Xavier Burrow</a>
  13. </div>
  14. <!-- Import maps polyfill -->
  15. <!-- Remove this when import maps will be widely supported -->
  16. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import Stats from 'three/addons/libs/stats.module.js';
  28. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  29. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  30. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  31. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  32. import { HalftonePass } from 'three/addons/postprocessing/HalftonePass.js';
  33. let renderer, clock, camera, stats;
  34. const rotationSpeed = Math.PI / 64;
  35. let composer, group;
  36. init();
  37. animate();
  38. function init() {
  39. renderer = new THREE.WebGLRenderer();
  40. renderer.setPixelRatio( window.devicePixelRatio );
  41. renderer.setSize( window.innerWidth, window.innerHeight );
  42. renderer.useLegacyLights = false;
  43. clock = new THREE.Clock();
  44. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 1000 );
  45. camera.position.z = 12;
  46. stats = new Stats();
  47. document.body.appendChild( renderer.domElement );
  48. document.body.appendChild( stats.dom );
  49. // camera controls
  50. const controls = new OrbitControls( camera, renderer.domElement );
  51. controls.target.set( 0, 0, 0 );
  52. controls.update();
  53. // scene
  54. const scene = new THREE.Scene();
  55. scene.background = new THREE.Color( 0x444444 );
  56. group = new THREE.Group();
  57. const floor = new THREE.Mesh( new THREE.BoxGeometry( 100, 1, 100 ), new THREE.MeshPhongMaterial( {} ) );
  58. floor.position.y = - 10;
  59. const light = new THREE.PointLight( 0xffffff, 250 );
  60. light.position.y = 2;
  61. group.add( floor, light );
  62. scene.add( group );
  63. const mat = new THREE.ShaderMaterial( {
  64. uniforms: {},
  65. vertexShader: [
  66. 'varying vec2 vUV;',
  67. 'varying vec3 vNormal;',
  68. 'void main() {',
  69. 'vUV = uv;',
  70. 'vNormal = vec3( normal );',
  71. 'gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
  72. '}'
  73. ].join( '\n' ),
  74. fragmentShader: [
  75. 'varying vec2 vUV;',
  76. 'varying vec3 vNormal;',
  77. 'void main() {',
  78. 'vec4 c = vec4( abs( vNormal ) + vec3( vUV, 0.0 ), 0.0 );',
  79. 'gl_FragColor = c;',
  80. '}'
  81. ].join( '\n' )
  82. } );
  83. for ( let i = 0; i < 50; ++ i ) {
  84. // fill scene with coloured cubes
  85. const mesh = new THREE.Mesh( new THREE.BoxGeometry( 2, 2, 2 ), mat );
  86. mesh.position.set( Math.random() * 16 - 8, Math.random() * 16 - 8, Math.random() * 16 - 8 );
  87. mesh.rotation.set( Math.random() * Math.PI * 2, Math.random() * Math.PI * 2, Math.random() * Math.PI * 2 );
  88. group.add( mesh );
  89. }
  90. // post-processing
  91. composer = new EffectComposer( renderer );
  92. const renderPass = new RenderPass( scene, camera );
  93. const params = {
  94. shape: 1,
  95. radius: 4,
  96. rotateR: Math.PI / 12,
  97. rotateB: Math.PI / 12 * 2,
  98. rotateG: Math.PI / 12 * 3,
  99. scatter: 0,
  100. blending: 1,
  101. blendingMode: 1,
  102. greyscale: false,
  103. disable: false
  104. };
  105. const halftonePass = new HalftonePass( window.innerWidth, window.innerHeight, params );
  106. composer.addPass( renderPass );
  107. composer.addPass( halftonePass );
  108. window.onresize = function () {
  109. // resize composer
  110. renderer.setSize( window.innerWidth, window.innerHeight );
  111. composer.setSize( window.innerWidth, window.innerHeight );
  112. camera.aspect = window.innerWidth / window.innerHeight;
  113. camera.updateProjectionMatrix();
  114. };
  115. // GUI
  116. const controller = {
  117. radius: halftonePass.uniforms[ 'radius' ].value,
  118. rotateR: halftonePass.uniforms[ 'rotateR' ].value / ( Math.PI / 180 ),
  119. rotateG: halftonePass.uniforms[ 'rotateG' ].value / ( Math.PI / 180 ),
  120. rotateB: halftonePass.uniforms[ 'rotateB' ].value / ( Math.PI / 180 ),
  121. scatter: halftonePass.uniforms[ 'scatter' ].value,
  122. shape: halftonePass.uniforms[ 'shape' ].value,
  123. greyscale: halftonePass.uniforms[ 'greyscale' ].value,
  124. blending: halftonePass.uniforms[ 'blending' ].value,
  125. blendingMode: halftonePass.uniforms[ 'blendingMode' ].value,
  126. disable: halftonePass.uniforms[ 'disable' ].value
  127. };
  128. function onGUIChange() {
  129. // update uniforms
  130. halftonePass.uniforms[ 'radius' ].value = controller.radius;
  131. halftonePass.uniforms[ 'rotateR' ].value = controller.rotateR * ( Math.PI / 180 );
  132. halftonePass.uniforms[ 'rotateG' ].value = controller.rotateG * ( Math.PI / 180 );
  133. halftonePass.uniforms[ 'rotateB' ].value = controller.rotateB * ( Math.PI / 180 );
  134. halftonePass.uniforms[ 'scatter' ].value = controller.scatter;
  135. halftonePass.uniforms[ 'shape' ].value = controller.shape;
  136. halftonePass.uniforms[ 'greyscale' ].value = controller.greyscale;
  137. halftonePass.uniforms[ 'blending' ].value = controller.blending;
  138. halftonePass.uniforms[ 'blendingMode' ].value = controller.blendingMode;
  139. halftonePass.uniforms[ 'disable' ].value = controller.disable;
  140. }
  141. const gui = new GUI();
  142. gui.add( controller, 'shape', { 'Dot': 1, 'Ellipse': 2, 'Line': 3, 'Square': 4 } ).onChange( onGUIChange );
  143. gui.add( controller, 'radius', 1, 25 ).onChange( onGUIChange );
  144. gui.add( controller, 'rotateR', 0, 90 ).onChange( onGUIChange );
  145. gui.add( controller, 'rotateG', 0, 90 ).onChange( onGUIChange );
  146. gui.add( controller, 'rotateB', 0, 90 ).onChange( onGUIChange );
  147. gui.add( controller, 'scatter', 0, 1, 0.01 ).onChange( onGUIChange );
  148. gui.add( controller, 'greyscale' ).onChange( onGUIChange );
  149. gui.add( controller, 'blending', 0, 1, 0.01 ).onChange( onGUIChange );
  150. gui.add( controller, 'blendingMode', { 'Linear': 1, 'Multiply': 2, 'Add': 3, 'Lighter': 4, 'Darker': 5 } ).onChange( onGUIChange );
  151. gui.add( controller, 'disable' ).onChange( onGUIChange );
  152. }
  153. function animate() {
  154. requestAnimationFrame( animate );
  155. const delta = clock.getDelta();
  156. stats.update();
  157. group.rotation.y += delta * rotationSpeed;
  158. composer.render( delta );
  159. }
  160. </script>
  161. </body>
  162. </html>