webgl_postprocessing_glitch.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. const material = new THREE.MeshPhongMaterial( { flatShading: true } );
  62. const mesh = new THREE.InstancedMesh( geometry, material, 100 );
  63. const dummy = new THREE.Object3D();
  64. const color = new THREE.Color();
  65. for ( let i = 0; i < 100; i ++ ) {
  66. dummy.position.set( Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 ).normalize();
  67. dummy.position.multiplyScalar( Math.random() * 400 );
  68. dummy.rotation.set( Math.random() * 2, Math.random() * 2, Math.random() * 2 );
  69. const scale = Math.random() * 50;
  70. dummy.scale.set( scale, scale, scale );
  71. dummy.updateMatrix();
  72. mesh.setMatrixAt( i, dummy.matrix );
  73. color.setHex( 0xffffff * Math.random() );
  74. mesh.setColorAt( i, color );
  75. }
  76. object.add( mesh );
  77. scene.add( new THREE.AmbientLight( 0xcccccc ) );
  78. light = new THREE.DirectionalLight( 0xffffff, 3 );
  79. light.position.set( 1, 1, 1 );
  80. scene.add( light );
  81. // postprocessing
  82. composer = new EffectComposer( renderer );
  83. composer.addPass( new RenderPass( scene, camera ) );
  84. glitchPass = new GlitchPass();
  85. composer.addPass( glitchPass );
  86. const outputPass = new OutputPass();
  87. composer.addPass( outputPass );
  88. //
  89. window.addEventListener( 'resize', onWindowResize );
  90. const wildGlitchOption = document.getElementById( 'wildGlitch' );
  91. wildGlitchOption.addEventListener( 'change', updateOptions );
  92. updateOptions();
  93. }
  94. function onWindowResize() {
  95. camera.aspect = window.innerWidth / window.innerHeight;
  96. camera.updateProjectionMatrix();
  97. renderer.setSize( window.innerWidth, window.innerHeight );
  98. composer.setSize( window.innerWidth, window.innerHeight );
  99. }
  100. function animate() {
  101. object.rotation.x += 0.005;
  102. object.rotation.y += 0.01;
  103. composer.render();
  104. }
  105. </script>
  106. </body>
  107. </html>
粤ICP备19079148号