123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - postprocessing backgrounds</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Backgrounds: ClearPass, TexturePass and CubeTexturePass<br/>
- by <a href="https://clara.io" target="_blank" rel="noopener">Ben Houston</a>
- </div>
- <div id="container"></div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- import { GUI } from './jsm/libs/dat.gui.module.js';
- import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
- import { RenderPass } from './jsm/postprocessing/RenderPass.js';
- import { TexturePass } from './jsm/postprocessing/TexturePass.js';
- import { CubeTexturePass } from './jsm/postprocessing/CubeTexturePass.js';
- import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
- import { ClearPass } from './jsm/postprocessing/ClearPass.js';
- import { CopyShader } from './jsm/shaders/CopyShader.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- var scene, renderer, composer;
- var clearPass, texturePass, renderPass;
- var cameraP, cubeTexturePassP;
- var gui, stats;
- var params = {
- clearPass: true,
- clearColor: 'white',
- clearAlpha: 1.0,
- texturePass: true,
- texturePassOpacity: 1.0,
- cubeTexturePass: true,
- cubeTexturePassOpacity: 1.0,
- renderPass: true
- };
- init();
- animate();
- clearGui();
- function clearGui() {
- if ( gui ) gui.destroy();
- gui = new GUI();
- gui.add( params, "clearPass" );
- gui.add( params, "clearColor", [ 'black', 'white', 'blue', 'green', 'red' ] );
- gui.add( params, "clearAlpha", 0, 1 );
- gui.add( params, "texturePass" );
- gui.add( params, "texturePassOpacity", 0, 1 );
- gui.add( params, "cubeTexturePass" );
- gui.add( params, "cubeTexturePassOpacity", 0, 1 );
- gui.add( params, "renderPass" );
- gui.open();
- }
- function init() {
- var container = document.getElementById( "container" );
- var width = window.innerWidth || 1;
- var height = window.innerHeight || 1;
- var aspect = width / height;
- var devicePixelRatio = window.devicePixelRatio || 1;
- renderer = new THREE.WebGLRenderer();
- renderer.setPixelRatio( devicePixelRatio );
- renderer.setSize( width, height );
- document.body.appendChild( renderer.domElement );
- stats = new Stats();
- container.appendChild( stats.dom );
- //
- cameraP = new THREE.PerspectiveCamera( 65, aspect, 1, 10 );
- cameraP.position.z = 7;
- scene = new THREE.Scene();
- var group = new THREE.Group();
- scene.add( group );
- var light = new THREE.PointLight( 0xddffdd, 1.0 );
- light.position.z = 70;
- light.position.y = - 70;
- light.position.x = - 70;
- scene.add( light );
- var light2 = new THREE.PointLight( 0xffdddd, 1.0 );
- light2.position.z = 70;
- light2.position.x = - 70;
- light2.position.y = 70;
- scene.add( light2 );
- var light3 = new THREE.PointLight( 0xddddff, 1.0 );
- light3.position.z = 70;
- light3.position.x = 70;
- light3.position.y = - 70;
- scene.add( light3 );
- var geometry = new THREE.SphereBufferGeometry( 1, 48, 24 );
- var material = new THREE.MeshStandardMaterial();
- material.roughness = 0.5 * Math.random() + 0.25;
- material.metalness = 0;
- material.color.setHSL( Math.random(), 1.0, 0.3 );
- var mesh = new THREE.Mesh( geometry, material );
- group.add( mesh );
- // postprocessing
- var genCubeUrls = function ( prefix, postfix ) {
- return [
- prefix + 'px' + postfix, prefix + 'nx' + postfix,
- prefix + 'py' + postfix, prefix + 'ny' + postfix,
- prefix + 'pz' + postfix, prefix + 'nz' + postfix
- ];
- };
- composer = new EffectComposer( renderer );
- clearPass = new ClearPass( params.clearColor, params.clearAlpha );
- composer.addPass( clearPass );
- texturePass = new TexturePass();
- composer.addPass( texturePass );
- var textureLoader = new THREE.TextureLoader();
- textureLoader.load( "textures/hardwood2_diffuse.jpg", function ( map ) {
- texturePass.map = map;
- } );
- cubeTexturePassP = null;
- var ldrUrls = genCubeUrls( "textures/cube/pisa/", ".png" );
- new THREE.CubeTextureLoader().load( ldrUrls, function ( ldrCubeMap ) {
- cubeTexturePassP = new CubeTexturePass( cameraP, ldrCubeMap );
- composer.insertPass( cubeTexturePassP, 2 );
- } );
- renderPass = new RenderPass( scene, cameraP );
- renderPass.clear = false;
- composer.addPass( renderPass );
- var copyPass = new ShaderPass( CopyShader );
- composer.addPass( copyPass );
- var controls = new OrbitControls( cameraP, renderer.domElement );
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- var width = window.innerWidth;
- var height = window.innerHeight;
- var aspect = width / height;
- cameraP.aspect = aspect;
- cameraP.updateProjectionMatrix();
- renderer.setSize( width, height );
- composer.setSize( width, height );
- }
- function animate() {
- requestAnimationFrame( animate );
- stats.begin();
- cameraP.updateMatrixWorld( true );
- var newColor = clearPass.clearColor;
- switch ( params.clearColor ) {
- case 'blue': newColor = 0x0000ff; break;
- case 'red': newColor = 0xff0000; break;
- case 'green': newColor = 0x00ff00; break;
- case 'white': newColor = 0xffffff; break;
- case 'black': newColor = 0x000000; break;
- }
- clearPass.enabled = params.clearPass;
- clearPass.clearColor = newColor;
- clearPass.clearAlpha = params.clearAlpha;
- texturePass.enabled = params.texturePass;
- texturePass.opacity = params.texturePassOpacity;
- if ( cubeTexturePassP != null ) {
- cubeTexturePassP.enabled = params.cubeTexturePass;
- cubeTexturePassP.opacity = params.cubeTexturePassOpacity;
- }
- renderPass.enabled = params.renderPass;
- composer.render();
- stats.end();
- }
- </script>
- </body>
- </html>
|