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 "Redefinition of _Time" error in Custom Function Node

Discussion in 'Shader Graph' started by Deleted User, Apr 22, 2019.

  1. Deleted User

    Deleted User

    Guest

    Hello,
    I have been running into trouble getting the new Custom Function Node to do just about anything. I have followed a number of examples online but any time I try use
    #include "Packages/com.unity.render-pipelines.lightweight/ShaderLibrary/Lighting.hlsl" the node throws the error "redefinition of _Time" and I can not seem to find anyone who has a solution to this problem. Any help would be appreciated!

    The different hlsl files I have tried to use are :
    Code (CSharp):
    1. #include "Packages/com.unity.render-pipelines.lightweight/ShaderLibrary/Lighting.hlsl"
    2. void GetMainLightD_float(out float3 Direction, out float3 Color, out float Attenuation)
    3. {
    4.     #ifdef SHADERGRAPH_PREVIEW
    5.         Direction = float3(-0.5, 0.5, -0.5);
    6.         Color = float3(1,1,1);
    7.         Attenuation = 0.4;
    8.     #else
    9.         Light light = GetMainLight();
    10.         Direction = light.direction;
    11.         Attenuation = light.distanceAttenuation;
    12.         Color = light.color;
    13.     #endif
    14. }
    15.  
    and

    Code (CSharp):
    1. #include "Packages/com.unity.render-pipelines.lightweight/ShaderLibrary/Lighting.hlsl"
    2. void GetMainLightDir_float(out float3 lightDir)
    3. {
    4.     lightDir = GetMainLight().direction;
    5. }
     
  2. alexandral_unity

    alexandral_unity

    Unity Technologies

    Joined:
    Jun 18, 2018
    Posts:
    163
    Instead of the include, try wrapping your function in an ifdef for lighting:
    Code (CSharp):
    1. #ifdef LIGHTWEIGHT_LIGHTING_INCLUDED
    2.     //function body
    3. #endif
    It's not the prettiest, but it should work just fine without the redefinition errors.
     
    bajja and (deleted member) like this.
  3. Deleted User

    Deleted User

    Guest

    Thank you so much! This fixes that problem, though now I am dealing with "output Parameter 'Direction' not completely initialized", which is also giving me trouble.

    Is there some documentation or information on using the Custom Function node that I am missing? Really appreciate the help and I don't want to take too much of your time!
     
  4. Deleted User

    Deleted User

    Guest

    Solved it! Simply had to add a little code, final results below, haven't really tested it yet though its at least nice to not face an error.

    Code (CSharp):
    1. void GetLightingInformation_float(out float3 Direction, out float3 Color,out float Attenuation)
    2. {
    3.     Direction = float3 (0,0,0);
    4.         Color = float3 (0,0,0);
    5.         Attenuation = 0;
    6.    
    7.     #ifdef LIGHTWEIGHT_LIGHTING_INCLUDED
    8.  
    9.     Light mainLight = GetMainLight();
    10.             Color = mainLight.color;
    11.             Direction = mainLight.direction;
    12.             float4 shadowCoord;
    13.             #ifdef _SHADOWS_ENABLED
    14.             #if SHADOWS_SCREEN
    15.                 float4 clipPos = TransformWorldToHClip(WorldPos);
    16.                 shadowCoord = ComputeShadowCoord(clipPos);
    17.             #else
    18.                 shadowCoord = TransformWorldToShadowCoord(WorldPos);
    19.             #endif
    20.             mainLight.attenuation = MainLightRealtimeShadowAttenuation(shadowCoord);
    21.             #endif
    22.             Attenuation = mainLight.attenuation;
    23. #endif
    24.    
    25. }
    Thanks again for the help!
     
    NeatWolf likes this.
  5. alexandral_unity

    alexandral_unity

    Unity Technologies

    Joined:
    Jun 18, 2018
    Posts:
    163
    We're working on getting our updated docs and a bit of demo content up for the Custom Function Node but it isn't live yet!
     
    Deleted User, Fainth and NeatWolf like this.
  6. Fainth

    Fainth

    Joined:
    Apr 19, 2019
    Posts:
    11
    Did you manage to get this code to work?
    If so, please let me know how. I only got the default 0 value's declared at the top returned.

    (if not, this should work as a temporary solution)
     
  7. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    632
    @alexandral_unity
    Since no documentation was written by Unity, and I've been frying my brain with trial and error for the last week.
    I would like to know how to make this shader code work with the Universal RP.

    No matter what #includes I add or remove, how many #ifdefs I tried I keep getting `identifier undeclared` errors.
    The furthest I got is by surrounding only the function bodies with:
    Code (CSharp):
    1. #ifdef UNIVERSAL_LIGHTING_INCLUDED
    2. ....
    3. #endif
    Because doing it globally makes my function names undeclared, and then initialize my out vars before, which is even more stupid.

    But then I would get this weird:
    Shader error in 'Unlit Master': 'GetAdditionalLight': no matching 2 parameter function 

    And yet one more
     undeclared identifier 'WorldPos'


    Complete code:
    Code (CSharp):
    1.  
    2.  
    3. half3 Lambert_half(half3 N, half3 color, half3 direction, half distAtten, half shadowAtten) // Could use LightingLambert directly from calling site instead
    4. {
    5. #ifdef UNIVERSAL_LIGHTING_INCLUDED
    6.  
    7.     half3 attenColor = color * distAtten * shadowAtten;
    8.     half NdotL = saturate(dot(N, direction));
    9.  
    10.     return NdotL * attenColor;
    11. #endif
    12. }
    13.  
    14. half3 BlinnPhong_half(half3 N, half3 V, half3 color, half3 direction, half3 specColor, half smoothness)
    15. {
    16. #ifdef UNIVERSAL_LIGHTING_INCLUDED
    17.  
    18.     smoothness = exp2(10 * smoothness + 1);
    19.  
    20.     N = normalize(N);
    21.     V = SafeNormalize(V);
    22.  
    23.     half3 Out = LightingSpecular(color, direction, N, V, half4(specColor, 0), smoothness);
    24.     return Out;
    25. #endif
    26. }
    27.  
    28. void DiffSpec_half(half3 N, half3 V, half3 WorlPos, half3 SpecColor, half Smoothness,  out half3 Diffuse, out half3 Specular)
    29. {  
    30.     Diffuse = 0;
    31.     Specular = 0;
    32. #ifdef UNIVERSAL_LIGHTING_INCLUDED
    33.  
    34.     half3 diffuseColor = 0;
    35.     half3 specularColor = 0;
    36.  
    37.     Light l = GetMainLight(); // GET MAIN LIGHT
    38.     int pixelLightCount = GetAdditionalLightsCount();
    39.  
    40.     diffuseColor += Lambert_half(N, l.color, l.direction, l.distanceAttenuation, l.shadowAttenuation);
    41.  
    42.     specularColor += BlinnPhong_half(N, V, l.color, l.direction, SpecColor, Smoothness);          
    43.  
    44.     for (int i = 0; i < pixelLightCount; ++i)
    45.     {
    46.         Light light = GetAdditionalLight(i, WorldPos); // GET ADDT LIGHTS
    47.         diffuseColor += Lambert_half(N, light.color, light.direction, light.distanceAttenuation, light.shadowAttenuation);
    48.         specularColor += BlinnPhong_half(N, V, light.color, light.direction, SpecColor, Smoothness);
    49.     }
    50.  
    51.     Diffuse = diffuseColor;
    52.     Specular = specularColor;
    53. #endif
    54. }
    55.  
    I also would like to know how to get vertex data and view data and matrices from shader code.
    And of cource proper documentation from the responsible team and opening issues section in the GitHur repo.

    Untitled.png
     
    Last edited: Dec 17, 2019
    bajja likes this.
  8. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    632
    @alexandral_unity Wrapping my for loop in

    #ifdef _ADDITIONAL_LIGHTS

    Stopped the error because it's not defined but I can't sample additional lights.
    Is there any improvements on custom shader functions?
     
  9. zezba9000

    zezba9000

    Joined:
    Sep 28, 2010
    Posts:
    985
    "#ifdef UNIVERSAL_LIGHTING_INCLUDED"
    This does not work. Was the name changed?
     
    SgerbwdGwyn and The_Swiss_Guy like this.
  10. mysticcoder

    mysticcoder

    Joined:
    Oct 9, 2014
    Posts:
    7
  11. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    632
  12. unity_R84Ylyu35MIx_g

    unity_R84Ylyu35MIx_g

    Joined:
    Nov 9, 2019
    Posts:
    2
    I'm using Unity 2020.3.7 and URP 10.4.0 and got the same problems of _Time redefinition in lighting.hlsl.

    I tried all the solution above and nothing works.

    So I ask if there is a way to downgrade the URP version ?
     
  13. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    632
    Yes you can downgrade but it won't solve your problem.
    Are defining or referencing a Light struct?
     
  14. qwmnerbvqwmn

    qwmnerbvqwmn

    Joined:
    Dec 20, 2018
    Posts:
    1
    LIGHTWEIGHT_LIGHTING_INCLUDED is defined by Lighting.hlsl in LWRP, it is renamed to
    UNIVERSAL_LIGHTING_INCLUDED in URP 。UNIVERSAL_LIGHTING_INCLUDED should work.
    the path of Lighting.hlsl is "Library/PackageCache/com.unity.render-pipelines.universal@10.5.1/ShaderLibrary/Lighting.hlsl"


    Code (CSharp):
    1. #ifdef UNIVERSAL_LIGHTING_INCLUDED
    2.     //function body
    3. #endif
     
  15. kodra_dev

    kodra_dev

    Joined:
    Oct 31, 2022
    Posts:
    108
    Just in case someone stumbled upon this issue:

    The bullet-proof way to define your own lighting function in a .hlsl is:

    Code (CSharp):
    1.  
    2. #if !SHADERGRAPH_PREVIEW
    3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
    4. #endif
    5.  
    6. #ifndef MY_CUSTOM_LIGHTING
    7. #define MY_CUSTOM_LIGHTING
    8.  
    9. void MainLight_float(out float3 Direction, out float3 Color)
    10. {
    11. #if SHADERGRAPH_PREVIEW
    12.     Direction = float3(0.5, 0.5, 0);
    13.     Color = float3(1, 1, 1);
    14. #else
    15.     Light light = GetMainLight();
    16.     Direction = light.direction;
    17.     Color = light.color;
    18. #endif
    19. }
    20.  
    21. #endif
    22.  
    Don't do anything with UNIVERSAL_LIGHTING_INCLUDED.

    The actual culprit is not Lighting.hlsl, but the preview in shader graph. For unknown stupid reasons, the preview seems to be generated by some built-in cg program, which doesn't work with those URP shaderlibrary files.
     
    Last edited: Nov 30, 2022
  16. Finijumper

    Finijumper

    Joined:
    Jul 12, 2016
    Posts:
    78

    How could I include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl" ?

    If I do what you said I'm getting: "unrecognised identifier InputData", and if I put the include after I get this error: "Redefinition of _Time".
     
  17. Louis-Viel

    Louis-Viel

    Joined:
    Oct 16, 2021
    Posts:
    1
    Hello, you dont need to include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl", because it is already included by "Packages/com.unity.render-pipelines.universal/ShaderLibrary/RealtimeLights.hlsl", wich is itself included by "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl".

    So with just "Lighting" you have all you're looking for.
    And if you want to whatever case have "Input.hlsl", you have to replace this :
    1. #if !SHADERGRAPH_PREVIEW
    2. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
    3. #endif
    With this :
    1. #if !SHADERGRAPH_PREVIEW
    2. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
    3. #else
    4. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl"
    5. #endif
    Hope this help you ^^
     
  18. patti_jajusaek1

    patti_jajusaek1

    Joined:
    Mar 21, 2023
    Posts:
    1
    YOU ARE A GOD!!!!!! THIS WORKS!!! And by using this method I can successfully include any hlsl in URP package!!!! LEGEND!!!!