Skip to content

Commit

Permalink
TextureNode: Add biasNode. (#28786)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mugen87 authored Jul 2, 2024
1 parent 513ea12 commit 716c5fd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/nodes/accessors/TextureNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import { IntType, UnsignedIntType } from '../../constants.js';

class TextureNode extends UniformNode {

constructor( value, uvNode = null, levelNode = null ) {
constructor( value, uvNode = null, levelNode = null, biasNode = null ) {

super( value );

this.isTextureNode = true;

this.uvNode = uvNode;
this.levelNode = levelNode;
this.biasNode = biasNode;
this.compareNode = null;
this.depthNode = null;
this.gradNode = null;
Expand Down Expand Up @@ -168,6 +169,7 @@ class TextureNode extends UniformNode {

properties.uvNode = uvNode;
properties.levelNode = levelNode;
properties.biasNode = this.biasNode;
properties.compareNode = this.compareNode;
properties.gradNode = this.gradNode;
properties.depthNode = this.depthNode;
Expand All @@ -180,7 +182,7 @@ class TextureNode extends UniformNode {

}

generateSnippet( builder, textureProperty, uvSnippet, levelSnippet, depthSnippet, compareSnippet, gradSnippet ) {
generateSnippet( builder, textureProperty, uvSnippet, levelSnippet, biasSnippet, depthSnippet, compareSnippet, gradSnippet ) {

const texture = this.value;

Expand All @@ -190,6 +192,10 @@ class TextureNode extends UniformNode {

snippet = builder.generateTextureLevel( texture, textureProperty, uvSnippet, levelSnippet, depthSnippet );

} else if ( biasSnippet ) {

snippet = builder.generateTextureBias( texture, textureProperty, uvSnippet, biasSnippet, depthSnippet );

} else if ( gradSnippet ) {

snippet = builder.generateTextureGrad( texture, textureProperty, uvSnippet, gradSnippet, depthSnippet );
Expand Down Expand Up @@ -242,10 +248,11 @@ class TextureNode extends UniformNode {

if ( propertyName === undefined ) {

const { uvNode, levelNode, compareNode, depthNode, gradNode } = properties;
const { uvNode, levelNode, biasNode, compareNode, depthNode, gradNode } = properties;

const uvSnippet = this.generateUV( builder, uvNode );
const levelSnippet = levelNode ? levelNode.build( builder, 'float' ) : null;
const biasSnippet = biasNode ? biasNode.build( builder, 'float' ) : null;
const depthSnippet = depthNode ? depthNode.build( builder, 'int' ) : null;
const compareSnippet = compareNode ? compareNode.build( builder, 'float' ) : null;
const gradSnippet = gradNode ? [ gradNode[ 0 ].build( builder, 'vec2' ), gradNode[ 1 ].build( builder, 'vec2' ) ] : null;
Expand All @@ -254,7 +261,7 @@ class TextureNode extends UniformNode {

propertyName = builder.getPropertyName( nodeVar );

const snippet = this.generateSnippet( builder, textureProperty, uvSnippet, levelSnippet, depthSnippet, compareSnippet, gradSnippet );
const snippet = this.generateSnippet( builder, textureProperty, uvSnippet, levelSnippet, biasSnippet, depthSnippet, compareSnippet, gradSnippet );

builder.addLineFlowCode( `${propertyName} = ${snippet}` );

Expand Down
6 changes: 6 additions & 0 deletions src/renderers/webgl-fallback/nodes/GLSLNodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,12 @@ ${ flowData.code }

}

generateTextureBias( texture, textureProperty, uvSnippet, biasSnippet ) {

return `texture( ${ textureProperty }, ${ uvSnippet }, ${ biasSnippet } )`;

}

generateTextureGrad( texture, textureProperty, uvSnippet, gradSnippet ) {

return `textureGrad( ${ textureProperty }, ${ uvSnippet }, ${ gradSnippet[ 0 ] }, ${ gradSnippet[ 1 ] } )`;
Expand Down
14 changes: 14 additions & 0 deletions src/renderers/webgpu/nodes/WGSLNodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,20 @@ class WGSLNodeBuilder extends NodeBuilder {

}

generateTextureBias( texture, textureProperty, uvSnippet, biasSnippet, depthSnippet, shaderStage = this.shaderStage ) {

if ( shaderStage === 'fragment' ) {

return `textureSampleBias( ${ textureProperty }, ${ textureProperty }_sampler, ${ uvSnippet }, ${ biasSnippet } )`;

} else {

console.error( `WebGPURenderer: THREE.TextureNode.biasNode does not support ${ shaderStage } shader.` );

}

}

getPropertyName( node, shaderStage = this.shaderStage ) {

if ( node.isNodeVarying === true && node.needsInterpolation === true ) {
Expand Down

0 comments on commit 716c5fd

Please sign in to comment.