添加关照、全局等高线、修改图层问题

This commit is contained in:
2025-07-17 18:54:05 +08:00
parent c781d38c0c
commit b274b62671
4594 changed files with 791769 additions and 4921 deletions

View File

@ -0,0 +1,148 @@
import TempNode from '../core/TempNode.js';
import { nodeObject, addNodeElement, tslFn, float, vec4 } from '../shadernode/ShaderNode.js';
import { NodeUpdateType } from '../core/constants.js';
import { uv } from '../accessors/UVNode.js';
import { texture } from '../accessors/TextureNode.js';
import { texturePass } from './PassNode.js';
import { uniform } from '../core/UniformNode.js';
import { RenderTarget } from 'three';
import { sign, max } from '../math/MathNode.js';
import QuadMesh from '../../objects/QuadMesh.js';
const quadMeshComp = new QuadMesh();
class AfterImageNode extends TempNode {
constructor( textureNode, damp = 0.96 ) {
super( textureNode );
this.textureNode = textureNode;
this.textureNodeOld = texture();
this.damp = uniform( damp );
this._compRT = new RenderTarget();
this._compRT.texture.name = 'AfterImageNode.comp';
this._oldRT = new RenderTarget();
this._oldRT.texture.name = 'AfterImageNode.old';
this._textureNode = texturePass( this, this._compRT.texture );
this.updateBeforeType = NodeUpdateType.RENDER;
}
getTextureNode() {
return this._textureNode;
}
setSize( width, height ) {
this._compRT.setSize( width, height );
this._oldRT.setSize( width, height );
}
updateBefore( frame ) {
const { renderer } = frame;
const textureNode = this.textureNode;
const map = textureNode.value;
const textureType = map.type;
this._compRT.texture.type = textureType;
this._oldRT.texture.type = textureType;
const currentRenderTarget = renderer.getRenderTarget();
const currentTexture = textureNode.value;
this.textureNodeOld.value = this._oldRT.texture;
// comp
renderer.setRenderTarget( this._compRT );
quadMeshComp.render( renderer );
// Swap the textures
const temp = this._oldRT;
this._oldRT = this._compRT;
this._compRT = temp;
// set size before swapping fails
this.setSize( map.image.width, map.image.height );
renderer.setRenderTarget( currentRenderTarget );
textureNode.value = currentTexture;
}
setup( builder ) {
const textureNode = this.textureNode;
const textureNodeOld = this.textureNodeOld;
if ( textureNode.isTextureNode !== true ) {
console.error( 'AfterImageNode requires a TextureNode.' );
return vec4();
}
//
const uvNode = textureNode.uvNode || uv();
textureNodeOld.uvNode = uvNode;
const sampleTexture = ( uv ) => textureNode.cache().context( { getUV: () => uv, forceUVContext: true } );
const when_gt = tslFn( ( [ x_immutable, y_immutable ] ) => {
const y = float( y_immutable ).toVar();
const x = vec4( x_immutable ).toVar();
return max( sign( x.sub( y ) ), 0.0 );
} );
const afterImg = tslFn( () => {
const texelOld = vec4( textureNodeOld );
const texelNew = vec4( sampleTexture( uvNode ) );
texelOld.mulAssign( this.damp.mul( when_gt( texelOld, 0.1 ) ) );
return max( texelNew, texelOld );
} );
//
const materialComposed = this._materialComposed || ( this._materialComposed = builder.createNodeMaterial() );
materialComposed.fragmentNode = afterImg();
quadMeshComp.material = materialComposed;
//
const properties = builder.getNodeProperties( this );
properties.textureNode = textureNode;
//
return this._textureNode;
}
}
export const afterImage = ( node, damp ) => nodeObject( new AfterImageNode( nodeObject( node ), damp ) );
addNodeElement( 'afterImage', afterImage );
export default AfterImageNode;

View File

@ -0,0 +1,148 @@
import TempNode from '../core/TempNode.js';
import { nodeObject, addNodeElement, tslFn, float, vec2, vec3, vec4 } from '../shadernode/ShaderNode.js';
import { loop } from '../utils/LoopNode.js';
import { uniform } from '../core/UniformNode.js';
import { NodeUpdateType } from '../core/constants.js';
import { threshold } from './ColorAdjustmentNode.js';
import { uv } from '../accessors/UVNode.js';
import { texturePass } from './PassNode.js';
import { Vector2, RenderTarget } from 'three';
import QuadMesh from '../../objects/QuadMesh.js';
const quadMesh = new QuadMesh();
class AnamorphicNode extends TempNode {
constructor( textureNode, tresholdNode, scaleNode, samples ) {
super( 'vec4' );
this.textureNode = textureNode;
this.tresholdNode = tresholdNode;
this.scaleNode = scaleNode;
this.colorNode = vec3( 0.1, 0.0, 1.0 );
this.samples = samples;
this.resolution = new Vector2( 1, 1 );
this._renderTarget = new RenderTarget();
this._renderTarget.texture.name = 'anamorphic';
this._invSize = uniform( new Vector2() );
this._textureNode = texturePass( this, this._renderTarget.texture );
this.updateBeforeType = NodeUpdateType.RENDER;
}
getTextureNode() {
return this._textureNode;
}
setSize( width, height ) {
this._invSize.value.set( 1 / width, 1 / height );
width = Math.max( Math.round( width * this.resolution.x ), 1 );
height = Math.max( Math.round( height * this.resolution.y ), 1 );
this._renderTarget.setSize( width, height );
}
updateBefore( frame ) {
const { renderer } = frame;
const textureNode = this.textureNode;
const map = textureNode.value;
this._renderTarget.texture.type = map.type;
const currentRenderTarget = renderer.getRenderTarget();
const currentTexture = textureNode.value;
quadMesh.material = this._material;
this.setSize( map.image.width, map.image.height );
// render
renderer.setRenderTarget( this._renderTarget );
quadMesh.render( renderer );
// restore
renderer.setRenderTarget( currentRenderTarget );
textureNode.value = currentTexture;
}
setup( builder ) {
const textureNode = this.textureNode;
if ( textureNode.isTextureNode !== true ) {
console.error( 'AnamorphNode requires a TextureNode.' );
return vec4();
}
//
const uvNode = textureNode.uvNode || uv();
const sampleTexture = ( uv ) => textureNode.cache().context( { getUV: () => uv, forceUVContext: true } );
const anamorph = tslFn( () => {
const samples = this.samples;
const halfSamples = Math.floor( samples / 2 );
const total = vec3( 0 ).toVar();
loop( { start: - halfSamples, end: halfSamples }, ( { i } ) => {
const softness = float( i ).abs().div( halfSamples ).oneMinus();
const uv = vec2( uvNode.x.add( this._invSize.x.mul( i ).mul( this.scaleNode ) ), uvNode.y );
const color = sampleTexture( uv );
const pass = threshold( color, this.tresholdNode ).mul( softness );
total.addAssign( pass );
} );
return total.mul( this.colorNode );
} );
//
const material = this._material || ( this._material = builder.createNodeMaterial() );
material.fragmentNode = anamorph();
//
const properties = builder.getNodeProperties( this );
properties.textureNode = textureNode;
//
return this._textureNode;
}
}
export const anamorphic = ( node, threshold = .9, scale = 3, samples = 32 ) => nodeObject( new AnamorphicNode( nodeObject( node ), nodeObject( threshold ), nodeObject( scale ), samples ) );
addNodeElement( 'anamorphic', anamorphic );
export default AnamorphicNode;

