Looking for some inside in light shader for 2d scene that is using multiple layers.
this works:
float4 PixelShaderFunction(float4 pos : SV_POSITION, float4 color1 : COLOR0, float2 texCoord : TEXCOORD0) : SV_TARGET0
{
float4 tex = tex2D(TextureSampler, texCoord);
if (tex.a != 0)
{
float4 texNormal = tex2D(Sampler, texCoord) - float4(.5, .5, .5, .5);
if (texNormal.a != 0)
{
float3 texC3;
texC3.x = texCoord.x * (xWidth);
texC3.y = texCoord.y * (xHeight);
texC3.z = 0;
float4 shadingResult = float4(0, 0, 0, 0);
float3 displacement = normalize(xLightPosition.xyz - texC3);
float intensity = dot(texNormal.xyz, displacement);
shadingResult += intensity * xLightColor;
float3 halfvector = normalize(displacement + float3(0, 0, -1.0));
float specintensity = pow(saturate(dot(halfvector, texNormal.xyz)), .1);
shadingResult += specintensity * xLightColor;
shadingResult += float4(.4, .4, .4, 1.0f);
return saturate(tex * shadingResult * color1);
}
return tex;
}
return tex;
}
Ingame Screenshot
Normalmap
Spriteilluminator
I am using normal maps,created with the tool “spriteilluminator”
I could need a shader where i get the same result in game and in tool. That would be really helpful.
Not quite sure what the layering part has to do with the shader.
I found this tutorial extremely helpful when I implemented lighting into my 2d engine:
When it comes to layers I simply render each layer into a separate render target, using three layers whereof each has unique lighting, for instance;
- a light in the landscape layer is only affecting objects rendered within the same layer
- a object within the layer in front of the landscape layer is not affected by the lighting in the landscape layer
This is of course by design and related to the rendering process itself rather than the shader.
Another helpful resource is the this tutorial:
http://www.soolstyle.com/2010/02/15/2d-deferred-lightning/
I thank you for your answer!
I tried the second link.
I just couldnt get the example Project running with monogame.(There is is download link for XNA 4.0)
I faced some strange errors:
-in FX-Files , variable there but not used(theres also a setValue in Game1.cs)
-in FX-Files truncate warnings because types are not match(and i dont know how the file should look like)
And maybe others i am not aware.
It runs but theres no “Light” as shown in video.
http://www.soolstyle.com/2010/06/29/2d-lightning-continued/
Change these two lines in MultiTarget.fx:
float coneAttenuation = saturate(1.0f - length(lightDirection) / lightDecay);
float3 halfVec = float3(0, 0, 1);
To this:
float coneAttenuation = saturate(length(lightDirection) / lightDecay);
float3 halfVec = float3(0, 0, -1);
Took me sometime to figure that out…
Looks way better now. Will playaround later further with it, but thought share this first.
I still have no idea how i get it like in the video. What would make it usable… ;/