- Home /
The question is answered, right answer was accepted
Point Lights only displaying in editor on custom shader
I'm trying to write a vertex/frag shader that casts a shadow from a directional light, and then draws diffuse lighting from point lights (ideally I'd like to be able to cast shadows from those point lights as well, but I'm taking baby steps).
It was all working fine until I separated the point lights to a second pass, and that's the part where I think I'm mistaken in my understanding. The lights display in a perfect square block, which is probably a problem with my attenuation, but the bigger problem is that the point lights actually only display in the editor, and bizarrely disappear and reappear randomly while interacting with the editor's UI. Sometimes it shows in the game window only, the editor window only, or both, but never in the game window while playing. I've attached a picture of this for reference
Ignore the completely invisible objects, they haven't been UV mapped yet so their transparency is completely expected
Shader "Toon" {
Properties{
_Color("Color", Color) = (1,0,0,1)
_MainTex("Texture", 2D) = "white" {}
_RampText("Ramp", 2D) = "white" {}
}
SubShader {
Tags{
"Queue" = "Geometry"
"RenderType" = "Transparent"
}
Pass {
Tags{"LightMode" = "ForwardBase"}
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
#pragma multi_compile_fwdbase
#include "AutoLight.cginc"
struct vertInput {
float4 pos : POSITION;
float4 diff: COLOR0;
float2 uv : TEXCOORD0;
half3 norm : NORMAL;
};
struct vertOutput {
float2 uv : TEXCOORD0;
fixed3 diff: COLOR0;
float4 pos : SV_POSITION;
LIGHTING_COORDS(1,2)
};
sampler2D _MainTex;
float4 _MainTex_ST;
half4 _Color;
vertOutput vert(vertInput input) {
vertOutput o;
o.pos = UnityObjectToClipPos(input.pos);
o.uv = input.uv;
half3 worldNormal = UnityObjectToWorldNormal(input.norm);
half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
o.diff = nl * _LightColor0;
TRANSFER_VERTEX_TO_FRAGMENT(o);
return o;
}
half4 frag(vertOutput output) : SV_TARGET {
half4 c = tex2D(_MainTex, output.uv) * _Color;
fixed atten = LIGHT_ATTENUATION(output);
fixed3 lighting = output.diff * atten + unity_AmbientSky;
c.rgb *= lighting;
return c;
}
ENDCG
}
Pass {
Tags{ "LightMode" = "ForwardAdd" }
Blend One One
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
#pragma multi_compile_fwdadd
#include "AutoLight.cginc"
struct vertInput {
float4 pos : POSITION;
half3 norm : NORMAL;
};
struct vertOutput {
float4 pos : SV_POSITION;
LIGHTING_COORDS(0,1)
float3 vertexLighting : TEXCOORD2;
};
half4 _Color;
vertOutput vert(vertInput input) {
vertOutput o;
o.pos = UnityObjectToClipPos(input.pos);
half3 worldNormal = UnityObjectToWorldNormal(input.norm);
o.vertexLighting = float3(0.0,0.0,0.0);
for (int i = 0; i < 4; i++){
float4 lightPosition = float4(unity_4LightPosX0[i], unity_4LightPosY0[i], unity_4LightPosZ0[i], 1.0);
float3 difference = lightPosition.xyz - o.pos.xyz;
float3 lightDirection = normalize(difference);
float squaredDistance = dot(difference, difference);
float atten = 1.0 / (1.0 + unity_4LightAtten0[i] * squaredDistance);
float3 diff = unity_LightColor[i].rgb * _Color.rgb * atten * max(0.0, dot(worldNormal, lightDirection));
o.vertexLighting = o.vertexLighting + diff;
}
return o;
}
half4 frag(vertOutput output) : SV_TARGET {
return float4 (output.vertexLighting, 1.0);
}
ENDCG
}
}
Fallback "Legacy Shaders/VertexLit"
}
EDIT:
Ok, weird UI glitch aside, (it randomly stopped happening), it turn out this was a blending issue. The diffuse was being multiplied by the color, which was white, which was of course blending in additive blending.
Changed this:
float3 diff = unity_LightColor[i].rgb * _Color.rgb * atten * max(0.0, dot(worldNormal, lightDirection));
o.vertexLighting = o.vertexLighting + diff;
To this:
float3 diff = atten * max(0.0, dot(worldNormal, lightDirection));
o.vertexLighting = o.vertexLighting * unity_LightColor[i].rgb + diff;
Follow this Question
Related Questions
Standard Surface Shader with fade too dark 0 Answers
Block/Obscure light without shadows 0 Answers
Can I modify light position data before it gets sent to the shader? 1 Answer
Receiving Shadows On a Tranparent Shader 1 Answer
Custom (Sprite-)Shader: Combine Sprite with light and original Sprite 0 Answers