123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - postprocessing with nodes</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> - Node-Based Post-Processing
- </div>
- <script type="module">
- import * as THREE from '../build/three.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 { NodePass } from './jsm/nodes/postprocessing/NodePass.js';
- import * as Nodes from './jsm/nodes/Nodes.js';
- var camera, scene, renderer, composer;
- var object, light, nodepass;
- var gui;
- var clock = new THREE.Clock();
- var frame = new Nodes.NodeFrame();
- var param = { example: 'color-adjustment' };
- var textureLoader = new THREE.TextureLoader();
- var lensflare2 = textureLoader.load( 'textures/lensflare/lensflare0.png' );
- lensflare2.wrapS = lensflare2.wrapT = THREE.RepeatWrapping;
- var decalNormal = textureLoader.load( 'textures/decal/decal-normal.jpg' );
- decalNormal.wrapS = decalNormal.wrapT = THREE.RepeatWrapping;
- init();
- animate();
- function clearGui() {
- if ( gui ) gui.destroy();
- gui = new GUI();
- gui.add( param, 'example', {
- 'basic / color-adjustment': 'color-adjustment',
- 'basic / blends': 'blends',
- 'basic / fade': 'fade',
- 'basic / invert': 'invert',
- 'basic / blur': 'blur',
- 'adv / saturation': 'saturation',
- 'adv / refraction': 'refraction',
- 'adv / mosaic': 'mosaic'
- } ).onFinishChange( function () {
- updateMaterial();
- } );
- gui.open();
- }
- function addGui( name, value, callback, isColor, min, max ) {
- var node;
- param[ name ] = value;
- if ( isColor ) {
- node = gui.addColor( param, name ).onChange( function () {
- callback( param[ name ] );
- } );
- } else if ( typeof value == 'object' ) {
- param[ name ] = value[ Object.keys( value )[ 0 ] ];
- node = gui.add( param, name, value ).onChange( function () {
- callback( param[ name ] );
- } );
- } else {
- node = gui.add( param, name, min, max ).onChange( function () {
- callback( param[ name ] );
- } );
- }
- return node;
- }
- function updateMaterial() {
- var name = param.example;
- clearGui();
- switch ( name ) {
- case 'color-adjustment':
- var screen = new Nodes.ScreenNode();
- var hue = new Nodes.FloatNode();
- var sataturation = new Nodes.FloatNode( 1 );
- var vibrance = new Nodes.FloatNode();
- var brightness = new Nodes.FloatNode( 0 );
- var contrast = new Nodes.FloatNode( 1 );
- var hueNode = new Nodes.ColorAdjustmentNode( screen, hue, Nodes.ColorAdjustmentNode.HUE );
- var satNode = new Nodes.ColorAdjustmentNode( hueNode, sataturation, Nodes.ColorAdjustmentNode.SATURATION );
- var vibranceNode = new Nodes.ColorAdjustmentNode( satNode, vibrance, Nodes.ColorAdjustmentNode.VIBRANCE );
- var brightnessNode = new Nodes.ColorAdjustmentNode( vibranceNode, brightness, Nodes.ColorAdjustmentNode.BRIGHTNESS );
- var contrastNode = new Nodes.ColorAdjustmentNode( brightnessNode, contrast, Nodes.ColorAdjustmentNode.CONTRAST );
- nodepass.input = contrastNode;
- // GUI
- addGui( 'hue', hue.value, function ( val ) {
- hue.value = val;
- }, false, 0, Math.PI * 2 );
- addGui( 'saturation', sataturation.value, function ( val ) {
- sataturation.value = val;
- }, false, 0, 2 );
- addGui( 'vibrance', vibrance.value, function ( val ) {
- vibrance.value = val;
- }, false, - 1, 1 );
- addGui( 'brightness', brightness.value, function ( val ) {
- brightness.value = val;
- }, false, 0, .5 );
- addGui( 'contrast', contrast.value, function ( val ) {
- contrast.value = val;
- }, false, 0, 2 );
- break;
- case 'fade':
- // PASS
- var color = new Nodes.ColorNode( 0xFFFFFF );
- var percent = new Nodes.FloatNode( .5 );
- var fade = new Nodes.MathNode(
- new Nodes.ScreenNode(),
- color,
- percent,
- Nodes.MathNode.MIX
- );
- nodepass.input = fade;
- // GUI
- addGui( 'color', color.value.getHex(), function ( val ) {
- color.value.setHex( val );
- }, true );
- addGui( 'fade', percent.value, function ( val ) {
- percent.value = val;
- }, false, 0, 1 );
- break;
- case 'invert':
- // PASS
- var alpha = new Nodes.FloatNode( 1 );
- var screen = new Nodes.ScreenNode();
- var inverted = new Nodes.MathNode( screen, Nodes.MathNode.INVERT );
- var fade = new Nodes.MathNode(
- screen,
- inverted,
- alpha,
- Nodes.MathNode.MIX
- );
- nodepass.input = fade;
- // GUI
- addGui( 'alpha', alpha.value, function ( val ) {
- alpha.value = val;
- }, false, 0, 1 );
- break;
- case 'blends':
- // PASS
- var multiply = new Nodes.OperatorNode(
- new Nodes.ScreenNode(),
- new Nodes.TextureNode( lensflare2 ),
- Nodes.OperatorNode.ADD
- );
- nodepass.input = multiply;
- // GUI
- addGui( 'blend', {
- 'addition': Nodes.OperatorNode.ADD,
- 'subtract': Nodes.OperatorNode.SUB,
- 'multiply': Nodes.OperatorNode.MUL,
- 'division': Nodes.OperatorNode.DIV
- }, function ( val ) {
- multiply.op = val;
- nodepass.needsUpdate = true;
- } );
- break;
- case 'saturation':
- // PASS
- var screen = new Nodes.ScreenNode();
- var sat = new Nodes.FloatNode( 0 );
- var satrgb = new Nodes.FunctionNode( [
- "vec3 satrgb( vec3 rgb, float adjustment ) {",
- // include luminance function from LuminanceNode
- " vec3 intensity = vec3( luminance( rgb ) );",
- " return mix( intensity, rgb, adjustment );",
- "}"
- ].join( "\n" ), [ Nodes.LuminanceNode.Nodes.luminance ] );
- var saturation = new Nodes.FunctionCallNode( satrgb );
- saturation.inputs.rgb = screen;
- saturation.inputs.adjustment = sat;
- nodepass.input = saturation;
- // GUI
- addGui( 'saturation', sat.value, function ( val ) {
- sat.value = val;
- }, false, 0, 2 );
- break;
- case 'refraction':
- // PASS
- var normal = new Nodes.TextureNode( decalNormal );
- var normalXY = new Nodes.SwitchNode( normal, 'xy' );
- var scale = new Nodes.FloatNode( .5 );
- var normalXYFlip = new Nodes.MathNode(
- normalXY,
- Nodes.MathNode.INVERT
- );
- var offsetNormal = new Nodes.OperatorNode(
- normalXYFlip,
- new Nodes.FloatNode( .5 ),
- Nodes.OperatorNode.ADD
- );
- var scaleTexture = new Nodes.OperatorNode(
- new Nodes.SwitchNode( normal, 'z' ),
- offsetNormal,
- Nodes.OperatorNode.MUL
- );
- var scaleNormal = new Nodes.MathNode(
- new Nodes.FloatNode( 1 ),
- scaleTexture,
- scale,
- Nodes.MathNode.MIX
- );
- var offsetCoord = new Nodes.OperatorNode(
- new Nodes.UVNode(),
- scaleNormal,
- Nodes.OperatorNode.MUL
- );
- var screen = new Nodes.ScreenNode( offsetCoord );
- nodepass.input = screen;
- // GUI
- addGui( 'scale', scale.value, function ( val ) {
- scale.value = val;
- }, false, 0, 1 );
- addGui( 'invert', false, function ( val ) {
- offsetNormal.a = val ? normalXYFlip : normalXY;
- nodepass.needsUpdate = true;
- } );
- break;
- case 'mosaic':
- // PASS
- var scale = new Nodes.FloatNode( 128 );
- var fade = new Nodes.FloatNode( 1 );
- var uv = new Nodes.UVNode();
- var blocks = new Nodes.OperatorNode(
- uv,
- scale,
- Nodes.OperatorNode.MUL
- );
- var blocksSize = new Nodes.MathNode(
- blocks,
- Nodes.MathNode.FLOOR
- );
- var mosaicUV = new Nodes.OperatorNode(
- blocksSize,
- scale,
- Nodes.OperatorNode.DIV
- );
- var fadeScreen = new Nodes.MathNode(
- uv,
- mosaicUV,
- fade,
- Nodes.MathNode.MIX
- );
- nodepass.input = new Nodes.ScreenNode( fadeScreen );
- // GUI
- addGui( 'scale', scale.value, function ( val ) {
- scale.value = val;
- }, false, 16, 1024 );
- addGui( 'fade', fade.value, function ( val ) {
- fade.value = val;
- }, false, 0, 1 );
- addGui( 'mask', false, function ( val ) {
- fadeScreen.c = val ? new Nodes.TextureNode( lensflare2 ) : fade;
- nodepass.needsUpdate = true;
- } );
- break;
- case 'blur':
- // PASS
- var size = renderer.getDrawingBufferSize( new THREE.Vector2() );
- var blurScreen = new Nodes.BlurNode( new Nodes.ScreenNode() );
- blurScreen.size = new THREE.Vector2( size.width, size.height );
- nodepass.input = blurScreen;
- // GUI
- addGui( 'blurX', blurScreen.radius.x, function ( val ) {
- blurScreen.radius.x = val;
- }, false, 0, 15 );
- addGui( 'blurY', blurScreen.radius.y, function ( val ) {
- blurScreen.radius.y = val;
- }, false, 0, 15 );
- break;
- }
- nodepass.needsUpdate = true;
- // test serialization
- /*
- var library = {};
- library[ lensflare2.uuid ] = lensflare2;
- library[ decalNormal.uuid ] = decalNormal;
- var json = nodepass.toJSON();
- nodepass.input = new Nodes.NodeMaterialLoader( null, library ).parse( json ).value;
- */
- }
- function init() {
- renderer = new THREE.WebGLRenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- document.body.appendChild( renderer.domElement );
- //
- camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
- camera.position.z = 400;
- scene = new THREE.Scene();
- scene.fog = new THREE.Fog( 0x0066FF, 1, 1000 );
- object = new THREE.Object3D();
- scene.add( object );
- var geometry = new THREE.SphereBufferGeometry( 1, 4, 4 );
- for ( var i = 0; i < 100; i ++ ) {
- var material = new THREE.MeshPhongMaterial( { color: 0x888888 + ( Math.random() * 0x888888 ), flatShading: true } );
- var mesh = new THREE.Mesh( geometry, material );
- mesh.position.set( Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 ).normalize();
- mesh.position.multiplyScalar( Math.random() * 400 );
- mesh.rotation.set( Math.random() * 2, Math.random() * 2, Math.random() * 2 );
- mesh.scale.x = mesh.scale.y = mesh.scale.z = 10 + ( Math.random() * 40 );
- object.add( mesh );
- }
- scene.add( new THREE.AmbientLight( 0x999999 ) );
- light = new THREE.DirectionalLight( 0xffffff );
- light.position.set( 1, 1, 1 );
- scene.add( light );
- // postprocessing
- composer = new EffectComposer( renderer );
- composer.addPass( new RenderPass( scene, camera ) );
- nodepass = new NodePass();
- composer.addPass( nodepass );
- //
- updateMaterial();
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- composer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- requestAnimationFrame( animate );
- var delta = clock.getDelta();
- object.rotation.x += 0.005;
- object.rotation.y += 0.01;
- frame.update( delta ).updateNode( nodepass.material );
- composer.render();
- }
- </script>
- </body>
- </html>
|