1
0

webgl_postprocessing_backgrounds.html 6.4 KB

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