webgl_postprocessing_pixel.html 8.5 KB

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