webgl_postprocessing_glitch.html 3.7 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 { GlitchPass } from 'three/addons/postprocessing/GlitchPass.js';
  31. let camera, scene, renderer;
  32. let object, light;
  33. let glitchPass;
  34. const button = document.querySelector( '#startButton' );
  35. button.addEventListener( 'click', function () {
  36. const overlay = document.getElementById( 'overlay' );
  37. overlay.remove();
  38. init();
  39. } );
  40. function updateOptions() {
  41. const wildGlitch = document.getElementById( 'wildGlitch' );
  42. glitchPass.goWild = wildGlitch.checked;
  43. }
  44. function init() {
  45. renderer = new THREE.WebGLRenderer( { outputBufferType: THREE.HalfFloatType } );
  46. renderer.setPixelRatio( window.devicePixelRatio );
  47. renderer.setSize( window.innerWidth, window.innerHeight );
  48. renderer.setAnimationLoop( animate );
  49. document.body.appendChild( renderer.domElement );
  50. //
  51. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  52. camera.position.z = 400;
  53. scene = new THREE.Scene();
  54. scene.fog = new THREE.Fog( 0x000000, 1, 1000 );
  55. object = new THREE.Object3D();
  56. scene.add( object );
  57. const geometry = new THREE.SphereGeometry( 1, 4, 4 );
  58. const material = new THREE.MeshPhongMaterial( { flatShading: true } );
  59. const mesh = new THREE.InstancedMesh( geometry, material, 100 );
  60. const dummy = new THREE.Object3D();
  61. const color = new THREE.Color();
  62. for ( let i = 0; i < 100; i ++ ) {
  63. dummy.position.set( Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 ).normalize();
  64. dummy.position.multiplyScalar( Math.random() * 400 );
  65. dummy.rotation.set( Math.random() * 2, Math.random() * 2, Math.random() * 2 );
  66. const scale = Math.random() * 50;
  67. dummy.scale.set( scale, scale, scale );
  68. dummy.updateMatrix();
  69. mesh.setMatrixAt( i, dummy.matrix );
  70. color.setHex( 0xffffff * Math.random() );
  71. mesh.setColorAt( i, color );
  72. }
  73. object.add( mesh );
  74. scene.add( new THREE.AmbientLight( 0xcccccc ) );
  75. light = new THREE.DirectionalLight( 0xffffff, 3 );
  76. light.position.set( 1, 1, 1 );
  77. scene.add( light );
  78. // postprocessing
  79. glitchPass = new GlitchPass();
  80. renderer.setEffects( [ glitchPass ] );
  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. }
  92. function animate() {
  93. object.rotation.x += 0.005;
  94. object.rotation.y += 0.01;
  95. renderer.render( scene, camera );
  96. }
  97. </script>
  98. </body>
  99. </html>
粤ICP备19079148号