Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change specular lighting #22

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions data/config/glsl/deferred.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,9 @@ unpacknorm = [
unpackspec = [
result [
vec3 camdir = normalize(camera - pos.xyz);
float facing = 2.0*dot(normal.xyz, camdir);
float specscale = min(3.0*(diffuse.a + 0.5/255.0), 2.999), gloss = floor(specscale);
specscale -= gloss;
specscale = (0.35 + 0.15*gloss) * specscale / (1.5 - specscale);
gloss = 5.0 + 17.0*gloss;
float roughness = 1.0-diffuse.a;
float rr = roughness*roughness;
float rrrr = rr*rr;
]
]

Expand Down Expand Up @@ -500,7 +498,12 @@ deferredlightvariantshader = [
light += diffuse.rgb*sunfacing * sunlightcolor * sunoccluded;
]] [result [
@(if (= (+ $numlights (dlopt "c")) 1) [unpackspec])
float sunspec = pow(clamp(sunfacing*facing - dot(camdir, sunlightdir), 0.0, 1.0), gloss) * specscale;
vec3 halfAngleVector = normalize(sunlightdir+camdir);
float hdotN = clamp(dot(halfAngleVector,normal.xyz), 0.0, 1.0);
float D = ( hdotN * hdotN ) * ( rrrr - 1.0 ) + 1.0;
float ldotH = clamp( dot( sunlightdir, halfAngleVector ), 0.0, 1.0 );
float VFapprox = ( ldotH * ldotH ) * ( roughness + 0.5 );
float sunspec = clamp(( rrrr / ( 12.5663706144 * D * D * VFapprox ) ) * sunfacing * 0.4,0.0, 9999.0);
@(if (dlopt "r") [result [
sunlight += (diffuse.rgb*sunfacing + sunspec) * sunoccluded;
]] [result [
Expand All @@ -521,6 +524,7 @@ deferredlightvariantshader = [

@(loopconcat j $numlights [result [
vec3 light@[j]dir = lightpos[@@j].xyz - pos.xyz * lightpos[@@j].w;
vec3 light@[j]dir2 = normalize(light@[j]dir);
float light@[j]dist2 = dot(light@[j]dir, light@[j]dir);
if(light@[j]dist2 < 1.0)
{
Expand Down Expand Up @@ -577,7 +581,12 @@ deferredlightvariantshader = [
light += diffuse.rgb*light@[j]facing * lightcolor[@@j].rgb * light@[j]atten;
]] [result [
@(if (= (+ $numlights (dlopt "c")) 1) [unpackspec])
float light@[j]spec = pow(clamp(light@[j]facing*facing - light@[j]invdist*dot(camdir, light@[j]dir), 0.0, 1.0), gloss) * specscale;
vec3 halfAngleVector = normalize(light@[j]dir2+camdir);
float hdotN = clamp(dot(halfAngleVector,normal.xyz), 0.0, 1.0);
float D = ( hdotN * hdotN ) * ( rrrr - 1.0 ) + 1.0;
float ldotH = clamp( dot( light@[j]dir2, halfAngleVector ), 0.0, 1.0 );
float VFapprox = ( ldotH * ldotH ) * ( roughness + 0.5 );
float light@[j]spec = clamp(( rrrr / ( 12.5663706144 * D * D * VFapprox ) ) * light@[j]facing * 0.1, 0.0, 9999.0);
@(if (dlopt "z") [result [
light@[j]spec *= lightcolor[@@j].a;
]])
Expand Down Expand Up @@ -685,4 +694,3 @@ deferredlightshader = [
]
]
]

7 changes: 6 additions & 1 deletion data/config/glsl/ui.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ lazyshader 0 "modelpreview" [
pos.xyz /= pos.w;
])
@(unpackspec)
float sunspec = pow(clamp(sunfacing*facing - dot(camdir, sunlightdir), 0.0, 1.0), gloss) * specscale;
vec3 halfAngleVector = normalize(sunlightdir+camdir);
float hdotN = clamp(dot(halfAngleVector,normal.xyz), 0.0, 1.0);
float D = ( hdotN * hdotN ) * ( rrrr - 1.0 ) + 1.0;
float ldotH = clamp( dot( sunlightdir, halfAngleVector ), 0.0, 1.0 );
float VFapprox = ( ldotH * ldotH ) * ( roughness + 0.5 );
float sunspec = clamp(( rrrr / ( 12.5663706144 * D * D * VFapprox ) ) * sunfacing * 0.6,0.0, 9999.0);
light += (diffuse.rgb*sunfacing + sunspec) * sunlightcolor;
}

Expand Down