-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshader_scene.fs.glsl
90 lines (75 loc) · 2.61 KB
/
shader_scene.fs.glsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#version 460 core
out vec4 color;
struct SpotLight {
vec3 position;
vec3 direction;
float cutOff;
float outerCutOff;
float constant;
float linear;
float quadratic;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
layout(location=4) uniform sampler2D texture_diffuse;
layout(location=5) uniform sampler2D texture_specular;
layout(location=6) uniform SpotLight spot_light;
in vec3 color_;
in vec2 texcoords_;
in vec3 view_pos_;
in vec3 frag_pos_;
in vec3 normal_;
in float time_;
vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
{
vec3 lightDir = normalize(light.position - fragPos);
vec3 h = normalize(lightDir + viewDir);
// diffuse shading
float diff = max(dot(normal, lightDir), 0.0);
// specular shading
vec3 reflectDir = reflect(-lightDir, normal);
//float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0);
float spec = pow(max(0.0, dot(normal, h)), 128.0) * 32.0 * sqrt(diff);
// attenuation
float distance = length(light.position - fragPos);
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
// spotlight intensity
float theta = dot(lightDir, normalize(-light.direction));
float epsilon = light.cutOff - light.outerCutOff;
float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);
// combine results
vec3 ambient = light.ambient * vec3(texture(texture_diffuse, texcoords_));
vec3 diffuse = light.diffuse * diff * vec3(texture(texture_diffuse, texcoords_));
vec3 specular = light.specular * spec * vec3(texture(texture_specular, texcoords_));
ambient *= attenuation * intensity;
diffuse *= attenuation * intensity;
specular *= attenuation * intensity;
return (ambient + diffuse + specular);
}
float circleSize=1.0/(3.0*pow(2.0,float(4)));
vec2 rot(vec2 uv,float a){
return vec2(uv.x*cos(a)-uv.y*sin(a),uv.y*cos(a)+uv.x*sin(a));
}
void main()
{
vec3 view_dir = normalize(view_pos_ - frag_pos_);
vec3 norm = normalize(normal_);
vec3 result = CalcSpotLight(spot_light, norm, frag_pos_, view_dir);
result += vec3(texture(texture_diffuse, texcoords_) * .5);
vec2 uv=vec2(1,1);
uv=-.5*(uv-2.0*texcoords_.xy)/uv.x;
float s=0.25;
for(int i=0;i<4;i++){
uv=abs(uv)-s;
uv=rot(uv, time_);
s=s/2.1;
}
//draw a circle
float c = length(uv) > circleSize ? 0.0 : 1.0;
//color = vec4(c,c,c,1.0);
//c -= (sin(time_/2)+1)/2;
color = vec4(result.x+c, result.y+(c*(sin(time_/4)+1)/2), result.z+(c*(sin(time_/2)+1)/2), 1.0);
//color = texture(texture_diffuse, texcoords_);
//color = vec4(.5, .5, .5, 1.0);
}