add texture in BlinnPhongDirectionLight example.
This commit is contained in:
parent
22868dafab
commit
90152ca74e
@ -61,7 +61,7 @@ Code
|
||||
|
||||
#Fragment
|
||||
|
||||
sampler2DArray TextureColor
|
||||
sampler2DArray TextureBaseColor
|
||||
|
||||
Output
|
||||
{
|
||||
@ -74,6 +74,6 @@ Code
|
||||
{
|
||||
MaterialInstance mi=GetMI();
|
||||
|
||||
FragColor=texture(TextureColor,vec3(Input.TexCoord,mi.id.x));
|
||||
FragColor=texture(TextureBaseColor,vec3(Input.TexCoord,mi.id.x));
|
||||
}
|
||||
}
|
||||
|
100
ShaderLibrary/Std3D/BlinnPhong/SunLightPureColorTexture.mtl
Normal file
100
ShaderLibrary/Std3D/BlinnPhong/SunLightPureColorTexture.mtl
Normal file
@ -0,0 +1,100 @@
|
||||
#Material
|
||||
Name BlinnPhong+HalfLambert shading model only color
|
||||
Reference https://zhuanlan.zhihu.com/p/442023993
|
||||
Base Std3D/BlinnPhong
|
||||
|
||||
//某些Require并不真的存在.ubo文件,写成一行一个是为了方便未来改成带路径的
|
||||
Require LocalToWorld
|
||||
Require Camera
|
||||
|
||||
define HAVE_SPECULAR off //默认不定义HAVE_SPECULAR
|
||||
|
||||
UBO
|
||||
{
|
||||
File BlinnPhongSun.ubo //文件名,如果/开头表示从ShaderLibrary根目录开始,没有则表示同一目录
|
||||
Struct BlinnPhongSun //结构名称
|
||||
Name sun //在代码中的变量名
|
||||
Stage Fragment //会引用的shader
|
||||
Set Global //Descriptor Set
|
||||
}
|
||||
|
||||
#MaterialInstance
|
||||
Length 32
|
||||
Stage Fragment
|
||||
Code
|
||||
{
|
||||
vec4 Color;
|
||||
uint tex_id;
|
||||
}
|
||||
|
||||
#VertexInput
|
||||
vec3 Normal
|
||||
vec2 TexCoord
|
||||
|
||||
#Vertex
|
||||
Output
|
||||
{
|
||||
vec4 Position;
|
||||
vec3 Normal;
|
||||
vec2 TexCoord
|
||||
}
|
||||
|
||||
Code
|
||||
{
|
||||
|
||||
void main()
|
||||
{
|
||||
Output.Normal =GetNormal();
|
||||
Output.Position =GetPosition3D();
|
||||
Output.TexCoord =TexCoord;
|
||||
|
||||
HandoverMI();
|
||||
|
||||
gl_Position =Output.Position;
|
||||
}
|
||||
}
|
||||
|
||||
#Fragment
|
||||
|
||||
sampler2DArray TextureBaseColor
|
||||
|
||||
Output
|
||||
{
|
||||
vec4 FragColor;
|
||||
}
|
||||
|
||||
Code
|
||||
{
|
||||
#define HAVE_SPECULAR
|
||||
|
||||
void main()
|
||||
{
|
||||
MaterialInstance mi=GetMI();
|
||||
|
||||
vec4 texture_color=texture(TextureBaseColor,vec3(Input.TexCoord,mi.tex_id));
|
||||
|
||||
//点乘法线和光照
|
||||
float intensity=0.5*max(dot(Input.Normal,sun.direction.xyz),0.0)+0.5;
|
||||
|
||||
//直接光颜色
|
||||
vec3 direct_color =intensity*sun.color.rgb*mi.Color.rgb*texture_color.rgb;
|
||||
|
||||
#ifndef HAVE_SPECULAR
|
||||
FragColor=vec4(direct_color,1.0);
|
||||
#else
|
||||
|
||||
vec3 spec_color=vec3(0.0);
|
||||
|
||||
if(intensity>0.0)
|
||||
{
|
||||
vec3 half_vector=normalize(sun.direction.xyz+normalize(Input.Position.xyz+camera.pos));
|
||||
|
||||
float specular=max(dot(half_vector,Input.Normal),0.0);
|
||||
|
||||
spec_color=specular*pow(specular,8)*sun.color.rgb;
|
||||
}
|
||||
|
||||
FragColor=vec4(direct_color+spec_color,1.0);
|
||||
#endif//HAVE_SPECULAR
|
||||
}
|
||||
}
|
@ -28,12 +28,26 @@ static mtl::blinnphong::SunLight sun_light=
|
||||
|
||||
constexpr const COLOR AxisColor[4]=
|
||||
{
|
||||
COLOR::Red, //X轴颜色
|
||||
COLOR::Green, //Y轴颜色
|
||||
COLOR::Blue, //Z轴颜色
|
||||
//COLOR::Red, //X轴颜色
|
||||
//COLOR::Green, //Y轴颜色
|
||||
//COLOR::Blue, //Z轴颜色
|
||||
COLOR::White, //全局颜色
|
||||
COLOR::White, //全局颜色
|
||||
COLOR::White, //全局颜色
|
||||
COLOR::White //全局颜色
|
||||
|
||||
};
|
||||
|
||||
constexpr const os_char *tex_filename[]=
|
||||
{
|
||||
OS_TEXT("eucalyptus-cross-versailles.Tex2D"),
|
||||
OS_TEXT("tints-ashton-green-stretcher.Tex2D"),
|
||||
OS_TEXT("worn-clay-cobble-in-desert-stretcher.Tex2D"),
|
||||
OS_TEXT("plain-grey-sheer.Tex2D"),
|
||||
};
|
||||
|
||||
constexpr const size_t TexCount=sizeof(tex_filename)/sizeof(os_char *);
|
||||
|
||||
class TestApp:public SceneAppFramework
|
||||
{
|
||||
private: //plane grid
|
||||
@ -47,6 +61,10 @@ private:
|
||||
|
||||
DeviceBuffer * ubo_sun =nullptr;
|
||||
|
||||
|
||||
Texture2DArray * texture =nullptr;
|
||||
Sampler * sampler =nullptr;
|
||||
|
||||
private: //sphere
|
||||
|
||||
Material * mtl_blinnphong =nullptr;
|
||||
@ -61,6 +79,29 @@ private: //sphere
|
||||
Primitive * prim_cylinder =nullptr;
|
||||
|
||||
private:
|
||||
|
||||
bool InitTexture()
|
||||
{
|
||||
texture=db->CreateTexture2DArray( "SeamlessBackground",
|
||||
256,256, ///<纹理尺寸
|
||||
TexCount, ///<纹理层数
|
||||
PF_BC1_RGBUN, ///<纹理格式
|
||||
false); ///<是否自动产生mipmaps
|
||||
|
||||
if(!texture)return(false);
|
||||
|
||||
OSString filename;
|
||||
|
||||
for(uint i=0;i<TexCount;i++)
|
||||
{
|
||||
filename=filesystem::MergeFilename(OS_TEXT("res/image/seamless"),tex_filename[i]);
|
||||
|
||||
if(!db->LoadTexture2DToArray(texture,i,filename))
|
||||
return(false);
|
||||
}
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
bool InitVertexLumMP()
|
||||
{
|
||||
@ -97,16 +138,32 @@ private:
|
||||
cfg.local_to_world=true;
|
||||
cfg.material_instance=true;
|
||||
|
||||
mtl_blinnphong=db->LoadMaterial("Std3D/BlinnPhong/SunLightPureColor",&cfg);
|
||||
mtl_blinnphong=db->LoadMaterial("Std3D/BlinnPhong/SunLightPureColorTexture",&cfg);
|
||||
if(!mtl_blinnphong)return(false);
|
||||
|
||||
mtl_blinnphong->BindUBO(DescriptorSetType::Global,"sun",ubo_sun);
|
||||
mtl_blinnphong->Update();
|
||||
|
||||
sampler=db->CreateSampler();
|
||||
|
||||
if(!mtl_blinnphong->BindImageSampler( DescriptorSetType::PerMaterial, ///<描述符合集
|
||||
mtl::SamplerName::BaseColor, ///<采样器名称
|
||||
texture, ///<纹理
|
||||
sampler)) ///<采样器
|
||||
return(false);
|
||||
|
||||
struct MIData
|
||||
{
|
||||
Color4f color;
|
||||
uint32 tex_id[4];
|
||||
}mi_data;
|
||||
|
||||
constexpr const uint MIDataStride=sizeof(MIData);
|
||||
|
||||
Color4f mi_data;
|
||||
for(uint i=0;i<4;i++)
|
||||
{
|
||||
mi_data=GetColor4f(AxisColor[i],4);
|
||||
mi_data.color=GetColor4f(AxisColor[i],4);
|
||||
mi_data.tex_id[0]=i;
|
||||
|
||||
mi_blinnphong[i]=db->CreateMaterialInstance(mtl_blinnphong, //材质
|
||||
nullptr, //顶点输入配置,这里使用nullptr,即代表会使用默认配置,那么结果将等同于上面的mtl_blinnphong->GetDefaultVIL()
|
||||
@ -177,9 +234,9 @@ private:
|
||||
{
|
||||
struct CylinderCreateInfo cci;
|
||||
|
||||
cci.halfExtend =4; //圆柱一半高度
|
||||
cci.halfExtend =1.25; //圆柱一半高度
|
||||
cci.numberSlices=16; //圆柱底部分割数
|
||||
cci.radius =0.25f; //圆柱半径
|
||||
cci.radius =1.25f; //圆柱半径
|
||||
|
||||
prim_cylinder=CreateCylinder(pc_blinnphong,&cci);
|
||||
}
|
||||
@ -235,8 +292,11 @@ public:
|
||||
if(!SceneAppFramework::Init(w,h))
|
||||
return(false);
|
||||
|
||||
if(!InitVertexLumMP())
|
||||
if(!InitTexture())
|
||||
return(false);
|
||||
|
||||
//if(!InitVertexLumMP())
|
||||
// return(false);
|
||||
|
||||
if(!CreateBlinnPhongUBO())
|
||||
return(false);
|
||||
|
@ -8,7 +8,7 @@ namespace hgl
|
||||
{
|
||||
namespace SamplerName
|
||||
{
|
||||
constexpr const char Color[] = "TextureColor";
|
||||
constexpr const char BaseColor[] = "TextureBaseColor";
|
||||
}//namespace SamplerName
|
||||
}//namespace mtl
|
||||
}//namespace graph
|
||||
|
2
res
2
res
@ -1 +1 @@
|
||||
Subproject commit 5e5814c15e936fd6e865c344b98f5ce79f364079
|
||||
Subproject commit 475d8ad43ceee084cd24f5d0bed59de9f6aa36fd
|
@ -45,7 +45,7 @@ void main()
|
||||
|
||||
bool CustomFragmentShader(ShaderCreateInfoFragment *fsc) override
|
||||
{
|
||||
mci->AddSampler(VK_SHADER_STAGE_FRAGMENT_BIT,DescriptorSetType::PerMaterial,SamplerType::Sampler2D,mtl::SamplerName::Color);
|
||||
mci->AddSampler(VK_SHADER_STAGE_FRAGMENT_BIT,DescriptorSetType::PerMaterial,SamplerType::Sampler2D,mtl::SamplerName::BaseColor);
|
||||
|
||||
fsc->AddOutput(VAT_VEC4,"FragColor"); //Fragment shader的输出等于最终的RT了,所以这个名称其实随便起。
|
||||
|
||||
|
@ -72,7 +72,7 @@ void main()
|
||||
|
||||
bool CustomFragmentShader(ShaderCreateInfoFragment *fsc) override
|
||||
{
|
||||
mci->AddSampler(VK_SHADER_STAGE_FRAGMENT_BIT,DescriptorSetType::PerMaterial,SamplerType::Sampler2D,mtl::SamplerName::Color);
|
||||
mci->AddSampler(VK_SHADER_STAGE_FRAGMENT_BIT,DescriptorSetType::PerMaterial,SamplerType::Sampler2D,mtl::SamplerName::BaseColor);
|
||||
|
||||
fsc->AddOutput(VAT_VEC4,"FragColor"); //Fragment shader的输出等于最终的RT了,所以这个名称其实随便起。
|
||||
|
||||
|
@ -79,7 +79,7 @@ void main()
|
||||
|
||||
bool CustomFragmentShader(ShaderCreateInfoFragment *fsc) override
|
||||
{
|
||||
mci->AddSampler(VK_SHADER_STAGE_FRAGMENT_BIT,DescriptorSetType::PerMaterial,SamplerType::Sampler2DArray,mtl::SamplerName::Color);
|
||||
mci->AddSampler(VK_SHADER_STAGE_FRAGMENT_BIT,DescriptorSetType::PerMaterial,SamplerType::Sampler2DArray,mtl::SamplerName::BaseColor);
|
||||
|
||||
fsc->AddOutput(VAT_VEC4,"FragColor"); //Fragment shader的输出等于最终的RT了,所以这个名称其实随便起。
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user