webgl_postprocessing_rgb_halftone.html 6.4 KB

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