webgl_postprocessing_rgb_halftone.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. // setup
  23. var renderer, clock, camera, controls, stats, rotationSpeed;
  24. renderer = new THREE.WebGLRenderer();
  25. renderer.setPixelRatio( window.devicePixelRatio );
  26. renderer.setSize( window.innerWidth, window.innerHeight );
  27. clock = new THREE.Clock();
  28. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 1000 );
  29. camera.position.z = 12;
  30. rotationSpeed = Math.PI / 64;
  31. stats = new Stats();
  32. document.body.appendChild( renderer.domElement );
  33. document.body.appendChild( stats.dom );
  34. // camera controls
  35. controls = new OrbitControls( camera, renderer.domElement );
  36. controls.target.set( 0, 0, 0 );
  37. controls.update();
  38. // scene
  39. var scene, group, mat, floor, light;
  40. scene = new THREE.Scene();
  41. scene.background = new THREE.Color( 0x444444 );
  42. group = new THREE.Group();
  43. floor = new THREE.Mesh( new THREE.BoxBufferGeometry( 100, 1, 100 ), new THREE.MeshPhongMaterial( {} ) );
  44. floor.position.y = - 10;
  45. light = new THREE.PointLight( 0xffffff, 1.0, 50, 2 );
  46. light.position.y = 2;
  47. group.add( floor, light );
  48. scene.add( group );
  49. mat = new THREE.ShaderMaterial( {
  50. uniforms: {},
  51. vertexShader: [
  52. "varying vec2 vUV;",
  53. "varying vec3 vNormal;",
  54. "void main() {",
  55. "vUV = uv;",
  56. "vNormal = vec3( normal );",
  57. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  58. "}"
  59. ].join( "\n" ),
  60. fragmentShader: [
  61. "varying vec2 vUV;",
  62. "varying vec3 vNormal;",
  63. "void main() {",
  64. "vec4 c = vec4( abs( vNormal ) + vec3( vUV, 0.0 ), 0.0 );",
  65. "gl_FragColor = c;",
  66. "}"
  67. ].join( "\n" )
  68. } );
  69. for ( var i = 0; i < 50; ++ i ) {
  70. // fill scene with coloured cubes
  71. var mesh = new THREE.Mesh( new THREE.BoxBufferGeometry( 2, 2, 2 ), mat );
  72. mesh.position.set( Math.random() * 16 - 8, Math.random() * 16 - 8, Math.random() * 16 - 8 );
  73. mesh.rotation.set( Math.random() * Math.PI * 2, Math.random() * Math.PI * 2, Math.random() * Math.PI * 2 );
  74. group.add( mesh );
  75. }
  76. // post-processing
  77. var composer, renderPass, halftonePass, params;
  78. composer = new EffectComposer( renderer );
  79. renderPass = new RenderPass( scene, camera );
  80. params = {
  81. shape: 1,
  82. radius: 4,
  83. rotateR: Math.PI / 12,
  84. rotateB: Math.PI / 12 * 2,
  85. rotateG: Math.PI / 12 * 3,
  86. scatter: 0,
  87. blending: 1,
  88. blendingMode: 1,
  89. greyscale: false,
  90. disable: false
  91. };
  92. halftonePass = new HalftonePass( window.innerWidth, window.innerHeight, params );
  93. composer.addPass( renderPass );
  94. composer.addPass( halftonePass );
  95. window.onresize = function () {
  96. // resize composer
  97. renderer.setSize( window.innerWidth, window.innerHeight );
  98. composer.setSize( window.innerWidth, window.innerHeight );
  99. camera.aspect = window.innerWidth / window.innerHeight;
  100. camera.updateProjectionMatrix();
  101. };
  102. // GUI
  103. var controller, gui, onGUIChange;
  104. 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. onGUIChange = function () {
  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. 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. function loop() {
  141. // demo loop
  142. var delta = clock.getDelta();
  143. requestAnimationFrame( loop );
  144. stats.update();
  145. group.rotation.y += delta * rotationSpeed;
  146. composer.render( delta );
  147. }
  148. loop();
  149. </script>
  150. </body>
  151. </html>