|
1 | | -import { Fn, vec4 } from '../tsl/TSLBase.js'; |
| 1 | +import { Fn, If, vec4 } from '../tsl/TSLBase.js'; |
2 | 2 | import { mix, min, step } from '../math/MathNode.js'; |
3 | 3 |
|
4 | 4 | /** |
@@ -130,6 +130,47 @@ export const blendColor = /*@__PURE__*/ Fn( ( [ base, blend ] ) => { |
130 | 130 | ] |
131 | 131 | } ); |
132 | 132 |
|
| 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 | + |
133 | 174 | // Deprecated |
134 | 175 |
|
135 | 176 | /** |
|
0 commit comments