- Home /
How to calculate light from behind a quad? (and other light positions)
So here's the deal:
I am using Quads to render sprites into the 3D world. I am NOT trying to create a 2D game. For a reference of something similar to what I am doing, think of Paper Mario.
So here's the problem:
I want the sprites to be lit up by lights, and they are. However, they are only lit up from in front of the quad. Any light behind it or perfectly to the side, above and below will not apply any light to the quad.
So here's what I think I need to do:
I think that I need to calculate the normal 6 times, 1 for each direction. Either that or calculate the light per vertex based solely on how far away the light source is without normals. The problem with the latter is that directional lights won't work, which is sorta ok, I can probably find a work around to that. However I don't know how to do either of these things in a shader.
So here's the shader I have made so far:
This shader takes 2 textures, 1 is the base sprite and the other is an overlay of details such as a stripe texture or a checker texture. Both of these can be colored individually.
Shader "Custom/Simple" {
Properties {
_MainColor ("Texture Tint", Color) = (1,1,1,1)
_Main ("Main", 2D) = "white" {}
_DetailColor ("Texture Tint", Color) = (1,1,1,1)
_Detail ("Detail", 2D) = "white" {}
}
SubShader {
Tags {"RenderType" = "Transparent"}
CGPROGRAM
#pragma surface surf Lambert alpha
struct Input {
float2 uv_Main;
float2 uv_Detail;
};
sampler2D _Main;
sampler2D _Detail;
fixed4 _MainColor;
fixed4 _DetailColor;
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D(_Main, IN.uv_Main) * _MainColor;
fixed4 d = tex2D (_Detail, IN.uv_Detail) * _DetailColor;
if (d.a > 0)
o.Albedo = c.rgb * d.rgb;
else
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Transparent/Diffuse"
}
If you know the solution to my problem or can point me in the right direction, then I will be very grateful.
Answer by Eonwulf · Oct 17, 2013 at 09:50 PM
So I started putting sprites into my scene and noticed a lot of problems with them being drawn in the wrong order so I had to do some research. Apparently the only thing that does a proper order is the Transparency Cutoff shaders, so I downloaded them and modified the diffuse one. I learned about controlling the lighting of a surface shader and finally came up with a solution that doesn't have any problems that I can see for now.
Shader "Custom/Sprite2" {
Properties {
_MainCol ("Main Tint", Color) = (1,1,1,1)
_MainTex ("Main Texture", 2D) = "white" {}
_DetailCol ("Detail Tint", Color) = (1,1,1,1)
_DetailTex ("Detail Texture", 2D) = "white" {}
_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
}
SubShader {
Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
Cull off
LOD 200
CGPROGRAM
#pragma surface surf SimpleLambert alphatest:_Cutoff
sampler2D _MainTex;
fixed4 _MainCol;
sampler2D _DetailTex;
fixed4 _DetailCol;
half4 LightingSimpleLambert (SurfaceOutput s, half3 lightDir, half atten) {
half4 c;
c.rgb = s.Albedo * _LightColor0.rgb * (atten);
c.a = s.Alpha;
return c;
}
struct Input {
float2 uv_MainTex;
float2 uv_DetailTex;
};
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _MainCol;
fixed4 d = tex2D(_DetailTex, IN.uv_DetailTex) * _DetailCol;
o.Albedo = lerp(c.rgb, d.rgb, d.a);
o.Alpha = c.a;
}
ENDCG
}
Fallback "Transparent/Cutout/VertexLit"
}