View File

@ -0,0 +1,128 @@
import TempNode from '../core/TempNode.js';
import { /*mix, step,*/ EPSILON } from '../math/MathNode.js';
import { addNodeClass } from '../core/Node.js';
import { addNodeElement, tslFn, nodeProxy, vec3 } from '../shadernode/ShaderNode.js';
export const BurnNode = tslFn( ( { base, blend } ) => {
const fn = ( c ) => blend[ c ].lessThan( EPSILON ).cond( blend[ c ], base[ c ].oneMinus().div( blend[ c ] ).oneMinus().max( 0 ) );
return vec3( fn( 'x' ), fn( 'y' ), fn( 'z' ) );
} ).setLayout( {
name: 'burnColor',
type: 'vec3',
inputs: [
{ name: 'base', type: 'vec3' },
{ name: 'blend', type: 'vec3' }
]
} );
export const DodgeNode = tslFn( ( { base, blend } ) => {
const fn = ( c ) => blend[ c ].equal( 1.0 ).cond( blend[ c ], base[ c ].div( blend[ c ].oneMinus() ).max( 0 ) );
return vec3( fn( 'x' ), fn( 'y' ), fn( 'z' ) );
} ).setLayout( {
name: 'dodgeColor',
type: 'vec3',
inputs: [
{ name: 'base', type: 'vec3' },
{ name: 'blend', type: 'vec3' }
]
} );
export const ScreenNode = tslFn( ( { base, blend } ) => {
const fn = ( c ) => base[ c ].oneMinus().mul( blend[ c ].oneMinus() ).oneMinus();
return vec3( fn( 'x' ), fn( 'y' ), fn( 'z' ) );
} ).setLayout( {
name: 'screenColor',
type: 'vec3',
inputs: [
{ name: 'base', type: 'vec3' },
{ name: 'blend', type: 'vec3' }
]
} );
export const OverlayNode = tslFn( ( { base, blend } ) => {
const fn = ( c ) => base[ c ].lessThan( 0.5 ).cond( base[ c ].mul( blend[ c ], 2.0 ), base[ c ].oneMinus().mul( blend[ c ].oneMinus() ).oneMinus() );
//const fn = ( c ) => mix( base[ c ].oneMinus().mul( blend[ c ].oneMinus() ).oneMinus(), base[ c ].mul( blend[ c ], 2.0 ), step( base[ c ], 0.5 ) );
return vec3( fn( 'x' ), fn( 'y' ), fn( 'z' ) );
} ).setLayout( {
name: 'overlayColor',
type: 'vec3',
inputs: [
{ name: 'base', type: 'vec3' },
{ name: 'blend', type: 'vec3' }
]
} );
class BlendModeNode extends TempNode {
constructor( blendMode, baseNode, blendNode ) {
super();
this.blendMode = blendMode;
this.baseNode = baseNode;
this.blendNode = blendNode;
}
setup() {
const { blendMode, baseNode, blendNode } = this;
const params = { base: baseNode, blend: blendNode };
let outputNode = null;
if ( blendMode === BlendModeNode.BURN ) {
outputNode = BurnNode( params );
} else if ( blendMode === BlendModeNode.DODGE ) {
outputNode = DodgeNode( params );
} else if ( blendMode === BlendModeNode.SCREEN ) {
outputNode = ScreenNode( params );
} else if ( blendMode === BlendModeNode.OVERLAY ) {
outputNode = OverlayNode( params );
}
return outputNode;
}
}
BlendModeNode.BURN = 'burn';
BlendModeNode.DODGE = 'dodge';
BlendModeNode.SCREEN = 'screen';
BlendModeNode.OVERLAY = 'overlay';
export default BlendModeNode;
export const burn = nodeProxy( BlendModeNode, BlendModeNode.BURN );
export const dodge = nodeProxy( BlendModeNode, BlendModeNode.DODGE );
export const overlay = nodeProxy( BlendModeNode, BlendModeNode.OVERLAY );
export const screen = nodeProxy( BlendModeNode, BlendModeNode.SCREEN );
addNodeElement( 'burn', burn );
addNodeElement( 'dodge', dodge );
addNodeElement( 'overlay', overlay );
addNodeElement( 'screen', screen );
addNodeClass( 'BlendModeNode', BlendModeNode );

View File

@ -0,0 +1,80 @@
import TempNode from '../core/TempNode.js';
import { addNodeClass } from '../core/Node.js';
import { uv } from '../accessors/UVNode.js';
import { normalView } from '../accessors/NormalNode.js';
import { positionView } from '../accessors/PositionNode.js';
import { faceDirection } from './FrontFacingNode.js';
import { addNodeElement, tslFn, nodeProxy, float, vec2 } from '../shadernode/ShaderNode.js';
// Bump Mapping Unparametrized Surfaces on the GPU by Morten S. Mikkelsen
// https://mmikk.github.io/papers3d/mm_sfgrad_bump.pdf
const dHdxy_fwd = tslFn( ( { textureNode, bumpScale } ) => {
// It's used to preserve the same TextureNode instance
const sampleTexture = ( callback ) => textureNode.cache().context( { getUV: ( texNode ) => callback( texNode.uvNode || uv() ), forceUVContext: true } );
const Hll = float( sampleTexture( ( uvNode ) => uvNode ) );
return vec2(
float( sampleTexture( ( uvNode ) => uvNode.add( uvNode.dFdx() ) ) ).sub( Hll ),
float( sampleTexture( ( uvNode ) => uvNode.add( uvNode.dFdy() ) ) ).sub( Hll )
).mul( bumpScale );
} );
// Evaluate the derivative of the height w.r.t. screen-space using forward differencing (listing 2)
const perturbNormalArb = tslFn( ( inputs ) => {
const { surf_pos, surf_norm, dHdxy } = inputs;
// normalize is done to ensure that the bump map looks the same regardless of the texture's scale
const vSigmaX = surf_pos.dFdx().normalize();
const vSigmaY = surf_pos.dFdy().normalize();
const vN = surf_norm; // normalized
const R1 = vSigmaY.cross( vN );
const R2 = vN.cross( vSigmaX );
const fDet = vSigmaX.dot( R1 ).mul( faceDirection );
const vGrad = fDet.sign().mul( dHdxy.x.mul( R1 ).add( dHdxy.y.mul( R2 ) ) );
return fDet.abs().mul( surf_norm ).sub( vGrad ).normalize();
} );
class BumpMapNode extends TempNode {
constructor( textureNode, scaleNode = null ) {
super( 'vec3' );
this.textureNode = textureNode;
this.scaleNode = scaleNode;
}
setup() {
const bumpScale = this.scaleNode !== null ? this.scaleNode : 1;
const dHdxy = dHdxy_fwd( { textureNode: this.textureNode, bumpScale } );
return perturbNormalArb( {
surf_pos: positionView,
surf_norm: normalView,
dHdxy
} );
}
}
export default BumpMapNode;
export const bumpMap = nodeProxy( BumpMapNode );
addNodeElement( 'bumpMap', bumpMap );
addNodeClass( 'BumpMapNode', BumpMapNode );

