webgl_postprocessing_glitch.html 3.6 KB

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