webgl_postprocessing_pixel.html 5.0 KB

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