webgl_postprocessing_glitch.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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="overlay">
  11. <h2>WARNING</h2>
  12. <p style="text-align: center; max-width:450px; margin-bottom:40px;">
  13. This example may potentially trigger seizures for people with <strong>photosensitive epilepsy</strong>.
  14. </p>
  15. <button id="startButton">Okay</button>
  16. </div>
  17. <div id="info">
  18. <label for="dotScreen">Glitch me wild:</label><input id="wildGlitch" type="checkbox"/><br />
  19. </div>
  20. <script type="module">
  21. import * as THREE from '../build/three.module.js';
  22. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  23. import { RenderPass } from './jsm/postprocessing/RenderPass.js';
  24. import { GlitchPass } from './jsm/postprocessing/GlitchPass.js';
  25. let camera, scene, renderer, composer;
  26. let object, light;
  27. let glitchPass;
  28. const button = document.querySelector( '#startButton' );
  29. button.addEventListener( 'click', function () {
  30. const overlay = document.getElementById( 'overlay' );
  31. overlay.remove();
  32. init();
  33. animate();
  34. } );
  35. function updateOptions() {
  36. const wildGlitch = document.getElementById( 'wildGlitch' );
  37. glitchPass.goWild = wildGlitch.checked;
  38. }
  39. function init() {
  40. renderer = new THREE.WebGLRenderer();
  41. renderer.setPixelRatio( window.devicePixelRatio );
  42. renderer.setSize( window.innerWidth, window.innerHeight );
  43. document.body.appendChild( renderer.domElement );
  44. //
  45. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  46. camera.position.z = 400;
  47. scene = new THREE.Scene();
  48. scene.fog = new THREE.Fog( 0x000000, 1, 1000 );
  49. object = new THREE.Object3D();
  50. scene.add( object );
  51. const geometry = new THREE.SphereGeometry( 1, 4, 4 );
  52. for ( let i = 0; i < 100; i ++ ) {
  53. const material = new THREE.MeshPhongMaterial( { color: 0xffffff * Math.random(), flatShading: true } );
  54. const mesh = new THREE.Mesh( geometry, material );
  55. mesh.position.set( Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 ).normalize();
  56. mesh.position.multiplyScalar( Math.random() * 400 );
  57. mesh.rotation.set( Math.random() * 2, Math.random() * 2, Math.random() * 2 );
  58. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 50;
  59. object.add( mesh );
  60. }
  61. scene.add( new THREE.AmbientLight( 0x222222 ) );
  62. light = new THREE.DirectionalLight( 0xffffff );
  63. light.position.set( 1, 1, 1 );
  64. scene.add( light );
  65. // postprocessing
  66. composer = new EffectComposer( renderer );
  67. composer.addPass( new RenderPass( scene, camera ) );
  68. glitchPass = new GlitchPass();
  69. composer.addPass( glitchPass );
  70. //
  71. window.addEventListener( 'resize', onWindowResize );
  72. const wildGlitchOption = document.getElementById( 'wildGlitch' );
  73. wildGlitchOption.addEventListener( 'change', updateOptions );
  74. updateOptions();
  75. }
  76. function onWindowResize() {
  77. camera.aspect = window.innerWidth / window.innerHeight;
  78. camera.updateProjectionMatrix();
  79. renderer.setSize( window.innerWidth, window.innerHeight );
  80. composer.setSize( window.innerWidth, window.innerHeight );
  81. }
  82. function animate() {
  83. requestAnimationFrame( animate );
  84. object.rotation.x += 0.005;
  85. object.rotation.y += 0.01;
  86. composer.render();
  87. }
  88. </script>
  89. </body>
  90. </html>