108 lines
2.3 KiB
Plaintext
108 lines
2.3 KiB
Plaintext
#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 16
|
||
Stage Fragment
|
||
Code
|
||
{
|
||
vec3 Color;
|
||
float Gloss;
|
||
}
|
||
|
||
#VertexInput
|
||
vec3 Normal
|
||
|
||
#Vertex
|
||
Output
|
||
{
|
||
vec4 Position;
|
||
vec3 Normal;
|
||
}
|
||
|
||
Code
|
||
{
|
||
mat3 GetNormalMatrix()
|
||
{
|
||
return mat3(camera.view*GetLocalToWorld());
|
||
}
|
||
|
||
vec3 GetNormal(mat3 normal_matrix,vec3 normal)
|
||
{
|
||
return normalize(normal_matrix*normal);
|
||
}
|
||
|
||
void main()
|
||
{
|
||
mat3 normal_matrix=GetNormalMatrix();
|
||
|
||
Output.Normal =GetNormal(normal_matrix,Normal);
|
||
Output.Position =GetPosition3D();
|
||
|
||
HandoverMI();
|
||
|
||
gl_Position =Output.Position;
|
||
}
|
||
}
|
||
|
||
#Fragment
|
||
Output
|
||
{
|
||
vec4 FragColor;
|
||
}
|
||
|
||
Code
|
||
{
|
||
void main()
|
||
{
|
||
MaterialInstance mi=GetMI();
|
||
|
||
//将法线归一化
|
||
// vec3 world_normal =normalize(Input.Normal);
|
||
|
||
//对世界坐标下的灯光方法归一化
|
||
// vec3 world_light_direction =normalize(sun.direction.xyz);
|
||
|
||
//点乘法线和光照
|
||
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;
|
||
|
||
// #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,mi.Gloss)*sun.color.rgb;
|
||
}
|
||
|
||
FragColor=vec4(direct_color+spec_color,1.0);
|
||
// #endif//HAVE_SPECULAR
|
||
}
|
||
}
|