2
0

webgl_postprocessing_pixel.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - postprocessing - pixel shader</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="container"></div>
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - pixel shader by <a href="http://wongbryan.github.io">wongbryan</a>
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import { GUI } from './jsm/libs/dat.gui.module.js';
  17. import { TrackballControls } from './jsm/controls/TrackballControls.js';
  18. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  19. import { RenderPass } from './jsm/postprocessing/RenderPass.js';
  20. import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
  21. import { PixelShader } from './jsm/shaders/PixelShader.js';
  22. let camera, scene, renderer, gui, composer, controls;
  23. let pixelPass, params;
  24. let group;
  25. init();
  26. animate();
  27. function updateGUI() {
  28. pixelPass.uniforms[ "pixelSize" ].value = params.pixelSize;
  29. }
  30. function init() {
  31. const container = document.getElementById( 'container' );
  32. renderer = new THREE.WebGLRenderer( { antialias: true } );
  33. renderer.setPixelRatio( window.devicePixelRatio );
  34. renderer.setSize( window.innerWidth, window.innerHeight );
  35. container.appendChild( renderer.domElement );
  36. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
  37. camera.position.set( 0, 0, 30 );
  38. controls = new TrackballControls( camera, renderer.domElement );
  39. controls.rotateSpeed = 2.0;
  40. controls.panSpeed = 0.8;
  41. controls.zoomSpeed = 1.5;
  42. scene = new THREE.Scene();
  43. const hemisphereLight = new THREE.HemisphereLight( 0xfceafc, 0x000000, .8 );
  44. scene.add( hemisphereLight );
  45. const dirLight = new THREE.DirectionalLight( 0xffffff, .5 );
  46. dirLight.position.set( 150, 75, 150 );
  47. scene.add( dirLight );
  48. const dirLight2 = new THREE.DirectionalLight( 0xffffff, .2 );
  49. dirLight2.position.set( - 150, 75, - 150 );
  50. scene.add( dirLight2 );
  51. const dirLight3 = new THREE.DirectionalLight( 0xffffff, .1 );
  52. dirLight3.position.set( 0, 125, 0 );
  53. scene.add( dirLight3 );
  54. const geometries = [
  55. new THREE.SphereGeometry( 1, 64, 64 ),
  56. new THREE.BoxGeometry( 1, 1, 1 ),
  57. new THREE.ConeGeometry( 1, 1, 32 ),
  58. new THREE.TetrahedronGeometry( 1 ),
  59. new THREE.TorusKnotGeometry( 1, .4 )
  60. ];
  61. group = new THREE.Group();
  62. for ( let i = 0; i < 25; i ++ ) {
  63. const geom = geometries[ Math.floor( Math.random() * geometries.length ) ];
  64. const color = new THREE.Color();
  65. color.setHSL( Math.random(), .7 + .2 * Math.random(), .5 + .1 * Math.random() );
  66. const mat = new THREE.MeshPhongMaterial( { color: color, shininess: 200 } );
  67. const mesh = new THREE.Mesh( geom, mat );
  68. const s = 4 + Math.random() * 10;
  69. mesh.scale.set( s, s, s );
  70. mesh.position.set( Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 ).normalize();
  71. mesh.position.multiplyScalar( Math.random() * 200 );
  72. mesh.rotation.set( Math.random() * 2, Math.random() * 2, Math.random() * 2 );
  73. group.add( mesh );
  74. }
  75. scene.add( group );
  76. composer = new EffectComposer( renderer );
  77. composer.addPass( new RenderPass( scene, camera ) );
  78. pixelPass = new ShaderPass( PixelShader );
  79. pixelPass.uniforms[ "resolution" ].value = new THREE.Vector2( window.innerWidth, window.innerHeight );
  80. pixelPass.uniforms[ "resolution" ].value.multiplyScalar( window.devicePixelRatio );
  81. composer.addPass( pixelPass );
  82. window.addEventListener( 'resize', onWindowResize );
  83. params = {
  84. pixelSize: 16,
  85. postprocessing: true
  86. };
  87. gui = new GUI();
  88. gui.add( params, 'pixelSize' ).min( 2 ).max( 32 ).step( 2 );
  89. gui.add( params, 'postprocessing' );
  90. }
  91. function onWindowResize() {
  92. camera.aspect = window.innerWidth / window.innerHeight;
  93. camera.updateProjectionMatrix();
  94. renderer.setSize( window.innerWidth, window.innerHeight );
  95. pixelPass.uniforms[ "resolution" ].value.set( window.innerWidth, window.innerHeight ).multiplyScalar( window.devicePixelRatio );
  96. }
  97. function update() {
  98. controls.update();
  99. updateGUI();
  100. group.rotation.y += .0015;
  101. group.rotation.z += .001;
  102. }
  103. function animate() {
  104. update();
  105. if ( params.postprocessing ) {
  106. composer.render();
  107. } else {
  108. renderer.render( scene, camera );
  109. }
  110. window.requestAnimationFrame( animate );
  111. }
  112. </script>
  113. </body>
  114. </html>