Skip to content

Commit 72a887f

Browse files
authored
TSL: Add hashBlur() options {repeats,mask,premultipliedAlpha} (#31115)
* fix clear texture and facing away if `bounce` is `false` * add `premult`, `unpremult` * cleanup * add `hashBlur` options { mask, premultipliedAlpha } * add docs and cleanup
1 parent 54e3665 commit 72a887f

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

examples/jsm/tsl/display/hashBlur.js

Lines changed: 28 additions & 5 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 } from 'three/tsl';
1+
import { float, Fn, vec2, uv, sin, rand, degrees, cos, Loop, vec4, premult, unpremult } from 'three/tsl';
22

33
/**
44
* Applies a hash blur effect to the given texture node.
@@ -8,12 +8,35 @@ import { float, Fn, vec2, uv, sin, rand, degrees, cos, Loop, vec4 } from 'three/
88
* @function
99
* @param {Node<vec4>} textureNode - The texture node that should be blurred.
1010
* @param {Node<float>} [bluramount=float(0.1)] - This node determines the amount of blur.
11-
* @param {Node<float>} [repeats=float(45)] - This node determines the quality of the blur. A higher value produces a less grainy result but is also more expensive.
11+
* @param {Object} [options={}] - Additional options for the hash blur effect.
12+
* @param {Node<float>} [options.repeats=float(45)] - The number of iterations for the blur effect.
13+
* @param {Node<vec4>} [options.mask=null] - A mask node to control the alpha blending of the blur.
14+
* @param {boolean} [options.premultipliedAlpha=false] - Whether to use premultiplied alpha for the blur effect.
1215
* @return {Node<vec4>} The blurred texture node.
1316
*/
14-
export const hashBlur = /*#__PURE__*/ Fn( ( [ textureNode, bluramount = float( 0.1 ), repeats = float( 45 ) ] ) => {
17+
export const hashBlur = /*#__PURE__*/ Fn( ( [ textureNode, bluramount = float( 0.1 ), options = {} ] ) => {
1518

16-
const draw = ( uv ) => textureNode.sample( uv );
19+
const {
20+
repeats = float( 45 ),
21+
mask = null,
22+
premultipliedAlpha = false
23+
} = options;
24+
25+
const draw = ( uv ) => {
26+
27+
let sample = textureNode.sample( uv );
28+
29+
if ( mask !== null ) {
30+
31+
const alpha = mask.sample( uv ).x;
32+
33+
sample = vec4( sample.rgb, sample.a.mul( alpha ) );
34+
35+
}
36+
37+
return premultipliedAlpha ? premult( sample ) : sample;
38+
39+
};
1740

1841
const targetUV = textureNode.uvNode || uv();
1942
const blurred_image = vec4( 0. ).toVar();
@@ -28,6 +51,6 @@ export const hashBlur = /*#__PURE__*/ Fn( ( [ textureNode, bluramount = float( 0
2851

2952
blurred_image.divAssign( repeats );
3053

31-
return blurred_image;
54+
return premultipliedAlpha ? unpremult( blurred_image ) : blurred_image;
3255

3356
} );

0 commit comments

Comments
 (0)