View File

@ -0,0 +1,99 @@
import TempNode from '../core/TempNode.js';
import { dot, mix } from '../math/MathNode.js';
import { add } from '../math/OperatorNode.js';
import { addNodeClass } from '../core/Node.js';
import { addNodeElement, tslFn, nodeProxy, float, vec3 } from '../shadernode/ShaderNode.js';
const saturationNode = tslFn( ( { color, adjustment } ) => {
return adjustment.mix( luminance( color.rgb ), color.rgb );
} );
const vibranceNode = tslFn( ( { color, adjustment } ) => {
const average = add( color.r, color.g, color.b ).div( 3.0 );
const mx = color.r.max( color.g.max( color.b ) );
const amt = mx.sub( average ).mul( adjustment ).mul( - 3.0 );
return mix( color.rgb, mx, amt );
} );
const hueNode = tslFn( ( { color, adjustment } ) => {
const k = vec3( 0.57735, 0.57735, 0.57735 );
const cosAngle = adjustment.cos();
return vec3( color.rgb.mul( cosAngle ).add( k.cross( color.rgb ).mul( adjustment.sin() ).add( k.mul( dot( k, color.rgb ).mul( cosAngle.oneMinus() ) ) ) ) );
} );
class ColorAdjustmentNode extends TempNode {
constructor( method, colorNode, adjustmentNode = float( 1 ) ) {
super( 'vec3' );
this.method = method;
this.colorNode = colorNode;
this.adjustmentNode = adjustmentNode;
}
setup() {
const { method, colorNode, adjustmentNode } = this;
const callParams = { color: colorNode, adjustment: adjustmentNode };
let outputNode = null;
if ( method === ColorAdjustmentNode.SATURATION ) {
outputNode = saturationNode( callParams );
} else if ( method === ColorAdjustmentNode.VIBRANCE ) {
outputNode = vibranceNode( callParams );
} else if ( method === ColorAdjustmentNode.HUE ) {
outputNode = hueNode( callParams );
} else {
console.error( `${ this.type }: Method "${ this.method }" not supported!` );
}
return outputNode;
}
}
ColorAdjustmentNode.SATURATION = 'saturation';
ColorAdjustmentNode.VIBRANCE = 'vibrance';
ColorAdjustmentNode.HUE = 'hue';
export default ColorAdjustmentNode;
export const saturation = nodeProxy( ColorAdjustmentNode, ColorAdjustmentNode.SATURATION );
export const vibrance = nodeProxy( ColorAdjustmentNode, ColorAdjustmentNode.VIBRANCE );
export const hue = nodeProxy( ColorAdjustmentNode, ColorAdjustmentNode.HUE );
export const lumaCoeffs = vec3( 0.2125, 0.7154, 0.0721 );
export const luminance = ( color, luma = lumaCoeffs ) => dot( color, luma );
export const threshold = ( color, threshold ) => mix( vec3( 0.0 ), color, luminance( color ).sub( threshold ).max( 0 ) );
addNodeElement( 'saturation', saturation );
addNodeElement( 'vibrance', vibrance );
addNodeElement( 'hue', hue );
addNodeElement( 'threshold', threshold );
addNodeClass( 'ColorAdjustmentNode', ColorAdjustmentNode );

View File

@ -0,0 +1,108 @@
import TempNode from '../core/TempNode.js';
import { mix } from '../math/MathNode.js';
import { addNodeClass } from '../core/Node.js';
import { addNodeElement, tslFn, nodeObject, nodeProxy, vec4 } from '../shadernode/ShaderNode.js';
import { LinearSRGBColorSpace, SRGBColorSpace } from 'three';
const sRGBToLinearShader = tslFn( ( inputs ) => {
const { value } = inputs;
const { rgb } = value;
const a = rgb.mul( 0.9478672986 ).add( 0.0521327014 ).pow( 2.4 );
const b = rgb.mul( 0.0773993808 );
const factor = rgb.lessThanEqual( 0.04045 );
const rgbResult = mix( a, b, factor );
return vec4( rgbResult, value.a );
} );
const LinearTosRGBShader = tslFn( ( inputs ) => {
const { value } = inputs;
const { rgb } = value;
const a = rgb.pow( 0.41666 ).mul( 1.055 ).sub( 0.055 );
const b = rgb.mul( 12.92 );
const factor = rgb.lessThanEqual( 0.0031308 );
const rgbResult = mix( a, b, factor );
return vec4( rgbResult, value.a );
} );
const getColorSpaceMethod = ( colorSpace ) => {
let method = null;
if ( colorSpace === LinearSRGBColorSpace ) {
method = 'Linear';
} else if ( colorSpace === SRGBColorSpace ) {
method = 'sRGB';
}
return method;
};
const getMethod = ( source, target ) => {
return getColorSpaceMethod( source ) + 'To' + getColorSpaceMethod( target );
};
class ColorSpaceNode extends TempNode {
constructor( method, node ) {
super( 'vec4' );
this.method = method;
this.node = node;
}
setup() {
const { method, node } = this;
if ( method === ColorSpaceNode.LINEAR_TO_LINEAR )
return node;
return Methods[ method ]( { value: node } );
}
}
ColorSpaceNode.LINEAR_TO_LINEAR = 'LinearToLinear';
ColorSpaceNode.LINEAR_TO_sRGB = 'LinearTosRGB';
ColorSpaceNode.sRGB_TO_LINEAR = 'sRGBToLinear';
const Methods = {
[ ColorSpaceNode.LINEAR_TO_sRGB ]: LinearTosRGBShader,
[ ColorSpaceNode.sRGB_TO_LINEAR ]: sRGBToLinearShader
};
export default ColorSpaceNode;
export const linearToColorSpace = ( node, colorSpace ) => nodeObject( new ColorSpaceNode( getMethod( LinearSRGBColorSpace, colorSpace ), nodeObject( node ) ) );
export const colorSpaceToLinear = ( node, colorSpace ) => nodeObject( new ColorSpaceNode( getMethod( colorSpace, LinearSRGBColorSpace ), nodeObject( node ) ) );
export const linearTosRGB = nodeProxy( ColorSpaceNode, ColorSpaceNode.LINEAR_TO_sRGB );
export const sRGBToLinear = nodeProxy( ColorSpaceNode, ColorSpaceNode.sRGB_TO_LINEAR );
addNodeElement( 'linearTosRGB', linearTosRGB );
addNodeElement( 'sRGBToLinear', sRGBToLinear );
addNodeElement( 'linearToColorSpace', linearToColorSpace );
addNodeElement( 'colorSpaceToLinear', colorSpaceToLinear );
addNodeClass( 'ColorSpaceNode', ColorSpaceNode );

