refactor(shader): 合并分离的着色器文件为单一GLSL文件

将原本分离的default.vert和default.frag文件合并为一个default.glsl文件,
使用`#type vertex`和`#type fragment`指令来区分着色器阶段。这简化了
着色器资源的管理,并保持了完全相同的渲染功能。
This commit is contained in:
ChestnutYueyue 2026-03-16 20:38:03 +08:00
parent 81bc7ae030
commit 8f03fa80fb
2 changed files with 30 additions and 53 deletions

View File

@ -1,40 +0,0 @@
#version 320 es
precision highp float;
// 从顶点着色器输入
in vec2 vTexCoord;
in vec4 vColor;
in vec4 vTintColor;
in float vOpacity;
// 纹理采样器
uniform sampler2D uTexture;
// 输出颜色
out vec4 fragColor;
/**
* @brief
*
*
*/
void main() {
// 采样纹理
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 * vTintColor;
// 应用透明度
fragColor.a *= vOpacity;
// Alpha 测试:丢弃几乎透明的像素
if (fragColor.a < 0.01) {
discard;
}
}

View File

@ -1,7 +1,7 @@
#type vertex
#version 320 es
precision highp float;
// 全局 UBO (binding = 0) - 每帧更新一次
layout(std140, binding = 0) uniform GlobalUBO {
mat4 uViewProjection;
vec4 uCameraPosition;
@ -10,39 +10,56 @@ layout(std140, binding = 0) uniform GlobalUBO {
vec2 uScreenSize;
};
// 材质 UBO (binding = 1) - 每物体更新
layout(std140, binding = 1) uniform MaterialUBO {
vec4 uColor;
vec4 uTintColor;
float uOpacity;
float uPadding[3]; // std140 对齐填充
float uPadding[3];
};
// 模型矩阵作为单独的统一变量(每个物体设置)
uniform mat4 uModelMatrix;
// 顶点属性
layout(location = 0) in vec2 aPosition;
layout(location = 1) in vec2 aTexCoord;
layout(location = 2) in vec4 aColor;
// 输出到片段着色器
out vec2 vTexCoord;
out vec4 vColor;
out vec4 vTintColor;
out float vOpacity;
/**
* @brief 顶点着色器入口
*
* 计算顶点在裁剪空间中的位置,
* 并传递纹理坐标和颜色到片段着色器
*/
void main() {
gl_Position = uViewProjection * uModelMatrix * vec4(aPosition, 0.0, 1.0);
vTexCoord = aTexCoord;
// 混合顶点颜色和材质 UBO 中的颜色
vColor = aColor * uColor;
vTintColor = uTintColor;
vOpacity = uOpacity;
}
#type fragment
#version 320 es
precision highp float;
in vec2 vTexCoord;
in vec4 vColor;
in vec4 vTintColor;
in float vOpacity;
uniform sampler2D uTexture;
out vec4 fragColor;
void main() {
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 * vTintColor;
fragColor.a *= vOpacity;
if (fragColor.a < 0.01) {
discard;
}
}