webgl_postprocessing_pixel.html 8.6 KB

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