webgl_postprocessing_rgb_halftone.html 6.5 KB

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