View File

@ -0,0 +1,40 @@
import Node, { addNodeClass } from '../core/Node.js';
import { nodeImmutable, float } from '../shadernode/ShaderNode.js';
import { BackSide, WebGLCoordinateSystem } from 'three';
class FrontFacingNode extends Node {
constructor() {
super( 'bool' );
this.isFrontFacingNode = true;
}
generate( builder ) {
const { renderer, material } = builder;
if ( renderer.coordinateSystem === WebGLCoordinateSystem ) {
if ( material.side === BackSide ) {
return 'false';
}
}
return builder.getFrontFacing();
}
}
export default FrontFacingNode;
export const frontFacing = nodeImmutable( FrontFacingNode );
export const faceDirection = float( frontFacing ).mul( 2.0 ).sub( 1.0 );
addNodeClass( 'FrontFacingNode', FrontFacingNode );

View File

@ -0,0 +1,190 @@
import TempNode from '../core/TempNode.js';
import { nodeObject, addNodeElement, tslFn, float, vec2, vec4 } from '../shadernode/ShaderNode.js';
import { NodeUpdateType } from '../core/constants.js';
import { mul } from '../math/OperatorNode.js';
import { uv } from '../accessors/UVNode.js';
import { texturePass } from './PassNode.js';
import { uniform } from '../core/UniformNode.js';
import { Vector2, RenderTarget } from 'three';
import QuadMesh from '../../objects/QuadMesh.js';
// WebGPU: The use of a single QuadMesh for both gaussian blur passes results in a single RenderObject with a SampledTexture binding that
// alternates between source textures and triggers creation of new BindGroups and BindGroupLayouts every frame.
const quadMesh1 = new QuadMesh();
const quadMesh2 = new QuadMesh();
class GaussianBlurNode extends TempNode {
constructor( textureNode, sigma = 2 ) {
super( 'vec4' );
this.textureNode = textureNode;
this.sigma = sigma;
this.directionNode = vec2( 1 );
this._invSize = uniform( new Vector2() );
this._passDirection = uniform( new Vector2() );
this._horizontalRT = new RenderTarget();
this._horizontalRT.texture.name = 'GaussianBlurNode.horizontal';
this._verticalRT = new RenderTarget();
this._verticalRT.texture.name = 'GaussianBlurNode.vertical';
this._textureNode = texturePass( this, this._verticalRT.texture );
this.updateBeforeType = NodeUpdateType.RENDER;
this.resolution = new Vector2( 1, 1 );
}
setSize( width, height ) {
width = Math.max( Math.round( width * this.resolution.x ), 1 );
height = Math.max( Math.round( height * this.resolution.y ), 1 );
this._invSize.value.set( 1 / width, 1 / height );
this._horizontalRT.setSize( width, height );
this._verticalRT.setSize( width, height );
}
updateBefore( frame ) {
const { renderer } = frame;
const textureNode = this.textureNode;
const map = textureNode.value;
const currentRenderTarget = renderer.getRenderTarget();
const currentTexture = textureNode.value;
quadMesh1.material = this._material;
quadMesh2.material = this._material;
this.setSize( map.image.width, map.image.height );
const textureType = map.type;
this._horizontalRT.texture.type = textureType;
this._verticalRT.texture.type = textureType;
// horizontal
renderer.setRenderTarget( this._horizontalRT );
this._passDirection.value.set( 1, 0 );
quadMesh1.render( renderer );
// vertical
textureNode.value = this._horizontalRT.texture;
renderer.setRenderTarget( this._verticalRT );
this._passDirection.value.set( 0, 1 );
quadMesh2.render( renderer );
// restore
renderer.setRenderTarget( currentRenderTarget );
textureNode.value = currentTexture;
}
getTextureNode() {
return this._textureNode;
}
setup( builder ) {
const textureNode = this.textureNode;
if ( textureNode.isTextureNode !== true ) {
console.error( 'GaussianBlurNode requires a TextureNode.' );
return vec4();
}
//
const uvNode = textureNode.uvNode || uv();
const sampleTexture = ( uv ) => textureNode.cache().context( { getUV: () => uv, forceUVContext: true } );
const blur = tslFn( () => {
const kernelSize = 3 + ( 2 * this.sigma );
const gaussianCoefficients = this._getCoefficients( kernelSize );
const invSize = this._invSize;
const direction = vec2( this.directionNode ).mul( this._passDirection );
const weightSum = float( gaussianCoefficients[ 0 ] ).toVar();
const diffuseSum = vec4( sampleTexture( uvNode ).mul( weightSum ) ).toVar();
for ( let i = 1; i < kernelSize; i ++ ) {
const x = float( i );
const w = float( gaussianCoefficients[ i ] );
const uvOffset = vec2( direction.mul( invSize.mul( x ) ) ).toVar();
const sample1 = vec4( sampleTexture( uvNode.add( uvOffset ) ) );
const sample2 = vec4( sampleTexture( uvNode.sub( uvOffset ) ) );
diffuseSum.addAssign( sample1.add( sample2 ).mul( w ) );
weightSum.addAssign( mul( 2.0, w ) );
}
return diffuseSum.div( weightSum );
} );
//
const material = this._material || ( this._material = builder.createNodeMaterial() );
material.fragmentNode = blur();
//
const properties = builder.getNodeProperties( this );
properties.textureNode = textureNode;
//
return this._textureNode;
}
_getCoefficients( kernelRadius ) {
const coefficients = [];
for ( let i = 0; i < kernelRadius; i ++ ) {
coefficients.push( 0.39894 * Math.exp( - 0.5 * i * i / ( kernelRadius * kernelRadius ) ) / kernelRadius );
}
return coefficients;
}
}
export const gaussianBlur = ( node, sigma ) => nodeObject( new GaussianBlurNode( nodeObject( node ), sigma ) );
addNodeElement( 'gaussianBlur', gaussianBlur );
export default GaussianBlurNode;

View File

