Skip to content

Commit f5b96f8

Browse files
authored
TSL: Rename premult to premultiplyAlpha (#31240)
* rename `premult`-> `premultiplyAlpha` * use native function * Update GaussianBlurNode.js * Update Three.TSL.js * Update SSAAPassNode.js
1 parent ee7fd45 commit f5b96f8

File tree

6 files changed

+15
-16
lines changed

6 files changed

+15
-16
lines changed

examples/jsm/tsl/display/GaussianBlurNode.js

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

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

@@ -249,8 +249,8 @@ class GaussianBlurNode extends TempNode {
249249

250250
// https://lisyarus.github.io/blog/posts/blur-coefficients-generator.html
251251

252-
sampleTexture = ( uv ) => premult( textureNode.sample( uv ) );
253-
output = ( color ) => unpremult( color );
252+
sampleTexture = ( uv ) => premultiplyAlpha( textureNode.sample( uv ) );
253+
output = ( color ) => unpremultiplyAlpha( color );
254254

255255
} else {
256256

examples/jsm/tsl/display/SSAAPassNode.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AdditiveBlending, Color, Vector2, RendererUtils, PassNode, QuadMesh, NodeMaterial } from 'three/webgpu';
2-
import { nodeObject, uniform, mrt, texture, getTextureIndex } from 'three/tsl';
2+
import { nodeObject, uniform, mrt, texture, getTextureIndex, unpremultiplyAlpha } from 'three/tsl';
33

44
const _size = /*@__PURE__*/ new Vector2();
55

@@ -277,11 +277,11 @@ class SSAAPassNode extends PassNode {
277277
}
278278

279279
this._quadMesh.material = new NodeMaterial();
280-
this._quadMesh.material.fragmentNode = sampleTexture;
280+
this._quadMesh.material.fragmentNode = unpremultiplyAlpha( sampleTexture );
281281
this._quadMesh.material.transparent = true;
282282
this._quadMesh.material.depthTest = false;
283283
this._quadMesh.material.depthWrite = false;
284-
//this._quadMesh.material.premultipliedAlpha = true;
284+
this._quadMesh.material.premultipliedAlpha = true;
285285
this._quadMesh.material.blending = AdditiveBlending;
286286
this._quadMesh.material.name = 'SSAA';
287287

examples/jsm/tsl/display/hashBlur.js

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

33
/**
44
* Applies a hash blur effect to the given texture node.
@@ -34,7 +34,7 @@ export const hashBlur = /*#__PURE__*/ Fn( ( [ textureNode, bluramount = float( 0
3434

3535
}
3636

37-
return premultipliedAlpha ? premult( sample ) : sample;
37+
return premultipliedAlpha ? premultiplyAlpha( sample ) : sample;
3838

3939
};
4040

@@ -51,6 +51,6 @@ export const hashBlur = /*#__PURE__*/ Fn( ( [ textureNode, bluramount = float( 0
5151

5252
blurred_image.divAssign( repeats );
5353

54-
return premultipliedAlpha ? unpremult( blurred_image ) : blurred_image;
54+
return premultipliedAlpha ? unpremultiplyAlpha( blurred_image ) : blurred_image;
5555

5656
} );

src/Three.TSL.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ export const pow = TSL.pow;
386386
export const pow2 = TSL.pow2;
387387
export const pow3 = TSL.pow3;
388388
export const pow4 = TSL.pow4;
389-
export const premult = TSL.premult;
389+
export const premultiplyAlpha = TSL.premultiplyAlpha;
390390
export const property = TSL.property;
391391
export const radians = TSL.radians;
392392
export const rand = TSL.rand;
@@ -511,7 +511,7 @@ export const uniformArray = TSL.uniformArray;
511511
export const uniformGroup = TSL.uniformGroup;
512512
export const uniformTexture = TSL.uniformTexture;
513513
export const uniforms = TSL.uniforms;
514-
export const unpremult = TSL.unpremult;
514+
export const unpremultiplyAlpha = TSL.unpremultiplyAlpha;
515515
export const userData = TSL.userData;
516516
export const uv = TSL.uv;
517517
export const uvec2 = TSL.uvec2;

src/materials/nodes/NodeMaterial.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import NodeMaterialObserver from './manager/NodeMaterialObserver.js';
2424
import getAlphaHashThreshold from '../../nodes/functions/material/getAlphaHashThreshold.js';
2525
import { modelViewMatrix } from '../../nodes/accessors/ModelNode.js';
2626
import { vertexColor } from '../../nodes/accessors/VertexColorNode.js';
27+
import { premultiplyAlpha } from '../../nodes/display/BlendModes.js';
2728

2829
/**
2930
* Base class for all node materials.
@@ -1086,9 +1087,7 @@ class NodeMaterial extends Material {
10861087
*/
10871088
setupPremultipliedAlpha( builder, outputNode ) {
10881089

1089-
outputNode = vec4( outputNode.rgb.mul( outputNode.a ), outputNode.a );
1090-
1091-
return outputNode;
1090+
return premultiplyAlpha( outputNode );
10921091

10931092
}
10941093

src/nodes/display/BlendModes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export const blendColor = /*@__PURE__*/ Fn( ( [ base, blend ] ) => {
143143
* @param {Node<vec4>} color - The input color with non-premultiplied alpha.
144144
* @return {Node<vec4>} The color with premultiplied alpha.
145145
*/
146-
export const premult = /*@__PURE__*/ Fn( ( [ color ] ) => {
146+
export const premultiplyAlpha = /*@__PURE__*/ Fn( ( [ color ] ) => {
147147

148148
return vec4( color.rgb.mul( color.a ), color.a );
149149

@@ -162,7 +162,7 @@ export const premult = /*@__PURE__*/ Fn( ( [ color ] ) => {
162162
* @param {Node<vec4>} color - The input color with premultiplied alpha.
163163
* @return {Node<vec4>} The color with non-premultiplied alpha.
164164
*/
165-
export const unpremult = /*@__PURE__*/ Fn( ( [ color ] ) => {
165+
export const unpremultiplyAlpha = /*@__PURE__*/ Fn( ( [ color ] ) => {
166166

167167
If( color.a.equal( 0.0 ), () => vec4( 0.0 ) );
168168

0 commit comments

Comments
 (0)