webgl_postprocessing.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - postprocessing</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. <script type="module">
  11. import * as THREE from '../build/three.module.js';
  12. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  13. import { RenderPass } from './jsm/postprocessing/RenderPass.js';
  14. import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
  15. import { RGBShiftShader } from './jsm/shaders/RGBShiftShader.js';
  16. import { DotScreenShader } from './jsm/shaders/DotScreenShader.js';
  17. var camera, scene, renderer, composer;
  18. var object, light;
  19. init();
  20. animate();
  21. function init() {
  22. renderer = new THREE.WebGLRenderer();
  23. renderer.setPixelRatio( window.devicePixelRatio );
  24. renderer.setSize( window.innerWidth, window.innerHeight );
  25. document.body.appendChild( renderer.domElement );
  26. //
  27. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  28. camera.position.z = 400;
  29. scene = new THREE.Scene();
  30. scene.fog = new THREE.Fog( 0x000000, 1, 1000 );
  31. object = new THREE.Object3D();
  32. scene.add( object );
  33. var geometry = new THREE.SphereBufferGeometry( 1, 4, 4 );
  34. var material = new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true } );
  35. for ( var i = 0; i < 100; i ++ ) {
  36. var mesh = new THREE.Mesh( geometry, material );
  37. mesh.position.set( Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 ).normalize();
  38. mesh.position.multiplyScalar( Math.random() * 400 );
  39. mesh.rotation.set( Math.random() * 2, Math.random() * 2, Math.random() * 2 );
  40. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 50;
  41. object.add( mesh );
  42. }
  43. scene.add( new THREE.AmbientLight( 0x222222 ) );
  44. light = new THREE.DirectionalLight( 0xffffff );
  45. light.position.set( 1, 1, 1 );
  46. scene.add( light );
  47. // postprocessing
  48. composer = new EffectComposer( renderer );
  49. composer.addPass( new RenderPass( scene, camera ) );
  50. var effect = new ShaderPass( DotScreenShader );
  51. effect.uniforms[ 'scale' ].value = 4;
  52. composer.addPass( effect );
  53. var effect = new ShaderPass( RGBShiftShader );
  54. effect.uniforms[ 'amount' ].value = 0.0015;
  55. composer.addPass( effect );
  56. //
  57. window.addEventListener( 'resize', onWindowResize, false );
  58. }
  59. function onWindowResize() {
  60. camera.aspect = window.innerWidth / window.innerHeight;
  61. camera.updateProjectionMatrix();
  62. renderer.setSize( window.innerWidth, window.innerHeight );
  63. composer.setSize( window.innerWidth, window.innerHeight );
  64. }
  65. function animate() {
  66. requestAnimationFrame( animate );
  67. object.rotation.x += 0.005;
  68. object.rotation.y += 0.01;
  69. composer.render();
  70. }
  71. </script>
  72. </body>
  73. </html>