Hi there!
Im trying to make a toon shader and Im currently using the recent Unite 2019 talk about that topic and the blog post mentioned in that video as resources for code and learning to understand shaders. This is my very first shader, and while I’m getting a long ways, I’m stuck at an issue I just can’t figure out or find anything about.
For the shader, I simulate lambertian lighting. For the main light, there’s a special piece of code that I don’t really understand that helps get the light and allows objects to cast shadows. Its straight from the video, but here it is anyway:
void MainLight_half (float3 WorldPos, out half3 Direction, out half3 Color, out half DistanceAtten, out half ShadowAtten)
{
#if SHADERGRAPH_PREVIEW
Direction = half3(0.5, 0.5, 0);
Color = 1;
DistanceAtten = 1;
ShadowAtten = 1;
#else
#if SHADOWS_SCREEN
half4 clipPos = TransformWorldToHClip(WorldPos);
half4 shadowCoord = ComputeScreenPos(clipPos);
#else
half4 shadowCoord = TransformWorldToShadowCoord(WorldPos);
#endif
Light mainLight = GetMainLight(shadowCoord);
Direction = mainLight.direction;
Color = mainLight.color;
DistanceAtten = mainLight.distanceAttenuation;
#if !defined(_MAIN_LIGHT_SHADOWS) || defined(_RECEIVE_SHADOWS_OFF)
ShadowAtten = 1.0h;
#endif
#if SHADOWS_SCREEN
ShadowAtten = SampleScreenSpaceShadowmap(shadowCoord);
#else
ShadowSamplingData shadowSamplingData = GetMainLightShadowSamplingData();
half shadowStrength = GetMainLightShadowStrength();
ShadowAtten = SampleShadowmap(shadowCoord, TEXTURE2D_ARGS(_MainLightShadowmapTexture, sampler_MainLightShadowmapTexture), shadowSamplingData, shadowStrength, false);
#endif
#endif
}
I’ve tried reverse engineering it and looking in the lighting.hlsl code to figure out how I can also allow my additional lights to cast shadows. I’ve got them working to the point where they light up objects with their own color, but I can’t figure out how objects can also stop the light from hitting other objects that they are obscuring (or rather, the parts of it that they’re obscuring).
Does anyone know how to do this?
Currently, the code I’m using looks like this:
void AdditionalLights_half(half3 WorldPosition, out half3 Direction, out half3 Color, out half DistanceAtten, out half ShadowAtten)
{
half3 diffuseColor = 0;
half3 specularColor = 0;
Color = 0;
half shadowAtten = 0;
half distanceAtten = 0;
Direction = 0;
#ifndef SHADERGRAPH_PREVIEW
int pixelLightCount = GetAdditionalLightsCount();
for (int i = 0; i < pixelLightCount; ++i)
{
Light light = GetAdditionalLight(i, WorldPosition);
Color = light.color;
distanceAtten = light.distanceAttenuation;
shadowAtten = light.shadowAttenuation;
Direction = light.direction;
}
#endif
ShadowAtten = shadowAtten;
DistanceAtten = distanceAtten;
}
And the nodes that set up the lighting:
If there’s any other info I need to provide, I’ll gladly do so to the best of my ability.
Thanks in advance!