webgl_postprocessing_glitch.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. <style>
  10. #overlay {
  11. flex-direction: column;
  12. }
  13. </style>
  14. <body>
  15. <div id="overlay">
  16. <h3>Photosensitive Epilepsy Warning</h3>
  17. <p class="warning">
  18. This demo contains strong and frequent flashes which may trigger a potential <strong>Photosensitive Epilepsy.</strong>
  19. <br/>
  20. If you got any uncomfortable, press <strong>Ctrl + W</strong> or <strong>Command + W</strong> to close this page.
  21. </p>
  22. <button id="startButton">Play</button>
  23. </div>
  24. <div id="info">
  25. <label for="dotScreen">Glitch me wild:</label><input id="wildGlitch" type="checkbox"/><br />
  26. </div>
  27. <script type="module">
  28. import * as THREE from '../build/three.module.js';
  29. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  30. import { RenderPass } from './jsm/postprocessing/RenderPass.js';
  31. import { GlitchPass } from './jsm/postprocessing/GlitchPass.js';
  32. let camera, scene, renderer, composer;
  33. let object, light;
  34. let glitchPass;
  35. const btn = document.querySelector('#startButton');
  36. btn.addEventListener('click', function() {
  37. init();
  38. animate();
  39. });
  40. function updateOptions() {
  41. const wildGlitch = document.getElementById( 'wildGlitch' );
  42. glitchPass.goWild = wildGlitch.checked;
  43. }
  44. function init() {
  45. const overlay = document.getElementById( 'overlay' );
  46. overlay.remove();
  47. renderer = new THREE.WebGLRenderer();
  48. renderer.setPixelRatio( window.devicePixelRatio );
  49. renderer.setSize( window.innerWidth, window.innerHeight );
  50. document.body.appendChild( renderer.domElement );
  51. //
  52. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  53. camera.position.z = 400;
  54. scene = new THREE.Scene();
  55. scene.fog = new THREE.Fog( 0x000000, 1, 1000 );
  56. object = new THREE.Object3D();
  57. scene.add( object );
  58. const geometry = new THREE.SphereBufferGeometry( 1, 4, 4 );
  59. for ( let i = 0; i < 100; i ++ ) {
  60. const material = new THREE.MeshPhongMaterial( { color: 0xffffff * Math.random(), flatShading: true } );
  61. const mesh = new THREE.Mesh( geometry, material );
  62. mesh.position.set( Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 ).normalize();
  63. mesh.position.multiplyScalar( Math.random() * 400 );
  64. mesh.rotation.set( Math.random() * 2, Math.random() * 2, Math.random() * 2 );
  65. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 50;
  66. object.add( mesh );
  67. }
  68. scene.add( new THREE.AmbientLight( 0x222222 ) );
  69. light = new THREE.DirectionalLight( 0xffffff );
  70. light.position.set( 1, 1, 1 );
  71. scene.add( light );
  72. // postprocessing
  73. composer = new EffectComposer( renderer );
  74. composer.addPass( new RenderPass( scene, camera ) );
  75. glitchPass = new GlitchPass();
  76. composer.addPass( glitchPass );
  77. //
  78. window.addEventListener( 'resize', onWindowResize, false );
  79. const wildGlitchOption = document.getElementById( 'wildGlitch' );
  80. wildGlitchOption.addEventListener( 'change', updateOptions );
  81. updateOptions();
  82. }
  83. function onWindowResize() {
  84. camera.aspect = window.innerWidth / window.innerHeight;
  85. camera.updateProjectionMatrix();
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. composer.setSize( window.innerWidth, window.innerHeight );
  88. }
  89. function animate() {
  90. requestAnimationFrame( animate );
  91. object.rotation.x += 0.005;
  92. object.rotation.y += 0.01;
  93. composer.render();
  94. }
  95. </script>
  96. </body>
  97. </html>