|
@@ -2,15 +2,15 @@ import UniformNode, { uniform } from '../core/UniformNode.js';
|
|
import { uv } from './UVNode.js';
|
|
import { uv } from './UVNode.js';
|
|
import { textureSize } from './TextureSizeNode.js';
|
|
import { textureSize } from './TextureSizeNode.js';
|
|
import { colorSpaceToLinear } from '../display/ColorSpaceNode.js';
|
|
import { colorSpaceToLinear } from '../display/ColorSpaceNode.js';
|
|
-import { context } from '../core/ContextNode.js';
|
|
|
|
import { expression } from '../code/ExpressionNode.js';
|
|
import { expression } from '../code/ExpressionNode.js';
|
|
import { addNodeClass } from '../core/Node.js';
|
|
import { addNodeClass } from '../core/Node.js';
|
|
|
|
+import { maxMipLevel } from '../utils/MaxMipLevelNode.js';
|
|
import { addNodeElement, nodeProxy, vec3, nodeObject } from '../shadernode/ShaderNode.js';
|
|
import { addNodeElement, nodeProxy, vec3, nodeObject } from '../shadernode/ShaderNode.js';
|
|
import { NodeUpdateType } from '../core/constants.js';
|
|
import { NodeUpdateType } from '../core/constants.js';
|
|
|
|
|
|
class TextureNode extends UniformNode {
|
|
class TextureNode extends UniformNode {
|
|
|
|
|
|
- constructor( value, uvNode = null, levelNode = null, compareNode = null ) {
|
|
|
|
|
|
+ constructor( value, uvNode = null, levelNode = null ) {
|
|
|
|
|
|
super( value );
|
|
super( value );
|
|
|
|
|
|
@@ -18,8 +18,10 @@ class TextureNode extends UniformNode {
|
|
|
|
|
|
this.uvNode = uvNode;
|
|
this.uvNode = uvNode;
|
|
this.levelNode = levelNode;
|
|
this.levelNode = levelNode;
|
|
- this.compareNode = compareNode;
|
|
|
|
|
|
+ this.compareNode = null;
|
|
|
|
+ this.depthNode = null;
|
|
|
|
|
|
|
|
+ this.sampler = true;
|
|
this.updateMatrix = false;
|
|
this.updateMatrix = false;
|
|
this.updateType = NodeUpdateType.NONE;
|
|
this.updateType = NodeUpdateType.NONE;
|
|
|
|
|
|
@@ -84,9 +86,9 @@ class TextureNode extends UniformNode {
|
|
|
|
|
|
let uvNode = this.uvNode;
|
|
let uvNode = this.uvNode;
|
|
|
|
|
|
- if ( ( uvNode === null || builder.context.forceUVContext === true ) && builder.context.getUVNode ) {
|
|
|
|
|
|
+ if ( ( uvNode === null || builder.context.forceUVContext === true ) && builder.context.getUV ) {
|
|
|
|
|
|
- uvNode = builder.context.getUVNode( this );
|
|
|
|
|
|
+ uvNode = builder.context.getUV( this );
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
@@ -102,41 +104,81 @@ class TextureNode extends UniformNode {
|
|
|
|
|
|
let levelNode = this.levelNode;
|
|
let levelNode = this.levelNode;
|
|
|
|
|
|
- if ( levelNode === null && builder.context.getSamplerLevelNode ) {
|
|
|
|
|
|
+ if ( levelNode === null && builder.context.getTextureLevel ) {
|
|
|
|
|
|
- levelNode = builder.context.getSamplerLevelNode( this );
|
|
|
|
|
|
+ levelNode = builder.context.getTextureLevel( this );
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if ( levelNode !== null && builder.context.getTextureLevelAlgorithm !== undefined ) {
|
|
|
|
+
|
|
|
|
+ levelNode = builder.context.getTextureLevelAlgorithm( this, levelNode );
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
//
|
|
//
|
|
|
|
|
|
properties.uvNode = uvNode;
|
|
properties.uvNode = uvNode;
|
|
- properties.levelNode = levelNode ? builder.context.getMIPLevelAlgorithmNode( this, levelNode ) : null;
|
|
|
|
|
|
+ properties.levelNode = levelNode;
|
|
|
|
+ properties.compareNode = this.compareNode;
|
|
|
|
+ properties.depthNode = this.depthNode;
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- generate( builder, output ) {
|
|
|
|
|
|
+ generateSnippet( builder, textureProperty, uvSnippet, levelSnippet, depthSnippet, compareSnippet ) {
|
|
|
|
|
|
- const properties = builder.getNodeProperties( this );
|
|
|
|
|
|
+ const texture = this.value;
|
|
|
|
|
|
- let { uvNode } = properties;
|
|
|
|
- const { levelNode } = properties;
|
|
|
|
|
|
+ let snippet;
|
|
|
|
|
|
- const compareNode = this.compareNode;
|
|
|
|
- const texture = this.value;
|
|
|
|
|
|
+ if ( levelSnippet ) {
|
|
|
|
|
|
- if ( ! texture || texture.isTexture !== true ) {
|
|
|
|
|
|
+ snippet = builder.generateTextureLevel( texture, textureProperty, uvSnippet, levelSnippet, depthSnippet );
|
|
|
|
|
|
- throw new Error( 'TextureNode: Need a three.js texture.' );
|
|
|
|
|
|
+ } else if ( compareSnippet ) {
|
|
|
|
+
|
|
|
|
+ snippet = builder.generateTextureCompare( texture, textureProperty, uvSnippet, compareSnippet, depthSnippet );
|
|
|
|
+
|
|
|
|
+ } else if ( this.sampler === false ) {
|
|
|
|
+
|
|
|
|
+ snippet = builder.generateTextureLoad( texture, textureProperty, uvSnippet, depthSnippet );
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+
|
|
|
|
+ snippet = builder.generateTexture( texture, textureProperty, uvSnippet, depthSnippet );
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ return snippet;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ generateUV( builder, uvNode ) {
|
|
|
|
+
|
|
|
|
+ const texture = this.value;
|
|
|
|
+
|
|
if ( builder.isFlipY() && ( texture.isFramebufferTexture === true || texture.isDepthTexture === true ) ) {
|
|
if ( builder.isFlipY() && ( texture.isFramebufferTexture === true || texture.isDepthTexture === true ) ) {
|
|
|
|
|
|
uvNode = uvNode.setY( uvNode.y.fract().oneMinus() );
|
|
uvNode = uvNode.setY( uvNode.y.fract().oneMinus() );
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ return uvNode.build( builder, this.sampler === true ? 'vec2' : 'uvec2' );
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ generate( builder, output ) {
|
|
|
|
+
|
|
|
|
+ const properties = builder.getNodeProperties( this );
|
|
|
|
+
|
|
|
|
+ const texture = this.value;
|
|
|
|
+
|
|
|
|
+ if ( ! texture || texture.isTexture !== true ) {
|
|
|
|
+
|
|
|
|
+ throw new Error( 'TextureNode: Need a three.js texture.' );
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
const textureProperty = super.generate( builder, 'property' );
|
|
const textureProperty = super.generate( builder, 'property' );
|
|
|
|
|
|
if ( output === 'sampler' ) {
|
|
if ( output === 'sampler' ) {
|
|
@@ -155,30 +197,18 @@ class TextureNode extends UniformNode {
|
|
|
|
|
|
if ( propertyName === undefined ) {
|
|
if ( propertyName === undefined ) {
|
|
|
|
|
|
- const uvSnippet = uvNode.build( builder, 'vec2' );
|
|
|
|
- const nodeVar = builder.getVarFromNode( this );
|
|
|
|
|
|
+ const { uvNode, levelNode, compareNode, depthNode } = properties;
|
|
|
|
|
|
- propertyName = builder.getPropertyName( nodeVar );
|
|
|
|
-
|
|
|
|
- let snippet = null;
|
|
|
|
-
|
|
|
|
- if ( levelNode && levelNode.isNode === true ) {
|
|
|
|
-
|
|
|
|
- const levelSnippet = levelNode.build( builder, 'float' );
|
|
|
|
-
|
|
|
|
- snippet = builder.getTextureLevel( texture, textureProperty, uvSnippet, levelSnippet );
|
|
|
|
|
|
+ const uvSnippet = this.generateUV( builder, uvNode );
|
|
|
|
+ const levelSnippet = levelNode ? levelNode.build( builder, 'float' ) : null;
|
|
|
|
+ const depthSnippet = depthNode ? depthNode.build( builder, 'uint' ) : null;
|
|
|
|
+ const compareSnippet = compareNode ? compareNode.build( builder, 'float' ) : null;
|
|
|
|
|
|
- } else if ( compareNode !== null ) {
|
|
|
|
-
|
|
|
|
- const compareSnippet = compareNode.build( builder, 'float' );
|
|
|
|
-
|
|
|
|
- snippet = builder.getTextureCompare( texture, textureProperty, uvSnippet, compareSnippet );
|
|
|
|
|
|
+ const nodeVar = builder.getVarFromNode( this );
|
|
|
|
|
|
- } else {
|
|
|
|
|
|
+ propertyName = builder.getPropertyName( nodeVar );
|
|
|
|
|
|
- snippet = builder.getTexture( texture, textureProperty, uvSnippet );
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
|
|
+ const snippet = this.generateSnippet( builder, textureProperty, uvSnippet, levelSnippet, depthSnippet, compareSnippet );
|
|
|
|
|
|
builder.addLineFlowCode( `${propertyName} = ${snippet}` );
|
|
builder.addLineFlowCode( `${propertyName} = ${snippet}` );
|
|
|
|
|
|
@@ -194,9 +224,9 @@ class TextureNode extends UniformNode {
|
|
let snippet = propertyName;
|
|
let snippet = propertyName;
|
|
const nodeType = this.getNodeType( builder );
|
|
const nodeType = this.getNodeType( builder );
|
|
|
|
|
|
- if ( builder.needsColorSpaceToLinear( this.value ) ) {
|
|
|
|
|
|
+ if ( builder.needsColorSpaceToLinear( texture ) ) {
|
|
|
|
|
|
- snippet = colorSpaceToLinear( expression( snippet, nodeType ), this.value.colorSpace ).setup( builder ).build( builder, nodeType );
|
|
|
|
|
|
+ snippet = colorSpaceToLinear( expression( snippet, nodeType ), texture.colorSpace ).setup( builder ).build( builder, nodeType );
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
@@ -206,6 +236,22 @@ class TextureNode extends UniformNode {
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ setSampler( value ) {
|
|
|
|
+
|
|
|
|
+ this.sampler = value;
|
|
|
|
+
|
|
|
|
+ return this;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ getSampler() {
|
|
|
|
+
|
|
|
|
+ return this.sampler;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // @TODO: Move to TSL
|
|
|
|
+
|
|
uv( uvNode ) {
|
|
uv( uvNode ) {
|
|
|
|
|
|
const textureNode = this.clone();
|
|
const textureNode = this.clone();
|
|
@@ -215,14 +261,21 @@ class TextureNode extends UniformNode {
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ blur( levelNode ) {
|
|
|
|
+
|
|
|
|
+ const textureNode = this.clone();
|
|
|
|
+ textureNode.levelNode = levelNode.mul( maxMipLevel( textureNode ) );
|
|
|
|
+
|
|
|
|
+ return nodeObject( textureNode );
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
level( levelNode ) {
|
|
level( levelNode ) {
|
|
|
|
|
|
const textureNode = this.clone();
|
|
const textureNode = this.clone();
|
|
textureNode.levelNode = levelNode;
|
|
textureNode.levelNode = levelNode;
|
|
|
|
|
|
- return context( textureNode, {
|
|
|
|
- getMIPLevelAlgorithmNode: ( reqTextureNode, levelNode ) => levelNode
|
|
|
|
- } );
|
|
|
|
|
|
+ return textureNode;
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
@@ -241,6 +294,17 @@ class TextureNode extends UniformNode {
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ depth( depthNode ) {
|
|
|
|
+
|
|
|
|
+ const textureNode = this.clone();
|
|
|
|
+ textureNode.depthNode = nodeObject( depthNode );
|
|
|
|
+
|
|
|
|
+ return nodeObject( textureNode );
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // --
|
|
|
|
+
|
|
serialize( data ) {
|
|
serialize( data ) {
|
|
|
|
|
|
super.serialize( data );
|
|
super.serialize( data );
|
|
@@ -271,7 +335,7 @@ class TextureNode extends UniformNode {
|
|
|
|
|
|
clone() {
|
|
clone() {
|
|
|
|
|
|
- return new this.constructor( this.value, this.uvNode, this.levelNode, this.compareNode );
|
|
|
|
|
|
+ return new this.constructor( this.value, this.uvNode, this.levelNode );
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|