Skip to content

Commit

Permalink
Calculate color in vertex shader instead
Browse files Browse the repository at this point in the history
  • Loading branch information
GlennFolker committed Feb 18, 2024
1 parent 5c80c06 commit 5edc246
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
19 changes: 2 additions & 17 deletions assets/shaders/confictura/celestial.frag
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
uniform vec3 u_ambientColor;
uniform vec3 u_camPos;

varying vec3 v_pos;
varying vec4 v_color;
varying vec3 v_normal;
varying vec3 v_light;
varying vec3 v_color;

void main(){
vec3 specular = vec3(0.0);
vec3 ref = reflect(-v_light, v_normal);
vec3 eye = normalize(u_camPos - v_pos);
float factor = dot(eye, ref);
if(factor > 0.0){
specular = vec3(pow(factor, 40.0)) * (1.0 - v_color.a);
}

vec3 diffuse = (u_ambientColor + specular) * vec3(0.01 + clamp((dot(v_normal, v_light) + 1.0) / 2.0, 0.0, 1.0));
gl_FragColor = vec4(v_color.rgb, 1.0) * vec4(diffuse, 1.0);
gl_FragColor = vec4(v_color, 1.0);
}
20 changes: 12 additions & 8 deletions assets/shaders/confictura/celestial.vert
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@ uniform mat4 u_trans;
uniform mat4 u_normal;

uniform vec3 u_light;
uniform vec3 u_ambientColor;
uniform vec3 u_camPos;

varying vec3 v_pos;
varying vec4 v_color;
varying vec3 v_normal;
varying vec3 v_light;
varying vec3 v_color;

void main(){
vec4 pos = u_trans * vec4(a_position, 1.0);
vec3 normal = (u_normal * vec4(a_normal, 1.0)).xyz;
vec3 lightDir = normalize(u_light - pos.xyz);

v_pos = pos.xyz;
v_color = a_color;
v_normal = normal;
v_light = lightDir;
vec3 specular = vec3(0.0);
vec3 ref = reflect(-lightDir, normal);
vec3 eye = normalize(u_camPos - pos.xyz);

float factor = dot(eye, ref);
if(factor > 0.0) specular = vec3(pow(factor, 40.0)) * (1.0 - a_color.a);

vec3 diffuse = (u_ambientColor + specular) * vec3(0.01 + clamp((dot(normal, lightDir) + 1.0) / 2.0, 0.0, 1.0));
v_color = vec4(a_color.rgb, 1.0) * vec4(diffuse, 1.0);
gl_Position = u_proj * pos;
}

0 comments on commit 5edc246

Please sign in to comment.