From 8f03fa80fb29035e0d400cd0b5c4d0cfb92c5b4f Mon Sep 17 00:00:00 2001 From: ChestnutYueyue <952134128@qq.com> Date: Mon, 16 Mar 2026 20:38:03 +0800 Subject: [PATCH] =?UTF-8?q?refactor(shader):=20=E5=90=88=E5=B9=B6=E5=88=86?= =?UTF-8?q?=E7=A6=BB=E7=9A=84=E7=9D=80=E8=89=B2=E5=99=A8=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=B8=BA=E5=8D=95=E4=B8=80GLSL=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将原本分离的default.vert和default.frag文件合并为一个default.glsl文件, 使用`#type vertex`和`#type fragment`指令来区分着色器阶段。这简化了 着色器资源的管理,并保持了完全相同的渲染功能。 --- .../romfs/shader/default.frag | 40 ----------------- .../shader/{default.vert => default.glsl} | 43 +++++++++++++------ 2 files changed, 30 insertions(+), 53 deletions(-) delete mode 100644 examples/scene_graph_demo/romfs/shader/default.frag rename examples/scene_graph_demo/romfs/shader/{default.vert => default.glsl} (58%) diff --git a/examples/scene_graph_demo/romfs/shader/default.frag b/examples/scene_graph_demo/romfs/shader/default.frag deleted file mode 100644 index 4a235f0..0000000 --- a/examples/scene_graph_demo/romfs/shader/default.frag +++ /dev/null @@ -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; - } -} diff --git a/examples/scene_graph_demo/romfs/shader/default.vert b/examples/scene_graph_demo/romfs/shader/default.glsl similarity index 58% rename from examples/scene_graph_demo/romfs/shader/default.vert rename to examples/scene_graph_demo/romfs/shader/default.glsl index 95fca8b..8a2f9f5 100644 --- a/examples/scene_graph_demo/romfs/shader/default.vert +++ b/examples/scene_graph_demo/romfs/shader/default.glsl @@ -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; + } +}