@ -0,0 +1,106 @@
import TempNode from '../core/TempNode.js';
import { add } from '../math/OperatorNode.js';
import { modelNormalMatrix } from '../accessors/ModelNode.js';
import { normalView } from '../accessors/NormalNode.js';
import { positionView } from '../accessors/PositionNode.js';
import { TBNViewMatrix } from '../accessors/AccessorsUtils.js';
import { uv } from '../accessors/UVNode.js';
import { faceDirection } from './FrontFacingNode.js';
import { addNodeClass } from '../core/Node.js';
import { addNodeElement, tslFn, nodeProxy, vec3 } from '../shadernode/ShaderNode.js';
import { TangentSpaceNormalMap, ObjectSpaceNormalMap } from 'three';
// Normal Mapping Without Precomputed Tangents
// http://www.thetenthplanet.de/archives/1180
const perturbNormal2Arb = tslFn( ( inputs ) => {
const { eye_pos, surf_norm, mapN, uv } = inputs;
const q0 = eye_pos.dFdx();
const q1 = eye_pos.dFdy();
const st0 = uv.dFdx();
const st1 = uv.dFdy();
const N = surf_norm; // normalized
const q1perp = q1.cross( N );
const q0perp = N.cross( q0 );
const T = q1perp.mul( st0.x ).add( q0perp.mul( st1.x ) );
const B = q1perp.mul( st0.y ).add( q0perp.mul( st1.y ) );
const det = T.dot( T ).max( B.dot( B ) );
const scale = faceDirection.mul( det.inverseSqrt() );
return add( T.mul( mapN.x, scale ), B.mul( mapN.y, scale ), N.mul( mapN.z ) ).normalize();
} );
class NormalMapNode extends TempNode {
constructor( node, scaleNode = null ) {
super( 'vec3' );
this.node = node;
this.scaleNode = scaleNode;
this.normalMapType = TangentSpaceNormalMap;
}
setup( builder ) {
const { normalMapType, scaleNode } = this;
let normalMap = this.node.mul( 2.0 ).sub( 1.0 );
if ( scaleNode !== null ) {
normalMap = vec3( normalMap.xy.mul( scaleNode ), normalMap.z );
}
let outputNode = null;
if ( normalMapType === ObjectSpaceNormalMap ) {
outputNode = modelNormalMatrix.mul( normalMap ).normalize();
} else if ( normalMapType === TangentSpaceNormalMap ) {
const tangent = builder.hasGeometryAttribute( 'tangent' );
if ( tangent === true ) {
outputNode = TBNViewMatrix.mul( normalMap ).normalize();
} else {
outputNode = perturbNormal2Arb( {
eye_pos: positionView,
surf_norm: normalView,
mapN: normalMap,
uv: uv()
} );
}
}
return outputNode;
}
}
export default NormalMapNode;
export const normalMap = nodeProxy( NormalMapNode );
addNodeElement( 'normalMap', normalMap );
addNodeClass( 'NormalMapNode', NormalMapNode );

View File

@ -0,0 +1,199 @@
import { addNodeClass } from '../core/Node.js';
import TempNode from '../core/TempNode.js';
import TextureNode from '../accessors/TextureNode.js';
import { NodeUpdateType } from '../core/constants.js';
import { nodeObject } from '../shadernode/ShaderNode.js';
import { uniform } from '../core/UniformNode.js';
import { viewZToOrthographicDepth, perspectiveDepthToViewZ } from './ViewportDepthNode.js';
import { RenderTarget, Vector2, HalfFloatType, DepthTexture, NoToneMapping/*, FloatType*/ } from 'three';
class PassTextureNode extends TextureNode {
constructor( passNode, texture ) {
super( texture );
this.passNode = passNode;
this.setUpdateMatrix( false );
}
setup( builder ) {
this.passNode.build( builder );
return super.setup( builder );
}
clone() {
return new this.constructor( this.passNode, this.value );
}
}
class PassNode extends TempNode {
constructor( scope, scene, camera ) {
super( 'vec4' );
this.scope = scope;
this.scene = scene;
this.camera = camera;
this._pixelRatio = 1;
this._width = 1;
this._height = 1;
const depthTexture = new DepthTexture();
depthTexture.isRenderTargetTexture = true;
//depthTexture.type = FloatType;
depthTexture.name = 'PostProcessingDepth';
const renderTarget = new RenderTarget( this._width * this._pixelRatio, this._height * this._pixelRatio, { type: HalfFloatType } );
renderTarget.texture.name = 'PostProcessing';
renderTarget.depthTexture = depthTexture;
this.renderTarget = renderTarget;
this.updateBeforeType = NodeUpdateType.FRAME;
this._textureNode = nodeObject( new PassTextureNode( this, renderTarget.texture ) );
this._depthTextureNode = nodeObject( new PassTextureNode( this, depthTexture ) );
this._depthNode = null;
this._viewZNode = null;
this._cameraNear = uniform( 0 );
this._cameraFar = uniform( 0 );
this.isPassNode = true;
}
isGlobal() {
return true;
}
getTextureNode() {
return this._textureNode;
}
getTextureDepthNode() {
return this._depthTextureNode;
}
getViewZNode() {
if ( this._viewZNode === null ) {
const cameraNear = this._cameraNear;
const cameraFar = this._cameraFar;
this._viewZNode = perspectiveDepthToViewZ( this._depthTextureNode, cameraNear, cameraFar );
}
return this._viewZNode;
}
getDepthNode() {
if ( this._depthNode === null ) {
const cameraNear = this._cameraNear;
const cameraFar = this._cameraFar;
this._depthNode = viewZToOrthographicDepth( this.getViewZNode(), cameraNear, cameraFar );
}
return this._depthNode;
}
setup() {
return this.scope === PassNode.COLOR ? this.getTextureNode() : this.getDepthNode();
}
updateBefore( frame ) {
const { renderer } = frame;
const { scene, camera } = this;
this._pixelRatio = renderer.getPixelRatio();
const size = renderer.getSize( new Vector2() );
this.setSize( size.width, size.height );
const currentToneMapping = renderer.toneMapping;
const currentToneMappingNode = renderer.toneMappingNode;
const currentRenderTarget = renderer.getRenderTarget();
this._cameraNear.value = camera.near;
this._cameraFar.value = camera.far;
renderer.toneMapping = NoToneMapping;
renderer.toneMappingNode = null;
renderer.setRenderTarget( this.renderTarget );
renderer.render( scene, camera );
renderer.toneMapping = currentToneMapping;
renderer.toneMappingNode = currentToneMappingNode;
renderer.setRenderTarget( currentRenderTarget );
}
setSize( width, height ) {
this._width = width;
this._height = height;
const effectiveWidth = this._width * this._pixelRatio;
const effectiveHeight = this._height * this._pixelRatio;
this.renderTarget.setSize( effectiveWidth, effectiveHeight );
}
setPixelRatio( pixelRatio ) {
this._pixelRatio = pixelRatio;
this.setSize( this._width, this._height );
}
dispose() {
this.renderTarget.dispose();
}
}
PassNode.COLOR = 'color';
PassNode.DEPTH = 'depth';
export default PassNode;
export const pass = ( scene, camera ) => nodeObject( new PassNode( PassNode.COLOR, scene, camera ) );
export const texturePass = ( pass, texture ) => nodeObject( new PassTextureNode( pass, texture ) );
export const depthPass = ( scene, camera ) => nodeObject( new PassNode( PassNode.DEPTH, scene, camera ) );
addNodeClass( 'PassNode', PassNode );

