I’m trying to make some kind of toon shader in unity responsive to additional lights and being able to receive shadows, so I tried merging some of the public proyects I found. I’m trying to fix every error I find, but I don’t know how to solve this specific error, because it mentions “shadowAttenuation” as if it doesn’t exist (or at least that’s what I thought).
The exact error is Shader error in 'Shader Graphs/Master': invalid subscript 'shadowAttenuation' at Assets/LightingCelShaded.hlsl(30) (on d3d11)
This is the script I’m working on:
#ifndef LIGHTING_CEL_SHADED_INCLUDED
#define LIGHTING_CEL_SHADED_INCLUDED
#ifndef SHADERGRAPH_PREVIEW
struct EdgeConstants {
float EdgeDiffuse;
float EdgeSpecular;
float EdgeSpecularOffset;
float EdgeDistanceAttenuation;
float EdgeShadowAttenuation;
float EdgeRim;
float EdgeRimOffset;
};
struct SurfaceVariables {
float smothness;
float shininess;
float rimThreshold;
float3 normal;
float3 view;
EdgeConstants ec;
};
float3 CalculateCelShading(Light l, SurfaceVariables s) {
float shadowAttenuationSmoothstepped = smoothstep(0.0f, s.ec.shadowAttenuation, l.shadowAttenuation);
float distanceAttenuationSmoothstepped = smoothstep(0.0f, s.ec.distanceAttenuation, l.distanceAttenuation);
float attenuation = shadowAttenuationSmoothstepped * distanceAttenuationSmoothstepped;
float diffuse = saturate(dot(s.normal, l.direction));
diffuse *= attenuation;
float3 h = SafeNormalize(l.direction + s.view);
float specular = saturate(dot(s.normal, h));
specular = pow(specular, s.shininess);
specular *= diffuse * s.smoothness;
float rim = 1 - dot(s.view, s.normal);
rim *= pow(diffuse, s.rimThreshold);
diffuse = smoothstep(0.0f, s.ec.diffuse, diffuse);
specular = s.smoothness * smoothstep(( 1 - s.smoothness) * s.ec.specular + s.ec.specularOffset, s.ec.specular + s.ec.specularOffset, specular);
rim = s.smoothness * smoothstep( s.ec.rim - 0.5f * s.ec.rimOffset, s.ec.rim + 0.5f * s.ec.rimOffset, rim);
return l.color * (diffuse + max(specular, rim));
}
#endif
void LightingCelShaded_float(float Smoothness, float RimThreshold, float3 Position, float3 Normal, float3 View, float EdgeDiffuse, float EdgeSpecular, float EdgeSpecularOffset, float EdgeDistanceAttenuation, float EdgeShadowAttenuation, float EdgeRim, float EdgeRimOffset, out float3 Color) {
#if defined(SHADERGRAPH_PREVIEW)
Color = float3(0.0f, 0.0f, 0.0f);
#else
SurfaceVariables s;
s.normal = normalize(Normal);
s.view = SafeNormalize(View);
s.smoothness = Smoothness;
s.shininess = exp2(10 * Smoothness + 1);
s.rimThreshold = RimThreshold;
s.ec.EdgeDiffuse = EdgeDiffuse;
s.ec.EdgeSpecular = EdgeSpecular;
s.ec.EdgeSpecularOffset = EdgeSpecularOffset;
s.ec.EdgeDistanceAttenuation = EdgeDistanceAttenuation;
s.ec.EdgeShadowAttenuation = EdgeShadowAttenuation;
s.ec.EdgeRim = EdgeRim;
s.ec.EdgeRimOffset = EdgeRimOffset;
#if SHADOWS_SCREEN
float4 clipPos = TransformWorldToHClip(Position);
float4 shadowCoord = ComputeScreenPos(clipPos);
#else
float4 shadowCoord = TransformWorldToShadowCoord(Position);
#endif
Light light = GetMainLight();
Color = CalculateCelShading(light, s);
int pixelLightCount = GetAdditionalLightsCount();
for (int i = 0; i < pixelLightCount; i++) {
light = GetAdditionalLight(i, Position, 1);
color += CalculateCelShading(light,s);
}
#endif
}
#endif
For sure is not the only thing I got wrong, but I’m trying to fix everything little by little, trying to understand how everything works, I’m a bit new with this.
I tried simulating and adding some values to “shadowAttenuation” and “distanceAttenuation” in this same script just at the beginning of the CalculateCelShading function, but I’m still getting the same error.
Anse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.