- Home /
Problem with Point light and shadow in Surface Shader custom Light Model.
The shadow problem : I cannot cast any shadow though i use the
#progma surface surf CustomBlinnPhong fullforwardshadows
IThe Point Light reaction problem: My model is react right in the Directional light.Then i add a Point Light,it react right when the point light is far away from it ,but my i move the point light closer to it, the model will light up the whole part suddenly when it's near to the point light's border that is not in the point light area. In the real world, it shouldn't light up if it doesn't touch the point light,and it will light up smoothly when it get closer to the light source.Here is more detail in the screenshots.And it seems the point light reaction looks different in different view direction.
and Here is my code:
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_MainTint("Diffuse Tint", Color) = (1, 1, 1, 1)
_Cutoff ("Cutoff Value", Range(0,1)) = 0.5
_NormalTex("Normal Map", 2D) = "bump" {}
_NormalIntensity("Normal Intensity", Range(0.01, 10)) = 1
_SpecularMask("Speculr Texture", 2D) = "white" {}
_SpecularColor("Specular Color", Color) = (1, 1, 1, 1)
_SpecPower("Specular Power", Range(0.01, 100)) = 1
_Gloss("Gloss", Range(0, 10)) = 0
_CubeMap ("Environment CubeMap", CUBE) = "" {}
_ReflAmount ("ReflAmount", Range(0, 10)) = 0.5
_ReflMask ("ReflectMask", 2D) = "white" {}
_EmissionMap ("Emission", 2D) = "Emission" {}
_EmissionColor ("Emission Color", Color) = (0, 0, 0, 0)
_EmissiveIntansity("Emissive Intansity", Range(0, 10)) = 1
_SSSMask("SSS Mask", 2D) = "white" {}
_BChanelIntensity("B Chanel Intensity", Range(0, 10)) = 1
_SSSColor("Skin Color", Color) = (1, 1, 1, 1)
}
SubShader
{
Tags {"RenderType"="Opaque" }
LOD 200
Cull Off
CGPROGRAM
#pragma surface surf CustomBlinnPhong alphatest:_Cutoff
#pragma target 3.0
sampler2D _MainTex;
float4 _MainTint;
sampler2D _NormalTex;
float _NormalIntensity;
sampler2D _SpecularMask;
float4 _SpecularColor;
float _SpecPower;
float _Gloss;
samplerCUBE _CubeMap;
float _ReflAmount;
sampler2D _ReflMask;
sampler2D _EmissionMap;
float4 _EmissionColor;
float _EmissiveIntansity;
struct SurfaceCustomOutput
{
fixed3 Albedo;
fixed3 Normal;
fixed3 Emission;
half Specular;
float Gloss;
float Alpha;
fixed3 SSSMask;
};
struct Input
{
float2 uv_MainTex;
float2 uv_NormalTex;
float2 uv_SpecularMask;
fixed3 rim;
float3 worldRefl;
float3 worldPos;
float3 worldNormal;
INTERNAL_DATA
};
inline fixed4 LightingCustomBlinnPhong(SurfaceCustomOutput s, fixed3 lightDir, half3 viewDir, fixed atten)
{
float3 halfVector = normalize(lightDir + viewDir);
float diff = max(0, dot(s.Normal, lightDir));
s.Normal = normalize(s.Normal);
float nh = max(0, dot(s.Normal, halfVector));
float spec = pow(nh, s.Specular * 128.0 )* s.Gloss;
float3 finalSpec = _SpecularColor.rgb * spec;
float3 sss = (1 - dot(viewDir, s.Normal)) / 50 * _SSSColor;
sss += sss * s.SSSMask.b * _BChanelIntensity;
float4 c;
c.rgb = (s.Albedo * _LightColor0.rgb * diff) + (_LightColor0.rgb * finalSpec) * (atten * 2) + sss * atten * 3;
c.a = s.Alpha;
return c;
}
void surf(Input IN, inout SurfaceCustomOutput o)
{
half4 c = tex2D(_MainTex, IN.uv_MainTex) * _MainTint;
float3 normalMap = UnpackNormal(tex2D(_NormalTex, IN.uv_NormalTex));
normalMap.z = normalMap.z / _NormalIntensity;
o.Normal = normalize(normalMap);
//Reflection- G channel in SpecularTexture
//Emissive - B channel in SpecularTexture
float4 reflMask = tex2D(_ReflMask, IN.uv_MainTex);
float3 reflection = texCUBE(_CubeMap, WorldReflectionVector(IN, o.Normal)).rgb;
float3 emission = tex2D(_EmissionMap, IN.uv_MainTex).b * _EmissionColor;
o.Emission = (reflection * reflMask.g ) * _ReflAmount;
o.Emission += emission.rgb * _EmissiveIntansity ;
//Specular - R channel in SpecularTexture
float4 specMask = tex2D(_SpecularMask, IN.uv_SpecularMask) * _SpecularColor;
o.Specular = specMask.r / _SpecPower;
o.Gloss = specMask.g * _Gloss;
//SSS - B channel in NormalTexture
float4 sssMask = tex2D(_SSSMask, IN.uv_MainTex);
o.SSSMask = sssMask.rgb;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
I am a fresh in shader.Hope your help.
$$anonymous$$aybe I should compute the position of the point light?
Answer by Abaobao · Mar 21, 2016 at 09:40 AM
I find a solution for my problem. I use the UnityGI struct
instead of the light information which is only the first light source in custom light model. The UnityGI struct is in the UnityLightingCommon.cginc
And the sample light model with UnityGI is in the Lighting.cginc
For the shadow add FallBack "Diffuse"
can solved.
But I still want to know how to solve this problem what if i don't use the UnityGI struct.
I'm also having difficulty with this. Would you be willing to explain in more detail how you solved it (i.e. a small code snippet) ?
Your answer
![](https://koobas.hobune.stream/wayback/20220612072554im_/https://answers.unity.com/themes/thub/images/avi.jpg)