refactor(shader): 合并分离的着色器文件为单一GLSL文件
将原本分离的default.vert和default.frag文件合并为一个default.glsl文件, 使用`#type vertex`和`#type fragment`指令来区分着色器阶段。这简化了 着色器资源的管理,并保持了完全相同的渲染功能。
This commit is contained in:
parent
81bc7ae030
commit
8f03fa80fb
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
|
#type vertex
|
||||||
#version 320 es
|
#version 320 es
|
||||||
precision highp float;
|
precision highp float;
|
||||||
|
|
||||||
// 全局 UBO (binding = 0) - 每帧更新一次
|
|
||||||
layout(std140, binding = 0) uniform GlobalUBO {
|
layout(std140, binding = 0) uniform GlobalUBO {
|
||||||
mat4 uViewProjection;
|
mat4 uViewProjection;
|
||||||
vec4 uCameraPosition;
|
vec4 uCameraPosition;
|
||||||
|
|
@ -10,39 +10,56 @@ layout(std140, binding = 0) uniform GlobalUBO {
|
||||||
vec2 uScreenSize;
|
vec2 uScreenSize;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 材质 UBO (binding = 1) - 每物体更新
|
|
||||||
layout(std140, binding = 1) uniform MaterialUBO {
|
layout(std140, binding = 1) uniform MaterialUBO {
|
||||||
vec4 uColor;
|
vec4 uColor;
|
||||||
vec4 uTintColor;
|
vec4 uTintColor;
|
||||||
float uOpacity;
|
float uOpacity;
|
||||||
float uPadding[3]; // std140 对齐填充
|
float uPadding[3];
|
||||||
};
|
};
|
||||||
|
|
||||||
// 模型矩阵作为单独的统一变量(每个物体设置)
|
|
||||||
uniform mat4 uModelMatrix;
|
uniform mat4 uModelMatrix;
|
||||||
|
|
||||||
// 顶点属性
|
|
||||||
layout(location = 0) in vec2 aPosition;
|
layout(location = 0) in vec2 aPosition;
|
||||||
layout(location = 1) in vec2 aTexCoord;
|
layout(location = 1) in vec2 aTexCoord;
|
||||||
layout(location = 2) in vec4 aColor;
|
layout(location = 2) in vec4 aColor;
|
||||||
|
|
||||||
// 输出到片段着色器
|
|
||||||
out vec2 vTexCoord;
|
out vec2 vTexCoord;
|
||||||
out vec4 vColor;
|
out vec4 vColor;
|
||||||
out vec4 vTintColor;
|
out vec4 vTintColor;
|
||||||
out float vOpacity;
|
out float vOpacity;
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 顶点着色器入口
|
|
||||||
*
|
|
||||||
* 计算顶点在裁剪空间中的位置,
|
|
||||||
* 并传递纹理坐标和颜色到片段着色器
|
|
||||||
*/
|
|
||||||
void main() {
|
void main() {
|
||||||
gl_Position = uViewProjection * uModelMatrix * vec4(aPosition, 0.0, 1.0);
|
gl_Position = uViewProjection * uModelMatrix * vec4(aPosition, 0.0, 1.0);
|
||||||
vTexCoord = aTexCoord;
|
vTexCoord = aTexCoord;
|
||||||
// 混合顶点颜色和材质 UBO 中的颜色
|
|
||||||
vColor = aColor * uColor;
|
vColor = aColor * uColor;
|
||||||
vTintColor = uTintColor;
|
vTintColor = uTintColor;
|
||||||
vOpacity = uOpacity;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue