Skip to content

Commit 54e3665

Browse files
authored
TSL: Add premult and unpremult (#31114)
* fix clear texture and facing away if `bounce` is `false` * add `premult`, `unpremult` * cleanup
1 parent 02d84d2 commit 54e3665

File tree

3 files changed

+45
-28
lines changed

3 files changed

+45
-28
lines changed

examples/jsm/tsl/display/GaussianBlurNode.js

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,10 @@
11
import { RenderTarget, Vector2, NodeMaterial, RendererUtils, QuadMesh, TempNode, NodeUpdateType } from 'three/webgpu';
2-
import { nodeObject, Fn, If, float, uv, uniform, convertToTexture, vec2, vec4, passTexture, mul } from 'three/tsl';
2+
import { nodeObject, Fn, float, uv, uniform, convertToTexture, vec2, vec4, passTexture, mul, premult, unpremult } from 'three/tsl';
33

44
const _quadMesh = /*@__PURE__*/ new QuadMesh();
55

66
let _rendererState;
77

8-
const premult = /*@__PURE__*/ Fn( ( [ color ] ) => {
9-
10-
return vec4( color.rgb.mul( color.a ), color.a );
11-
12-
} ).setLayout( {
13-
name: 'premult',
14-
type: 'vec4',
15-
inputs: [
16-
{ name: 'color', type: 'vec4' }
17-
]
18-
} );
19-
20-
const unpremult = /*@__PURE__*/ Fn( ( [ color ] ) => {
21-
22-
If( color.a.equal( 0.0 ), () => vec4( 0.0 ) );
23-
24-
return vec4( color.rgb.div( color.a ), color.a );
25-
26-
} ).setLayout( {
27-
name: 'unpremult',
28-
type: 'vec4',
29-
inputs: [
30-
{ name: 'color', type: 'vec4' }
31-
]
32-
} );
33-
348
/**
359
* Post processing node for creating a gaussian blur effect.
3610
*

src/Three.TSL.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ export const pow = TSL.pow;
385385
export const pow2 = TSL.pow2;
386386
export const pow3 = TSL.pow3;
387387
export const pow4 = TSL.pow4;
388+
export const premult = TSL.premult;
388389
export const property = TSL.property;
389390
export const radians = TSL.radians;
390391
export const rand = TSL.rand;
@@ -509,6 +510,7 @@ export const uniform = TSL.uniform;
509510
export const uniformArray = TSL.uniformArray;
510511
export const uniformGroup = TSL.uniformGroup;
511512
export const uniforms = TSL.uniforms;
513+
export const unpremult = TSL.unpremult;
512514
export const userData = TSL.userData;
513515
export const uv = TSL.uv;
514516
export const uvec2 = TSL.uvec2;

src/nodes/display/BlendModes.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Fn, vec4 } from '../tsl/TSLBase.js';
1+
import { Fn, If, vec4 } from '../tsl/TSLBase.js';
22
import { mix, min, step } from '../math/MathNode.js';
33

44
/**
@@ -130,6 +130,47 @@ export const blendColor = /*@__PURE__*/ Fn( ( [ base, blend ] ) => {
130130
]
131131
} );
132132

133+
/**
134+
* Premultiplies the RGB channels of a color by its alpha channel.
135+
*
136+
* This function is useful for converting a non-premultiplied alpha color
137+
* into a premultiplied alpha format, where the RGB values are scaled
138+
* by the alpha value. Premultiplied alpha is often used in graphics
139+
* rendering for certain operations, such as compositing and image processing.
140+
*
141+
* @tsl
142+
* @function
143+
* @param {Node<vec4>} color - The input color with non-premultiplied alpha.
144+
* @return {Node<vec4>} The color with premultiplied alpha.
145+
*/
146+
export const premult = /*@__PURE__*/ Fn( ( [ color ] ) => {
147+
148+
return vec4( color.rgb.mul( color.a ), color.a );
149+
150+
}, { color: 'vec4', return: 'vec4' } );
151+
152+
/**
153+
* Unpremultiplies the RGB channels of a color by its alpha channel.
154+
*
155+
* This function is useful for converting a premultiplied alpha color
156+
* back into a non-premultiplied alpha format, where the RGB values are
157+
* divided by the alpha value. Unpremultiplied alpha is often used in graphics
158+
* rendering for certain operations, such as compositing and image processing.
159+
*
160+
* @tsl
161+
* @function
162+
* @param {Node<vec4>} color - The input color with premultiplied alpha.
163+
* @return {Node<vec4>} The color with non-premultiplied alpha.
164+
*/
165+
export const unpremult = /*@__PURE__*/ Fn( ( [ color ] ) => {
166+
167+
If( color.a.equal( 0.0 ), () => vec4( 0.0 ) );
168+
169+
return vec4( color.rgb.div( color.a ), color.a );
170+
171+
}, { color: 'vec4', return: 'vec4' } );
172+
173+
133174
// Deprecated
134175

135176
/**

0 commit comments

Comments
 (0)