webgl_postprocessing_glitch.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <label for="dotScreen">Glitch me wild:</label><input id="wildGlitch" type="checkbox"/><br />
  12. </div>
  13. <script type="module">
  14. import * as THREE from '../build/three.module.js';
  15. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  16. import { RenderPass } from './jsm/postprocessing/RenderPass.js';
  17. import { GlitchPass } from './jsm/postprocessing/GlitchPass.js';
  18. var camera, scene, renderer, composer;
  19. var object, light;
  20. var glitchPass;
  21. init();
  22. animate();
  23. function updateOptions() {
  24. var wildGlitch = document.getElementById( 'wildGlitch' );
  25. glitchPass.goWild = wildGlitch.checked;
  26. }
  27. function init() {
  28. renderer = new THREE.WebGLRenderer();
  29. renderer.setPixelRatio( window.devicePixelRatio );
  30. renderer.setSize( window.innerWidth, window.innerHeight );
  31. document.body.appendChild( renderer.domElement );
  32. //
  33. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  34. camera.position.z = 400;
  35. scene = new THREE.Scene();
  36. scene.fog = new THREE.Fog( 0x000000, 1, 1000 );
  37. object = new THREE.Object3D();
  38. scene.add( object );
  39. var geometry = new THREE.SphereBufferGeometry( 1, 4, 4 );
  40. for ( var i = 0; i < 100; i ++ ) {
  41. var material = new THREE.MeshPhongMaterial( { color: 0xffffff * Math.random(), flatShading: true } );
  42. var mesh = new THREE.Mesh( geometry, material );
  43. mesh.position.set( Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 ).normalize();
  44. mesh.position.multiplyScalar( Math.random() * 400 );
  45. mesh.rotation.set( Math.random() * 2, Math.random() * 2, Math.random() * 2 );
  46. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 50;
  47. object.add( mesh );
  48. }
  49. scene.add( new THREE.AmbientLight( 0x222222 ) );
  50. light = new THREE.DirectionalLight( 0xffffff );
  51. light.position.set( 1, 1, 1 );
  52. scene.add( light );
  53. // postprocessing
  54. composer = new EffectComposer( renderer );
  55. composer.addPass( new RenderPass( scene, camera ) );
  56. glitchPass = new GlitchPass();
  57. composer.addPass( glitchPass );
  58. //
  59. window.addEventListener( 'resize', onWindowResize, false );
  60. var wildGlitchOption = document.getElementById( 'wildGlitch' );
  61. wildGlitchOption.addEventListener( 'change', updateOptions );
  62. updateOptions();
  63. }
  64. function onWindowResize() {
  65. camera.aspect = window.innerWidth / window.innerHeight;
  66. camera.updateProjectionMatrix();
  67. renderer.setSize( window.innerWidth, window.innerHeight );
  68. composer.setSize( window.innerWidth, window.innerHeight );
  69. }
  70. function animate() {
  71. requestAnimationFrame( animate );
  72. object.rotation.x += 0.005;
  73. object.rotation.y += 0.01;
  74. composer.render();
  75. }
  76. </script>
  77. </body>
  78. </html>