Skip to content

Commit b8c03ae

Browse files
authored
TSL: Introduce textureBicubicLevel() (#31288)
* fix texture uv reference * add `textureBicubicLevel` * cleanup * cleanup
1 parent 1c78abf commit b8c03ae

File tree

5 files changed

+45
-10
lines changed

5 files changed

+45
-10
lines changed

src/Three.TSL.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ export const texture = TSL.texture;
477477
export const texture3D = TSL.texture3D;
478478
export const textureBarrier = TSL.textureBarrier;
479479
export const textureBicubic = TSL.textureBicubic;
480+
export const textureBicubicLevel = TSL.textureBicubicLevel;
480481
export const textureCubeUV = TSL.textureCubeUV;
481482
export const textureLoad = TSL.textureLoad;
482483
export const textureSize = TSL.textureSize;

src/nodes/accessors/TextureBicubic.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { add, mul, div } from '../math/OperatorNode.js';
22
import { floor, ceil, fract, pow } from '../math/MathNode.js';
3-
import { Fn, float, vec2, vec4, int } from '../tsl/TSLBase.js';
3+
import { Fn, vec2, vec4, int } from '../tsl/TSLBase.js';
4+
import { maxMipLevel } from '../utils/MaxMipLevelNode.js';
45

56
// Mipped Bicubic Texture Filtering by N8
67
// https://www.shadertoy.com/view/Dl2SDW
@@ -57,10 +58,10 @@ const bicubic = ( textureNode, texelSize, lod ) => {
5758
* @tsl
5859
* @function
5960
* @param {TextureNode} textureNode - The texture node that should be filtered.
60-
* @param {Node<float>} [lodNode=float(3)] - Defines the LOD to sample from.
61+
* @param {Node<float>} lodNode - Defines the LOD to sample from.
6162
* @return {Node} The filtered texture sample.
6263
*/
63-
export const textureBicubic = /*@__PURE__*/ Fn( ( [ textureNode, lodNode = float( 3 ) ] ) => {
64+
export const textureBicubicLevel = /*@__PURE__*/ Fn( ( [ textureNode, lodNode ] ) => {
6465

6566
const fLodSize = vec2( textureNode.size( int( lodNode ) ) );
6667
const cLodSize = vec2( textureNode.size( int( lodNode.add( 1.0 ) ) ) );
@@ -72,3 +73,20 @@ export const textureBicubic = /*@__PURE__*/ Fn( ( [ textureNode, lodNode = float
7273
return fract( lodNode ).mix( fSample, cSample );
7374

7475
} );
76+
77+
/**
78+
* Applies mipped bicubic texture filtering to the given texture node.
79+
*
80+
* @tsl
81+
* @function
82+
* @param {TextureNode} textureNode - The texture node that should be filtered.
83+
* @param {Node<float>} [strength] - Defines the strength of the bicubic filtering.
84+
* @return {Node} The filtered texture sample.
85+
*/
86+
export const textureBicubic = /*@__PURE__*/ Fn( ( [ textureNode, strength ] ) => {
87+
88+
const lod = strength.mul( maxMipLevel( textureNode ) );
89+
90+
return textureBicubicLevel( textureNode, lod );
91+
92+
} );

src/nodes/display/PassNode.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,16 @@ class PassMultipleTextureNode extends PassTextureNode {
126126

127127
clone() {
128128

129-
return new this.constructor( this.passNode, this.textureName, this.previousTexture );
129+
const newNode = new this.constructor( this.passNode, this.textureName, this.previousTexture );
130+
newNode.uvNode = this.uvNode;
131+
newNode.levelNode = this.levelNode;
132+
newNode.biasNode = this.biasNode;
133+
newNode.sampler = this.sampler;
134+
newNode.depthNode = this.depthNode;
135+
newNode.compareNode = this.compareNode;
136+
newNode.gradNode = this.gradNode;
137+
138+
return newNode;
130139

131140
}
132141

src/nodes/functions/PhysicalLightingModel.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { cameraPosition, cameraProjectionMatrix, cameraViewMatrix } from '../acc
1818
import { modelWorldMatrix } from '../accessors/ModelNode.js';
1919
import { screenSize } from '../display/ScreenNode.js';
2020
import { viewportMipTexture } from '../display/ViewportTextureNode.js';
21-
import { textureBicubic } from '../accessors/TextureBicubic.js';
21+
import { textureBicubicLevel } from '../accessors/TextureBicubic.js';
2222
import { Loop } from '../utils/LoopNode.js';
2323
import { BackSide } from '../../constants.js';
2424

@@ -80,7 +80,7 @@ const getTransmissionSample = /*@__PURE__*/ Fn( ( [ fragCoord, roughness, ior ],
8080

8181
const lod = log2( screenSize.x ).mul( applyIorToRoughness( roughness, ior ) );
8282

83-
return textureBicubic( transmissionSample, lod );
83+
return textureBicubicLevel( transmissionSample, lod );
8484

8585
} );
8686

src/nodes/utils/ReflectorNode.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,17 @@ class ReflectorNode extends TextureNode {
154154

155155
clone() {
156156

157-
const texture = new this.constructor( this.reflectorNode );
158-
texture._reflectorBaseNode = this._reflectorBaseNode;
159-
160-
return texture;
157+
const newNode = new this.constructor( this.reflectorNode );
158+
newNode.uvNode = this.uvNode;
159+
newNode.levelNode = this.levelNode;
160+
newNode.biasNode = this.biasNode;
161+
newNode.sampler = this.sampler;
162+
newNode.depthNode = this.depthNode;
163+
newNode.compareNode = this.compareNode;
164+
newNode.gradNode = this.gradNode;
165+
newNode._reflectorBaseNode = this._reflectorBaseNode;
166+
167+
return newNode;
161168

162169
}
163170

0 commit comments

Comments
 (0)