webgl_postprocessing_pixel.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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="http://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 {
  16. BoxBufferGeometry,
  17. Color,
  18. ConeBufferGeometry,
  19. DirectionalLight,
  20. Group,
  21. HemisphereLight,
  22. Mesh,
  23. MeshPhongMaterial,
  24. PerspectiveCamera,
  25. Scene,
  26. SphereBufferGeometry,
  27. TetrahedronBufferGeometry,
  28. TorusKnotBufferGeometry,
  29. Vector2,
  30. WebGLRenderer
  31. } from "../build/three.module.js";
  32. import { GUI } from './jsm/libs/dat.gui.module.js';
  33. import { TrackballControls } from './jsm/controls/TrackballControls.js';
  34. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  35. import { RenderPass } from './jsm/postprocessing/RenderPass.js';
  36. import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
  37. import { PixelShader } from './jsm/shaders/PixelShader.js';
  38. var camera, scene, renderer, gui, composer, controls;
  39. var pixelPass, params;
  40. var group;
  41. init();
  42. animate();
  43. function updateGUI() {
  44. pixelPass.uniforms[ "pixelSize" ].value = params.pixelSize;
  45. }
  46. function resize() {
  47. camera.aspect = window.innerWidth / window.innerHeight;
  48. camera.updateProjectionMatrix();
  49. renderer.setSize( window.innerWidth, window.innerHeight );
  50. pixelPass.uniforms[ "resolution" ].value.set( window.innerWidth, window.innerHeight ).multiplyScalar( window.devicePixelRatio );
  51. }
  52. function init() {
  53. var container = document.getElementById( 'container' );
  54. renderer = new WebGLRenderer( { antialias: true } );
  55. renderer.setPixelRatio( window.devicePixelRatio );
  56. renderer.setSize( window.innerWidth, window.innerHeight );
  57. container.appendChild( renderer.domElement );
  58. camera = new PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
  59. camera.position.set( 0, 0, 30 );
  60. controls = new TrackballControls( camera, renderer.domElement );
  61. controls.rotateSpeed = 2.0;
  62. controls.panSpeed = 0.8;
  63. controls.zoomSpeed = 1.5;
  64. scene = new Scene();
  65. var hemisphereLight = new HemisphereLight( 0xfceafc, 0x000000, .8 );
  66. scene.add( hemisphereLight );
  67. var dirLight = new DirectionalLight( 0xffffff, .5 );
  68. dirLight.position.set( 150, 75, 150 );
  69. scene.add( dirLight );
  70. var dirLight2 = new DirectionalLight( 0xffffff, .2 );
  71. dirLight2.position.set( - 150, 75, - 150 );
  72. scene.add( dirLight2 );
  73. var dirLight3 = new DirectionalLight( 0xffffff, .1 );
  74. dirLight3.position.set( 0, 125, 0 );
  75. scene.add( dirLight3 );
  76. var geometries = [
  77. new SphereBufferGeometry( 1, 64, 64 ),
  78. new BoxBufferGeometry( 1, 1, 1 ),
  79. new ConeBufferGeometry( 1, 1, 32 ),
  80. new TetrahedronBufferGeometry( 1 ),
  81. new TorusKnotBufferGeometry( 1, .4 )
  82. ];
  83. group = new Group();
  84. for ( var i = 0; i < 25; i ++ ) {
  85. var geom = geometries[ Math.floor( Math.random() * geometries.length ) ];
  86. var color = new Color();
  87. color.setHSL( Math.random(), .7 + .2 * Math.random(), .5 + .1 * Math.random() );
  88. var mat = new MeshPhongMaterial( { color: color, shininess: 200 } );
  89. var mesh = new Mesh( geom, mat );
  90. var s = 4 + Math.random() * 10;
  91. mesh.scale.set( s, s, s );
  92. mesh.position.set( Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 ).normalize();
  93. mesh.position.multiplyScalar( Math.random() * 200 );
  94. mesh.rotation.set( Math.random() * 2, Math.random() * 2, Math.random() * 2 );
  95. group.add( mesh );
  96. }
  97. scene.add( group );
  98. composer = new EffectComposer( renderer );
  99. composer.addPass( new RenderPass( scene, camera ) );
  100. pixelPass = new ShaderPass( PixelShader );
  101. pixelPass.uniforms[ "resolution" ].value = new Vector2( window.innerWidth, window.innerHeight );
  102. pixelPass.uniforms[ "resolution" ].value.multiplyScalar( window.devicePixelRatio );
  103. composer.addPass( pixelPass );
  104. window.addEventListener( 'resize', resize );
  105. params = {
  106. pixelSize: 16,
  107. postprocessing: true
  108. };
  109. gui = new GUI();
  110. gui.add( params, 'pixelSize' ).min( 2 ).max( 32 ).step( 2 );
  111. gui.add( params, 'postprocessing' );
  112. }
  113. function update() {
  114. controls.update();
  115. updateGUI();
  116. group.rotation.y += .0015;
  117. group.rotation.z += .001;
  118. }
  119. function animate() {
  120. update();
  121. if ( params.postprocessing ) {
  122. composer.render();
  123. } else {
  124. renderer.render( scene, camera );
  125. }
  126. window.requestAnimationFrame( animate );
  127. }
  128. </script>
  129. </body>
  130. </html>