webgl_postprocessing_backgrounds.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Backgrounds: ClearPass, TexturePass and CubeTexturePass<br/>
  12. by <a href="https://clara.io" target="_blank" rel="noopener">Ben Houston</a>
  13. </div>
  14. <div id="container"></div>
  15. <script type="module">
  16. import * as THREE from '../build/three.module.js';
  17. import Stats from './jsm/libs/stats.module.js';
  18. import { GUI } from './jsm/libs/dat.gui.module.js';
  19. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  20. import { RenderPass } from './jsm/postprocessing/RenderPass.js';
  21. import { TexturePass } from './jsm/postprocessing/TexturePass.js';
  22. import { CubeTexturePass } from './jsm/postprocessing/CubeTexturePass.js';
  23. import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
  24. import { ClearPass } from './jsm/postprocessing/ClearPass.js';
  25. import { CopyShader } from './jsm/shaders/CopyShader.js';
  26. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  27. var scene, renderer, composer;
  28. var clearPass, texturePass, renderPass;
  29. var cameraP, cubeTexturePassP;
  30. var gui, stats;
  31. var params = {
  32. clearPass: true,
  33. clearColor: 'white',
  34. clearAlpha: 1.0,
  35. texturePass: true,
  36. texturePassOpacity: 1.0,
  37. cubeTexturePass: true,
  38. cubeTexturePassOpacity: 1.0,
  39. renderPass: true
  40. };
  41. init();
  42. animate();
  43. clearGui();
  44. function clearGui() {
  45. if ( gui ) gui.destroy();
  46. gui = new GUI();
  47. gui.add( params, "clearPass" );
  48. gui.add( params, "clearColor", [ 'black', 'white', 'blue', 'green', 'red' ] );
  49. gui.add( params, "clearAlpha", 0, 1 );
  50. gui.add( params, "texturePass" );
  51. gui.add( params, "texturePassOpacity", 0, 1 );
  52. gui.add( params, "cubeTexturePass" );
  53. gui.add( params, "cubeTexturePassOpacity", 0, 1 );
  54. gui.add( params, "renderPass" );
  55. gui.open();
  56. }
  57. function init() {
  58. var container = document.getElementById( "container" );
  59. var width = window.innerWidth || 1;
  60. var height = window.innerHeight || 1;
  61. var aspect = width / height;
  62. var devicePixelRatio = window.devicePixelRatio || 1;
  63. renderer = new THREE.WebGLRenderer();
  64. renderer.setPixelRatio( devicePixelRatio );
  65. renderer.setSize( width, height );
  66. document.body.appendChild( renderer.domElement );
  67. stats = new Stats();
  68. container.appendChild( stats.dom );
  69. //
  70. cameraP = new THREE.PerspectiveCamera( 65, aspect, 1, 10 );
  71. cameraP.position.z = 7;
  72. scene = new THREE.Scene();
  73. var group = new THREE.Group();
  74. scene.add( group );
  75. var light = new THREE.PointLight( 0xddffdd, 1.0 );
  76. light.position.z = 70;
  77. light.position.y = - 70;
  78. light.position.x = - 70;
  79. scene.add( light );
  80. var light2 = new THREE.PointLight( 0xffdddd, 1.0 );
  81. light2.position.z = 70;
  82. light2.position.x = - 70;
  83. light2.position.y = 70;
  84. scene.add( light2 );
  85. var light3 = new THREE.PointLight( 0xddddff, 1.0 );
  86. light3.position.z = 70;
  87. light3.position.x = 70;
  88. light3.position.y = - 70;
  89. scene.add( light3 );
  90. var geometry = new THREE.SphereBufferGeometry( 1, 48, 24 );
  91. var material = new THREE.MeshStandardMaterial();
  92. material.roughness = 0.5 * Math.random() + 0.25;
  93. material.metalness = 0;
  94. material.color.setHSL( Math.random(), 1.0, 0.3 );
  95. var mesh = new THREE.Mesh( geometry, material );
  96. group.add( mesh );
  97. // postprocessing
  98. var genCubeUrls = function ( prefix, postfix ) {
  99. return [
  100. prefix + 'px' + postfix, prefix + 'nx' + postfix,
  101. prefix + 'py' + postfix, prefix + 'ny' + postfix,
  102. prefix + 'pz' + postfix, prefix + 'nz' + postfix
  103. ];
  104. };
  105. composer = new EffectComposer( renderer );
  106. clearPass = new ClearPass( params.clearColor, params.clearAlpha );
  107. composer.addPass( clearPass );
  108. texturePass = new TexturePass();
  109. composer.addPass( texturePass );
  110. var textureLoader = new THREE.TextureLoader();
  111. textureLoader.load( "textures/hardwood2_diffuse.jpg", function ( map ) {
  112. texturePass.map = map;
  113. } );
  114. cubeTexturePassP = null;
  115. var ldrUrls = genCubeUrls( "textures/cube/pisa/", ".png" );
  116. new THREE.CubeTextureLoader().load( ldrUrls, function ( ldrCubeMap ) {
  117. cubeTexturePassP = new CubeTexturePass( cameraP, ldrCubeMap );
  118. composer.insertPass( cubeTexturePassP, 2 );
  119. } );
  120. renderPass = new RenderPass( scene, cameraP );
  121. renderPass.clear = false;
  122. composer.addPass( renderPass );
  123. var copyPass = new ShaderPass( CopyShader );
  124. composer.addPass( copyPass );
  125. var controls = new OrbitControls( cameraP, renderer.domElement );
  126. window.addEventListener( 'resize', onWindowResize, false );
  127. }
  128. function onWindowResize() {
  129. var width = window.innerWidth;
  130. var height = window.innerHeight;
  131. var aspect = width / height;
  132. cameraP.aspect = aspect;
  133. cameraP.updateProjectionMatrix();
  134. renderer.setSize( width, height );
  135. composer.setSize( width, height );
  136. }
  137. function animate() {
  138. requestAnimationFrame( animate );
  139. stats.begin();
  140. cameraP.updateMatrixWorld( true );
  141. var newColor = clearPass.clearColor;
  142. switch ( params.clearColor ) {
  143. case 'blue': newColor = 0x0000ff; break;
  144. case 'red': newColor = 0xff0000; break;
  145. case 'green': newColor = 0x00ff00; break;
  146. case 'white': newColor = 0xffffff; break;
  147. case 'black': newColor = 0x000000; break;
  148. }
  149. clearPass.enabled = params.clearPass;
  150. clearPass.clearColor = newColor;
  151. clearPass.clearAlpha = params.clearAlpha;
  152. texturePass.enabled = params.texturePass;
  153. texturePass.opacity = params.texturePassOpacity;
  154. if ( cubeTexturePassP != null ) {
  155. cubeTexturePassP.enabled = params.cubeTexturePass;
  156. cubeTexturePassP.opacity = params.cubeTexturePassOpacity;
  157. }
  158. renderPass.enabled = params.renderPass;
  159. composer.render();
  160. stats.end();
  161. }
  162. </script>
  163. </body>
  164. </html>