1
0

webgpu_postprocessing_pixel.html 8.6 KB

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