SimSun Shader - Simulated directional light for models

Forum rules
The Projects forums are only for projects. If you are asking questions about a project, either find that project's thread, or start a thread in the General section instead.

Got a cool project idea but nothing else? Put it in the project ideas thread instead!

Projects for any Doom-based engine (especially 3DGE) are perfectly acceptable here too.

Please read the full rules for more details.
User avatar
Cherno
Posts: 1286
Joined: Tue Dec 06, 2016 11:25 am

SimSun Shader - Simulated directional light for models

Post by Cherno »

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 all

//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
Kizoky
Posts: 285
Joined: Mon Nov 14, 2011 9:59 am
Discord: Kizoky#0375
Github ID: Kizoky
Graphics Processor: nVidia with Vulkan support
Location: Around weirdos, I'm the biggest weirdo among them

Re: SimSun Shader - Simulated directional light for models

Post by Kizoky »

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
Cherno
Posts: 1286
Joined: Tue Dec 06, 2016 11:25 am

Re: SimSun Shader - Simulated directional light for models

Post by Cherno »

Looks great as well! :)

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

Code: Select all

vec3 lightDir = vec3(0.75,-1.0,0.5);
and
the

Code: Select all

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.
Darkcrafter
Posts: 518
Joined: Sat Sep 23, 2017 8:42 am
Graphics Processor: nVidia with Vulkan support

Re: SimSun Shader - Simulated directional light for models

Post by Darkcrafter »

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
Redneckerz
Spotlight Team
Posts: 976
Joined: Mon Nov 25, 2019 8:54 am
Discord: Redneckerz#8399
Graphics Processor: Intel (Modern GZDoom)

Re: SimSun Shader - Simulated directional light for models

Post by Redneckerz »

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
MFG38
Posts: 399
Joined: Sun Apr 14, 2019 8:26 am
Graphics Processor: nVidia (Modern GZDoom)
Location: Finland

Re: SimSun Shader - Simulated directional light for models

Post by MFG38 »

I'm curious, though: how good would this look on a 2D sprite?
User avatar
Cherno
Posts: 1286
Joined: Tue Dec 06, 2016 11:25 am

Re: SimSun Shader - Simulated directional light for models

Post by Cherno »

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
Nash
 
 
Posts: 17296
Joined: Mon Oct 27, 2003 12:07 am
Twitch ID: nashmuhandes
Github ID: nashmuhandes
Location: Kuala Lumpur, Malaysia

Re: SimSun Shader - Simulated directional light for models

Post by Nash »

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
Cherno
Posts: 1286
Joined: Tue Dec 06, 2016 11:25 am

Re: SimSun Shader - Simulated directional light for models

Post by Cherno »

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
Ozymandias81
Posts: 2052
Joined: Thu Jul 04, 2013 8:01 am
Discord: Ozymandias81#2768
Github ID: Ozymandias81
Graphics Processor: nVidia with Vulkan support
Location: Mount Olympus, Mars

Re: SimSun Shader - Simulated directional light for models

Post by Ozymandias81 »

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
Cherno
Posts: 1286
Joined: Tue Dec 06, 2016 11:25 am

Re: SimSun Shader - Simulated directional light for models

Post by Cherno »

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
r&r
Posts: 240
Joined: Fri Sep 08, 2017 2:54 pm

Re: SimSun Shader - Simulated directional light for models

Post by r&r »

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
Cherno
Posts: 1286
Joined: Tue Dec 06, 2016 11:25 am

Re: SimSun Shader - Simulated directional light for models

Post by Cherno »

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
r&r
Posts: 240
Joined: Fri Sep 08, 2017 2:54 pm

Re: SimSun Shader - Simulated directional light for models

Post by r&r »

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?
dpJudas
 
 
Posts: 2869
Joined: Sat May 28, 2016 1:01 pm

Re: SimSun Shader - Simulated directional light for models

Post by dpJudas »

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 all

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);".

Return to “Shaders”