webgpu_postprocessing_pixel.html 9.1 KB

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