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