2
0

webgl_postprocessing_pixel.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. <style>
  8. body {
  9. background-color: #000000;
  10. margin: 0px;
  11. overflow: hidden;
  12. font-family:Monospace;
  13. font-size:13px;
  14. /*text-align:center;*/
  15. font-weight: lighter;
  16. }
  17. #info {
  18. color: #fff;
  19. position: absolute;
  20. top: 10px;
  21. width: 100%;
  22. text-align: center;
  23. display: block;
  24. }
  25. #info a {
  26. color: #f88;
  27. font-weight: bold;
  28. text-decoration: underline;
  29. cursor: pointer;
  30. }
  31. .dg.ac {
  32. z-index: 1 !important;
  33. }
  34. </style>
  35. </head>
  36. <body>
  37. <script src="../build/three.js"></script>
  38. <script src="js/shaders/CopyShader.js"></script>
  39. <script src="js/shaders/PixelShader.js"></script>
  40. <script src="js/postprocessing/EffectComposer.js"></script>
  41. <script src="js/postprocessing/RenderPass.js"></script>
  42. <script src="js/postprocessing/ShaderPass.js"></script>
  43. <script src="js/controls/TrackballControls.js"></script>
  44. <script src='js/libs/dat.gui.min.js'></script>
  45. <div id="container"></div>
  46. <div id="info">
  47. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - pixel shader by <a href="http://wongbryan.github.io">wongbryan</a>
  48. </div>
  49. <script>
  50. var camera, scene, renderer, gui, composer, controls;
  51. var pixelPass, params;
  52. var group;
  53. init();
  54. animate();
  55. function updateGUI() {
  56. pixelPass.uniforms.pixelSize.value = params.pixelSize;
  57. }
  58. function resize() {
  59. camera.aspect = window.innerWidth / window.innerHeight;
  60. camera.updateProjectionMatrix();
  61. renderer.setSize( window.innerWidth, window.innerHeight );
  62. pixelPass.uniforms.resolution.value.set( window.innerWidth, window.innerHeight ).multiplyScalar( window.devicePixelRatio );
  63. }
  64. function init() {
  65. var container = document.getElementById( 'container' );
  66. renderer = new THREE.WebGLRenderer( { antialias: true } );
  67. renderer.setPixelRatio( window.devicePixelRatio );
  68. renderer.setSize( window.innerWidth, window.innerHeight );
  69. container.appendChild( renderer.domElement );
  70. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
  71. camera.position.set( 0, 0, 30 );
  72. controls = new THREE.TrackballControls( camera, renderer.domElement );
  73. controls.rotateSpeed = 2.0;
  74. controls.panSpeed = 0.8;
  75. controls.zoomSpeed = 1.5;
  76. scene = new THREE.Scene();
  77. var hemisphereLight = new THREE.HemisphereLight( 0xfceafc, 0x000000, .8 );
  78. scene.add( hemisphereLight );
  79. var dirLight = new THREE.DirectionalLight( 0xffffff, .5 );
  80. dirLight.position.set( 150, 75, 150 );
  81. scene.add( dirLight );
  82. var dirLight2 = new THREE.DirectionalLight( 0xffffff, .2 );
  83. dirLight2.position.set( - 150, 75, - 150 );
  84. scene.add( dirLight2 );
  85. var dirLight3 = new THREE.DirectionalLight( 0xffffff, .1 );
  86. dirLight3.position.set( 0, 125, 0 );
  87. scene.add( dirLight3 );
  88. var geometries = [
  89. new THREE.SphereBufferGeometry( 1, 64, 64 ),
  90. new THREE.BoxBufferGeometry( 1, 1, 1 ),
  91. new THREE.ConeBufferGeometry( 1, 1, 32 ),
  92. new THREE.TetrahedronBufferGeometry( 1 ),
  93. new THREE.TorusKnotBufferGeometry( 1, .4 )
  94. ];
  95. group = new THREE.Group();
  96. for ( var i = 0; i < 25; i ++ ) {
  97. var geom = geometries[ Math.floor( Math.random() * geometries.length ) ];
  98. var color = new THREE.Color();
  99. color.setHSL( Math.random(), .7 + .2 * Math.random(), .5 + .1 * Math.random() );
  100. var mat = new THREE.MeshPhongMaterial( { color: color, shininess: 200 } );
  101. var mesh = new THREE.Mesh( geom, mat );
  102. var s = 4 + Math.random() * 10;
  103. mesh.scale.set( s, s, s );
  104. mesh.position.set( Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 ).normalize();
  105. mesh.position.multiplyScalar( Math.random() * 200 );
  106. mesh.rotation.set( Math.random() * 2, Math.random() * 2, Math.random() * 2 );
  107. group.add( mesh );
  108. }
  109. scene.add( group );
  110. composer = new THREE.EffectComposer( renderer );
  111. composer.addPass( new THREE.RenderPass( scene, camera ) );
  112. pixelPass = new THREE.ShaderPass( THREE.PixelShader );
  113. pixelPass.uniforms.resolution.value = new THREE.Vector2( window.innerWidth, window.innerHeight );
  114. pixelPass.uniforms.resolution.value.multiplyScalar( window.devicePixelRatio );
  115. pixelPass.renderToScreen = true;
  116. composer.addPass( pixelPass );
  117. window.addEventListener( 'resize', resize );
  118. params = {
  119. pixelSize: 16,
  120. postprocessing: true
  121. };
  122. gui = new dat.GUI();
  123. gui.add( params, 'pixelSize' ).min( 2 ).max( 32 ).step( 2 );
  124. gui.add( params, 'postprocessing' );
  125. }
  126. function update() {
  127. controls.update();
  128. updateGUI();
  129. group.rotation.y += .0015;
  130. group.rotation.z += .001;
  131. }
  132. function animate() {
  133. update();
  134. if ( params.postprocessing ) {
  135. composer.render();
  136. } else {
  137. renderer.render( scene, camera );
  138. }
  139. window.requestAnimationFrame( animate );
  140. }
  141. </script>
  142. </body>
  143. </html>