123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - postprocessing</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> - webgl postprocessing example<br/>
- <a href="http://g3d.cs.williams.edu/g3d/data10/index.html#mesh19" target="_blank" rel="noopener">Lee Perry-Smith</a> head
- </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 { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
- import { RenderPass } from './jsm/postprocessing/RenderPass.js';
- import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
- import { BloomPass } from './jsm/postprocessing/BloomPass.js';
- import { FilmPass } from './jsm/postprocessing/FilmPass.js';
- import { DotScreenPass } from './jsm/postprocessing/DotScreenPass.js';
- import { MaskPass, ClearMaskPass } from './jsm/postprocessing/MaskPass.js';
- import { TexturePass } from './jsm/postprocessing/TexturePass.js';
- import { BleachBypassShader } from './jsm/shaders/BleachBypassShader.js';
- import { ColorifyShader } from './jsm/shaders/ColorifyShader.js';
- import { HorizontalBlurShader } from './jsm/shaders/HorizontalBlurShader.js';
- import { VerticalBlurShader } from './jsm/shaders/VerticalBlurShader.js';
- import { SepiaShader } from './jsm/shaders/SepiaShader.js';
- import { VignetteShader } from './jsm/shaders/VignetteShader.js';
- import { GammaCorrectionShader } from './jsm/shaders/GammaCorrectionShader.js';
- import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
- var container, stats;
- var composerScene, composer1, composer2, composer3, composer4;
- var cameraOrtho, cameraPerspective, sceneModel, sceneBG, renderer, mesh, directionalLight;
- var width = window.innerWidth || 2;
- var height = window.innerHeight || 2;
- var halfWidth = width / 2;
- var halfHeight = height / 2;
- var quadBG, quadMask, renderScene;
- var delta = 0.01;
- init();
- animate();
- function init() {
- container = document.getElementById( 'container' );
- //
- cameraOrtho = new THREE.OrthographicCamera( - halfWidth, halfWidth, halfHeight, - halfHeight, - 10000, 10000 );
- cameraOrtho.position.z = 100;
- cameraPerspective = new THREE.PerspectiveCamera( 50, width / height, 1, 10000 );
- cameraPerspective.position.z = 900;
- //
- sceneModel = new THREE.Scene();
- sceneBG = new THREE.Scene();
- //
- directionalLight = new THREE.DirectionalLight( 0xffffff );
- directionalLight.position.set( 0, - 0.1, 1 ).normalize();
- sceneModel.add( directionalLight );
- var loader = new GLTFLoader();
- loader.load( "models/gltf/LeePerrySmith/LeePerrySmith.glb", function ( gltf ) {
- createMesh( gltf.scene.children[ 0 ].geometry, sceneModel, 100 );
- } );
- //
- var diffuseMap = new THREE.TextureLoader().load( "textures/cube/SwedishRoyalCastle/pz.jpg" );
- diffuseMap.encoding = THREE.sRGBEncoding;
- var materialColor = new THREE.MeshBasicMaterial( {
- map: diffuseMap,
- depthTest: false
- } );
- quadBG = new THREE.Mesh( new THREE.PlaneBufferGeometry( 1, 1 ), materialColor );
- quadBG.position.z = - 500;
- quadBG.scale.set( width, height, 1 );
- sceneBG.add( quadBG );
- //
- var sceneMask = new THREE.Scene();
- quadMask = new THREE.Mesh( new THREE.PlaneBufferGeometry( 1, 1 ), new THREE.MeshBasicMaterial( { color: 0xffaa00 } ) );
- quadMask.position.z = - 300;
- quadMask.scale.set( width / 2, height / 2, 1 );
- sceneMask.add( quadMask );
- //
- renderer = new THREE.WebGLRenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( width, height );
- renderer.autoClear = false;
- //
- container.appendChild( renderer.domElement );
- //
- stats = new Stats();
- container.appendChild( stats.dom );
- //
- var shaderBleach = BleachBypassShader;
- var shaderSepia = SepiaShader;
- var shaderVignette = VignetteShader;
- // var shaderCopy = CopyShader;
- var effectBleach = new ShaderPass( shaderBleach );
- var effectSepia = new ShaderPass( shaderSepia );
- var effectVignette = new ShaderPass( shaderVignette );
- var gammaCorrection = new ShaderPass( GammaCorrectionShader );
- // var effectCopy = new ShaderPass( shaderCopy );
- effectBleach.uniforms[ "opacity" ].value = 0.95;
- effectSepia.uniforms[ "amount" ].value = 0.9;
- effectVignette.uniforms[ "offset" ].value = 0.95;
- effectVignette.uniforms[ "darkness" ].value = 1.6;
- var effectBloom = new BloomPass( 0.5 );
- var effectFilm = new FilmPass( 0.35, 0.025, 648, false );
- var effectFilmBW = new FilmPass( 0.35, 0.5, 2048, true );
- var effectDotScreen = new DotScreenPass( new THREE.Vector2( 0, 0 ), 0.5, 0.8 );
- var effectHBlur = new ShaderPass( HorizontalBlurShader );
- var effectVBlur = new ShaderPass( VerticalBlurShader );
- effectHBlur.uniforms[ 'h' ].value = 2 / ( width / 2 );
- effectVBlur.uniforms[ 'v' ].value = 2 / ( height / 2 );
- var effectColorify1 = new ShaderPass( ColorifyShader );
- var effectColorify2 = new ShaderPass( ColorifyShader );
- effectColorify1.uniforms[ 'color' ] = new THREE.Uniform( new THREE.Color( 1, 0.8, 0.8 ) );
- effectColorify2.uniforms[ 'color' ] = new THREE.Uniform( new THREE.Color( 1, 0.75, 0.5 ) );
- var clearMask = new ClearMaskPass();
- var renderMask = new MaskPass( sceneModel, cameraPerspective );
- var renderMaskInverse = new MaskPass( sceneModel, cameraPerspective );
- renderMaskInverse.inverse = true;
- //
- var rtParameters = {
- minFilter: THREE.LinearFilter,
- magFilter: THREE.LinearFilter,
- format: THREE.RGBFormat,
- stencilBuffer: true
- };
- var rtWidth = width / 2;
- var rtHeight = height / 2;
- //
- var renderBackground = new RenderPass( sceneBG, cameraOrtho );
- var renderModel = new RenderPass( sceneModel, cameraPerspective );
- renderModel.clear = false;
- composerScene = new EffectComposer( renderer, new THREE.WebGLRenderTarget( rtWidth * 2, rtHeight * 2, rtParameters ) );
- composerScene.addPass( renderBackground );
- composerScene.addPass( renderModel );
- composerScene.addPass( renderMaskInverse );
- composerScene.addPass( effectHBlur );
- composerScene.addPass( effectVBlur );
- composerScene.addPass( clearMask );
- //
- renderScene = new TexturePass( composerScene.renderTarget2.texture );
- //
- composer1 = new EffectComposer( renderer, new THREE.WebGLRenderTarget( rtWidth, rtHeight, rtParameters ) );
- composer1.addPass( renderScene );
- composer1.addPass( gammaCorrection );
- //composer1.addPass( renderMask );
- composer1.addPass( effectFilmBW );
- //composer1.addPass( clearMask );
- composer1.addPass( effectVignette );
- //
- composer2 = new EffectComposer( renderer, new THREE.WebGLRenderTarget( rtWidth, rtHeight, rtParameters ) );
- composer2.addPass( renderScene );
- composer2.addPass( gammaCorrection );
- composer2.addPass( effectDotScreen );
- composer2.addPass( renderMask );
- composer2.addPass( effectColorify1 );
- composer2.addPass( clearMask );
- composer2.addPass( renderMaskInverse );
- composer2.addPass( effectColorify2 );
- composer2.addPass( clearMask );
- composer2.addPass( effectVignette );
- //
- composer3 = new EffectComposer( renderer, new THREE.WebGLRenderTarget( rtWidth, rtHeight, rtParameters ) );
- composer3.addPass( renderScene );
- composer3.addPass( gammaCorrection );
- //composer3.addPass( renderMask );
- composer3.addPass( effectSepia );
- composer3.addPass( effectFilm );
- //composer3.addPass( clearMask );
- composer3.addPass( effectVignette );
- //
- composer4 = new EffectComposer( renderer, new THREE.WebGLRenderTarget( rtWidth, rtHeight, rtParameters ) );
- composer4.addPass( renderScene );
- composer4.addPass( gammaCorrection );
- //composer4.addPass( renderMask );
- composer4.addPass( effectBloom );
- composer4.addPass( effectFilm );
- composer4.addPass( effectBleach );
- //composer4.addPass( clearMask );
- composer4.addPass( effectVignette );
- renderScene.uniforms[ "tDiffuse" ].value = composerScene.renderTarget2.texture;
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- halfWidth = window.innerWidth / 2;
- halfHeight = window.innerHeight / 2;
- cameraPerspective.aspect = window.innerWidth / window.innerHeight;
- cameraPerspective.updateProjectionMatrix();
- cameraOrtho.left = - halfWidth;
- cameraOrtho.right = halfWidth;
- cameraOrtho.top = halfHeight;
- cameraOrtho.bottom = - halfHeight;
- cameraOrtho.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- composerScene.setSize( halfWidth * 2, halfHeight * 2 );
- composer1.setSize( halfWidth, halfHeight );
- composer2.setSize( halfWidth, halfHeight );
- composer3.setSize( halfWidth, halfHeight );
- composer4.setSize( halfWidth, halfHeight );
- renderScene.uniforms[ "tDiffuse" ].value = composerScene.renderTarget2.texture;
- quadBG.scale.set( window.innerWidth, window.innerHeight, 1 );
- quadMask.scale.set( window.innerWidth / 2, window.innerHeight / 2, 1 );
- }
- function createMesh( geometry, scene, scale ) {
- var diffuseMap = new THREE.TextureLoader().load( "models/gltf/LeePerrySmith/Map-COL.jpg" );
- diffuseMap.encoding = THREE.sRGBEncoding;
- var mat2 = new THREE.MeshPhongMaterial( {
- color: 0x999999,
- specular: 0x080808,
- shininess: 20,
- map: diffuseMap,
- normalMap: new THREE.TextureLoader().load( "models/gltf/LeePerrySmith/Infinite-Level_02_Tangent_SmoothUV.jpg" ),
- normalScale: new THREE.Vector2( 0.75, 0.75 )
- } );
- mesh = new THREE.Mesh( geometry, mat2 );
- mesh.position.set( 0, - 50, 0 );
- mesh.scale.set( scale, scale, scale );
- scene.add( mesh );
- }
- //
- function animate() {
- requestAnimationFrame( animate );
- stats.begin();
- render();
- stats.end();
- }
- function render() {
- var time = Date.now() * 0.0004;
- if ( mesh ) mesh.rotation.y = - time;
- renderer.setViewport( 0, 0, halfWidth, halfHeight );
- composerScene.render( delta );
- renderer.setViewport( 0, 0, halfWidth, halfHeight );
- composer1.render( delta );
- renderer.setViewport( halfWidth, 0, halfWidth, halfHeight );
- composer2.render( delta );
- renderer.setViewport( 0, halfHeight, halfWidth, halfHeight );
- composer3.render( delta );
- renderer.setViewport( halfWidth, halfHeight, halfWidth, halfHeight );
- composer4.render( delta );
- }
- </script>
- </body>
- </html>
|