webgl_postprocessing_backgrounds.html 6.6 KB

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