webgl_postprocessing_pixel.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. var camera, scene, renderer, gui, composer, controls;
  23. var pixelPass, params;
  24. var group;
  25. init();
  26. animate();
  27. function updateGUI() {
  28. pixelPass.uniforms[ "pixelSize" ].value = params.pixelSize;
  29. }
  30. function resize() {
  31. camera.aspect = window.innerWidth / window.innerHeight;
  32. camera.updateProjectionMatrix();
  33. renderer.setSize( window.innerWidth, window.innerHeight );
  34. pixelPass.uniforms[ "resolution" ].value.set( window.innerWidth, window.innerHeight ).multiplyScalar( window.devicePixelRatio );
  35. }
  36. function init() {
  37. var container = document.getElementById( 'container' );
  38. renderer = new THREE.WebGLRenderer( { antialias: true } );
  39. renderer.setPixelRatio( window.devicePixelRatio );
  40. renderer.setSize( window.innerWidth, window.innerHeight );
  41. container.appendChild( renderer.domElement );
  42. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
  43. camera.position.set( 0, 0, 30 );
  44. controls = new TrackballControls( camera, renderer.domElement );
  45. controls.rotateSpeed = 2.0;
  46. controls.panSpeed = 0.8;
  47. controls.zoomSpeed = 1.5;
  48. scene = new THREE.Scene();
  49. var hemisphereLight = new THREE.HemisphereLight( 0xfceafc, 0x000000, .8 );
  50. scene.add( hemisphereLight );
  51. var dirLight = new THREE.DirectionalLight( 0xffffff, .5 );
  52. dirLight.position.set( 150, 75, 150 );
  53. scene.add( dirLight );
  54. var dirLight2 = new THREE.DirectionalLight( 0xffffff, .2 );
  55. dirLight2.position.set( - 150, 75, - 150 );
  56. scene.add( dirLight2 );
  57. var dirLight3 = new THREE.DirectionalLight( 0xffffff, .1 );
  58. dirLight3.position.set( 0, 125, 0 );
  59. scene.add( dirLight3 );
  60. var geometries = [
  61. new THREE.SphereBufferGeometry( 1, 64, 64 ),
  62. new THREE.BoxBufferGeometry( 1, 1, 1 ),
  63. new THREE.ConeBufferGeometry( 1, 1, 32 ),
  64. new THREE.TetrahedronBufferGeometry( 1 ),
  65. new THREE.TorusKnotBufferGeometry( 1, .4 )
  66. ];
  67. group = new THREE.Group();
  68. for ( var i = 0; i < 25; i ++ ) {
  69. var geom = geometries[ Math.floor( Math.random() * geometries.length ) ];
  70. var color = new THREE.Color();
  71. color.setHSL( Math.random(), .7 + .2 * Math.random(), .5 + .1 * Math.random() );
  72. var mat = new THREE.MeshPhongMaterial( { color: color, shininess: 200 } );
  73. var mesh = new THREE.Mesh( geom, mat );
  74. var s = 4 + Math.random() * 10;
  75. mesh.scale.set( s, s, s );
  76. mesh.position.set( Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 ).normalize();
  77. mesh.position.multiplyScalar( Math.random() * 200 );
  78. mesh.rotation.set( Math.random() * 2, Math.random() * 2, Math.random() * 2 );
  79. group.add( mesh );
  80. }
  81. scene.add( group );
  82. composer = new EffectComposer( renderer );
  83. composer.addPass( new RenderPass( scene, camera ) );
  84. pixelPass = new ShaderPass( PixelShader );
  85. pixelPass.uniforms[ "resolution" ].value = new THREE.Vector2( window.innerWidth, window.innerHeight );
  86. pixelPass.uniforms[ "resolution" ].value.multiplyScalar( window.devicePixelRatio );
  87. composer.addPass( pixelPass );
  88. window.addEventListener( 'resize', resize );
  89. params = {
  90. pixelSize: 16,
  91. postprocessing: true
  92. };
  93. gui = new GUI();
  94. gui.add( params, 'pixelSize' ).min( 2 ).max( 32 ).step( 2 );
  95. gui.add( params, 'postprocessing' );
  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>