• 【Overload游戏引擎分析】画场景栅格的Shader分析


    Overload引擎地址: GitHub - adriengivry/Overload: 3D Game engine with editor

    一、栅格绘制基本原理

    Overload Editor启动之后,场景视图中有栅格线,这个在很多软件中都有。刚开始我猜测它应该是通过绘制线实现的。阅读代码发现,这个栅格的几何网格只有两个三角形面片组成的正方形,使用特殊Shader绘制出来的。

    绘制栅格的代码在EditorRenderer.cpp中,代码如下:

    1. void OvEditor::Core::EditorRenderer::RenderGrid(const OvMaths::FVector3& p_viewPos, const OvMaths::FVector3& p_color)
    2. {
    3. constexpr float gridSize = 5000.0f; // 栅格的总的大小
    4. FMatrix4 model = FMatrix4::Translation({ p_viewPos.x, 0.0f, p_viewPos.z }) * FMatrix4::Scaling({ gridSize * 2.0f, 1.f, gridSize * 2.0f }); // 栅格的模型矩阵
    5. m_gridMaterial.Set("u_Color", p_color); // 栅格的颜色
    6. m_context.renderer->DrawModelWithSingleMaterial(*m_context.editorResources->GetModel("Plane"), m_gridMaterial, &model); // 绘制栅格
    7. // 绘制坐标轴的三条线
    8. m_context.shapeDrawer->DrawLine(OvMaths::FVector3(-gridSize + p_viewPos.x, 0.0f, 0.0f), OvMaths::FVector3(gridSize + p_viewPos.x, 0.0f, 0.0f), OvMaths::FVector3(1.0f, 0.0f, 0.0f), 1.0f);
    9. m_context.shapeDrawer->DrawLine(OvMaths::FVector3(0.0f, -gridSize + p_viewPos.y, 0.0f), OvMaths::FVector3(0.0f, gridSize + p_viewPos.y, 0.0f), OvMaths::FVector3(0.0f, 1.0f, 0.0f), 1.0f);
    10. m_context.shapeDrawer->DrawLine(OvMaths::FVector3(0.0f, 0.0f, -gridSize + p_viewPos.z), OvMaths::FVector3(0.0f, 0.0f, gridSize + p_viewPos.z), OvMaths::FVector3(0.0f, 0.0f, 1.0f), 1.0f);
    11. }

    从中看出,先将面片平移到视点的前方,使得三角形始终在视锥体范围内,同时将三角形进行缩放,总的尺寸缩放到10000。然后使用m_gridMaterial材质进行绘制。所谓的材质就是Shader的封装。最后再绘制坐标轴的三条线。

    可以使用RenderDoc抓帧,可以验证确实是这么实现的。

    二、栅格绘制的Shader代码

    绘制栅格的Vertex Shader代码如下:

    1. #version 430 core
    2. layout (location = 0) in vec3 geo_Pos;
    3. layout (location = 1) in vec2 geo_TexCoords;
    4. layout (location = 2) in vec3 geo_Normal;
    5. layout (std140) uniform EngineUBO
    6. {
    7. mat4 ubo_Model;
    8. mat4 ubo_View;
    9. mat4 ubo_Projection;
    10. vec3 ubo_ViewPos;
    11. float ubo_Time;
    12. };
    13. out VS_OUT
    14. {
    15. vec3 FragPos;
    16. vec2 TexCoords;
    17. } vs_out;
    18. void main()
    19. {
    20. vs_out.FragPos = vec3(ubo_Model * vec4(geo_Pos, 1.0)); // 计算顶点世界坐标系坐标
    21. vs_out.TexCoords = vs_out.FragPos.xz; // 对应的纹理坐标,取对应的世界坐标
    22. gl_Position = ubo_Projection * ubo_View * vec4(vs_out.FragPos, 1.0); // 计算NDC坐标
    23. }

     Vertex Shader的代码相对较简单,有效的输入只有geo_Pos。EngineUBO是OpenGL的UBO变量,传入了模型、视图、投影矩阵。main方法中,计算了三角形的世界坐标系坐标、纹理坐标、输出gl_Position变量。

    Fragment Shader的代码如下:

    1. #version 430 core
    2. out vec4 FRAGMENT_COLOR;
    3. layout (std140) uniform EngineUBO
    4. {
    5. mat4 ubo_Model;
    6. mat4 ubo_View;
    7. mat4 ubo_Projection;
    8. vec3 ubo_ViewPos;
    9. float ubo_Time;
    10. };
    11. in VS_OUT
    12. {
    13. vec3 FragPos;
    14. vec2 TexCoords;
    15. } fs_in;
    16. uniform vec3 u_Color;
    17. float MAG(float p_lp)
    18. {
    19. const float lineWidth = 1.0f;
    20. const vec2 coord = fs_in.TexCoords / p_lp;
    21. const vec2 grid = abs(fract(coord - 0.5) - 0.5) / fwidth(coord);
    22. const float line = min(grid.x, grid.y);
    23. const float lineResult = lineWidth - min(line, lineWidth);
    24. return lineResult;
    25. }
    26. float Grid(float height, float a, float b, float c)
    27. {
    28. const float cl = MAG(a);
    29. const float ml = MAG(b);
    30. const float fl = MAG(c);
    31. const float cmit = 10.0f;
    32. const float cmet = 40.0f;
    33. const float mfit = 80.0f;
    34. const float mfet = 160.0f;
    35. const float df = clamp((height - cmit) / (cmet - cmit), 0.0f, 1.0f);
    36. const float dff = clamp((height - mfit) / (mfet - mfit), 0.0f, 1.0f);
    37. const float inl = mix(cl, ml, df);
    38. const float fnl = mix(inl, fl, dff);
    39. return fnl;
    40. }
    41. void main()
    42. {
    43. const float height = distance(ubo_ViewPos.y, fs_in.FragPos.y);
    44. const float gridA = Grid(height, 1.0f, 4.0f, 8.0f);
    45. const float gridB = Grid(height, 4.0f, 16.0f, 32.0f);
    46. const float grid = gridA * 0.5f + gridB;
    47. // const vec2 viewdirW = ubo_ViewPos.xz - fs_in.FragPos.xz;
    48. // const float viewdist = length(viewdirW);
    49. FRAGMENT_COLOR = vec4(u_Color, grid);
    50. }

    Fragment shader的代码没有看太明白,需要的时候再分析吧。

    三、绘制坐标轴线Shader

    相比之下,绘制坐标轴线的Shader就简单太多了。线的顶点使用两个uniform变量传入线的两个顶点,根据gl_VertexID判断使用哪个顶点。FS直接给出颜色。

    1. ############ Vertex Shader ###########
    2. #version 430 core
    3. uniform vec3 start;
    4. uniform vec3 end;
    5. uniform mat4 viewProjection;
    6. void main()
    7. {
    8. vec3 position = gl_VertexID == 0 ? start : end;
    9. gl_Position = viewProjection * vec4(position, 1.0);
    10. }
    11. ######## Fragment Shader #############
    12. #version 430 core
    13. uniform vec3 color;
    14. out vec4 FRAGMENT_COLOR;
    15. void main()
    16. {
    17. FRAGMENT_COLOR = vec4(color, 1.0);
    18. }

    对应CPU端的代码:

    1. void OvRendering::Core::ShapeDrawer::DrawLine(const OvMaths::FVector3& p_start, const OvMaths::FVector3& p_end, const OvMaths::FVector3& p_color, float p_lineWidth)
    2. {
    3. // 绑定line Shader
    4. m_lineShader->Bind();
    5. m_lineShader->SetUniformVec3("start", p_start); // 线的起点
    6. m_lineShader->SetUniformVec3("end", p_end); // 线的终点
    7. m_lineShader->SetUniformVec3("color", p_color); // 线的颜色
    8. // 绘制线
    9. m_renderer.SetRasterizationMode(OvRendering::Settings::ERasterizationMode::LINE);
    10. m_renderer.SetRasterizationLinesWidth(p_lineWidth);
    11. // 掉Draw call
    12. m_renderer.Draw(*m_lineMesh, Settings::EPrimitiveMode::LINES);
    13. m_renderer.SetRasterizationLinesWidth(1.0f);
    14. m_renderer.SetRasterizationMode(OvRendering::Settings::ERasterizationMode::FILL);
    15. m_lineShader->Unbind();
    16. }

    这里有个m_lineMesh对象,其包含两个随意的顶点即可,只是为了启动两次顶点着色器,真实的顶点坐标是靠uniform传入的。Overload将其全部初始化为0:

    1. std::vector vertices;
    2. vertices.push_back
    3. ({
    4. 0, 0, 0,// 坐标
    5. 0, 0, // 纹理
    6. 0, 0, 0,// 法线
    7. 0, 0, 0,
    8. 0, 0, 0
    9. });
    10. vertices.push_back
    11. ({
    12. 0, 0, 0,
    13. 0, 0,
    14. 0, 0, 0,
    15. 0, 0, 0,
    16. 0, 0, 0
    17. });
    18. m_lineMesh = new Resources::Mesh(vertices, { 0, 1 }, 0);
  • 相关阅读:
    windows2008+iis7+asp+php环境配置
    如何在macOS上使用最新版的Bison来构建项目,而不是Xcode工具链内嵌的2.3版本
    2021数学建模国赛B题复盘详细解析
    JavaScript学习_01——JavaScript简介
    数据库MySQL(二):DDL数据定义语言
    Hadoop的概述与安装
    Unity3D 常用得内置函数(Cg与GLSL)详解
    云原生存储解决方案
    思维导图在初中化学“物质构成的奥秘”教学中的应用
    STL vector的操作
  • 原文地址:https://blog.csdn.net/loveoobaby/article/details/133562704