webgl_postprocessing_glitch.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. <!-- Import maps polyfill -->
  21. <!-- Remove this when import maps will be widely supported -->
  22. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../build/three.module.js",
  27. "three/addons/": "./jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three';
  33. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  34. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  35. import { GlitchPass } from 'three/addons/postprocessing/GlitchPass.js';
  36. THREE.ColorManagement.enabled = false; // TODO: Consider enabling color management.
  37. let camera, scene, renderer, composer;
  38. let object, light;
  39. let glitchPass;
  40. const button = document.querySelector( '#startButton' );
  41. button.addEventListener( 'click', function () {
  42. const overlay = document.getElementById( 'overlay' );
  43. overlay.remove();
  44. init();
  45. animate();
  46. } );
  47. function updateOptions() {
  48. const wildGlitch = document.getElementById( 'wildGlitch' );
  49. glitchPass.goWild = wildGlitch.checked;
  50. }
  51. function init() {
  52. renderer = new THREE.WebGLRenderer();
  53. renderer.setPixelRatio( window.devicePixelRatio );
  54. renderer.setSize( window.innerWidth, window.innerHeight );
  55. document.body.appendChild( renderer.domElement );
  56. //
  57. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  58. camera.position.z = 400;
  59. scene = new THREE.Scene();
  60. scene.fog = new THREE.Fog( 0x000000, 1, 1000 );
  61. object = new THREE.Object3D();
  62. scene.add( object );
  63. const geometry = new THREE.SphereGeometry( 1, 4, 4 );
  64. for ( let i = 0; i < 100; i ++ ) {
  65. const material = new THREE.MeshPhongMaterial( { color: 0xffffff * Math.random(), flatShading: true } );
  66. const mesh = new THREE.Mesh( geometry, material );
  67. mesh.position.set( Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 ).normalize();
  68. mesh.position.multiplyScalar( Math.random() * 400 );
  69. mesh.rotation.set( Math.random() * 2, Math.random() * 2, Math.random() * 2 );
  70. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 50;
  71. object.add( mesh );
  72. }
  73. scene.add( new THREE.AmbientLight( 0x222222 ) );
  74. light = new THREE.DirectionalLight( 0xffffff );
  75. light.position.set( 1, 1, 1 );
  76. scene.add( light );
  77. // postprocessing
  78. composer = new EffectComposer( renderer );
  79. composer.addPass( new RenderPass( scene, camera ) );
  80. glitchPass = new GlitchPass();
  81. composer.addPass( glitchPass );
  82. //
  83. window.addEventListener( 'resize', onWindowResize );
  84. const wildGlitchOption = document.getElementById( 'wildGlitch' );
  85. wildGlitchOption.addEventListener( 'change', updateOptions );
  86. updateOptions();
  87. }
  88. function onWindowResize() {
  89. camera.aspect = window.innerWidth / window.innerHeight;
  90. camera.updateProjectionMatrix();
  91. renderer.setSize( window.innerWidth, window.innerHeight );
  92. composer.setSize( window.innerWidth, window.innerHeight );
  93. }
  94. function animate() {
  95. requestAnimationFrame( animate );
  96. object.rotation.x += 0.005;
  97. object.rotation.y += 0.01;
  98. composer.render();
  99. }
  100. </script>
  101. </body>
  102. </html>