SimSun Shader - Simulated directional light for models

SimSun Shader - Simulated directional light for models

Postby Cherno » Sun Feb 02, 2020 4:44 am

This shader shades a model as if there was a lightsource coming from above, like the sun, sky, or moon outside and a ceiling light inside. The effect is subtle but noticeable. The flat lighting on models bothers me since most Doom maps don't have any, or not enough, dynamic light sources to properly light a model, resulting in a very flat look. You can combine this shader with GZDoom's ambient occlusion to further enhance it.
The shader works per-texture. It can be used with regular map textures as well of course. If it's a graphic, the shader can be applied :)
If you are not familiar with using shaders, just check GLDEFS and add your own model textures. Basically, you need the shader file in the shaders directory, and a GLDEFS lump which assign any graphic to the shader. If you use it with normal map textures, you can just pass the name of the texture without the path or file extension. For other graphics, you need to pass the full path plus file extension.



Image

Image

Image
(thanks to Kizoky)

Download:
simsun1.1.pk3

load TESTMAP to see some sample models in action.

Feel free to include this with any of your projects. If you would like to credit me and/or the others who provided help, that would be nice.

Code: Select allExpand view
//SimSun Shader by Cherno

const float pi = 3.14159265359;
vec4 Process(vec4 color)
{
   vec3 lightDir = vec3(0.75,-1.0,-0.5);
   //Doom map axis: x,z,y
   //with 1.0-1.0,-1.0, the light comes from the north-west-west diagonally downwards, if north is towards the top of the (auto or editor)map

   vec2 texCoord = gl_TexCoord[0].st;
   vec3 l = lightDir;
   vec3 n = normalize(vWorldNormal.xyz);
   float angle = acos
   (
      (l.x*n.x + l.y*n.y + l.z * n.z)
      /
      (
         (   
            sqrt
            (
               (l.x*l.x)+(l.y*l.y)+(l.z*l.z)
            )
            *
            sqrt
            (
               (n.x*n.x) + (n.y*n.y) + (n.z*n.z)
            )
         )
      )
   );
   float lightLevel = angle;
   lightLevel /= pi;
   //from here on out, you have a lightLevel between 0.0 and 1.0, depending on the angle of the surface relative to lightDir.
   //a lightLevel of 0.5 results in the pixel at it would appear with no shading from SimSun at all.
   //you can add calculations to the lightvalue if you wish to increase, decrease, or shoft the effect.
   lightLevel += 0.25;//shifts the lightLevel so it gets slightly lighter, so there are less extremely dark areas
   return getTexel(texCoord) * color * vec4(lightLevel,lightLevel,lightLevel,1.0);
}
Last edited by Cherno on Sun May 03, 2020 7:22 pm, edited 10 times in total.
User avatar
Cherno
 
Joined: 06 Dec 2016

Re: SimSun Shader - Simulated directional light for models

Postby Kizoky » Sun Feb 02, 2020 7:30 am

Looks great! I gave this shader on a hand texture, you can see it here: https://gph.is/g/aQOOJwR

I always hated how all the models looked static unless they were under a dynamic light source
User avatar
Kizoky
I smell like gravity
 
Joined: 14 Nov 2011
Location: Around weirdos, I'm the biggest weirdo among them
Discord: Kizoky#0375
Operating System: Windows 10/8.1/8/201x 64-bit
Graphics Processor: nVidia with Vulkan support

Re: SimSun Shader - Simulated directional light for models

Postby Cherno » Sun Feb 02, 2020 7:54 am

Looks great as well! :)

The shader can easily be customized (not a truntime, though), chiefly by modifying these lines:

Code: Select allExpand view
vec3 lightDir = vec3(0.75,-1.0,0.5);


and
the
Code: Select allExpand view
lightLevel

variable.

lightDir is just the direction of the light source as a Vector3. To have it come from straight above, it would be vec3(0,-1,0), for example (note that this is GLSL language, not zScript, so the second Vector3 variable (y) is up-down).
lightLevel by default goes from 1.0 to 0.0, where 1.0 faces the light source directly and results in the texture's color, and 0.0 is directly away from the light source and darkens the texture. You could change this by adding your own calculation, for example by making the light-facing pixels brighter than the texture, and so on.
User avatar
Cherno
 
Joined: 06 Dec 2016

Re: SimSun Shader - Simulated directional light for models

Postby Darkcrafter » Thu Feb 06, 2020 4:58 am

This needs to make into GZDoom and into Doom Builder, imagine if sector would have this direction/lighting settings how convenient it would be. Or even having additional setup per actor overriding sector properties.
User avatar
Darkcrafter
 
Joined: 23 Sep 2017
Location: South Russia
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia with Vulkan support

Re: SimSun Shader - Simulated directional light for models

Postby Redneckerz » Thu Feb 06, 2020 11:45 am

So subtle but its definitely noticeable. I see Kizoky's Share The Doom looks even more like its inspiration by it :)

This is really useful stuff. Sadly ZDforums has no ping mechanism, but Nash, this is the kind of stuff i know you like a lot!
User avatar
Redneckerz
To it's ports i may have seen
Spotlight Team
 
