webgl_postprocessing_glitch.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. <meta property="og:title" content="three.js webgl - postprocessing - digital glitch">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_postprocessing_glitch.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_postprocessing_glitch.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="overlay">
  15. <h2>WARNING</h2>
  16. <p style="text-align: center; max-width:450px; margin-bottom:40px;">
  17. This example may potentially trigger seizures for people with <strong>photosensitive epilepsy</strong>.
  18. </p>
  19. <button id="startButton">Okay</button>
  20. </div>
  21. <div id="info">
  22. <label for="dotScreen">Glitch me wild:</label><input id="wildGlitch" type="checkbox"/><br />
  23. </div>
  24. <script type="importmap">
  25. {
  26. "imports": {
  27. "three": "../build/three.module.js",
  28. "three/addons/": "./jsm/"
  29. }
  30. }
  31. </script>
  32. <script type="module">
  33. import * as THREE from 'three';
  34. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  35. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  36. import { GlitchPass } from 'three/addons/postprocessing/GlitchPass.js';
  37. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  38. let camera, scene, renderer, composer;
  39. let object, light;
  40. let glitchPass;
  41. const button = document.querySelector( '#startButton' );
  42. button.addEventListener( 'click', function () {
  43. const overlay = document.getElementById( 'overlay' );
  44. overlay.remove();
  45. init();
  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. renderer.setAnimationLoop( animate );
  56. document.body.appendChild( renderer.domElement );
  57. //
  58. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  59. camera.position.z = 400;
  60. scene = new THREE.Scene();
  61. scene.fog = new THREE.Fog( 0x000000, 1, 1000 );
  62. object = new THREE.Object3D();
  63. scene.add( object );
  64. const geometry = new THREE.SphereGeometry( 1, 4, 4 );
  65. const material = new THREE.MeshPhongMaterial( { flatShading: true } );
  66. const mesh = new THREE.InstancedMesh( geometry, material, 100 );
  67. const dummy = new THREE.Object3D();
  68. const color = new THREE.Color();
  69. for ( let i = 0; i < 100; i ++ ) {
  70. dummy.position.set( Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 ).normalize();
  71. dummy.position.multiplyScalar( Math.random() * 400 );
  72. dummy.rotation.set( Math.random() * 2, Math.random() * 2, Math.random() * 2 );
  73. const scale = Math.random() * 50;
  74. dummy.scale.set( scale, scale, scale );
  75. dummy.updateMatrix();
  76. mesh.setMatrixAt( i, dummy.matrix );
  77. color.setHex( 0xffffff * Math.random() );
  78. mesh.setColorAt( i, color );
  79. }
  80. object.add( mesh );
  81. scene.add( new THREE.AmbientLight( 0xcccccc ) );
  82. light = new THREE.DirectionalLight( 0xffffff, 3 );
  83. light.position.set( 1, 1, 1 );
  84. scene.add( light );
  85. // postprocessing
  86. composer = new EffectComposer( renderer );
  87. composer.addPass( new RenderPass( scene, camera ) );
  88. glitchPass = new GlitchPass();
  89. composer.addPass( glitchPass );
  90. const outputPass = new OutputPass();
  91. composer.addPass( outputPass );
  92. //
  93. window.addEventListener( 'resize', onWindowResize );
  94. const wildGlitchOption = document.getElementById( 'wildGlitch' );
  95. wildGlitchOption.addEventListener( 'change', updateOptions );
  96. updateOptions();
  97. }
  98. function onWindowResize() {
  99. camera.aspect = window.innerWidth / window.innerHeight;
  100. camera.updateProjectionMatrix();
  101. renderer.setSize( window.innerWidth, window.innerHeight );
  102. composer.setSize( window.innerWidth, window.innerHeight );
  103. }
  104. function animate() {
  105. object.rotation.x += 0.005;
  106. object.rotation.y += 0.01;
  107. composer.render();
  108. }
  109. </script>
  110. </body>
  111. </html>
粤ICP备19079148号