1
0

webgpu_postprocessing_pixel.html 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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="example.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  12. <div class="title-wrapper">
  13. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Pixelation</span>
  14. </div>
  15. <small>Node based pixelation pass with optional single pixel outlines by <a href="https://github.com/KodyJKing" target="_blank" rel="noopener">Kody King</a>.</small>
  16. </div>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.webgpu.js",
  21. "three/webgpu": "../build/three.webgpu.js",
  22. "three/tsl": "../build/three.tsl.js",
  23. "three/addons/": "./jsm/"
  24. }
  25. }
  26. </script>
  27. <script type="module">
  28. import * as THREE from 'three/webgpu';
  29. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  30. import { Inspector } from 'three/addons/inspector/Inspector.js';
  31. import { uniform } from 'three/tsl';
  32. import { pixelationPass } from 'three/addons/tsl/display/PixelationPassNode.js';
  33. let camera, scene, renderer, postProcessing, crystalMesh, clock;
  34. let effectController;
  35. init();
  36. function init() {
  37. const aspectRatio = window.innerWidth / window.innerHeight;
  38. camera = new THREE.OrthographicCamera( - aspectRatio, aspectRatio, 1, - 1, 0.1, 10 );
  39. camera.position.y = 2 * Math.tan( Math.PI / 6 );
  40. camera.position.z = 2;
  41. scene = new THREE.Scene();
  42. scene.background = new THREE.Color( 0x151729 );
  43. clock = new THREE.Clock();
  44. // textures
  45. const loader = new THREE.TextureLoader();
  46. const texChecker = pixelTexture( loader.load( 'textures/checker.png' ) );
  47. const texChecker2 = pixelTexture( loader.load( 'textures/checker.png' ) );
  48. texChecker.repeat.set( 3, 3 );
  49. texChecker2.repeat.set( 1.5, 1.5 );
  50. // meshes
  51. const boxMaterial = new THREE.MeshPhongMaterial( { map: texChecker2 } );
  52. function addBox( boxSideLength, x, z, rotation ) {
  53. const mesh = new THREE.Mesh( new THREE.BoxGeometry( boxSideLength, boxSideLength, boxSideLength ), boxMaterial );
  54. mesh.castShadow = true;
  55. mesh.receiveShadow = true;
  56. mesh.rotation.y = rotation;
  57. mesh.position.y = boxSideLength / 2;
  58. mesh.position.set( x, boxSideLength / 2 + .0001, z );
  59. scene.add( mesh );
  60. return mesh;
  61. }
  62. addBox( .4, 0, 0, Math.PI / 4 );
  63. addBox( .5, - .5, - .5, Math.PI / 4 );
  64. const planeSideLength = 2;
  65. const planeMesh = new THREE.Mesh(
  66. new THREE.PlaneGeometry( planeSideLength, planeSideLength ),
  67. new THREE.MeshPhongMaterial( { map: texChecker } )
  68. );
  69. planeMesh.receiveShadow = true;
  70. planeMesh.rotation.x = - Math.PI / 2;
  71. scene.add( planeMesh );
  72. const radius = .2;
  73. const geometry = new THREE.IcosahedronGeometry( radius );
  74. crystalMesh = new THREE.Mesh(
  75. geometry,
  76. new THREE.MeshPhongMaterial( {
  77. color: 0x68b7e9,
  78. emissive: 0x4f7e8b,
  79. shininess: 10,
  80. specular: 0xffffff
  81. } )
  82. );
  83. crystalMesh.receiveShadow = true;
  84. crystalMesh.castShadow = true;
  85. scene.add( crystalMesh );
  86. // lights
  87. scene.add( new THREE.AmbientLight( 0x757f8e, 3 ) );
  88. const directionalLight = new THREE.DirectionalLight( 0xfffecd, 1.5 );
  89. directionalLight.position.set( 100, 100, 100 );
  90. directionalLight.castShadow = true;
  91. directionalLight.shadow.mapSize.set( 2048, 2048 );
  92. directionalLight.shadow.bias = - 0.0001;
  93. scene.add( directionalLight );
  94. const spotLight = new THREE.SpotLight( 0xffc100, 10, 10, Math.PI / 16, .02, 2 );
  95. spotLight.position.set( 2, 2, 0 );
  96. const target = spotLight.target;
  97. scene.add( target );
  98. target.position.set( 0, 0, 0 );
  99. spotLight.castShadow = true;
  100. spotLight.shadow.bias = - 0.001;
  101. scene.add( spotLight );
  102. renderer = new THREE.WebGPURenderer();
  103. renderer.setSize( window.innerWidth, window.innerHeight );
  104. renderer.setAnimationLoop( animate );
  105. renderer.inspector = new Inspector();
  106. renderer.shadowMap.enabled = true;
  107. renderer.shadowMap.type = THREE.BasicShadowMap;
  108. document.body.appendChild( renderer.domElement );
  109. effectController = {
  110. pixelSize: uniform( 6 ),
  111. normalEdgeStrength: uniform( 0.3 ),
  112. depthEdgeStrength: uniform( 0.4 ),
  113. pixelAlignedPanning: true
  114. };
  115. postProcessing = new THREE.PostProcessing( renderer );
  116. const scenePass = pixelationPass( scene, camera, effectController.pixelSize, effectController.normalEdgeStrength, effectController.depthEdgeStrength );
  117. postProcessing.outputNode = scenePass;
  118. window.addEventListener( 'resize', onWindowResize );
  119. const controls = new OrbitControls( camera, renderer.domElement );
  120. controls.maxZoom = 2;
  121. // gui
  122. const gui = renderer.inspector.createParameters( 'Settings' );
  123. gui.add( effectController.pixelSize, 'value', 1, 16, 1 ).name( 'Pixel Size' );
  124. gui.add( effectController.normalEdgeStrength, 'value', 0, 2, 0.05 ).name( 'Normal Edge Strength' );
  125. gui.add( effectController.depthEdgeStrength, 'value', 0, 1, 0.05 ).name( 'Depth Edge Strength' );
  126. gui.add( effectController, 'pixelAlignedPanning' );
  127. }
  128. function onWindowResize() {
  129. const aspectRatio = window.innerWidth / window.innerHeight;
  130. camera.left = - aspectRatio;
  131. camera.right = aspectRatio;
  132. camera.updateProjectionMatrix();
  133. renderer.setSize( window.innerWidth, window.innerHeight );
  134. }
  135. function animate() {
  136. const t = clock.getElapsedTime();
  137. crystalMesh.material.emissiveIntensity = Math.sin( t * 3 ) * .5 + .5;
  138. crystalMesh.position.y = .7 + Math.sin( t * 2 ) * .05;
  139. crystalMesh.rotation.y = stopGoEased( t, 2, 4 ) * 2 * Math.PI;
  140. const rendererSize = renderer.getSize( new THREE.Vector2() );
  141. const aspectRatio = rendererSize.x / rendererSize.y;
  142. if ( effectController.pixelAlignedPanning ) {
  143. const pixelSize = effectController.pixelSize.value;
  144. pixelAlignFrustum( camera, aspectRatio, Math.floor( rendererSize.x / pixelSize ),
  145. Math.floor( rendererSize.y / 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. postProcessing.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号