Change Light direction in Material event...?

Posted By: Error014

Change Light direction in Material event...? - 07/24/15 13:55

Hello!

I'm about to do a dirty hack for my game. Let's agree that this is terrible and I shouldn't be doing this. Also, let's do it.


In my game, many entities are models that are actually flat (like sprites)! This causes dynamic lighting to look "wrong", since they're shaded like models. This means that if I move "in front of" a lightsource, the model turns "dark", which makes sense, but is not what I want.

I figured - well, it means that in the calculation of the dotProduct, I'd need to replace the y-coordinate calculation with something like -abs(LightDir.y * Normal.y).

But the thing is - the entity uses mtl_model. Changing that now would be a pain. And even if it weren't, I still don't have a shader ready with Fog, Lighting and all that jazz. Is there one readily available? (I am aware that it shouldn't be too hard to do it, but I'm kinda cautious with turning this into a shader).


So my other idea was: Why not simply change the vecLightPos-information that DoLight (in default.fx) is using?
But I can't. I cannot change those values in the material event.



What can I do to render the (flat) model as a Sprite, as far as lighting is concerned?
Posted By: txesmi

Re: Change Light direction in Material event...? - 07/24/15 18:25

Hi,
I usually completelly remove the goraud lighting on character sprites. I do a mix of a floor shadowmap as ambient color plus simple distance lighting. When the sprite is over a block the vecAmbient vector does the job as ambient color, but over terrains or models it is needed to set the sprites UNLIT and LIGHT flags and set its color vector to the objects shadowmap color. I do all this stuff on the trace to the ground.

Code:
float4 PS (
	float4 tex : TEXCOORD0,
	float4 worldPos : TEXCOORD1 ) : COLOR0
	{
		float4 textureColor = tex2D ( ColorSampler, tex.xy );
		float4 color = vecAmbient * vecLight;
		float fDepth = distance ( vecViewPos.xyz, worldPos.xyz );
		for ( int i=0; i<iLights; i++ )
		{
			float Distance = distance ( vecLightPos[i].xyz, worldPos.xyz );
			float Radiance = saturate ( ( vecLightPos[i].w - Distance ) / vecLightPos[i].w );
			color.rgb += vecLightColor[i].rgb * Radiance;
		}
		color.rgb = saturate ( color.rgb );
		color.rgb *= textureColor.rgb;
		float Fog = saturate ( ( fDepth - vecFog.x ) * vecFog.z );
		color.rgb = lerp ( color.rgb, vecFogColor, Fog );
		color.a = min ( vecLight.w, textureColor.a );
	   return color;
	}



Code:
var height = c_trace ( &my->x, &vPos, IGNORE_ME | IGNORE_MODELS | IGNORE_SPRITES | SCAN_TEXTURE );
vec_set ( &my->blue, COLOR_WHITE );
if ( HIT_TARGET )
{
	if ( you ) // if you exits, is the terrain
	{
		if ( you->lightmap )
		{
			BMAP *bmpSkin1 = ent_getskin ( you, 1 );
			var scale = bmap_width(you->lightmap) / bmap_width(bmpSkin1);
			var format = bmap_lock ( you->lightmap, 0 );
			var cX = hit.u1 * scale;
			var cY = hit.v1 * scale;
			var pixel = pixel_for_bmap ( you->lightmap, cX, cY );
			COLOR col;
			pixel_to_vec ( &col, NULL, format, pixel );
			vec_set ( &my->blue, &col );
			set ( my, LIGHT | UNLIT );
			bmap_unlock ( you->lightmap );
		}
	}
	else // hit a block
	{
		reset ( my, LIGHT | UNLIT );
	}
}



Other way I can imagine but never did is building a normalmap that describes opposite normals to the face on the borders of the sprite, so when a light is in the sprites back some of the pixels of the border of the sprite are lighted regarding to the light direction. It could look pretty cool.

Hope it helps.
Salud!
Posted By: Error014

Re: Change Light direction in Material event...? - 07/28/15 21:03

Thank you!

It wasn't quite completely what I was looking for, but that was due to my kinda vaguely formulated post.
No worries, though! The Shader-snippet you posted actually helped me a lot to write a shader that does what I need it to.

Thanks again, and all the best!
Posted By: txesmi

Re: Change Light direction in Material event...? - 07/29/15 11:14

Glad to help laugh
Posted By: sivan

Re: Change Light direction in Material event...? - 07/29/15 20:11

it could be also solved by manipulation the vertex normals, once on entity creation.
e.g. for sprite grass I set the vertical (0,0,1), but it is better to lerp them between default normal and vertical normal, thus you get some lighting, but very softly. for me it is better as this solution is cheap and does not break the lighting of the entire scene.
© 2024 lite-C Forums