webgl_postprocessing_pixel.html 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - postprocessing - digital glitch</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. <style>
  8. body {
  9. background-color: #000000;
  10. margin: 0px;
  11. overflow: hidden;
  12. font-family:Monospace;
  13. font-size:13px;
  14. /*text-align:center;*/
  15. font-weight: lighter;
  16. }
  17. a {
  18. color: gray;
  19. font-weight: bold;
  20. }
  21. #info {
  22. color: gray;
  23. position: absolute;
  24. bottom: 0px;
  25. width: 100%;
  26. padding: 15px;
  27. }
  28. .dg.ac {
  29. z-index: 1 !important;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <script src="../build/three.js"></script>
  35. <script src="js/shaders/CopyShader.js"></script>
  36. <script src="js/shaders/PixelShader.js"></script>
  37. <script src="js/postprocessing/EffectComposer.js"></script>
  38. <script src="js/postprocessing/RenderPass.js"></script>
  39. <script src="js/postprocessing/ShaderPass.js"></script>
  40. <script src="js/controls/TrackballControls.js"></script>
  41. <script src='js/libs/dat.gui.min.js'></script>
  42. <div id="container"></div>
  43. <div id="info">
  44. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - pixel shader example<br/>
  45. shader by <a href="http://wongbryan.github.io">wongbryan</a>
  46. </div>
  47. <script>
  48. var camera, scene, renderer, gui, composer;
  49. var pixelPass, params;
  50. var geomData = [], matData = [], group;
  51. function updateGUI(){
  52. pixelPass.uniforms.pixels.value = params.pixels;
  53. pixelPass.uniforms.step.value = params.step;
  54. }
  55. function resize(){
  56. camera.aspect = window.innerWidth / window.innerHeight;
  57. camera.updateProjectionMatrix();
  58. renderer.setSize( window.innerWidth, window.innerHeight );
  59. }
  60. function init() {
  61. var container = document.getElementById( 'container' );
  62. renderer = new THREE.WebGLRenderer({antialias: true});
  63. renderer.shadowMap.enabled = true;
  64. renderer.shadowMap.type = THREE.PCSoftShadowMap;
  65. renderer.setPixelRatio( window.devicePixelRatio );
  66. renderer.setSize( window.innerWidth, window.innerHeight);
  67. renderer.setClearColor(0xbfe7ff);
  68. container.appendChild( renderer.domElement );
  69. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
  70. camera.position.set(0, 0, 30);
  71. controls = new THREE.TrackballControls(camera, renderer.domElement);
  72. controls.rotateSpeed = 2.0;
  73. controls.panSpeed = 0.8;
  74. controls.zoomSpeed = 1.5;
  75. scene = new THREE.Scene();
  76. var hemisphereLight = new THREE.HemisphereLight(0xfceafc, 0x000000, .8)
  77. var shadowLight = new THREE.DirectionalLight(0xffffff, .5);
  78. shadowLight.position.set(150, 75, 150);
  79. shadowLight.castShadow = true;
  80. shadowLight.shadow.camera.left = -75;
  81. shadowLight.shadow.camera.right = 75;
  82. shadowLight.shadow.camera.top = 75;
  83. shadowLight.shadow.camera.bottom = -75;
  84. shadowLight.shadow.camera.near = 1;
  85. shadowLight.shadow.camera.far = 1000;
  86. shadowLight.shadow.mapSize.width = 1024;
  87. shadowLight.shadow.mapSize.height = 1024;
  88. var shadowLight2 = shadowLight.clone();
  89. shadowLight2.castShadow = false;
  90. shadowLight2.intensity = .2;
  91. shadowLight2.position.set(-150, 75, -150);
  92. var shadowLight3 = shadowLight.clone();
  93. shadowLight3.castShadow = false;
  94. shadowLight3.intensity = .1;
  95. shadowLight3.position.set(0, 125, 0);
  96. scene.add(hemisphereLight);
  97. scene.add(shadowLight);
  98. scene.add(shadowLight2);
  99. scene.add(shadowLight3);
  100. geomData.push(new THREE.SphereGeometry(1, 64, 64));
  101. geomData.push(new THREE.BoxGeometry(1, 1, 1,));
  102. geomData.push(new THREE.ConeGeometry(1, 1, 32));
  103. geomData.push(new THREE.TetrahedronGeometry(1));
  104. geomData.push(new THREE.TorusKnotGeometry(1, .4));
  105. matData.push(new THREE.MeshStandardMaterial({
  106. color: 0xd9486b,
  107. emissive: 0x790f15,
  108. roughness: .14,
  109. flatShading: false,
  110. metalness: .3
  111. }));
  112. matData.push(new THREE.MeshStandardMaterial({
  113. color: 0xb3f28b,
  114. emissive: 0x68841f,
  115. metalness: .5,
  116. flatShading: false,
  117. roughness: .06
  118. }));
  119. matData.push(new THREE.MeshStandardMaterial({
  120. color: 0xfcfa37,
  121. emissive: 0xbd4215,
  122. metalness: .5,
  123. flatShading: false,
  124. roughness: .06
  125. }));
  126. matData.push(new THREE.MeshStandardMaterial({
  127. color: 0x5c70fb,
  128. emissive: 0x1235ae,
  129. roughness: 0,
  130. flatShading: false,
  131. metalness: 0
  132. }));
  133. matData.push(new THREE.MeshStandardMaterial({
  134. color: 0xbe9a47,
  135. emissive: 0x676925,
  136. roughness: .16,
  137. flatShading: false,
  138. metalness: 0
  139. }));
  140. matData.push(new THREE.MeshStandardMaterial({
  141. color: 0xb3f28b,
  142. emissive: 0x68841f,
  143. metalness: .5,
  144. flatShading: false,
  145. roughness: .06
  146. }));
  147. var numShapes = 25;
  148. group = new THREE.Group();
  149. for(var i=0; i<numShapes; i++){
  150. var geom = geomData[Math.floor(Math.random()*geomData.length)];
  151. var mat = matData[Math.floor(Math.random()*matData.length)]
  152. var mesh = new THREE.Mesh( geom, mat );
  153. var s = 4+Math.random()*10;
  154. mesh.scale.set(s, s, s);
  155. mesh.position.set( Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 ).normalize();
  156. mesh.position.multiplyScalar( Math.random() * 200 );
  157. mesh.rotation.set( Math.random() * 2, Math.random() * 2, Math.random() * 2 );
  158. group.add( mesh );
  159. }
  160. scene.add(group);
  161. composer = new THREE.EffectComposer(renderer);
  162. composer.addPass(new THREE.RenderPass(scene, camera));
  163. pixelPass = new THREE.ShaderPass(THREE.PixelShader);
  164. pixelPass.renderToScreen = true;
  165. composer.addPass(pixelPass);
  166. window.addEventListener('resize', resize);
  167. params = {
  168. pixels: 256,
  169. step: 2.5,
  170. postprocessing: true
  171. }
  172. var gui = new dat.GUI();
  173. gui.add(params, 'pixels').min(12.0).max(512).step(5.0);
  174. gui.add(params, 'step').min(1.0).max(5.0).step(.25);
  175. gui.add(params, 'postprocessing')
  176. }
  177. function update(){
  178. controls.update();
  179. updateGUI();
  180. group.rotation.y+=.0015;
  181. group.rotation.z+=.001;
  182. }
  183. function animate(){
  184. update();
  185. if(params.postprocessing)
  186. composer.render();
  187. else
  188. renderer.render(scene, camera);
  189. window.requestAnimationFrame(animate);
  190. }
  191. init();
  192. animate();
  193. </script>
  194. </body>
  195. </html>