Joined: 25 Nov 2019
Discord: Redneckerz#8399
Operating System: Windows Vista/7/2008 64-bit
Graphics Processor: nVidia (Legacy GZDoom)

Re: SimSun Shader - Simulated directional light for models

Postby MFG38 » Fri Feb 07, 2020 8:46 am

I'm curious, though: how good would this look on a 2D sprite?
User avatar
MFG38
 
Joined: 14 Apr 2019
Location: Finland
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia (Modern GZDoom)

Re: SimSun Shader - Simulated directional light for models

Postby Cherno » Fri Feb 07, 2020 11:35 am

I haven't thought about it; I'd wager it wouldn't be looking too great ;) If it works at all, that is.
User avatar
Cherno
 
Joined: 06 Dec 2016

Re: SimSun Shader - Simulated directional light for models

Postby Nash » Tue Feb 11, 2020 12:20 pm

It most likely won't work with sprites because sprites are lit uniformly (same reason why you can't have materials on sprites).

Speaking of materials - this doesn't look like it has material handling (diffuse + specular + normal map, or PBR), does it?
User avatar
Nash
 
 
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia
Github ID: nashmuhandes

Re: SimSun Shader - Simulated directional light for models

Postby Cherno » Tue Feb 11, 2020 1:43 pm

No material support. I had thought about adding support for normal maps so the get the same lighting from an imaginary source, but I personally don't use normal maps in my GZDoom projects (yet) so I didn't bother. Some other user on Discord wrote that he had written something similar for normal maps, can't remember who it was but at least it seems doable. If anyone wants to have a go at it, I'm sure it would be welcomed as well.
Last edited by Cherno on Mon Mar 16, 2020 9:09 pm, edited 1 time in total.
User avatar
Cherno
 
Joined: 06 Dec 2016

Re: SimSun Shader - Simulated directional light for models

Postby Ozymandias81 » Wed Mar 04, 2020 2:51 pm

This actually looks pretty cool, wonder if this could work on Blade of Agony and Quake 2 TC, I will test it when I can during the week! Surely this could give some modern depth to your Hired Guns TC too @Cherno
User avatar
Ozymandias81
Doom is a State of Mind... Out of Control.
 
Joined: 04 Jul 2013
Location: Mount Olympus, Mars
Github ID: Ozymandias81
Operating System: Windows Vista/7/2008 64-bit
Graphics Processor: nVidia (Modern GZDoom)

Re: SimSun Shader - Simulated directional light for models

Postby Cherno » Wed Mar 04, 2020 3:59 pm

Ozymandias81 wrote: Surely this could give some modern depth to your Hired Guns TC too @Cherno


I only use a handful of model for Hired Guns, but it would nevertheless benefit, yes. I'm not sure if I will continue that particular project though, as the performance hit with 4+1 viewports is too much to handle :3:
User avatar
Cherno
 
Joined: 06 Dec 2016

Re: SimSun Shader - Simulated directional light for models

Postby r&r » Mon Mar 16, 2020 3:15 pm

Was there a mod or a concept wip
mod that post to work for 2D sprites?

But it's a Parallax concept for 2D weapons instead?
User avatar
r&r
 
Joined: 08 Sep 2017

Re: SimSun Shader - Simulated directional light for models

Postby Cherno » Mon Mar 16, 2020 9:10 pm

r&r wrote:Was there a mod or a concept wip
mod that post to work for 2D sprites?

But it's a Parallax concept for 2D weapons instead?


I am having a really hard time understanding what you are trying to ask :3:
User avatar
Cherno
 
Joined: 06 Dec 2016

Re: SimSun Shader - Simulated directional light for models

Postby r&r » Tue Mar 17, 2020 8:46 pm

Cherno wrote:
r&r wrote:Was there a mod or a concept wip
mod that post to work for 2D sprites?

But it's a Parallax concept for 2D weapons instead?


I am having a really hard time understanding what you are trying to ask :3:


Sorry i was asking, was there SimSun Shader like concept/work in progress for 2d sprites weapons out there?
User avatar
r&r
 
Joined: 08 Sep 2017

Re: SimSun Shader - Simulated directional light for models

Postby dpJudas » Tue Mar 17, 2020 9:23 pm

I took a look at the code and simplified it. Maybe it will improve the performance a little bit on low end GPUs.

Code: Select allExpand view
vec4 Process(vec4 color)
{
    vec3 lightDir = vec3(0.75,-1.0,0.5);

    vec2 texCoord = gl_TexCoord[0].st;
    vec3 l = normalize(lightDir);
    vec3 n = normalize(vWorldNormal.xyz);

    float lightLevel = acos(dot(l,n)) / 3.14159265359;

    return getTexel(texCoord) * vec4(color.rgb * lightLevel, color.a);
}


Edit: looking a bit further at this code, there's two more things:

1) There's a missing clamp for the light level. Things pointing straight away from the light source actually gets negative light and thus a negative material color. The correct math is to add a "lightLevel = clamp(lightLevel, 0.0, 1.0);" line.
2) The physical correct falloff is actually not to convert it to an angle. Light level should be "float lightLevel = clamp(dot(l,n), 0.0, 1.0);".
dpJudas
 
 
 
Joined: 28 May 2016

Next

Return to Shaders

Who is online

Users browsing this forum: No registered users and 1 guest