webgl_postprocessing.html 3.2 KB

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