webgl_postprocessing_rgb_halftone.html 6.2 KB

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