Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Variable Custom Function used without having been declared

Discussion in 'Shader Graph' started by Lex_212, May 1, 2021.

  1. Lex_212

    Lex_212

    Joined:
    Oct 3, 2020
    Posts:
    84
    Hi, I am making a toon shader with URP and using a CustomLighting code and when I put the 4 outputs on my custom function it says that the variable used without having been declared. It happends with the 4 variables.
    Does anybody know how to fix it?
    Thanks

    Code (CSharp):
    1. #ifndef CUSTOM_LIGHTING_INCLUDED
    2. #define CUSTOM_LIGHTING_INCLUDED
    3.  
    4. void MainLight_float(float3 WorldPos, out float3 Direction, out float3 Color, out float DistanceAtten, out float ShadowAtten)
    5. {
    6. #if SHADERGRAPH_PREVIEW
    7.     Direction = float3(0.5, 0.5, 0);
    8.     Color = 1;
    9.     DistanceAtten = 1;
    10.     ShadowAtten = 1;
    11. #else
    12. #if SHADOWS_SCREEN
    13.     float4 clipPos = TransformWorldToHClip(WorldPos);
    14.     float4 shadowCoord = ComputeScreenPos(clipPos);
    15. #else
    16.     float4 shadowCoord = TransformWorldToShadowCoord(WorldPos);
    17. #endif
    18.     Light mainLight = GetMainLight(shadowCoord);
    19.     Direction = mainLight.direction;
    20.     Color = mainLight.color;
    21.     DistanceAtten = mainLight.distanceAttenuation;
    22.     ShadowAtten = mainLight.shadowAttenuation;
    23. #endif
    24. }
    25.  
    26. void MainLight_half(float3 WorldPos, out half3 Direction, out half3 Color, out half DistanceAtten, out half ShadowAtten)
    27. {
    28. #if SHADERGRAPH_PREVIEW
    29.     Direction = half3(0.5, 0.5, 0);
    30.     Color = 1;
    31.     DistanceAtten = 1;
    32.     ShadowAtten = 1;
    33. #else
    34. #if SHADOWS_SCREEN
    35.     half4 clipPos = TransformWorldToHClip(WorldPos);
    36.     half4 shadowCoord = ComputeScreenPos(clipPos);
    37. #else
    38.     half4 shadowCoord = TransformWorldToShadowCoord(WorldPos);
    39. #endif
    40.     Light mainLight = GetMainLight(shadowCoord);
    41.     Direction = mainLight.direction;
    42.     Color = mainLight.color;
    43.     DistanceAtten = mainLight.distanceAttenuation;
    44.     ShadowAtten = mainLight.shadowAttenuation;
    45. #endif
    46. }
    47.  
    48. void DirectSpecular_float(float3 Specular, float Smoothness, float3 Direction, float3 Color, float3 WorldNormal, float3 WorldView, out float3 Out)
    49. {
    50. #if SHADERGRAPH_PREVIEW
    51.     Out = 0;
    52. #else
    53.     Smoothness = exp2(10 * Smoothness + 1);
    54.     WorldNormal = normalize(WorldNormal);
    55.     WorldView = SafeNormalize(WorldView);
    56.     Out = LightingSpecular(Color, Direction, WorldNormal, WorldView, float4(Specular, 0), Smoothness);
    57. #endif
    58. }
    59.  
    60. void DirectSpecular_half(half3 Specular, half Smoothness, half3 Direction, half3 Color, half3 WorldNormal, half3 WorldView, out half3 Out)
    61. {
    62. #if SHADERGRAPH_PREVIEW
    63.     Out = 0;
    64. #else
    65.     Smoothness = exp2(10 * Smoothness + 1);
    66.     WorldNormal = normalize(WorldNormal);
    67.     WorldView = SafeNormalize(WorldView);
    68.     Out = LightingSpecular(Color, Direction, WorldNormal, WorldView,half4(Specular, 0), Smoothness);
    69. #endif
    70. }
    71.  
    72. void AdditionalLights_float(float3 SpecColor, float Smoothness, float3 WorldPosition, float3 WorldNormal, float3 WorldView, out float3 Diffuse, out float3 Specular)
    73. {
    74.     float3 diffuseColor = 0;
    75.     float3 specularColor = 0;
    76.  
    77. #ifndef SHADERGRAPH_PREVIEW
    78.     Smoothness = exp2(10 * Smoothness + 1);
    79.     WorldNormal = normalize(WorldNormal);
    80.     WorldView = SafeNormalize(WorldView);
    81.     int pixelLightCount = GetAdditionalLightsCount();
    82.     for (int i = 0; i < pixelLightCount; ++i)
    83.     {
    84.         Light light = GetAdditionalLight(i, WorldPosition);
    85.         half3 attenuatedLightColor = light.color * (light.distanceAttenuation * light.shadowAttenuation);
    86.         diffuseColor += LightingLambert(attenuatedLightColor, light.direction, WorldNormal);
    87.         specularColor += LightingSpecular(attenuatedLightColor, light.direction, WorldNormal, WorldView, float4(SpecColor, 0), Smoothness);
    88.     }
    89. #endif
    90.  
    91.     Diffuse = diffuseColor;
    92.     Specular = specularColor;
    93. }
    94.  
    95. void AdditionalLights_half(half3 SpecColor, half Smoothness, half3 WorldPosition, half3 WorldNormal, half3 WorldView, out half3 Diffuse, out half3 Specular)
    96. {
    97.     half3 diffuseColor = 0;
    98.     half3 specularColor = 0;
    99.  
    100. #ifndef SHADERGRAPH_PREVIEW
    101.     Smoothness = exp2(10 * Smoothness + 1);
    102.     WorldNormal = normalize(WorldNormal);
    103.     WorldView = SafeNormalize(WorldView);
    104.     int pixelLightCount = GetAdditionalLightsCount();
    105.     for (int i = 0; i < pixelLightCount; ++i)
    106.     {
    107.         Light light = GetAdditionalLight(i, WorldPosition);
    108.         half3 attenuatedLightColor = light.color * (light.distanceAttenuation * light.shadowAttenuation);
    109.         diffuseColor += LightingLambert(attenuatedLightColor, light.direction, WorldNormal);
    110.         specularColor += LightingSpecular(attenuatedLightColor, light.direction, WorldNormal, WorldView, half4(SpecColor, 0), Smoothness);
    111.     }
    112. #endif
    113.  
    114.     Diffuse = diffuseColor;
    115.     Specular = specularColor;
    116. }
    117.  
    118. #endif
     

    Attached Files: