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 Custom Function Undeclared Identifier HELP

Discussion in 'Shader Graph' started by unity_416jraimondi, Aug 18, 2021.

  1. unity_416jraimondi

    unity_416jraimondi

    Joined:
    Aug 6, 2018
    Posts:
    2
    I'm following a written explanation by someone who us creating a custom function. I'm working in 2020.3.8f1 in URP.

    There was an error I ran into regarding an 'undeclared identifier' that got solved when changing #ifdef SHADERGRAPH_PREVIEW to #if defined(SHADERGRAPH_PREVIEW) .

    In another custom function I'm just adding onto that but having the same issue with a #ifndef SHADERGRAPH_PREVIEW . Did #ifndef also get changed? I'll post below this function and let me know if something else is getting buggy.

    Code (CSharp):
    1. void AddAdditionalLights_float(float3 WorldPosition, float3 WorldNormal, float MainDiffuse, out float Diffuse) { //takes the world pos, world normal and main diffuse
    2.     Diffuse = MainDiffuse; //diffuse is mainlight diffuse
    3. #ifndef SHADERGRAPH_PREVIEW //if shadergraph preview not defined
    4.     int pixelLightCount = GetAdditionalLightsCount(); //get the number of lights in scene
    5.     for (int i = 0; i < pixelLightCount; ++i) { //for each loop, starting at 0 through to total light count:
    6.         Light light = GetAdditionalLight(i, WorldPosition); //get additional light i (from the loop) at world pos
    7.         half NdotL = saturate(dot(WorldNormal, light.direction)); //clamps the dot product 0-1 between the world normal and light direction
    8.         half atten = light.distanceAttenuation * light.shadowAttenuation; //multiplies distance and shadow atten
    9.         half thisDiffuse = atten * NdotL; //diffuse is atten * NdotL
    10.         Diffuse += thisDiffuse; //adds this to the original diffuse
    11.     }
    12. #endif
    13.     half total = Diffuse;
    14. }
    15. #endif
     
  2. Sinterklaas

    Sinterklaas

    Joined:
    Jun 6, 2018
    Posts:
    93
    Hi, you can use #if !defined() instead of #ifndef, just as #if defined() can be used instead of #ifdef. Note the exclamation mark within the former directive.

    Though I should probably mention that the bug where using #ifdef and #ifndef would cause compile errors seems to be have been fixed in Shader Graph 10.5.0 (the version currently associated with Unity 2020.3 LTS). At least, I no longer get them ever since I switched to LTS. Not that it's super important, since both directives have alternatives, but you know.

    That being said, you seem to be having one #endif directive too many in your code, which is going to cause a "unexpected #endif directive" error.