Skip to content

Commit

Permalink
Merge pull request #794 from galacean/perf/sprite-alpha-clip
Browse files Browse the repository at this point in the history
perf: opt sprite shader alpha clip performance
  • Loading branch information
yiiqii authored Dec 19, 2024
2 parents 87e9213 + aa560aa commit 08b486d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
6 changes: 6 additions & 0 deletions packages/effects-core/src/components/base-render-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ export class BaseRenderComponent extends RendererComponent {
texParams.x = renderer.occlusion ? +(renderer.transparentOcclusion) : 1;
texParams.y = +this.preMultiAlpha;
texParams.z = renderer.renderMode;

if (texParams.x === 0) {
this.material.enableMacro('ALPHA_CLIP');
} else {
this.material.disableMacro('ALPHA_CLIP');
}
}

const attributes = {
Expand Down
6 changes: 3 additions & 3 deletions packages/effects-core/src/plugins/sprite/sprite-mesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ export function spriteMeshShaderFromRenderInfo (renderInfo: ItemRenderInfo, coun
});

shader.shared = true;
if (!wireframe) {
shader.cacheId = spriteMeshShaderIdFromRenderInfo(renderInfo, count);
}
// if (!wireframe) {
// shader.cacheId = spriteMeshShaderIdFromRenderInfo(renderInfo, count);
// }

return shader;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/effects-core/src/shader/item.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ void main() {
vec4 color = vec4(0.);
vec4 texColor = texture2D(_MainTex, vTexCoord.xy);
color = blendColor(texColor, vColor, floor(0.5 + vParams.y));

#ifdef ALPHA_CLIP
if(vParams.z == 0. && color.a < 0.04) { // 1/256 = 0.04
discard;
}
#endif
//color.rgb = pow(color.rgb, vec3(2.2));
color.a = clamp(color.a, 0.0, 1.0);
gl_FragColor = color;
Expand Down

0 comments on commit 08b486d

Please sign in to comment.