webgl_postprocessing_backgrounds.html 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - postprocessing backgrounds</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. margin: 0px;
  10. background-color: #000;
  11. overflow: hidden;
  12. font-family:Monospace;
  13. font-size:13px;
  14. margin: 0px;
  15. text-align:center;
  16. overflow: hidden;
  17. }
  18. a { color: #88f; }
  19. #info {
  20. color: #fff;
  21. position: absolute;
  22. top: 10px;
  23. width: 100%;
  24. text-align: center;
  25. display:block;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <div id="info">
  31. <a href="http://threejs.org" target="_blank">three.js</a> - Backgrounds: ClearPass, TexturePass and CubeTexturePass by <a href="https://clara.io" target="_blank">Ben Houston</a></div>
  32. <div id="container"></div>
  33. <script src="../build/three.js"></script>
  34. <script src="js/libs/stats.min.js"></script>
  35. <script src="js/libs/dat.gui.min.js"></script>
  36. <script src="js/shaders/CopyShader.js"></script>
  37. <script src="js/postprocessing/EffectComposer.js"></script>
  38. <script src="js/postprocessing/ClearPass.js"></script>
  39. <script src="js/postprocessing/RenderPass.js"></script>
  40. <script src="js/postprocessing/TexturePass.js"></script>
  41. <script src="js/postprocessing/CubeTexturePass.js"></script>
  42. <script src="js/postprocessing/ShaderPass.js"></script>
  43. <script src="js/controls/OrbitControls.js"></script>
  44. <script>
  45. var scene, renderer, composer;
  46. var clearPass, texturePass, renderPass;
  47. var cameraP, cubeTexturePassP;
  48. //var cameraO, cubeTexturePassO;
  49. var gui, stats, texture;
  50. var params = {
  51. clearPass: true,
  52. clearColor: 'white',
  53. clearAlpha: 1.0,
  54. texturePass: true,
  55. texturePassOpacity: 1.0,
  56. cubeTexturePass: true,
  57. cubeTexturePassOpacity: 1.0,
  58. renderPass: true,
  59. //autoRotate: true,
  60. //camera: 'perspective'
  61. };
  62. init();
  63. animate();
  64. clearGui();
  65. function clearGui() {
  66. if ( gui ) gui.destroy();
  67. gui = new dat.GUI();
  68. gui.add( params, "clearPass" );
  69. gui.add( params, "clearColor", [ 'black', 'white', 'blue', 'green', 'red' ] );
  70. gui.add( params, "clearAlpha", 0, 1 );
  71. gui.add( params, "texturePass" );
  72. gui.add( params, "texturePassOpacity", 0, 1 );
  73. gui.add( params, "cubeTexturePass" );
  74. gui.add( params, "cubeTexturePassOpacity", 0, 1 );
  75. gui.add( params, "renderPass" );
  76. //gui.add( params, "autoRotate" );
  77. //gui.add( params, 'camera', [ 'perspective', 'orthographic' ] );
  78. gui.open();
  79. }
  80. function init() {
  81. container = document.getElementById( "container" );
  82. var width = window.innerWidth || 1;
  83. var height = window.innerHeight || 1;
  84. var aspect = width / height;
  85. var devicePixelRatio = window.devicePixelRatio || 1;
  86. renderer = new THREE.WebGLRenderer( { antialias: false } );
  87. renderer.setPixelRatio( devicePixelRatio );
  88. renderer.setSize( width, height );
  89. document.body.appendChild( renderer.domElement );
  90. stats = new Stats();
  91. container.appendChild( stats.dom );
  92. //
  93. cameraP = new THREE.PerspectiveCamera( 65, aspect, 1, 10 );
  94. cameraP.position.z = 7;
  95. // cameraO = new THREE.OrthographicCamera( width / - 2, width / 2, height / 2, height / - 2, 3, 10 );
  96. // cameraO.position.z = 7;
  97. // var fov = THREE.Math.degToRad( cameraP.fov );
  98. // var hyperfocus = ( cameraP.near + cameraP.far ) / 2;
  99. // var _height = 2 * Math.tan( fov / 2 ) * hyperfocus;
  100. // cameraO.zoom = height / _height;
  101. scene = new THREE.Scene();
  102. group = new THREE.Object3D();
  103. scene.add( group );
  104. var light = new THREE.PointLight( 0xddffdd, 1.0 );
  105. light.position.z = 70;
  106. light.position.y = -70;
  107. light.position.x = -70;
  108. scene.add( light );
  109. var light2 = new THREE.PointLight( 0xffdddd, 1.0 );
  110. light2.position.z = 70;
  111. light2.position.x = -70;
  112. light2.position.y = 70;
  113. scene.add( light2 );
  114. var light3 = new THREE.PointLight( 0xddddff, 1.0 );
  115. light3.position.z = 70;
  116. light3.position.x = 70;
  117. light3.position.y = -70;
  118. scene.add( light3 );
  119. var geometry = new THREE.SphereBufferGeometry( 1, 48, 24 );
  120. var material = new THREE.MeshStandardMaterial();
  121. material.roughness = 0.5 * Math.random() + 0.25;
  122. material.metalness = 0;
  123. material.color.setHSL( Math.random(), 1.0, 0.3 );
  124. var mesh = new THREE.Mesh( geometry, material );
  125. group.add( mesh );
  126. // postprocessing
  127. var genCubeUrls = function( prefix, postfix ) {
  128. return [
  129. prefix + 'px' + postfix, prefix + 'nx' + postfix,
  130. prefix + 'py' + postfix, prefix + 'ny' + postfix,
  131. prefix + 'pz' + postfix, prefix + 'nz' + postfix
  132. ];
  133. };
  134. composer = new THREE.EffectComposer( renderer );
  135. clearPass = new THREE.ClearPass( params.clearColor, params.clearAlpha );
  136. composer.addPass( clearPass );
  137. texturePass = new THREE.TexturePass();
  138. composer.addPass( texturePass );
  139. var textureLoader = new THREE.TextureLoader();
  140. textureLoader.load( "textures/hardwood2_diffuse.jpg", function( map ) {
  141. texturePass.map = map;
  142. });
  143. cubeTexturePassP = new THREE.CubeTexturePass( cameraP );
  144. composer.addPass( cubeTexturePassP );
  145. var ldrUrls = genCubeUrls( "textures/cube/pisa/", ".png" );
  146. new THREE.CubeTextureLoader().load( ldrUrls, function ( ldrCubeMap ) {
  147. cubeTexturePassP.envMap = ldrCubeMap;
  148. console.log( "loaded envmap");
  149. });
  150. renderPass = new THREE.RenderPass( scene, cameraP );
  151. renderPass.clear = false;
  152. composer.addPass( renderPass );
  153. copyPass = new THREE.ShaderPass( THREE.CopyShader );
  154. copyPass.renderToScreen = true;
  155. composer.addPass( copyPass );
  156. var controls = new THREE.OrbitControls( cameraP, renderer.domElement );
  157. controls.target.set( 0, 0, 0 );
  158. controls.update();
  159. window.addEventListener( 'resize', onWindowResize, false );
  160. }
  161. function onWindowResize() {
  162. var width = window.innerWidth;
  163. var height = window.innerHeight;
  164. var aspect = width / height;
  165. cameraP.aspect = aspect;
  166. cameraP.updateProjectionMatrix();
  167. /*cameraO.left = - height * aspect;
  168. cameraO.right = height * aspect;
  169. cameraO.top = height;
  170. cameraO.bottom = - height;
  171. cameraO.updateProjectionMatrix();*/
  172. renderer.setSize( width, height );
  173. var pixelRatio = renderer.getPixelRatio();
  174. var newWidth = Math.floor( width / pixelRatio ) || 1;
  175. var newHeight = Math.floor( height / pixelRatio ) || 1;
  176. composer.setSize( newWidth, newHeight );
  177. }
  178. function animate() {
  179. requestAnimationFrame( animate );
  180. stats.begin();
  181. cameraP.updateMatrixWorld( true );
  182. var newColor = clearPass.clearColor;
  183. switch( params.clearColor ) {
  184. case 'blue': newColor = 0x0000ff; break;
  185. case 'red': newColor = 0xff0000; break;
  186. case 'green': newColor = 0x00ff00; break;
  187. case 'white': newColor = 0xffffff; break;
  188. case 'black': newColor = 0x000000; break;
  189. }
  190. clearPass.enabled = params.clearPass;
  191. clearPass.clearColor = newColor;
  192. clearPass.clearAlpha = params.clearAlpha;
  193. texturePass.enabled = params.texturePass;
  194. texturePass.opacity = params.texturePassOpacity;
  195. cubeTexturePassP.enabled = params.cubeTexturePass;
  196. cubeTexturePassP.opacity = params.cubeTexturePassOpacity;
  197. renderPass.enabled = params.renderPass;
  198. composer.render();
  199. stats.end();
  200. }
  201. </script>
  202. <div>
  203. </body>
  204. </html>