ubuntu20
4 years ago
4 changed files with 80 additions and 16 deletions
@ -0,0 +1,57 @@ |
|||
uniform vec4 U_AmbientMaterial; |
|||
uniform vec4 U_AmbientLight; |
|||
uniform vec4 U_DiffuseMaterial; |
|||
uniform vec4 U_DiffuseLight; |
|||
uniform vec4 U_SpecularMaterial; |
|||
uniform vec4 U_SpecularLight; |
|||
uniform vec4 U_LightPos; |
|||
uniform vec4 U_CameraPos; |
|||
uniform vec4 U_LightDirection; |
|||
uniform vec4 U_LightOption; |
|||
varying vec4 V_Normal; |
|||
varying vec4 V_WorldPos; |
|||
|
|||
void main(){ |
|||
//ambient |
|||
vec4 ambientColor = U_AmbientMaterial * U_AmbientLight; |
|||
|
|||
//diffuse |
|||
vec3 n = normalize(V_Normal.xyz); |
|||
vec3 l = normalize(U_LightPos.xyz - V_WorldPos.xyz); |
|||
float duffuseIntensity = max(0.0, dot(l,n)); |
|||
|
|||
//¾Û¹â¼ÆËã |
|||
float attenuation = 1.0; |
|||
float distance = 0; |
|||
float constantFactor = 0.5; |
|||
float linearFactor = 0.3; |
|||
float expFactor = 0.1; |
|||
|
|||
distance =length(U_LightPos.xyz - V_WorldPos.xyz); |
|||
attenuation = 1.0 / (constantFactor + linearFactor*distance + expFactor*distance*distance); |
|||
|
|||
if(duffuseIntensity > 0){ |
|||
vec3 spot_direction = normalize(U_LightDirection.xyz); |
|||
float currentCosThta = max(0.0, dot(-l, spot_direction)); |
|||
float radianCutoff = U_LightDirection.w * 3.14 / 180; |
|||
float cosThta = cos(radianCutoff); |
|||
if(currentCosThta > cosThta) { |
|||
duffuseIntensity = pow(currentCosThta, U_LightOption.x)*U_LightOption.y; |
|||
} else { |
|||
duffuseIntensity = 0.0; |
|||
} |
|||
} |
|||
|
|||
vec4 duffuseColor = U_DiffuseMaterial * U_DiffuseLight * duffuseIntensity * attenuation; |
|||
|
|||
//specular |
|||
vec4 specularColor = vec4(0); |
|||
if(duffuseIntensity > 0){ |
|||
vec3 rd = normalize(reflect(-l,n)); |
|||
vec3 vd = normalize((U_CameraPos - V_WorldPos).xyz); |
|||
float specularIntensity = pow(max(0.0,dot(vd,rd)), 128); |
|||
specularColor = U_SpecularMaterial * U_SpecularLight * specularIntensity; |
|||
} |
|||
|
|||
gl_FragColor = ambientColor + duffuseColor + specularColor; |
|||
} |
@ -0,0 +1,14 @@ |
|||
attribute vec4 position; |
|||
attribute vec4 texcoord; |
|||
attribute vec4 normal; |
|||
uniform mat4 ModelMatrix; |
|||
uniform mat4 ProjectionMatrix; |
|||
uniform mat4 ViewMatrix; |
|||
uniform mat4 IT_ModelMatrix; |
|||
varying vec4 V_Normal; |
|||
varying vec4 V_WorldPos; |
|||
void main(){ |
|||
V_WorldPos = ModelMatrix * position; |
|||
V_Normal = IT_ModelMatrix * normal; |
|||
gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * position; |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue