webgl_postprocessing_pixel.html 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - post processing - pixelation</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">three.js</a> - Pixelation pass with optional single pixel outlines by
  12. <a href="https://github.com/KodyJKing" target="_blank" rel="noopener">Kody King</a><br /><br />
  13. </div>
  14. <div id="container"></div>
  15. <!-- Import maps polyfill -->
  16. <!-- Remove this when import maps will be widely supported -->
  17. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  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 { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  30. import { RenderPixelatedPass } from 'three/addons/postprocessing/RenderPixelatedPass.js';
  31. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  32. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  33. let camera, scene, renderer, composer, crystalMesh, clock;
  34. let gui, params;
  35. init();
  36. animate();
  37. function init() {
  38. const aspectRatio = window.innerWidth / window.innerHeight;
  39. camera = new THREE.OrthographicCamera( - aspectRatio, aspectRatio, 1, - 1, 0.1, 10 );
  40. camera.position.y = 2 * Math.tan( Math.PI / 6 );
  41. camera.position.z = 2;
  42. scene = new THREE.Scene();
  43. scene.background = new THREE.Color( 0x151729 );
  44. clock = new THREE.Clock();
  45. renderer = new THREE.WebGLRenderer();
  46. renderer.shadowMap.enabled = true;
  47. //renderer.setPixelRatio( window.devicePixelRatio );
  48. renderer.setSize( window.innerWidth, window.innerHeight );
  49. renderer.useLegacyLights = false;
  50. document.body.appendChild( renderer.domElement );
  51. composer = new EffectComposer( renderer );
  52. const renderPixelatedPass = new RenderPixelatedPass( 6, scene, camera );
  53. composer.addPass( renderPixelatedPass );
  54. const outputPass = new OutputPass();
  55. composer.addPass( outputPass );
  56. window.addEventListener( 'resize', onWindowResize );
  57. const controls = new OrbitControls( camera, renderer.domElement );
  58. controls.maxZoom = 2;
  59. // gui
  60. gui = new GUI();
  61. params = { pixelSize: 6, normalEdgeStrength: .3, depthEdgeStrength: .4, pixelAlignedPanning: true };
  62. gui.add( params, 'pixelSize' ).min( 1 ).max( 16 ).step( 1 )
  63. .onChange( () => {
  64. renderPixelatedPass.setPixelSize( params.pixelSize );
  65. } );
  66. gui.add( renderPixelatedPass, 'normalEdgeStrength' ).min( 0 ).max( 2 ).step( .05 );
  67. gui.add( renderPixelatedPass, 'depthEdgeStrength' ).min( 0 ).max( 1 ).step( .05 );
  68. gui.add( params, 'pixelAlignedPanning' );
  69. // textures
  70. const loader = new THREE.TextureLoader();
  71. const texChecker = pixelTexture( loader.load( 'textures/checker.png' ) );
  72. const texChecker2 = pixelTexture( loader.load( 'textures/checker.png' ) );
  73. texChecker.repeat.set( 3, 3 );
  74. texChecker2.repeat.set( 1.5, 1.5 );
  75. // meshes
  76. const boxMaterial = new THREE.MeshPhongMaterial( { map: texChecker2 } );
  77. function addBox( boxSideLength, x, z, rotation ) {
  78. const mesh = new THREE.Mesh( new THREE.BoxGeometry( boxSideLength, boxSideLength, boxSideLength ), boxMaterial );
  79. mesh.castShadow = true;
  80. mesh.receiveShadow = true;
  81. mesh.rotation.y = rotation;
  82. mesh.position.y = boxSideLength / 2;
  83. mesh.position.set( x, boxSideLength / 2 + .0001, z );
  84. scene.add( mesh );
  85. return mesh;
  86. }
  87. addBox( .4, 0, 0, Math.PI / 4 );
  88. addBox( .5, - .5, - .5, Math.PI / 4 );
  89. const planeSideLength = 2;
  90. const planeMesh = new THREE.Mesh(
  91. new THREE.PlaneGeometry( planeSideLength, planeSideLength ),
  92. new THREE.MeshPhongMaterial( { map: texChecker } )
  93. );
  94. planeMesh.receiveShadow = true;
  95. planeMesh.rotation.x = - Math.PI / 2;
  96. scene.add( planeMesh );
  97. const radius = .2;
  98. const geometry = new THREE.IcosahedronGeometry( radius );
  99. crystalMesh = new THREE.Mesh(
  100. geometry,
  101. new THREE.MeshPhongMaterial( {
  102. color: 0x68b7e9,
  103. emissive: 0x4f7e8b,
  104. shininess: 10,
  105. specular: 0xffffff
  106. } )
  107. );
  108. crystalMesh.receiveShadow = true;
  109. crystalMesh.castShadow = true;
  110. scene.add( crystalMesh );
  111. // lights
  112. scene.add( new THREE.AmbientLight( 0x757f8e, 3 ) );
  113. const directionalLight = new THREE.DirectionalLight( 0xfffecd, 1.5 );
  114. directionalLight.position.set( 100, 100, 100 );
  115. directionalLight.castShadow = true;
  116. directionalLight.shadow.mapSize.set( 2048, 2048 );
  117. scene.add( directionalLight );
  118. const spotLight = new THREE.SpotLight( 0xffc100, 10, 10, Math.PI / 16, .02, 2 );
  119. spotLight.position.set( 2, 2, 0 );
  120. const target = spotLight.target;
  121. scene.add( target );
  122. target.position.set( 0, 0, 0 );
  123. spotLight.castShadow = true;
  124. scene.add( spotLight );
  125. }
  126. function onWindowResize() {
  127. const aspectRatio = window.innerWidth / window.innerHeight;
  128. camera.left = - aspectRatio;
  129. camera.right = aspectRatio;
  130. camera.updateProjectionMatrix();
  131. renderer.setSize( window.innerWidth, window.innerHeight );
  132. composer.setSize( window.innerWidth, window.innerHeight );
  133. }
  134. function animate() {
  135. requestAnimationFrame( animate );
  136. const t = clock.getElapsedTime();
  137. crystalMesh.material.emissiveIntensity = Math.sin( t * 3 ) * .5 + .5;
  138. crystalMesh.position.y = .7 + Math.sin( t * 2 ) * .05;
  139. crystalMesh.rotation.y = stopGoEased( t, 2, 4 ) * 2 * Math.PI;
  140. const rendererSize = renderer.getSize( new THREE.Vector2() );
  141. const aspectRatio = rendererSize.x / rendererSize.y;
  142. if ( params[ 'pixelAlignedPanning' ] ) {
  143. pixelAlignFrustum( camera, aspectRatio, Math.floor( rendererSize.x / params[ 'pixelSize' ] ),
  144. Math.floor( rendererSize.y / params[ 'pixelSize' ] ) );
  145. } else if ( camera.left != - aspectRatio || camera.top != 1.0 ) {
  146. // Reset the Camera Frustum if it has been modified
  147. camera.left = - aspectRatio;
  148. camera.right = aspectRatio;
  149. camera.top = 1.0;
  150. camera.bottom = - 1.0;
  151. camera.updateProjectionMatrix();
  152. }
  153. composer.render();
  154. }
  155. // Helper functions
  156. function pixelTexture( texture ) {
  157. texture.minFilter = THREE.NearestFilter;
  158. texture.magFilter = THREE.NearestFilter;
  159. texture.generateMipmaps = false;
  160. texture.wrapS = THREE.RepeatWrapping;
  161. texture.wrapT = THREE.RepeatWrapping;
  162. texture.colorSpace = THREE.SRGBColorSpace;
  163. return texture;
  164. }
  165. function easeInOutCubic( x ) {
  166. return x ** 2 * 3 - x ** 3 * 2;
  167. }
  168. function linearStep( x, edge0, edge1 ) {
  169. const w = edge1 - edge0;
  170. const m = 1 / w;
  171. const y0 = - m * edge0;
  172. return THREE.MathUtils.clamp( y0 + m * x, 0, 1 );
  173. }
  174. function stopGoEased( x, downtime, period ) {
  175. const cycle = ( x / period ) | 0;
  176. const tween = x - cycle * period;
  177. const linStep = easeInOutCubic( linearStep( tween, downtime, period ) );
  178. return cycle + linStep;
  179. }
  180. function pixelAlignFrustum( camera, aspectRatio, pixelsPerScreenWidth, pixelsPerScreenHeight ) {
  181. // 0. Get Pixel Grid Units
  182. const worldScreenWidth = ( ( camera.right - camera.left ) / camera.zoom );
  183. const worldScreenHeight = ( ( camera.top - camera.bottom ) / camera.zoom );
  184. const pixelWidth = worldScreenWidth / pixelsPerScreenWidth;
  185. const pixelHeight = worldScreenHeight / pixelsPerScreenHeight;
  186. // 1. Project the current camera position along its local rotation bases
  187. const camPos = new THREE.Vector3(); camera.getWorldPosition( camPos );
  188. const camRot = new THREE.Quaternion(); camera.getWorldQuaternion( camRot );
  189. const camRight = new THREE.Vector3( 1.0, 0.0, 0.0 ).applyQuaternion( camRot );
  190. const camUp = new THREE.Vector3( 0.0, 1.0, 0.0 ).applyQuaternion( camRot );
  191. const camPosRight = camPos.dot( camRight );
  192. const camPosUp = camPos.dot( camUp );
  193. // 2. Find how far along its position is along these bases in pixel units
  194. const camPosRightPx = camPosRight / pixelWidth;
  195. const camPosUpPx = camPosUp / pixelHeight;
  196. // 3. Find the fractional pixel units and convert to world units
  197. const fractX = camPosRightPx - Math.round( camPosRightPx );
  198. const fractY = camPosUpPx - Math.round( camPosUpPx );
  199. // 4. Add fractional world units to the left/right top/bottom to align with the pixel grid
  200. camera.left = - aspectRatio - ( fractX * pixelWidth );
  201. camera.right = aspectRatio - ( fractX * pixelWidth );
  202. camera.top = 1.0 - ( fractY * pixelHeight );
  203. camera.bottom = - 1.0 - ( fractY * pixelHeight );
  204. camera.updateProjectionMatrix();
  205. }
  206. </script>
  207. </body>
  208. </html>