View File

@ -0,0 +1,32 @@
import TempNode from '../core/TempNode.js';
import { addNodeClass } from '../core/Node.js';
import { addNodeElement, nodeProxy } from '../shadernode/ShaderNode.js';
class PosterizeNode extends TempNode {
constructor( sourceNode, stepsNode ) {
super();
this.sourceNode = sourceNode;
this.stepsNode = stepsNode;
}
setup() {
const { sourceNode, stepsNode } = this;
return sourceNode.mul( stepsNode ).floor().div( stepsNode );
}
}
export default PosterizeNode;
export const posterize = nodeProxy( PosterizeNode );
addNodeElement( 'posterize', posterize );
addNodeClass( 'PosterizeNode', PosterizeNode );

View File

@ -0,0 +1,188 @@
import TempNode from '../core/TempNode.js';
import { addNodeClass } from '../core/Node.js';
import { addNodeElement, tslFn, nodeObject, float, mat3, vec3 } from '../shadernode/ShaderNode.js';
import { rendererReference } from '../accessors/RendererReferenceNode.js';
import { clamp, log2, max, pow } from '../math/MathNode.js';
import { mul } from '../math/OperatorNode.js';
import { NoToneMapping, LinearToneMapping, ReinhardToneMapping, CineonToneMapping, ACESFilmicToneMapping, AgXToneMapping } from 'three';
// exposure only
const LinearToneMappingNode = tslFn( ( { color, exposure } ) => {
return color.mul( exposure ).clamp();
} );
// source: https://www.cs.utah.edu/docs/techreports/2002/pdf/UUCS-02-001.pdf
const ReinhardToneMappingNode = tslFn( ( { color, exposure } ) => {
color = color.mul( exposure );
return color.div( color.add( 1.0 ) ).clamp();
} );
// source: http://filmicworlds.com/blog/filmic-tonemapping-operators/
const OptimizedCineonToneMappingNode = tslFn( ( { color, exposure } ) => {
// optimized filmic operator by Jim Hejl and Richard Burgess-Dawson
color = color.mul( exposure );
color = color.sub( 0.004 ).max( 0.0 );
const a = color.mul( color.mul( 6.2 ).add( 0.5 ) );
const b = color.mul( color.mul( 6.2 ).add( 1.7 ) ).add( 0.06 );
return a.div( b ).pow( 2.2 );
} );
// source: https://github.com/selfshadow/ltc_code/blob/master/webgl/shaders/ltc/ltc_blit.fs
const RRTAndODTFit = tslFn( ( { color } ) => {
const a = color.mul( color.add( 0.0245786 ) ).sub( 0.000090537 );
const b = color.mul( color.add( 0.4329510 ).mul( 0.983729 ) ).add( 0.238081 );
return a.div( b );
} );
// source: https://github.com/selfshadow/ltc_code/blob/master/webgl/shaders/ltc/ltc_blit.fs
const ACESFilmicToneMappingNode = tslFn( ( { color, exposure } ) => {
// sRGB => XYZ => D65_2_D60 => AP1 => RRT_SAT
const ACESInputMat = mat3(
0.59719, 0.35458, 0.04823,
0.07600, 0.90834, 0.01566,
0.02840, 0.13383, 0.83777
);
// ODT_SAT => XYZ => D60_2_D65 => sRGB
const ACESOutputMat = mat3(
1.60475, - 0.53108, - 0.07367,
- 0.10208, 1.10813, - 0.00605,
- 0.00327, - 0.07276, 1.07602
);
color = color.mul( exposure ).div( 0.6 );
color = ACESInputMat.mul( color );
// Apply RRT and ODT
color = RRTAndODTFit( { color } );
color = ACESOutputMat.mul( color );
// Clamp to [0, 1]
return color.clamp();
} );
const LINEAR_REC2020_TO_LINEAR_SRGB = mat3( vec3( 1.6605, - 0.1246, - 0.0182 ), vec3( - 0.5876, 1.1329, - 0.1006 ), vec3( - 0.0728, - 0.0083, 1.1187 ) );
const LINEAR_SRGB_TO_LINEAR_REC2020 = mat3( vec3( 0.6274, 0.0691, 0.0164 ), vec3( 0.3293, 0.9195, 0.0880 ), vec3( 0.0433, 0.0113, 0.8956 ) );
const agxDefaultContrastApprox = tslFn( ( [ x_immutable ] ) => {
const x = vec3( x_immutable ).toVar();
const x2 = vec3( x.mul( x ) ).toVar();
const x4 = vec3( x2.mul( x2 ) ).toVar();
return float( 15.5 ).mul( x4.mul( x2 ) ).sub( mul( 40.14, x4.mul( x ) ) ).add( mul( 31.96, x4 ).sub( mul( 6.868, x2.mul( x ) ) ).add( mul( 0.4298, x2 ).add( mul( 0.1191, x ).sub( 0.00232 ) ) ) );
} );
const AGXToneMappingNode = tslFn( ( { color, exposure } ) => {
const colortone = vec3( color ).toVar();
const AgXInsetMatrix = mat3( vec3( 0.856627153315983, 0.137318972929847, 0.11189821299995 ), vec3( 0.0951212405381588, 0.761241990602591, 0.0767994186031903 ), vec3( 0.0482516061458583, 0.101439036467562, 0.811302368396859 ) );
const AgXOutsetMatrix = mat3( vec3( 1.1271005818144368, - 0.1413297634984383, - 0.14132976349843826 ), vec3( - 0.11060664309660323, 1.157823702216272, - 0.11060664309660294 ), vec3( - 0.016493938717834573, - 0.016493938717834257, 1.2519364065950405 ) );
const AgxMinEv = float( - 12.47393 );
const AgxMaxEv = float( 4.026069 );
colortone.mulAssign( exposure );
colortone.assign( LINEAR_SRGB_TO_LINEAR_REC2020.mul( colortone ) );
colortone.assign( AgXInsetMatrix.mul( colortone ) );
colortone.assign( max( colortone, 1e-10 ) );
colortone.assign( log2( colortone ) );
colortone.assign( colortone.sub( AgxMinEv ).div( AgxMaxEv.sub( AgxMinEv ) ) );
colortone.assign( clamp( colortone, 0.0, 1.0 ) );
colortone.assign( agxDefaultContrastApprox( colortone ) );
colortone.assign( AgXOutsetMatrix.mul( colortone ) );
colortone.assign( pow( max( vec3( 0.0 ), colortone ), vec3( 2.2 ) ) );
colortone.assign( LINEAR_REC2020_TO_LINEAR_SRGB.mul( colortone ) );
colortone.assign( clamp( colortone, 0.0, 1.0 ) );
return colortone;
} );
const toneMappingLib = {
[ LinearToneMapping ]: LinearToneMappingNode,
[ ReinhardToneMapping ]: ReinhardToneMappingNode,
[ CineonToneMapping ]: OptimizedCineonToneMappingNode,
[ ACESFilmicToneMapping ]: ACESFilmicToneMappingNode,
[ AgXToneMapping ]: AGXToneMappingNode
};
class ToneMappingNode extends TempNode {
constructor( toneMapping = NoToneMapping, exposureNode = toneMappingExposure, colorNode = null ) {
super( 'vec3' );
this.toneMapping = toneMapping;
this.exposureNode = exposureNode;
this.colorNode = colorNode;
}
getCacheKey() {
let cacheKey = super.getCacheKey();
cacheKey = '{toneMapping:' + this.toneMapping + ',nodes:' + cacheKey + '}';
return cacheKey;
}
setup( builder ) {
const colorNode = this.colorNode || builder.context.color;
const toneMapping = this.toneMapping;
if ( toneMapping === NoToneMapping ) return colorNode;
const toneMappingParams = { exposure: this.exposureNode, color: colorNode };
const toneMappingNode = toneMappingLib[ toneMapping ];
let outputNode = null;
if ( toneMappingNode ) {
outputNode = toneMappingNode( toneMappingParams );
} else {
console.error( 'ToneMappingNode: Unsupported Tone Mapping configuration.', toneMapping );
outputNode = colorNode;
}
return outputNode;
}
}
export default ToneMappingNode;
export const toneMapping = ( mapping, exposure, color ) => nodeObject( new ToneMappingNode( mapping, nodeObject( exposure ), nodeObject( color ) ) );
export const toneMappingExposure = rendererReference( 'toneMappingExposure', 'float' );
addNodeElement( 'toneMapping', ( color, mapping, exposure ) => toneMapping( mapping, exposure, color ) );
addNodeClass( 'ToneMappingNode', ToneMappingNode );

