webgl_postprocessing_pixel.html 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. document.body.appendChild( renderer.domElement );
  50. composer = new EffectComposer( renderer );
  51. const renderPixelatedPass = new RenderPixelatedPass( 6, scene, camera );
  52. composer.addPass( renderPixelatedPass );
  53. const outputPass = new OutputPass();
  54. composer.addPass( outputPass );
  55. window.addEventListener( 'resize', onWindowResize );
  56. const controls = new OrbitControls( camera, renderer.domElement );
  57. controls.maxZoom = 2;
  58. // gui
  59. gui = new GUI();
  60. params = { pixelSize: 6, normalEdgeStrength: .3, depthEdgeStrength: .4, pixelAlignedPanning: true };
  61. gui.add( params, 'pixelSize' ).min( 1 ).max( 16 ).step( 1 )
  62. .onChange( () => {
  63. renderPixelatedPass.setPixelSize( params.pixelSize );
  64. } );
  65. gui.add( renderPixelatedPass, 'normalEdgeStrength' ).min( 0 ).max( 2 ).step( .05 );
  66. gui.add( renderPixelatedPass, 'depthEdgeStrength' ).min( 0 ).max( 1 ).step( .05 );
  67. gui.add( params, 'pixelAlignedPanning' );
  68. // textures
  69. const loader = new THREE.TextureLoader();
  70. const texChecker = pixelTexture( loader.load( 'textures/checker.png' ) );
  71. const texChecker2 = pixelTexture( loader.load( 'textures/checker.png' ) );
  72. texChecker.repeat.set( 3, 3 );
  73. texChecker2.repeat.set( 1.5, 1.5 );
  74. // meshes
  75. const boxMaterial = new THREE.MeshPhongMaterial( { map: texChecker2 } );
  76. function addBox( boxSideLength, x, z, rotation ) {
  77. const mesh = new THREE.Mesh( new THREE.BoxGeometry( boxSideLength, boxSideLength, boxSideLength ), boxMaterial );
  78. mesh.castShadow = true;
  79. mesh.receiveShadow = true;
  80. mesh.rotation.y = rotation;
  81. mesh.position.y = boxSideLength / 2;
  82. mesh.position.set( x, boxSideLength / 2 + .0001, z );
  83. scene.add( mesh );
  84. return mesh;
  85. }
  86. addBox( .4, 0, 0, Math.PI / 4 );
  87. addBox( .5, - .5, - .5, Math.PI / 4 );
  88. const planeSideLength = 2;
  89. const planeMesh = new THREE.Mesh(
  90. new THREE.PlaneGeometry( planeSideLength, planeSideLength ),
  91. new THREE.MeshPhongMaterial( { map: texChecker } )
  92. );
  93. planeMesh.receiveShadow = true;
  94. planeMesh.rotation.x = - Math.PI / 2;
  95. scene.add( planeMesh );
  96. const radius = .2;
  97. const geometry = new THREE.IcosahedronGeometry( radius );
  98. crystalMesh = new THREE.Mesh(
  99. geometry,
  100. new THREE.MeshPhongMaterial( {
  101. color: 0x68b7e9,
  102. emissive: 0x4f7e8b,
  103. shininess: 10,
  104. specular: 0xffffff
  105. } )
  106. );
  107. crystalMesh.receiveShadow = true;
  108. crystalMesh.castShadow = true;
  109. scene.add( crystalMesh );
  110. // lights
  111. scene.add( new THREE.AmbientLight( 0x757f8e, 3 ) );
  112. const directionalLight = new THREE.DirectionalLight( 0xfffecd, 1.5 );
  113. directionalLight.position.set( 100, 100, 100 );
  114. directionalLight.castShadow = true;
  115. directionalLight.shadow.mapSize.set( 2048, 2048 );
  116. scene.add( directionalLight );
  117. const spotLight = new THREE.SpotLight( 0xffc100, 10, 10, Math.PI / 16, .02, 2 );
  118. spotLight.position.set( 2, 2, 0 );
  119. const target = spotLight.target;
  120. scene.add( target );
  121. target.position.set( 0, 0, 0 );
  122. spotLight.castShadow = true;
  123. scene.add( spotLight );
  124. }
  125. function onWindowResize() {
  126. const aspectRatio = window.innerWidth / window.innerHeight;
  127. camera.left = - aspectRatio;
  128. camera.right = aspectRatio;
  129. camera.updateProjectionMatrix();
  130. renderer.setSize( window.innerWidth, window.innerHeight );
  131. composer.setSize( window.innerWidth, window.innerHeight );
  132. }
  133. function animate() {
  134. requestAnimationFrame( animate );
  135. const t = clock.getElapsedTime();
  136. crystalMesh.material.emissiveIntensity = Math.sin( t * 3 ) * .5 + .5;
  137. crystalMesh.position.y = .7 + Math.sin( t * 2 ) * .05;
  138. crystalMesh.rotation.y = stopGoEased( t, 2, 4 ) * 2 * Math.PI;
  139. const rendererSize = renderer.getSize( new THREE.Vector2() );
  140. const aspectRatio = rendererSize.x / rendererSize.y;
  141. if ( params[ 'pixelAlignedPanning' ] ) {
  142. pixelAlignFrustum( camera, aspectRatio, Math.floor( rendererSize.x / params[ 'pixelSize' ] ),
  143. Math.floor( rendererSize.y / params[ 'pixelSize' ] ) );
  144. } else if ( camera.left != - aspectRatio || camera.top != 1.0 ) {
  145. // Reset the Camera Frustum if it has been modified
  146. camera.left = - aspectRatio;
  147. camera.right = aspectRatio;
  148. camera.top = 1.0;
  149. camera.bottom = - 1.0;
  150. camera.updateProjectionMatrix();
  151. }
  152. composer.render();
  153. }
  154. // Helper functions
  155. function pixelTexture( texture ) {
  156. texture.minFilter = THREE.NearestFilter;
  157. texture.magFilter = THREE.NearestFilter;
  158. texture.generateMipmaps = false;
  159. texture.wrapS = THREE.RepeatWrapping;
  160. texture.wrapT = THREE.RepeatWrapping;
  161. texture.colorSpace = THREE.SRGBColorSpace;
  162. return texture;
  163. }
  164. function easeInOutCubic( x ) {
  165. return x ** 2 * 3 - x ** 3 * 2;
  166. }
  167. function linearStep( x, edge0, edge1 ) {
  168. const w = edge1 - edge0;
  169. const m = 1 / w;
  170. const y0 = - m * edge0;
  171. return THREE.MathUtils.clamp( y0 + m * x, 0, 1 );
  172. }
  173. function stopGoEased( x, downtime, period ) {
  174. const cycle = ( x / period ) | 0;
  175. const tween = x - cycle * period;
  176. const linStep = easeInOutCubic( linearStep( tween, downtime, period ) );
  177. return cycle + linStep;
  178. }
  179. function pixelAlignFrustum( camera, aspectRatio, pixelsPerScreenWidth, pixelsPerScreenHeight ) {
  180. // 0. Get Pixel Grid Units
  181. const worldScreenWidth = ( ( camera.right - camera.left ) / camera.zoom );
  182. const worldScreenHeight = ( ( camera.top - camera.bottom ) / camera.zoom );
  183. const pixelWidth = worldScreenWidth / pixelsPerScreenWidth;
  184. const pixelHeight = worldScreenHeight / pixelsPerScreenHeight;
  185. // 1. Project the current camera position along its local rotation bases
  186. const camPos = new THREE.Vector3(); camera.getWorldPosition( camPos );
  187. const camRot = new THREE.Quaternion(); camera.getWorldQuaternion( camRot );
  188. const camRight = new THREE.Vector3( 1.0, 0.0, 0.0 ).applyQuaternion( camRot );
  189. const camUp = new THREE.Vector3( 0.0, 1.0, 0.0 ).applyQuaternion( camRot );
  190. const camPosRight = camPos.dot( camRight );
  191. const camPosUp = camPos.dot( camUp );
  192. // 2. Find how far along its position is along these bases in pixel units
  193. const camPosRightPx = camPosRight / pixelWidth;
  194. const camPosUpPx = camPosUp / pixelHeight;
  195. // 3. Find the fractional pixel units and convert to world units
  196. const fractX = camPosRightPx - Math.round( camPosRightPx );
  197. const fractY = camPosUpPx - Math.round( camPosUpPx );
  198. // 4. Add fractional world units to the left/right top/bottom to align with the pixel grid
  199. camera.left = - aspectRatio - ( fractX * pixelWidth );
  200. camera.right = aspectRatio - ( fractX * pixelWidth );
  201. camera.top = 1.0 - ( fractY * pixelHeight );
  202. camera.bottom = - 1.0 - ( fractY * pixelHeight );
  203. camera.updateProjectionMatrix();
  204. }
  205. </script>
  206. </body>
  207. </html>