Skip to content

Commit 58b511e

Browse files
authored
TSL: Align blur filters. (#31557)
1 parent ff9b259 commit 58b511e

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

examples/jsm/tsl/display/boxBlur.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { Fn, vec2, uv, Loop, vec4, premultiplyAlpha, unpremultiplyAlpha, max, in
1919
* @param {Object} [options={}] - Additional options for the hash blur effect.
2020
* @param {Node<int>} [options.size=int(1)] - Controls the blur's kernel. For performant results, the range should within [1, 3].
2121
* @param {Node<int>} [options.separation=int(1)] - Spreads out the blur without having to sample additional fragments. Ranges from [1, Infinity].
22+
* @param {Node<vec4>} [options.mask=null] - A mask node to control the alpha blending of the blur.
2223
* @param {boolean} [options.premultipliedAlpha=false] - Whether to use premultiplied alpha for the blur effect.
2324
* @return {Node<vec4>} The blurred texture node.
2425
*/
@@ -27,11 +28,21 @@ export const boxBlur = /*#__PURE__*/ Fn( ( [ textureNode, options = {} ] ) => {
2728
textureNode = convertToTexture( textureNode );
2829
const size = nodeObject( options.size ) || int( 1 );
2930
const separation = nodeObject( options.separation ) || int( 1 );
31+
const mask = options.mask || null;
3032
const premultipliedAlpha = options.premultipliedAlpha || false;
3133

3234
const tap = ( uv ) => {
3335

34-
const sample = textureNode.sample( uv );
36+
let sample = textureNode.sample( uv );
37+
38+
if ( mask !== null ) {
39+
40+
const alpha = mask.sample( uv ).x;
41+
42+
sample = vec4( sample.rgb, sample.a.mul( alpha ) );
43+
44+
}
45+
3546
return premultipliedAlpha ? premultiplyAlpha( sample ) : sample;
3647

3748
};

examples/jsm/tsl/display/hashBlur.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { float, Fn, vec2, uv, sin, rand, degrees, cos, Loop, vec4, premultiplyAlpha, unpremultiplyAlpha } from 'three/tsl';
1+
import { float, Fn, vec2, uv, sin, rand, degrees, cos, Loop, vec4, premultiplyAlpha, unpremultiplyAlpha, convertToTexture, nodeObject } from 'three/tsl';
22

33
/**
44
* Applies a hash blur effect to the given texture node.
@@ -16,11 +16,11 @@ import { float, Fn, vec2, uv, sin, rand, degrees, cos, Loop, vec4, premultiplyAl
1616
*/
1717
export const hashBlur = /*#__PURE__*/ Fn( ( [ textureNode, bluramount = float( 0.1 ), options = {} ] ) => {
1818

19-
const {
20-
repeats = float( 45 ),
21-
mask = null,
22-
premultipliedAlpha = false
23-
} = options;
19+
textureNode = convertToTexture( textureNode );
20+
bluramount = nodeObject( bluramount );
21+
const repeats = nodeObject( options.size ) || float( 45 );
22+
const mask = options.mask || null;
23+
const premultipliedAlpha = options.premultipliedAlpha || false;
2424

2525
const draw = ( uv ) => {
2626

0 commit comments

Comments
 (0)