You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

357 lines
11 KiB

2 years ago
<!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="https://casual-effects.com/data/" target="_blank" rel="noopener">Lee Perry-Smith</a> head
</div>
<div id="container"></div>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import Stats from 'three/addons/libs/stats.module.js';
import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js';
import { BloomPass } from 'three/addons/postprocessing/BloomPass.js';
import { FilmPass } from 'three/addons/postprocessing/FilmPass.js';
import { DotScreenPass } from 'three/addons/postprocessing/DotScreenPass.js';
import { MaskPass, ClearMaskPass } from 'three/addons/postprocessing/MaskPass.js';
import { TexturePass } from 'three/addons/postprocessing/TexturePass.js';
import { BleachBypassShader } from 'three/addons/shaders/BleachBypassShader.js';
import { ColorifyShader } from 'three/addons/shaders/ColorifyShader.js';
import { HorizontalBlurShader } from 'three/addons/shaders/HorizontalBlurShader.js';
import { VerticalBlurShader } from 'three/addons/shaders/VerticalBlurShader.js';
import { SepiaShader } from 'three/addons/shaders/SepiaShader.js';
import { VignetteShader } from 'three/addons/shaders/VignetteShader.js';
import { GammaCorrectionShader } from 'three/addons/shaders/GammaCorrectionShader.js';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
let container, stats;
let composerScene, composer1, composer2, composer3, composer4;
let cameraOrtho, cameraPerspective, sceneModel, sceneBG, renderer, mesh, directionalLight;
const width = window.innerWidth || 2;
const height = window.innerHeight || 2;
let halfWidth = width / 2;
let halfHeight = height / 2;
let quadBG, quadMask, renderScene;
const 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 );
const loader = new GLTFLoader();
loader.load( 'models/gltf/LeePerrySmith/LeePerrySmith.glb', function ( gltf ) {
createMesh( gltf.scene.children[ 0 ].geometry, sceneModel, 100 );
} );
//
const diffuseMap = new THREE.TextureLoader().load( 'textures/cube/SwedishRoyalCastle/pz.jpg' );
diffuseMap.encoding = THREE.sRGBEncoding;
const materialColor = new THREE.MeshBasicMaterial( {
map: diffuseMap,
depthTest: false
} );
quadBG = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), materialColor );
quadBG.position.z = - 500;
quadBG.scale.set( width, height, 1 );
sceneBG.add( quadBG );
//
const sceneMask = new THREE.Scene();
quadMask = new THREE.Mesh( new THREE.PlaneGeometry( 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 );
//
const shaderBleach = BleachBypassShader;
const shaderSepia = SepiaShader;
const shaderVignette = VignetteShader;
const effectBleach = new ShaderPass( shaderBleach );
const effectSepia = new ShaderPass( shaderSepia );
const effectVignette = new ShaderPass( shaderVignette );
const gammaCorrection = new ShaderPass( GammaCorrectionShader );
effectBleach.uniforms[ 'opacity' ].value = 0.95;
effectSepia.uniforms[ 'amount' ].value = 0.9;
effectVignette.uniforms[ 'offset' ].value = 0.95;
effectVignette.uniforms[ 'darkness' ].value = 1.6;
const effectBloom = new BloomPass( 0.5 );
const effectFilm = new FilmPass( 0.35, 0.025, 648, false );
const effectFilmBW = new FilmPass( 0.35, 0.5, 2048, true );
const effectDotScreen = new DotScreenPass( new THREE.Vector2( 0, 0 ), 0.5, 0.8 );
const effectHBlur = new ShaderPass( HorizontalBlurShader );
const effectVBlur = new ShaderPass( VerticalBlurShader );
effectHBlur.uniforms[ 'h' ].value = 2 / ( width / 2 );
effectVBlur.uniforms[ 'v' ].value = 2 / ( height / 2 );
const effectColorify1 = new ShaderPass( ColorifyShader );
const 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 ) );
const clearMask = new ClearMaskPass();
const renderMask = new MaskPass( sceneModel, cameraPerspective );
const renderMaskInverse = new MaskPass( sceneModel, cameraPerspective );
renderMaskInverse.inverse = true;
//
const rtParameters = {
stencilBuffer: true
};
const rtWidth = width / 2;
const rtHeight = height / 2;
//
const renderBackground = new RenderPass( sceneBG, cameraOrtho );
const 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( effectFilmBW );
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( effectSepia );
composer3.addPass( effectFilm );
composer3.addPass( effectVignette );
//
composer4 = new EffectComposer( renderer, new THREE.WebGLRenderTarget( rtWidth, rtHeight, rtParameters ) );
composer4.addPass( renderScene );
composer4.addPass( gammaCorrection );
composer4.addPass( effectBloom );
composer4.addPass( effectFilm );
composer4.addPass( effectBleach );
composer4.addPass( effectVignette );
renderScene.uniforms[ 'tDiffuse' ].value = composerScene.renderTarget2.texture;
window.addEventListener( 'resize', onWindowResize );
}
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 ) {
const diffuseMap = new THREE.TextureLoader().load( 'models/gltf/LeePerrySmith/Map-COL.jpg' );
diffuseMap.encoding = THREE.sRGBEncoding;
const 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() {
const 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>