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