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