增加象素方向光照

This commit is contained in:
hyzboy 2020-06-20 15:25:16 +08:00
parent 519871e43c
commit 1c75bad0e6
3 changed files with 101 additions and 20 deletions

View File

@ -0,0 +1,46 @@
#version 450 core
layout(location = 0) in vec3 FragmentNormal;
layout(location = 0) out vec4 FragColor;
layout(binding=10) uniform WorldMatrix // hgl/math/Math.h
{
mat4 ortho;
mat4 projection;
mat4 inverse_projection;
mat4 modelview;
mat4 inverse_modelview;
mat4 mvp;
mat4 inverse_mvp;
vec4 view_pos;
vec2 canvas_resolution;
vec2 viewport_resolution;
}fs_world;
layout(binding=1) uniform ColorMaterial
{
vec4 color;
vec4 ambient;
} color_material;
layout(binding=2) uniform Sun
{
vec3 direction;
}sun;
vec4 ComputeSunlightFinalColor()
{
float intensity=max(dot(normalize(FragmentNormal*mat3(fs_world.mvp)),sun.direction),0.0);
return max(color_material.color*intensity,color_material.ambient);
}
void main()
{
FragColor=ComputeSunlightFinalColor();
}

View File

@ -25,29 +25,11 @@ layout(push_constant) uniform Consts {
mat4 local_to_world;
}pc;
layout(binding=1) uniform ColorMaterial
{
vec4 color;
vec4 ambient;
} color_material;
layout(binding=2) uniform Sun
{
vec3 direction;
}sun;
layout(location=0) out vec4 FragmentColor;
vec4 ComputeSunlightFinalColor(vec4 color,vec4 ambient)
{
float intensity=max(dot(normalize(Normal*mat3(world.mvp)),sun.direction),0.0);
return max(color*intensity,ambient);
}
layout(location=0) out vec3 FragmentNormal;
void main()
{
FragmentColor=ComputeSunlightFinalColor(color_material.color,color_material.ambient);
FragmentNormal=Normal;
gl_Position=vec4(Vertex,1.0)*(pc.local_to_world*world.mvp);
}

53
shader/VertexLight.vert Normal file
View File

@ -0,0 +1,53 @@
#version 450 core
layout(location = 0) in vec3 Vertex;
layout(location = 1) in vec3 Normal;
layout(binding=0) uniform WorldMatrix // hgl/math/Math.h
{
mat4 ortho;
mat4 projection;
mat4 inverse_projection;
mat4 modelview;
mat4 inverse_modelview;
mat4 mvp;
mat4 inverse_mvp;
vec4 view_pos;
vec2 canvas_resolution;
vec2 viewport_resolution;
}world;
layout(push_constant) uniform Consts {
mat4 local_to_world;
}pc;
layout(binding=1) uniform ColorMaterial
{
vec4 color;
vec4 ambient;
} color_material;
layout(binding=2) uniform Sun
{
vec3 direction;
}sun;
layout(location=0) out vec4 FragmentColor;
vec4 ComputeSunlightFinalColor(vec4 color,vec4 ambient)
{
float intensity=max(dot(normalize(Normal*mat3(world.mvp)),sun.direction),0.0);
return max(color*intensity,ambient);
}
void main()
{
FragmentColor=ComputeSunlightFinalColor(color_material.color,color_material.ambient);
gl_Position=vec4(Vertex,1.0)*(pc.local_to_world*world.mvp);
}