webgl_postprocessing_glitch.html 3.9 KB

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