View File

@ -0,0 +1,97 @@
import Node, { addNodeClass } from '../core/Node.js';
import { nodeImmutable, nodeProxy } from '../shadernode/ShaderNode.js';
import { cameraNear, cameraFar } from '../accessors/CameraNode.js';
import { positionView } from '../accessors/PositionNode.js';
import { viewportDepthTexture } from './ViewportDepthTextureNode.js';
class ViewportDepthNode extends Node {
constructor( scope, valueNode = null ) {
super( 'float' );
this.scope = scope;
this.valueNode = valueNode;
this.isViewportDepthNode = true;
}
generate( builder ) {
const { scope } = this;
if ( scope === ViewportDepthNode.DEPTH_PIXEL ) {
return builder.getFragDepth();
}
return super.generate( builder );
}
setup( /*builder*/ ) {
const { scope } = this;
let node = null;
if ( scope === ViewportDepthNode.DEPTH ) {
node = viewZToOrthographicDepth( positionView.z, cameraNear, cameraFar );
} else if ( scope === ViewportDepthNode.DEPTH_TEXTURE ) {
const texture = this.valueNode || viewportDepthTexture();
const viewZ = perspectiveDepthToViewZ( texture, cameraNear, cameraFar );
node = viewZToOrthographicDepth( viewZ, cameraNear, cameraFar );
} else if ( scope === ViewportDepthNode.DEPTH_PIXEL ) {
if ( this.valueNode !== null ) {
node = depthPixelBase().assign( this.valueNode );
}
}
return node;
}
}
// NOTE: viewZ, the z-coordinate in camera space, is negative for points in front of the camera
// -near maps to 0; -far maps to 1
export const viewZToOrthographicDepth = ( viewZ, near, far ) => viewZ.add( near ).div( near.sub( far ) );
// maps orthographic depth in [ 0, 1 ] to viewZ
export const orthographicDepthToViewZ = ( depth, near, far ) => near.sub( far ).mul( depth ).sub( near );
// NOTE: https://twitter.com/gonnavis/status/1377183786949959682
// -near maps to 0; -far maps to 1
export const viewZToPerspectiveDepth = ( viewZ, near, far ) => near.add( viewZ ).mul( far ).div( far.sub( near ).mul( viewZ ) );
// maps perspective depth in [ 0, 1 ] to viewZ
export const perspectiveDepthToViewZ = ( depth, near, far ) => near.mul( far ).div( far.sub( near ).mul( depth ).sub( far ) );
ViewportDepthNode.DEPTH = 'depth';
ViewportDepthNode.DEPTH_TEXTURE = 'depthTexture';
ViewportDepthNode.DEPTH_PIXEL = 'depthPixel';
export default ViewportDepthNode;
const depthPixelBase = nodeProxy( ViewportDepthNode, ViewportDepthNode.DEPTH_PIXEL );
export const depth = nodeImmutable( ViewportDepthNode, ViewportDepthNode.DEPTH );
export const depthTexture = nodeProxy( ViewportDepthNode, ViewportDepthNode.DEPTH_TEXTURE );
export const depthPixel = nodeImmutable( ViewportDepthNode, ViewportDepthNode.DEPTH_PIXEL );
depthPixel.assign = ( value ) => depthPixelBase( value );
addNodeClass( 'ViewportDepthNode', ViewportDepthNode );

View File

@ -0,0 +1,31 @@
import ViewportTextureNode from './ViewportTextureNode.js';
import { addNodeClass } from '../core/Node.js';
import { addNodeElement, nodeProxy } from '../shadernode/ShaderNode.js';
import { viewportTopLeft } from './ViewportNode.js';
import { DepthTexture } from 'three';
let sharedDepthbuffer = null;
class ViewportDepthTextureNode extends ViewportTextureNode {
constructor( uvNode = viewportTopLeft, levelNode = null ) {
if ( sharedDepthbuffer === null ) {
sharedDepthbuffer = new DepthTexture();
}
super( uvNode, levelNode, sharedDepthbuffer );
}
}
export default ViewportDepthTextureNode;
export const viewportDepthTexture = nodeProxy( ViewportDepthTextureNode );
addNodeElement( 'viewportDepthTexture', viewportDepthTexture );
addNodeClass( 'ViewportDepthTextureNode', ViewportDepthTextureNode );

View File

