webgl_postprocessing_rgb_halftone.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import Stats from './jsm/libs/stats.module.js';
  27. import { GUI } from './jsm/libs/lil-gui.module.min.js';
  28. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  29. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  30. import { RenderPass } from './jsm/postprocessing/RenderPass.js';
  31. import { HalftonePass } from './jsm/postprocessing/HalftonePass.js';
  32. let renderer, clock, camera, stats;
  33. const rotationSpeed = Math.PI / 64;
  34. let composer, group;
  35. init();
  36. animate();
  37. function init() {
  38. renderer = new THREE.WebGLRenderer();
  39. renderer.setPixelRatio( window.devicePixelRatio );
  40. renderer.setSize( window.innerWidth, window.innerHeight );
  41. clock = new THREE.Clock();
  42. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 1000 );
  43. camera.position.z = 12;
  44. stats = new Stats();
  45. document.body.appendChild( renderer.domElement );
  46. document.body.appendChild( stats.dom );
  47. // camera controls
  48. const controls = new OrbitControls( camera, renderer.domElement );
  49. controls.target.set( 0, 0, 0 );
  50. controls.update();
  51. // scene
  52. const scene = new THREE.Scene();
  53. scene.background = new THREE.Color( 0x444444 );
  54. group = new THREE.Group();
  55. const floor = new THREE.Mesh( new THREE.BoxGeometry( 100, 1, 100 ), new THREE.MeshPhongMaterial( {} ) );
  56. floor.position.y = - 10;
  57. const light = new THREE.PointLight( 0xffffff, 1.0, 50, 2 );
  58. light.position.y = 2;
  59. group.add( floor, light );
  60. scene.add( group );
  61. const mat = new THREE.ShaderMaterial( {
  62. uniforms: {},
  63. vertexShader: [
  64. 'varying vec2 vUV;',
  65. 'varying vec3 vNormal;',
  66. 'void main() {',
  67. 'vUV = uv;',
  68. 'vNormal = vec3( normal );',
  69. 'gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
  70. '}'
  71. ].join( '\n' ),
  72. fragmentShader: [
  73. 'varying vec2 vUV;',
  74. 'varying vec3 vNormal;',
  75. 'void main() {',
  76. 'vec4 c = vec4( abs( vNormal ) + vec3( vUV, 0.0 ), 0.0 );',
  77. 'gl_FragColor = c;',
  78. '}'
  79. ].join( '\n' )
  80. } );
  81. for ( let i = 0; i < 50; ++ i ) {
  82. // fill scene with coloured cubes
  83. const mesh = new THREE.Mesh( new THREE.BoxGeometry( 2, 2, 2 ), mat );
  84. mesh.position.set( Math.random() * 16 - 8, Math.random() * 16 - 8, Math.random() * 16 - 8 );
  85. mesh.rotation.set( Math.random() * Math.PI * 2, Math.random() * Math.PI * 2, Math.random() * Math.PI * 2 );
  86. group.add( mesh );
  87. }
  88. // post-processing
  89. composer = new EffectComposer( renderer );
  90. const renderPass = new RenderPass( scene, camera );
  91. const params = {
  92. shape: 1,
  93. radius: 4,
  94. rotateR: Math.PI / 12,
  95. rotateB: Math.PI / 12 * 2,
  96. rotateG: Math.PI / 12 * 3,
  97. scatter: 0,
  98. blending: 1,
  99. blendingMode: 1,
  100. greyscale: false,
  101. disable: false
  102. };
  103. const halftonePass = new HalftonePass( window.innerWidth, window.innerHeight, params );
  104. composer.addPass( renderPass );
  105. composer.addPass( halftonePass );
  106. window.onresize = function () {
  107. // resize composer
  108. renderer.setSize( window.innerWidth, window.innerHeight );
  109. composer.setSize( window.innerWidth, window.innerHeight );
  110. camera.aspect = window.innerWidth / window.innerHeight;
  111. camera.updateProjectionMatrix();
  112. };
  113. // GUI
  114. const controller = {
  115. radius: halftonePass.uniforms[ 'radius' ].value,
  116. rotateR: halftonePass.uniforms[ 'rotateR' ].value / ( Math.PI / 180 ),
  117. rotateG: halftonePass.uniforms[ 'rotateG' ].value / ( Math.PI / 180 ),
  118. rotateB: halftonePass.uniforms[ 'rotateB' ].value / ( Math.PI / 180 ),
  119. scatter: halftonePass.uniforms[ 'scatter' ].value,
  120. shape: halftonePass.uniforms[ 'shape' ].value,
  121. greyscale: halftonePass.uniforms[ 'greyscale' ].value,
  122. blending: halftonePass.uniforms[ 'blending' ].value,
  123. blendingMode: halftonePass.uniforms[ 'blendingMode' ].value,
  124. disable: halftonePass.uniforms[ 'disable' ].value
  125. };
  126. function onGUIChange() {
  127. // update uniforms
  128. halftonePass.uniforms[ 'radius' ].value = controller.radius;
  129. halftonePass.uniforms[ 'rotateR' ].value = controller.rotateR * ( Math.PI / 180 );
  130. halftonePass.uniforms[ 'rotateG' ].value = controller.rotateG * ( Math.PI / 180 );
  131. halftonePass.uniforms[ 'rotateB' ].value = controller.rotateB * ( Math.PI / 180 );
  132. halftonePass.uniforms[ 'scatter' ].value = controller.scatter;
  133. halftonePass.uniforms[ 'shape' ].value = controller.shape;
  134. halftonePass.uniforms[ 'greyscale' ].value = controller.greyscale;
  135. halftonePass.uniforms[ 'blending' ].value = controller.blending;
  136. halftonePass.uniforms[ 'blendingMode' ].value = controller.blendingMode;
  137. halftonePass.uniforms[ 'disable' ].value = controller.disable;
  138. }
  139. const gui = new GUI();
  140. gui.add( controller, 'shape', { 'Dot': 1, 'Ellipse': 2, 'Line': 3, 'Square': 4 } ).onChange( onGUIChange );
  141. gui.add( controller, 'radius', 1, 25 ).onChange( onGUIChange );
  142. gui.add( controller, 'rotateR', 0, 90 ).onChange( onGUIChange );
  143. gui.add( controller, 'rotateG', 0, 90 ).onChange( onGUIChange );
  144. gui.add( controller, 'rotateB', 0, 90 ).onChange( onGUIChange );
  145. gui.add( controller, 'scatter', 0, 1, 0.01 ).onChange( onGUIChange );
  146. gui.add( controller, 'greyscale' ).onChange( onGUIChange );
  147. gui.add( controller, 'blending', 0, 1, 0.01 ).onChange( onGUIChange );
  148. gui.add( controller, 'blendingMode', { 'Linear': 1, 'Multiply': 2, 'Add': 3, 'Lighter': 4, 'Darker': 5 } ).onChange( onGUIChange );
  149. gui.add( controller, 'disable' ).onChange( onGUIChange );
  150. }
  151. function animate() {
  152. requestAnimationFrame( animate );
  153. const delta = clock.getDelta();
  154. stats.update();
  155. group.rotation.y += delta * rotationSpeed;
  156. composer.render( delta );
  157. }
  158. </script>
  159. </body>
  160. </html>