diff --git a/ShaderLibrary/Std3D/BlinnPhong/BlinnPhongSun.ubo b/ShaderLibrary/Std3D/BlinnPhong/BlinnPhongSun.ubo index 9eda1481..c1c92cdf 100644 --- a/ShaderLibrary/Std3D/BlinnPhong/BlinnPhongSun.ubo +++ b/ShaderLibrary/Std3D/BlinnPhong/BlinnPhongSun.ubo @@ -1,2 +1,2 @@ -vec3 direction; -vec3 color; +vec4 direction; +vec4 color; diff --git a/ShaderLibrary/Std3D/BlinnPhong/SunLightPureColor.mtl b/ShaderLibrary/Std3D/BlinnPhong/SunLightPureColor.mtl index f93bd9b8..3b1b4426 100644 --- a/ShaderLibrary/Std3D/BlinnPhong/SunLightPureColor.mtl +++ b/ShaderLibrary/Std3D/BlinnPhong/SunLightPureColor.mtl @@ -14,11 +14,17 @@ UBO File BlinnPhongSun.ubo //文件名,如果/开头表示从ShaderLibrary根目录开始,没有则表示同一目录 Struct BlinnPhongSun //结构名称 Name sun //在代码中的变量名 - Stage Vertex,Fragment //会引用的shader + Stage Fragment //会引用的shader Set Global //Descriptor Set } #MaterialInstance +Length 16 +Stage Fragment +Code +{ + vec4 Color; +} #VertexInput vec3 Normal @@ -33,6 +39,7 @@ Code { void main() { + HandoverMI(); gl_Position=GetPosition3D(); } } @@ -47,31 +54,33 @@ Code { void main() { + MaterialInstance mi=GetMI(); + //将法线归一化 vec3 world_normal =normalize(Input.Normal); //对世界坐标下的灯光方法归一化 - vec3 world_light_direction =normalize(sun.direction); + vec3 world_light_direction =normalize(sun.direction.xyz); //点乘法线和光照 - vec3 diffuse =0.5*dot(world_light_direction,world_normal)+0.5; + vec3 diffuse =vec3(0.5)*dot(world_light_direction.xyz,world_normal)+vec3(0.5); //直接光颜色 - vec3 direct_color =sun.diffuse*diffuse*sun.color; + vec3 direct_color =sun.color.rgb*mi.Color.rgb; - #ifndef HAVE_SPECULAR +// #ifndef HAVE_SPECULAR FragColor=vec4(direct_color,1.0); - #else - //归一代视角方向 - vec3 view_direction =normalize(camera.pos-world_position); - - //世界坐标下的反射光方向 - vec3 reflect_direction =normalize(reflect(-world_light_direction,world_normal)); - - //高光 - vec3 specular =sun.specular*pow(saturate(dot(reflect_direction,view_direction)),gloss); - - FragColor=vec4(direct_color+specular,1.0); - #endif//HAVE_SPECULAR +// #else +// //归一代视角方向 +// vec3 view_direction =normalize(camera.pos-world_position); +// +// //世界坐标下的反射光方向 +// vec3 reflect_direction =normalize(reflect(-world_light_direction,world_normal)); +// +// //高光 +// vec3 specular =sun.specular*pow(saturate(dot(reflect_direction,view_direction)),gloss); +// +// FragColor=vec4(direct_color+specular,1.0); +// #endif//HAVE_SPECULAR } } diff --git a/example/LightBasic/BlinnPhongDirectionLight.cpp b/example/LightBasic/BlinnPhongDirectionLight.cpp index 79054c9d..6322f7ae 100644 --- a/example/LightBasic/BlinnPhongDirectionLight.cpp +++ b/example/LightBasic/BlinnPhongDirectionLight.cpp @@ -20,8 +20,8 @@ static Color4f white_color(1,1,1,1); static mtl::blinnphong::SunLight sun_light= { - Vector3f(1,1,1), - Vector3f(1,0.975,0.95) + Vector4f(1,1,1,0), + Vector4f(1,0,0,1) }; class TestApp:public SceneAppFramework @@ -68,8 +68,6 @@ private: bool CreateBlinnPhongUBO() { - sun_light.color=Vector3f(1,1,1); - ubo_sun=db->CreateUBO("sun",sizeof(sun_light),&sun_light); if(!ubo_sun)return(false); @@ -86,8 +84,9 @@ private: if(!mtl_sun_light)return(false); mtl_sun_light->BindUBO(DescriptorSetType::Global,"sun",ubo_sun); + mtl_sun_light->Update(); - mi_sphere=db->CreateMaterialInstance(mtl_sun_light); + mi_sphere=db->CreateMaterialInstance(mtl_sun_light,nullptr,&white_color); if(!mi_sphere)return(false); p_sphere=CreatePipeline(mtl_sun_light,InlinePipeline::Solid3D,Prim::Triangles); @@ -162,6 +161,9 @@ public: if(!InitVertexLumMP()) return(false); + + if(!CreateBlinnPhongUBO()) + return(false); if(!InitBlinnPhongSunLightMP()) return(false); diff --git a/inc/hgl/graph/VKMaterial.h b/inc/hgl/graph/VKMaterial.h index 6f428892..dd99ffba 100644 --- a/inc/hgl/graph/VKMaterial.h +++ b/inc/hgl/graph/VKMaterial.h @@ -49,15 +49,15 @@ public: virtual ~Material(); - const UTF8String & GetName ()const{return name;} + const AnsiString & GetName ()const{return name;} const VertexInput * GetVertexInput ()const{return vertex_input;} const ShaderStageCreateInfoList & GetStageList ()const{return shader_stage_list;} - const MaterialDescriptorManager * GetDescriptorSets ()const{return desc_manager;} +// const MaterialDescriptorManager * GetDescriptorManager ()const{return desc_manager;} const VkPipelineLayout GetPipelineLayout ()const; - const PipelineLayoutData * GetPipelineLayoutData ()const{return pipeline_layout_data;} +// const PipelineLayoutData * GetPipelineLayoutData ()const{return pipeline_layout_data;} public: diff --git a/inc/hgl/graph/mtl/BlinnPhong.h b/inc/hgl/graph/mtl/BlinnPhong.h index 62ee9b1b..a42d1895 100644 --- a/inc/hgl/graph/mtl/BlinnPhong.h +++ b/inc/hgl/graph/mtl/BlinnPhong.h @@ -8,8 +8,8 @@ namespace blinnphong { struct SunLight { - Vector3f direction; - Vector3f color; + Vector4f direction; + Vector4f color; };//struct SunLight constexpr const ShaderBufferSource SBS_SunLight= @@ -18,8 +18,8 @@ namespace blinnphong "sun", R"( - vec3 direction; - vec3 color; + vec4 direction; + vec4 color; )" }; }//namespace blinnphong