webgl_postprocessing_pixel.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - post processing - pixelation</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> - 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. <!-- Import maps polyfill -->
  16. <!-- Remove this when import maps will be widely supported -->
  17. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.module.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  30. import { RenderPixelatedPass } from 'three/addons/postprocessing/RenderPixelatedPass.js';
  31. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  32. let camera, scene, renderer, composer, crystalMesh, clock;
  33. let gui, params;
  34. init();
  35. animate();
  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. renderer = new THREE.WebGLRenderer();
  45. renderer.shadowMap.enabled = true;
  46. //renderer.setPixelRatio( window.devicePixelRatio );
  47. renderer.setSize( window.innerWidth, window.innerHeight );
  48. document.body.appendChild( renderer.domElement );
  49. composer = new EffectComposer( renderer );
  50. const renderPixelatedPass = new RenderPixelatedPass( 6, scene, camera );
  51. composer.addPass( renderPixelatedPass );
  52. window.addEventListener( 'resize', onWindowResize );
  53. const controls = new OrbitControls( camera, renderer.domElement );
  54. controls.maxZoom = 2;
  55. // gui
  56. gui = new GUI();
  57. params = { pixelSize: 6, normalEdgeStrength: .3, depthEdgeStrength: .4 };
  58. gui.add( params, 'pixelSize' ).min( 1 ).max( 16 ).step( 1 )
  59. .onChange( () => {
  60. renderPixelatedPass.setPixelSize( params.pixelSize );
  61. } );
  62. gui.add( renderPixelatedPass, 'normalEdgeStrength' ).min( 0 ).max( 2 ).step( .05 );
  63. gui.add( renderPixelatedPass, 'depthEdgeStrength' ).min( 0 ).max( 1 ).step( .05 );
  64. // textures
  65. const loader = new THREE.TextureLoader();
  66. const texChecker = pixelTexture( loader.load( 'textures/checker.png' ) );
  67. const texChecker2 = pixelTexture( loader.load( 'textures/checker.png' ) );
  68. texChecker.repeat.set( 3, 3 );
  69. texChecker2.repeat.set( 1.5, 1.5 );
  70. // meshes
  71. const boxMaterial = new THREE.MeshPhongMaterial( { map: texChecker2 } );
  72. function addBox( boxSideLength, x, z, rotation ) {
  73. const mesh = new THREE.Mesh( new THREE.BoxGeometry( boxSideLength, boxSideLength, boxSideLength ), boxMaterial );
  74. mesh.castShadow = true;
  75. mesh.receiveShadow = true;
  76. mesh.rotation.y = rotation;
  77. mesh.position.y = boxSideLength / 2;
  78. mesh.position.set( x, boxSideLength / 2 + .0001, z );
  79. scene.add( mesh );
  80. return mesh;
  81. }
  82. addBox( .4, 0, 0, Math.PI / 4 );
  83. addBox( .5, - .5, - .5, Math.PI / 4 );
  84. const planeSideLength = 2;
  85. const planeMesh = new THREE.Mesh(
  86. new THREE.PlaneGeometry( planeSideLength, planeSideLength ),
  87. new THREE.MeshPhongMaterial( { map: texChecker } )
  88. );
  89. planeMesh.receiveShadow = true;
  90. planeMesh.rotation.x = - Math.PI / 2;
  91. scene.add( planeMesh );
  92. const radius = .2;
  93. const geometry = new THREE.IcosahedronGeometry( radius );
  94. crystalMesh = new THREE.Mesh(
  95. geometry,
  96. new THREE.MeshPhongMaterial( {
  97. color: 0x2379cf,
  98. emissive: 0x143542,
  99. shininess: 10,
  100. specular: 0xffffff
  101. } )
  102. );
  103. crystalMesh.receiveShadow = true;
  104. crystalMesh.castShadow = true;
  105. scene.add( crystalMesh );
  106. // lights
  107. scene.add( new THREE.AmbientLight( 0x2d3645, 1.5 ) );
  108. const directionalLight = new THREE.DirectionalLight( 0xfffc9c, .5 );
  109. directionalLight.position.set( 100, 100, 100 );
  110. directionalLight.castShadow = true;
  111. directionalLight.shadow.mapSize.set( 2048, 2048 );
  112. scene.add( directionalLight );
  113. const spotLight = new THREE.SpotLight( 0xff8800, 1, 10, Math.PI / 16, .02, 2 );
  114. spotLight.position.set( 2, 2, 0 );
  115. const target = spotLight.target;
  116. scene.add( target );
  117. target.position.set( 0, 0, 0 );
  118. spotLight.castShadow = true;
  119. scene.add( spotLight );
  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. composer.setSize( window.innerWidth, window.innerHeight );
  128. }
  129. function animate() {
  130. requestAnimationFrame( animate );
  131. const t = clock.getElapsedTime();
  132. crystalMesh.material.emissiveIntensity = Math.sin( t * 3 ) * .5 + .5;
  133. crystalMesh.position.y = .7 + Math.sin( t * 2 ) * .05;
  134. crystalMesh.rotation.y = stopGoEased( t, 2, 4 ) * 2 * Math.PI;
  135. composer.render();
  136. }
  137. // Helper functions
  138. function pixelTexture( texture ) {
  139. texture.minFilter = THREE.NearestFilter;
  140. texture.magFilter = THREE.NearestFilter;
  141. texture.generateMipmaps = false;
  142. texture.wrapS = THREE.RepeatWrapping;
  143. texture.wrapT = THREE.RepeatWrapping;
  144. return texture;
  145. }
  146. function easeInOutCubic( x ) {
  147. return x ** 2 * 3 - x ** 3 * 2;
  148. }
  149. function linearStep( x, edge0, edge1 ) {
  150. const w = edge1 - edge0;
  151. const m = 1 / w;
  152. const y0 = - m * edge0;
  153. return THREE.MathUtils.clamp( y0 + m * x, 0, 1 );
  154. }
  155. function stopGoEased( x, downtime, period ) {
  156. const cycle = ( x / period ) | 0;
  157. const tween = x - cycle * period;
  158. const linStep = easeInOutCubic( linearStep( tween, downtime, period ) );
  159. return cycle + linStep;
  160. }
  161. </script>
  162. </body>
  163. </html>