@ -0,0 +1,136 @@
import Node, { addNodeClass } from '../core/Node.js';
import { NodeUpdateType } from '../core/constants.js';
import { uniform } from '../core/UniformNode.js';
import { nodeImmutable, vec2 } from '../shadernode/ShaderNode.js';
import { Vector2, Vector4 } from 'three';
let resolution, viewportResult;
class ViewportNode extends Node {
constructor( scope ) {
super();
this.scope = scope;
this.isViewportNode = true;
}
getNodeType() {
if ( this.scope === ViewportNode.VIEWPORT ) return 'vec4';
else if ( this.scope === ViewportNode.COORDINATE ) return 'vec3';
else return 'vec2';
}
getUpdateType() {
let updateType = NodeUpdateType.NONE;
if ( this.scope === ViewportNode.RESOLUTION || this.scope === ViewportNode.VIEWPORT ) {
updateType = NodeUpdateType.FRAME;
}
this.updateType = updateType;
return updateType;
}
update( { renderer } ) {
if ( this.scope === ViewportNode.VIEWPORT ) {
renderer.getViewport( viewportResult );
} else {
renderer.getDrawingBufferSize( resolution );
}
}
setup( /*builder*/ ) {
const scope = this.scope;
let output = null;
if ( scope === ViewportNode.RESOLUTION ) {
output = uniform( resolution || ( resolution = new Vector2() ) );
} else if ( scope === ViewportNode.VIEWPORT ) {
output = uniform( viewportResult || ( viewportResult = new Vector4() ) );
} else {
output = viewportCoordinate.div( viewportResolution );
let outX = output.x;
let outY = output.y;
if ( /bottom/i.test( scope ) ) outY = outY.oneMinus();
if ( /right/i.test( scope ) ) outX = outX.oneMinus();
output = vec2( outX, outY );
}
return output;
}
generate( builder ) {
if ( this.scope === ViewportNode.COORDINATE ) {
let coord = builder.getFragCoord();
if ( builder.isFlipY() ) {
// follow webgpu standards
const resolution = builder.getNodeProperties( viewportResolution ).outputNode.build( builder );
coord = `${ builder.getType( 'vec3' ) }( ${ coord }.x, ${ resolution }.y - ${ coord }.y, ${ coord }.z )`;
}
return coord;
}
return super.generate( builder );
}
}
ViewportNode.COORDINATE = 'coordinate';
ViewportNode.RESOLUTION = 'resolution';
ViewportNode.VIEWPORT = 'viewport';
ViewportNode.TOP_LEFT = 'topLeft';
ViewportNode.BOTTOM_LEFT = 'bottomLeft';
ViewportNode.TOP_RIGHT = 'topRight';
ViewportNode.BOTTOM_RIGHT = 'bottomRight';
export default ViewportNode;
export const viewportCoordinate = nodeImmutable( ViewportNode, ViewportNode.COORDINATE );
export const viewportResolution = nodeImmutable( ViewportNode, ViewportNode.RESOLUTION );
export const viewport = nodeImmutable( ViewportNode, ViewportNode.VIEWPORT );
export const viewportTopLeft = nodeImmutable( ViewportNode, ViewportNode.TOP_LEFT );
export const viewportBottomLeft = nodeImmutable( ViewportNode, ViewportNode.BOTTOM_LEFT );
export const viewportTopRight = nodeImmutable( ViewportNode, ViewportNode.TOP_RIGHT );
export const viewportBottomRight = nodeImmutable( ViewportNode, ViewportNode.BOTTOM_RIGHT );
addNodeClass( 'ViewportNode', ViewportNode );

View File

@ -0,0 +1,37 @@
import ViewportTextureNode from './ViewportTextureNode.js';
import { addNodeClass } from '../core/Node.js';
import { addNodeElement, nodeProxy } from '../shadernode/ShaderNode.js';
import { viewportTopLeft } from './ViewportNode.js';
import { FramebufferTexture } from 'three';
let _sharedFramebuffer = null;
class ViewportSharedTextureNode extends ViewportTextureNode {
constructor( uvNode = viewportTopLeft, levelNode = null ) {
if ( _sharedFramebuffer === null ) {
_sharedFramebuffer = new FramebufferTexture();
}
super( uvNode, levelNode, _sharedFramebuffer );
}
updateReference() {
return this;
}
}
export default ViewportSharedTextureNode;
export const viewportSharedTexture = nodeProxy( ViewportSharedTextureNode );
addNodeElement( 'viewportSharedTexture', viewportSharedTexture );
addNodeClass( 'ViewportSharedTextureNode', ViewportSharedTextureNode );

View File

@ -0,0 +1,78 @@
import TextureNode from '../accessors/TextureNode.js';
import { NodeUpdateType } from '../core/constants.js';
import { addNodeClass } from '../core/Node.js';
import { addNodeElement, nodeProxy } from '../shadernode/ShaderNode.js';
import { viewportTopLeft } from './ViewportNode.js';
import { Vector2, FramebufferTexture, LinearMipmapLinearFilter } from 'three';
const _size = new Vector2();
class ViewportTextureNode extends TextureNode {
constructor( uvNode = viewportTopLeft, levelNode = null, framebufferTexture = null ) {
if ( framebufferTexture === null ) {
framebufferTexture = new FramebufferTexture();
framebufferTexture.minFilter = LinearMipmapLinearFilter;
}
super( framebufferTexture, uvNode, levelNode );
this.generateMipmaps = false;
this.isOutputTextureNode = true;
this.updateBeforeType = NodeUpdateType.FRAME;
}
updateBefore( frame ) {
const renderer = frame.renderer;
renderer.getDrawingBufferSize( _size );
//
const framebufferTexture = this.value;
if ( framebufferTexture.image.width !== _size.width || framebufferTexture.image.height !== _size.height ) {
framebufferTexture.image.width = _size.width;
framebufferTexture.image.height = _size.height;
framebufferTexture.needsUpdate = true;
}
//
const currentGenerateMipmaps = framebufferTexture.generateMipmaps;
framebufferTexture.generateMipmaps = this.generateMipmaps;
renderer.copyFramebufferToTexture( framebufferTexture );
framebufferTexture.generateMipmaps = currentGenerateMipmaps;
}
clone() {
const viewportTextureNode = new this.constructor( this.uvNode, this.levelNode, this.value );
viewportTextureNode.generateMipmaps = this.generateMipmaps;
return viewportTextureNode;
}
}
export default ViewportTextureNode;
export const viewportTexture = nodeProxy( ViewportTextureNode );
export const viewportMipTexture = nodeProxy( ViewportTextureNode, null, null, { generateMipmaps: true } );
addNodeElement( 'viewportTexture', viewportTexture );
addNodeElement( 'viewportMipTexture', viewportMipTexture );
addNodeClass( 'ViewportTextureNode', ViewportTextureNode );