Extra2D/shader/default.frag

44 lines
1.0 KiB
GLSL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#version 320 es
precision highp float;
// 从顶点着色器输入
in vec2 vTexCoord;
in vec4 vColor;
// 纹理采样器
uniform sampler2D uTexture;
// 材质参数
uniform vec4 uTintColor;
uniform float uOpacity;
// 输出颜色
out vec4 fragColor;
/**
* @brief 片段着色器入口
*
* 采样纹理并与顶点颜色、色调和透明度混合
*/
void main() {
// 采样纹理如果没有绑定纹理texture 会返回 vec4(0,0,0,1) 或 vec4(1,1,1,1) 取决于实现)
vec4 texColor = texture(uTexture, vTexCoord);
// 如果纹理采样结果是黑色或透明,使用白色作为默认值
// 这样即使在没有纹理的情况下也能显示颜色
if (texColor.rgb == vec3(0.0) || texColor.a < 0.01) {
texColor = vec4(1.0, 1.0, 1.0, 1.0);
}
// 混合:纹理 * 顶点颜色 * 色调
fragColor = texColor * vColor * uTintColor;
// 应用透明度
fragColor.a *= uOpacity;
// Alpha 测试:丢弃几乎透明的像素
if (fragColor.a < 0.01) {
discard;
}
}