webgpu_postprocessing_pixel.html 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - postprocessing pixel</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> - Node based 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.webgpu.js",
  19. "three/tsl": "../build/three.webgpu.js",
  20. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  27. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  28. import { uniform } from 'three/tsl';
  29. import { pixelationPass } from 'three/addons/tsl/display/PixelationPassNode.js';
  30. let camera, scene, renderer, postProcessing, crystalMesh, clock;
  31. let gui, effectController;
  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. // textures
  42. const loader = new THREE.TextureLoader();
  43. const texChecker = pixelTexture( loader.load( 'textures/checker.png' ) );
  44. const texChecker2 = pixelTexture( loader.load( 'textures/checker.png' ) );
  45. texChecker.repeat.set( 3, 3 );
  46. texChecker2.repeat.set( 1.5, 1.5 );
  47. // meshes
  48. const boxMaterial = new THREE.MeshPhongMaterial( { map: texChecker2 } );
  49. function addBox( boxSideLength, x, z, rotation ) {
  50. const mesh = new THREE.Mesh( new THREE.BoxGeometry( boxSideLength, boxSideLength, boxSideLength ), boxMaterial );
  51. mesh.castShadow = true;
  52. mesh.receiveShadow = true;
  53. mesh.rotation.y = rotation;
  54. mesh.position.y = boxSideLength / 2;
  55. mesh.position.set( x, boxSideLength / 2 + .0001, z );
  56. scene.add( mesh );
  57. return mesh;
  58. }
  59. addBox( .4, 0, 0, Math.PI / 4 );
  60. addBox( .5, - .5, - .5, Math.PI / 4 );
  61. const planeSideLength = 2;
  62. const planeMesh = new THREE.Mesh(
  63. new THREE.PlaneGeometry( planeSideLength, planeSideLength ),
  64. new THREE.MeshPhongMaterial( { map: texChecker } )
  65. );
  66. planeMesh.receiveShadow = true;
  67. planeMesh.rotation.x = - Math.PI / 2;
  68. scene.add( planeMesh );
  69. const radius = .2;
  70. const geometry = new THREE.IcosahedronGeometry( radius );
  71. crystalMesh = new THREE.Mesh(
  72. geometry,
  73. new THREE.MeshPhongMaterial( {
  74. color: 0x68b7e9,
  75. emissive: 0x4f7e8b,
  76. shininess: 10,
  77. specular: 0xffffff
  78. } )
  79. );
  80. crystalMesh.receiveShadow = true;
  81. crystalMesh.castShadow = true;
  82. scene.add( crystalMesh );
  83. // lights
  84. scene.add( new THREE.AmbientLight( 0x757f8e, 3 ) );
  85. const directionalLight = new THREE.DirectionalLight( 0xfffecd, 1.5 );
  86. directionalLight.position.set( 100, 100, 100 );
  87. directionalLight.castShadow = true;
  88. directionalLight.shadow.mapSize.set( 2048, 2048 );
  89. directionalLight.shadow.bias = - 0.0001;
  90. scene.add( directionalLight );
  91. const spotLight = new THREE.SpotLight( 0xffc100, 10, 10, Math.PI / 16, .02, 2 );
  92. spotLight.position.set( 2, 2, 0 );
  93. const target = spotLight.target;
  94. scene.add( target );
  95. target.position.set( 0, 0, 0 );
  96. spotLight.castShadow = true;
  97. spotLight.shadow.bias = - 0.001;
  98. scene.add( spotLight );
  99. renderer = new THREE.WebGPURenderer();
  100. renderer.shadowMap.enabled = true;
  101. renderer.shadowMap.type = THREE.BasicShadowMap;
  102. renderer.setSize( window.innerWidth, window.innerHeight );
  103. renderer.setAnimationLoop( animate );
  104. document.body.appendChild( renderer.domElement );
  105. effectController = {
  106. pixelSize: uniform( 6 ),
  107. normalEdgeStrength: uniform( 0.3 ),
  108. depthEdgeStrength: uniform( 0.4 ),
  109. pixelAlignedPanning: true
  110. };
  111. postProcessing = new THREE.PostProcessing( renderer );
  112. const scenePass = pixelationPass( scene, camera, effectController.pixelSize, effectController.normalEdgeStrength, effectController.depthEdgeStrength );
  113. postProcessing.outputNode = scenePass;
  114. window.addEventListener( 'resize', onWindowResize );
  115. const controls = new OrbitControls( camera, renderer.domElement );
  116. controls.maxZoom = 2;
  117. // gui
  118. gui = new GUI();
  119. gui.add( effectController.pixelSize, 'value', 1, 16, 1 ).name( 'Pixel Size' );
  120. gui.add( effectController.normalEdgeStrength, 'value', 0, 2, 0.05 ).name( 'Normal Edge Strength' );
  121. gui.add( effectController.depthEdgeStrength, 'value', 0, 1, 0.05 ).name( 'Depth Edge Strength' );
  122. gui.add( effectController, 'pixelAlignedPanning' );
  123. }
  124. function onWindowResize() {
  125. const aspectRatio = window.innerWidth / window.innerHeight;
  126. camera.left = - aspectRatio;
  127. camera.right = aspectRatio;
  128. camera.updateProjectionMatrix();
  129. renderer.setSize( window.innerWidth, window.innerHeight );
  130. }
  131. function animate() {
  132. const t = clock.getElapsedTime();
  133. crystalMesh.material.emissiveIntensity = Math.sin( t * 3 ) * .5 + .5;
  134. crystalMesh.position.y = .7 + Math.sin( t * 2 ) * .05;
  135. crystalMesh.rotation.y = stopGoEased( t, 2, 4 ) * 2 * Math.PI;
  136. const rendererSize = renderer.getSize( new THREE.Vector2() );
  137. const aspectRatio = rendererSize.x / rendererSize.y;
  138. if ( effectController.pixelAlignedPanning ) {
  139. const pixelSize = effectController.pixelSize.value;
  140. pixelAlignFrustum( camera, aspectRatio, Math.floor( rendererSize.x / pixelSize ),
  141. Math.floor( rendererSize.y / 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. postProcessing.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. texture.colorSpace = THREE.SRGBColorSpace;
  160. return texture;
  161. }
  162. function easeInOutCubic( x ) {
  163. return x ** 2 * 3 - x ** 3 * 2;
  164. }
  165. function linearStep( x, edge0, edge1 ) {
  166. const w = edge1 - edge0;
  167. const m = 1 / w;
  168. const y0 = - m * edge0;
  169. return THREE.MathUtils.clamp( y0 + m * x, 0, 1 );
  170. }
  171. function stopGoEased( x, downtime, period ) {
  172. const cycle = ( x / period ) | 0;
  173. const tween = x - cycle * period;
  174. const linStep = easeInOutCubic( linearStep( tween, downtime, period ) );
  175. return cycle + linStep;
  176. }
  177. function pixelAlignFrustum( camera, aspectRatio, pixelsPerScreenWidth, pixelsPerScreenHeight ) {
  178. // 0. Get Pixel Grid Units
  179. const worldScreenWidth = ( ( camera.right - camera.left ) / camera.zoom );
  180. const worldScreenHeight = ( ( camera.top - camera.bottom ) / camera.zoom );
  181. const pixelWidth = worldScreenWidth / pixelsPerScreenWidth;
  182. const pixelHeight = worldScreenHeight / pixelsPerScreenHeight;
  183. // 1. Project the current camera position along its local rotation bases
  184. const camPos = new THREE.Vector3(); camera.getWorldPosition( camPos );
  185. const camRot = new THREE.Quaternion(); camera.getWorldQuaternion( camRot );
  186. const camRight = new THREE.Vector3( 1.0, 0.0, 0.0 ).applyQuaternion( camRot );
  187. const camUp = new THREE.Vector3( 0.0, 1.0, 0.0 ).applyQuaternion( camRot );
  188. const camPosRight = camPos.dot( camRight );
  189. const camPosUp = camPos.dot( camUp );
  190. // 2. Find how far along its position is along these bases in pixel units
  191. const camPosRightPx = camPosRight / pixelWidth;
  192. const camPosUpPx = camPosUp / pixelHeight;
  193. // 3. Find the fractional pixel units and convert to world units
  194. const fractX = camPosRightPx - Math.round( camPosRightPx );
  195. const fractY = camPosUpPx - Math.round( camPosUpPx );
  196. // 4. Add fractional world units to the left/right top/bottom to align with the pixel grid
  197. camera.left = - aspectRatio - ( fractX * pixelWidth );
  198. camera.right = aspectRatio - ( fractX * pixelWidth );
  199. camera.top = 1.0 - ( fractY * pixelHeight );
  200. camera.bottom = - 1.0 - ( fractY * pixelHeight );
  201. camera.updateProjectionMatrix();
  202. }
  203. </script>
  204. </body>
  205. </html>