Page 1 of 3

SimSun Shader - Simulated directional light for models

Posted: Sun Feb 02, 2020 4:44 am
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);
}

Re: SimSun Shader - Simulated directional light for models

Posted: Sun Feb 02, 2020 7:30 am
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

Re: SimSun Shader - Simulated directional light for models

Posted: Sun Feb 02, 2020 7:54 am
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.

Re: SimSun Shader - Simulated directional light for models

Posted: Thu Feb 06, 2020 4:58 am
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.

Re: SimSun Shader - Simulated directional light for models

Posted: Thu Feb 06, 2020 11:45 am
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!

Re: SimSun Shader - Simulated directional light for models

Posted: Fri Feb 07, 2020 8:46 am
by MFG38
I'm curious, though: how good would this look on a 2D sprite?

Re: SimSun Shader - Simulated directional light for models

Posted: Fri Feb 07, 2020 11:35 am
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.

Re: SimSun Shader - Simulated directional light for models

Posted: Tue Feb 11, 2020 12:20 pm
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?

Re: SimSun Shader - Simulated directional light for models

Posted: Tue Feb 11, 2020 1:43 pm
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.

Re: SimSun Shader - Simulated directional light for models

Posted: Wed Mar 04, 2020 2:51 pm
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

Re: SimSun Shader - Simulated directional light for models

Posted: Wed Mar 04, 2020 3:59 pm
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:

Re: SimSun Shader - Simulated directional light for models

Posted: Mon Mar 16, 2020 3:15 pm
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?

Re: SimSun Shader - Simulated directional light for models

Posted: Mon Mar 16, 2020 9:10 pm
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:

Re: SimSun Shader - Simulated directional light for models

Posted: Tue Mar 17, 2020 8:46 pm
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?

Re: SimSun Shader - Simulated directional light for models

Posted: Tue Mar 17, 2020 9:23 pm
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);".