From 4ed0e281b2cd8bb828a14a04b24aed959bf224c9 Mon Sep 17 00:00:00 2001 From: "HuYingzhuo(hugo/hyzboy)" Date: Fri, 13 Oct 2023 19:22:11 +0800 Subject: [PATCH] first run MetricCellsGrid OK! --- ShaderLibrary/Std3D/MetricCellsGrid.mtl | 84 ++++++++++++++ ShaderLibrary/Std3D/PureColor3D.mtl | 38 +++++++ example/Gizmo/CMakeLists.txt | 2 + example/Gizmo/MetricCellsGrid.cpp | 142 ++++++++++++++++++++++++ example/Gizmo/PlaneGrid3D.cpp | 2 +- example/Gizmo/RayPicking.cpp | 2 +- inc/hgl/graph/VKRenderResource.h | 2 +- inc/hgl/graph/VertexAttribDataAccess.h | 17 +++ 8 files changed, 286 insertions(+), 3 deletions(-) create mode 100644 ShaderLibrary/Std3D/MetricCellsGrid.mtl create mode 100644 ShaderLibrary/Std3D/PureColor3D.mtl create mode 100644 example/Gizmo/MetricCellsGrid.cpp diff --git a/ShaderLibrary/Std3D/MetricCellsGrid.mtl b/ShaderLibrary/Std3D/MetricCellsGrid.mtl new file mode 100644 index 00000000..882078dc --- /dev/null +++ b/ShaderLibrary/Std3D/MetricCellsGrid.mtl @@ -0,0 +1,84 @@ +#Material +Name MetricCellsGrid +Base Std3D +Reference https://www.shadertoy.com/view/wdSXzm + +#VertexInput +vec2 TexCoord + +#MaterialInstance +Length 120 +Stage Fragment +Code +{ + vec4 x_color; + vec4 y_color; + vec4 x_axis_color; + vec4 y_axis_color; + vec4 center_color; + + vec2 lum; //x=0.1 sub cell line,y=0.2 big cell line + vec2 cell_step; + vec2 big_cell_step; + vec2 scale; + + float axis_line_width; + float center_radius; +} + +#Vertex +Output +{ + vec2 TexCoord +} +Code +{ + void main() + { + HandoverMI(); + + Output.TexCoord=TexCoord; + + gl_Position=GetPosition3D(); + } +} + +#Fragment +Output +{ + vec4 FragColor; +} + +Code +{ + void main() + { + MaterialInstance mi=GetMI(); + + float edge=viewport.inv_viewport_resolution.y; + + float x=(Input.TexCoord.x-0.5)*mi.scale.x; + float y=(Input.TexCoord.y-0.5)*mi.scale.y; + + vec4 color=vec4(0,0,0,1); + + // ======= Lines + Bold lines + color.xyz += step(1.0 - 1.0 / mi.cell_step.x, fract(x / mi.cell_step.x )) * mi.x_color.rgb * mi.lum.x; + color.xyz += step(1.0 - 1.0 / mi.big_cell_step.x, fract(x / mi.big_cell_step.x)) * mi.x_color.rgb * mi.lum.y; + + color.xyz += step(1.0 - 1.0 / mi.cell_step.y, fract(y / mi.cell_step.y )) * mi.y_color.rgb * mi.lum.x; + color.xyz += step(1.0 - 1.0 / mi.big_cell_step.y, fract(y / mi.big_cell_step.y)) * mi.y_color.rgb * mi.lum.y; + + // ======= AXES + float xb = step(abs(x) - mi.axis_line_width, 0.0); + float yb = step(abs(y) - mi.axis_line_width, 0.0); + color.rgb = mix(color.rgb, mi.x_axis_color.rgb, (xb)); + color.rgb = mix(color.rgb, mi.y_axis_color.rgb, (yb)); + + // ======= CENTER + float cb = length(vec2(x,y))-mi.center_radius; + color.rgb = mix(color.rgb, mi.center_color.rgb, cb>0.0?0.0:smoothstep(0,edge*256.0,abs(cb))); + + FragColor=color; + } +} diff --git a/ShaderLibrary/Std3D/PureColor3D.mtl b/ShaderLibrary/Std3D/PureColor3D.mtl new file mode 100644 index 00000000..5171bc36 --- /dev/null +++ b/ShaderLibrary/Std3D/PureColor3D.mtl @@ -0,0 +1,38 @@ +#Material +Name PureColor3D +Base Std3D + +#MaterialInstance +Length 16 +Stage Fragment +Code +{ + vec4 Color; +} + +#Vertex +Code +{ + void main() + { + HandoverMI(); + + gl_Position=GetPosition3D(); + } +} + +#Fragment +Output +{ + vec4 FragColor; +} + +Code +{ + void main() + { + MaterialInstance mi=GetMI(); + + FragColor=mi.Color; + } +} diff --git a/example/Gizmo/CMakeLists.txt b/example/Gizmo/CMakeLists.txt index f1e70833..6839ad18 100644 --- a/example/Gizmo/CMakeLists.txt +++ b/example/Gizmo/CMakeLists.txt @@ -12,4 +12,6 @@ endmacro() CreateProject(01_PlaneGrid3D PlaneGrid3D.cpp) CreateProject(02_RayPicking RayPicking.cpp) +CreateProject(03_MetricCellsGrid MetricCellsGrid.cpp) + #CreateProject(03_BlenderAxis BlenderAxis.cpp) diff --git a/example/Gizmo/MetricCellsGrid.cpp b/example/Gizmo/MetricCellsGrid.cpp new file mode 100644 index 00000000..79a26727 --- /dev/null +++ b/example/Gizmo/MetricCellsGrid.cpp @@ -0,0 +1,142 @@ +// Metric Cells Grid + +#include"VulkanAppFramework.h" +#include +#include +#include +#include +#include +#include +#include + +using namespace hgl; +using namespace hgl::graph; + +struct MetricCellsGridData +{ + Color4f x_color; + Color4f y_color; + Color4f x_axis_color; + Color4f y_axis_color; + Color4f center_color; + + Vector2f lum; + Vector2f cell_step; + Vector2f big_cell_step; + Vector2f scale; + + float axis_line_width; + float center_radius; +}; + +constexpr const size_t MCG_SIZE=sizeof(MetricCellsGridData); + +constexpr const float PLANE_SIZE=512; + +class TestApp:public SceneAppFramework +{ +private: + + MetricCellsGridData mcg_data; + + Material * material =nullptr; + Pipeline * pipeline =nullptr; + + Primitive * ro_plane =nullptr; + MaterialInstance * material_instance =nullptr; + +private: + + bool InitMDP() + { + mtl::Material3DCreateConfig cfg(device->GetDeviceAttribute(),"MetricCellsGrid",Prim::Fan); + + cfg.local_to_world=true; + + material=db->LoadMaterial("Std3D/MetricCellsGrid",&cfg); + if(!material)return(false); + + { + mcg_data.x_color=Color4f(1,1,1,1); + mcg_data.y_color=Color4f(1,1,1,1); + + mcg_data.x_axis_color=GetColor4f(COLOR::BlenderAxisRed, 1.0); + mcg_data.y_axis_color=GetColor4f(COLOR::BlenderAxisGreen, 1.0); + + mcg_data.center_color=Color4f(1,1,0,1); + + mcg_data.lum =Vector2f(0.1,0.2); + mcg_data.cell_step =Vector2f(8,8); + mcg_data.big_cell_step =Vector2f(32,32); + mcg_data.scale =Vector2f(PLANE_SIZE,PLANE_SIZE); + + mcg_data.axis_line_width=1.0; + mcg_data.center_radius =4.0; + } + + material_instance=db->CreateMaterialInstance(material,nullptr,&mcg_data); + + pipeline=CreatePipeline(material,InlinePipeline::Solid3D,Prim::Fan); + + return pipeline; + } + + bool CreateRenderObject() + { + ro_plane=inline_geometry::CreatePlane(db,material->GetDefaultVIL()); + + return ro_plane; + } + + Renderable *Add(MaterialInstance *mi,const Matrix4f &mat) + { + Renderable *ri=db->CreateRenderable(ro_plane,mi,pipeline); + + if(!ri) + return(nullptr); + + render_root.CreateSubNode(mat,ri); + + return ri; + } + + bool InitScene() + { + Add(material_instance,scale(PLANE_SIZE,PLANE_SIZE,1)); + + camera->pos=Vector3f(PLANE_SIZE/2,PLANE_SIZE/2,PLANE_SIZE/2); + camera_control->SetTarget(Vector3f(0,0,0)); + camera_control->Refresh(); + +// camera_control->SetReserveDirection(true,true); //反转x,y + + render_root.RefreshMatrix(); + render_list->Expend(&render_root); + + return(true); + } + +public: + + bool Init(uint width,uint height) override + { + if(!SceneAppFramework::Init(width,height)) + return(false); + + if(!InitMDP()) + return(false); + + if(!CreateRenderObject()) + return(false); + + if(!InitScene()) + return(false); + + return(true); + } +};//class TestApp:public CameraAppFramework + +int main(int,char **) +{ + return RunApp(1280,720); +} diff --git a/example/Gizmo/PlaneGrid3D.cpp b/example/Gizmo/PlaneGrid3D.cpp index 50b405c4..9d18940b 100644 --- a/example/Gizmo/PlaneGrid3D.cpp +++ b/example/Gizmo/PlaneGrid3D.cpp @@ -69,7 +69,7 @@ private: return ro_plane_grid; } - + Renderable *Add(MaterialInstance *mi,const Matrix4f &mat) { Renderable *ri=db->CreateRenderable(ro_plane_grid,mi,pipeline); diff --git a/example/Gizmo/RayPicking.cpp b/example/Gizmo/RayPicking.cpp index 355d23b1..55988fc3 100644 --- a/example/Gizmo/RayPicking.cpp +++ b/example/Gizmo/RayPicking.cpp @@ -1,4 +1,4 @@ -// 18.RayPicking +// RayPicking #include"VulkanAppFramework.h" #include diff --git a/inc/hgl/graph/VKRenderResource.h b/inc/hgl/graph/VKRenderResource.h index 5e1ab10d..d5b74a26 100644 --- a/inc/hgl/graph/VKRenderResource.h +++ b/inc/hgl/graph/VKRenderResource.h @@ -136,7 +136,7 @@ public: //Material template MaterialInstance * CreateMaterialInstance(Material *mtl,const VILConfig *vil_cfg,const T *data) { - return CreateMaterialInstance(mtl,vil_cfg,*data,sizeof(T)); + return CreateMaterialInstance(mtl,vil_cfg,data,sizeof(T)); } MaterialInstance * CreateMaterialInstance(const mtl::MaterialCreateInfo *,const VILConfig *vil_cfg=nullptr); diff --git a/inc/hgl/graph/VertexAttribDataAccess.h b/inc/hgl/graph/VertexAttribDataAccess.h index 773363f2..db79cdad 100644 --- a/inc/hgl/graph/VertexAttribDataAccess.h +++ b/inc/hgl/graph/VertexAttribDataAccess.h @@ -303,6 +303,23 @@ namespace hgl return(true); } + bool Write(const T *v,const uint count) + { + if(!this->access||this->access+2>this->data_end) + { + LOG_HINT(OS_TEXT("VertexAttribDataAccess2::Write(T *) out")); + return(false); + } + + for(uint i=0;iaccess++=*v++; + *this->access++=*v++; + } + + return(true); + } + template bool Write(const V2 &v) {