- Home /
Lighting error in Forward Rendering with vertex shader
Hi,
So I'm building some vertex shaders, but I can't seem to work out this lighting issue.
For some reason, when my vertex shader displaces vertices, there seems to be some vertex lighting that exists only on the original vertex set. It also seems these expanded vertices do not depth-write, as they appear to have sorting issues.
However, upon switching to Deferred rendering, the issue goes away entirely.
My shader code is very simple, as it's literally just the default shader with a basic vertex shader added on top:
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Standard fullforwardshadows vertex:vert
#pragma target 3.0
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
void vert(inout appdata_full v)
{
v.vertex.xyz += v.normal.xyz*0.5;
}
half _Glossiness;
half _Metallic;
fixed4 _Color;
void surf (Input IN, inout SurfaceOutputStandard o) {
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Standard"
Does anyone know what might be happening here, and how I can resolve it?
EDIT: I've noticed that this ALSO goes away if I disable shadow casting from the main directional light, so I suspect it must be something to do with that... I think the "depth" issues I was seeing were also just shadows from other objects showing through the expanded vertices.
So, I'm a little rusty on Unity's shader system, and adding addshadow
after the #pragma vertex:vert
$$anonymous$$IND OF fixed the issue. Or rather, it fixed it for that little example, but not for the case I want to actually use, which utilises multiple "passes" for the surface shader by just listing them one after the other. The shadow caster and collector passes only look at the first "pass", not any of the others.
To clarify, the actual shader I'm using works, in principle, like this:
CGINCLUDE
#pragma target 3.0
#pragma surface surf Standard fullforwardshadows vertex:vert addshadow
ENDCG
CGPROGRA$$anonymous$$
sampler2D _$$anonymous$$ainTex;
struct Input {
float2 uv_$$anonymous$$ainTex;
};
void vert(inout appdata_full v)
{
v.vertex.xyz += v.normal.xyz*0.5;
}
half _Glossiness;
half _$$anonymous$$etallic;
fixed4 _Color;
void surf (Input IN, inout SurfaceOutputStandard o) {
fixed4 c = tex2D (_$$anonymous$$ainTex, IN.uv_$$anonymous$$ainTex) * _Color;
o.Albedo = c.rgb;
o.$$anonymous$$etallic = _$$anonymous$$etallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
CGPROGRA$$anonymous$$
sampler2D _$$anonymous$$ainTex;
struct Input {
float2 uv_$$anonymous$$ainTex;
};
void vert(inout appdata_full v)
{
v.vertex.xyz += v.normal.xyz*0.5;
}
half _Glossiness;
half _$$anonymous$$etallic;
fixed4 _Color;
void surf (Input IN, inout SurfaceOutputStandard o) {
fixed4 c = tex2D (_$$anonymous$$ainTex, IN.uv_$$anonymous$$ainTex) * _Color;
o.Albedo = c.rgb;
o.$$anonymous$$etallic = _$$anonymous$$etallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
Your answer
Follow this Question
Related Questions
Diffuse fullforwardshadows shader 0 Answers
How to create light like in minecraft? 1 Answer
cast shadow - export lightmap from 3ds max to unity 2 Answers
Unity 5 Light problem 2 Answers
Point Light Behave different from editor and IOS device 0 Answers