webgpu_postprocessing_pixel.html 8.4 KB

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