Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Redefinition of _Time

Discussion in 'Shaders' started by calpolican, May 4, 2023.

  1. calpolican

    calpolican

    Joined:
    Feb 2, 2015
    Posts:
    400
    I'm a bit new to shader coding, and I'm experiencing an issue. When I try to use the RealLight.hlsl that come in the URP package to get some structs and macros, I get this redefinition error nightmare.
    First it tells me there's a redefinition of _Time, and if you change that name, it keeps going to _SinTime and such. The AI told me that you could use a "#pragma once" directive to avoid any redefinition, but it wasn't recognized by unity.
    I'm using Unity 2021.3.15f and URP 12.1.8.

    Here's my shader:
    Code (CSharp):
    1. Shader "Custom/TestAnisotropic"
    2. {
    3.  
    4.     Properties
    5.     {
    6.         _Color ("Color Tint", Color) = (1,1,1,1)
    7.         _Albedo ("Albedo", 2D) = "white" {}
    8.         _Normal ("Normal Map", 2D) = "bump" {}
    9.         _NormalScale ("Normal Scale", Float) = 1
    10.         _AO ("Ambient Occlusion", 2D) = "white" {}
    11.         _AOScale ("AO Scale", Float) = 1
    12.         _Smoothness ("Smoothness", Range(0,1)) = 0.5
    13.         _Metallic ("Metallic", Range(0,1)) = 0.5
    14.         _Anisotropy ("Anisotropy", Range(-1,1)) = 0
    15.         _UVScale ("UV Scale", Vector) = (1,1,0,0)
    16.     }
    17.  
    18.  
    19.  
    20.     SubShader
    21.     {
    22.         Tags { "RenderType"="Opaque" }
    23.         LOD 100
    24.      
    25.  
    26.         Pass
    27.         {
    28.      
    29.             CGPROGRAM
    30.             #pragma vertex vert
    31.             #pragma fragment frag          
    32.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    33.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/RealtimeLights.hlsl"
    34.  
    35.  
    36.             struct appdata
    37.             {
    38.                 float4 vertex : POSITION;
    39.                 float2 uv : TEXCOORD0;
    40.                 float3 normal : NORMAL;
    41.                 float4 tangent : TANGENT;
    42.             };
    43.  
    44.             struct v2f
    45.             {
    46.                 float2 uv : TEXCOORD0;
    47.                 float3 worldPos : TEXCOORD1;
    48.                 float3 worldNormal : TEXCOORD2;
    49.                 float3 worldTangent : TEXCOORD3;
    50.                 float3 worldBitangent : TEXCOORD4;
    51.                 float3 viewDir : TEXCOORD5;
    52.                 float4 vertex : SV_POSITION;
    53.             };
    54.  
    55.  
    56.             sampler2D _Albedo;
    57.             sampler2D _Normal;
    58.             sampler2D _AO;
    59.             float4 _Color;
    60.             float _NormalScale;
    61.             float _AOScale;
    62.             float _Smoothness;
    63.             float _Metallic;
    64.             float _Anisotropy;
    65.             float4 _UVScale;
    66.             float4 _Albedo_ST;
    67.             float4 _Normal_ST;
    68.             float4 _AO_ST;
    69.  
    70.  
    71.             v2f vert (appdata v)
    72.             {
    73.                 v2f o;
    74.                 o.vertex = UnityObjectToClipPos(v.vertex);
    75.                 o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
    76.                 o.worldNormal = UnityObjectToWorldNormal(v.normal);
    77.                 o.worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
    78.                 o.worldBitangent = cross(o.worldNormal, o.worldTangent) * v.tangent.w;
    79.                 o.viewDir = normalize(_WorldSpaceCameraPos - o.worldPos);
    80.                 o.uv = TRANSFORM_TEX(v.uv, _Albedo) * _UVScale.xy + _UVScale.zw;
    81.                 return o;
    82.             }
    83.  
    84.             fixed4 frag (v2f i) : SV_Target
    85.             {
    86.                 // Sample textures
    87.                 fixed4 albedo = tex2D(_Albedo, i.uv) * _Color;
    88.                 fixed3 normal = UnpackNormal(tex2D(_Normal, i.uv)) * _NormalScale;
    89.                 fixed ao = tex2D(_AO, i.uv).r * _AOScale;
    90.  
    91.                 // Calculate the TBN matrix
    92.                 fixed3x3 TBN = fixed3x3(i.worldTangent, i.worldBitangent, i.worldNormal);
    93.  
    94.                 // Transform the normal to world space
    95.                 normal = normalize(mul(TBN, normal));
    96.  
    97.                 // Calculate the light direction and intensity
    98.                 fixed3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
    99.                 Light light = GetAdditionalLight(0, i.worldPos);
    100.                 fixed atten = light.distanceAttenuation * light.shadowAttenuation;
    101.                 fixed3 lightColor = atten * _LightColor0.rgb;
    102.  
    103.                 // Calculate the half vector
    104.                 fixed3 Hn = normalize(lightDir + i.viewDir);
    105.  
    106.                 // Rotate the half vector by Anisotropy radians around the normal
    107.                 fixed3 Ht = normalize(cos(_Anisotropy) * Hn + sin(_Anisotropy) * cross(normal, Hn));
    108.  
    109.                 // Calculate the dot products
    110.                 fixed dotNL = saturate(dot(normal, lightDir));
    111.                 fixed dotNV = saturate(dot(normal, i.viewDir));
    112.  
    113.                 // Calculate the anisotropic distribution function
    114.                 fixed alpha = _Smoothness * _Smoothness;
    115.                 fixed D = alpha / (PI * pow(dotHT * dotHT * (alpha - 1) + 1, 2));
    116.  
    117.                 // Calculate the fresnel term
    118.                 fixed F = lerp(0.04, 1, pow(1 - dotNV, 5));
    119.  
    120.                 // Calculate the geometric term
    121.                 fixed k = alpha * 0.5;
    122.                 fixed G = dotNL / (dotNL * (1 - k) + k) * dotNV / (dotNV * (1 - k) + k);
    123.  
    124.                 // Calculate the specular term
    125.                 fixed3 specular = D * F * G * _Anisotropy;
    126.  
    127.                 // Calculate the diffuse term
    128.                 fixed3 diffuse = lerp(fixed3(1) - F, fixed3(0), _Metallic) * albedo.rgb;
    129.  
    130.                 // Calculate the final color
    131.                 fixed3 color = (diffuse + specular) * lightColor * dotNL;
    132.  
    133.                 // Apply ambient occlusion
    134.                 color *= ao;
    135.  
    136.                 // Output the color
    137.                 return fixed4(color, 1);
    138.             }
    139.  
    140.             ENDCG
    141.         }
    142.     }
    143.  
    144. }
     
    Last edited: May 4, 2023
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,495
    calpolican likes this.
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,236
    That should be
    HLSLPROGRAM
    and
    ENDHLSL
    for URP shaders.
     
    calpolican likes this.
  4. calpolican

    calpolican

    Joined:
    Feb 2, 2015
    Posts:
    400
    Thanks for those docs. Indeed I was using AI. I have very little knolowedge at writing shaders, but I can write code in C#, and I noticed it makes lots of mistakes there, so I know it's not a good idea to relay on it for anything serious. In this case is just for exploration, and it's much better than the alternative (wich is not even trying XD). I can say though, that I have learned a lot from asking it questions about the code. It was able to satisfy most of my doubts, and even find the error in the code above, eventually. In my defense, shader coding has a super steep learning curve if you don't have much guidance. Specially since there's no autocomplition, the IDE doesn't warn you of any mistake in the code, and there's no way to find the references to anything. Haven't found a lot of documentation either, many official samples don't compile, and even when I create a default URP surface shader to learn from it, it comes out pink. For this reason I have move on to Shader Graph for the most part.

    Thanks a lot, that was it.

    Here's the code fixed, that compiles. The result looks pretty awful though. From the looks of it, it's probably because it's not using reflection probes, and I haven't figured a way of puting them there. Smoothness is not doing what's suppouse to either. So, I guess I'm gonna keep exploring whenever I have some time:

    Code (CSharp):
    1. Shader "Custom/TestAnisotropic"
    2. {
    3.  
    4.     Properties
    5.     {
    6.         _Color ("Color Tint", Color) = (1,1,1,1)
    7.         _Albedo ("Albedo", 2D) = "white" {}
    8.         _Normal ("Normal Map", 2D) = "bump" {}
    9.         _NormalScale ("Normal Scale", Float) = 1
    10.         _AO ("Ambient Occlusion", 2D) = "white" {}
    11.         _AOScale ("AO Scale", Float) = 1
    12.         _Smoothness ("Smoothness", Range(0,1)) = 0.5
    13.         _Metallic ("Metallic", Range(0,1)) = 0.5
    14.         _Anisotropy ("Anisotropy", Range(-3.14,3.14)) = 0
    15.         _UVScale ("UV Scale", Vector) = (1,1,0,0)
    16.  
    17.         //Only for transparent/CutOut type of surface
    18.         _Surface("__surface", Float) = 0.0 //1.0 es transparente
    19.         _Blend("__blend", Float) = 0.0 // 1 es On.
    20.         _SrcBlend("__src", Float) = 1.0 //SrcAlpha = 5.0
    21.         _DstBlend("__dst", Float) = 0.0 //10.0 = OneMinusSrcAlpha
    22.         _ZWrite("__zw", Float) = 1.0 //Off
    23.     }
    24.  
    25.  
    26.     SubShader
    27.     {
    28.  
    29.  
    30.         Tags { "RenderType"="Opaque" "RenderPipeline" = "UniversalRenderPipeline" "IgnoreProjector" = "True"  }
    31.         Blend [_SrcBlend] [_DstBlend]
    32.         ZWrite [_ZWrite]
    33.  
    34.         //Only used if there are other subshader to choose wich is prefered
    35.         LOD 100
    36.  
    37.         Pass
    38.         {
    39.  
    40.  
    41.             HLSLPROGRAM
    42.  
    43.             #pragma vertex vert
    44.             #pragma fragment frag
    45.  
    46.  
    47.             #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
    48.             #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Filtering.hlsl"
    49.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    50.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
    51.             #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
    52.             #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"
    53.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl"
    54.  
    55.  
    56.             struct appdata
    57.             {
    58.                 float4 vertex : POSITION;
    59.                 float2 uv : TEXCOORD0;
    60.                 float3 normal : NORMAL;
    61.                 float4 tangent : TANGENT;
    62.             };
    63.  
    64.  
    65.             struct v2f
    66.             {
    67.                 float4 vertex : SV_POSITION; // The clip space position of the vertex. This is used by HLSL to rasterize the triangle.
    68.                 float3 worldPos : TEXCOORD1; // The world space position of the vertex. This is interpolated perspective-correctly across the triangle and passed to the fragment function.
    69.                 float3 worldNormal : TEXCOORD2; // The world space normal vector of the vertex. This is interpolated perspective-correctly across the triangle and passed to the fragment function.
    70.                 float3 worldTangent : TEXCOORD3; // The world space tangent vector of the vertex. This is interpolated perspective-correctly across the triangle and passed to the fragment function.
    71.                 float3 worldBitangent : TEXCOORD4; // The world space bitangent vector of the vertex. This is interpolated perspective-correctly across the triangle and passed to the fragment function.
    72.                 float3 viewDir : TEXCOORD5; // The world space view direction vector of the vertex. This is interpolated perspective-correctly across the triangle and passed to the fragment function.
    73.                 float2 uv : TEXCOORD6; // The texture coordinate of the vertex. This is interpolated perspective-correctly across the triangle and passed to the fragment function.
    74.             };
    75.  
    76.  
    77.             //Explain to HLSL what each property is in it's own lenguage:
    78.             sampler2D _Albedo;
    79.             sampler2D _Normal;
    80.             sampler2D _AO;
    81.             float4 _Color;
    82.             float _NormalScale;
    83.             float _AOScale;
    84.             float _Smoothness;
    85.             float _Metallic;
    86.             float _Anisotropy;
    87.             float4 _UVScale;
    88.             float4 _Albedo_ST;
    89.             float4 _Normal_ST;
    90.             float4 _AO_ST;
    91.  
    92.             v2f vert (appdata v)
    93.             {
    94.                 v2f o;
    95.  
    96.                 // Transform the vertex position from object space to clip space
    97.                 o.vertex = TransformObjectToHClip(v.vertex.xyz);
    98.  
    99.                 // Transform the vertex position from object space to world space
    100.                 o.worldPos = TransformObjectToWorld(v.vertex);//mul(unity_ObjectToWorld, v.vertex).xyz;
    101.  
    102.                 // Transform the normal vector from object space to world space
    103.                 o.worldNormal = TransformObjectToWorldDir(v.normal);
    104.  
    105.                 // Transform the tangent vector from object space to world space
    106.                 o.worldTangent = TransformObjectToWorldDir(v.tangent.xyz);
    107.  
    108.                 // Calculate the bitangent vector from the normal and tangent vectors
    109.                 o.worldBitangent = cross(o.worldNormal, o.worldTangent) * v.tangent.w;
    110.  
    111.                 // Calculate the view direction vector from the world position and camera position
    112.                 o.viewDir = normalize(_WorldSpaceCameraPos - o.worldPos);
    113.  
    114.                 // Transform the texture coordinate by the tiling and offset property
    115.                 o.uv = TRANSFORM_TEX(v.uv, _Albedo) * _UVScale.xy + _UVScale.zw;//o.uv = (v.uv * _Albedo_ST + _Albedo_ST) *_UVScale.xy + _UVScale.zw;
    116.  
    117.                 return o;
    118.             }
    119.  
    120.             half4 frag (v2f i) : SV_Target
    121.             {
    122.                 // Sample textures
    123.                 half4 albedo = tex2D(_Albedo, i.uv) * _Color;
    124.                 half3 normal = UnpackNormal(tex2D(_Normal, i.uv)) * _NormalScale;
    125.                 half ao = tex2D(_AO, i.uv).r * _AOScale;
    126.  
    127.                 // Calculate the TBN matrix (TBN = Tangent, Bitangent, Normal) Para pasar a world space.
    128.                 half3x3 TBN = half3x3(i.worldTangent, i.worldBitangent, i.worldNormal);
    129.  
    130.                 // Transform the normal to world space
    131.                 normal = normalize(mul(TBN, normal));
    132.  
    133.                 // Calculate the light direction and intensity
    134.                 Light light = GetMainLight();//GetAdditionalLight(0, i.worldPos);
    135.                 half3 lightDir = normalize(light.direction.xyz);//normalize(_WorldSpaceLightPos0.xyz);
    136.                 half atten = light.distanceAttenuation * light.shadowAttenuation;
    137.                 half3 lightColor = atten * light.color.rgb;//atten * _LightColor0.rgb;
    138.  
    139.                 // Calculate the half vector
    140.                 half3 Hn = normalize(lightDir + i.viewDir);
    141.  
    142.                 // Rotate the half vector by Anisotropy radians around the normal
    143.                 half3 Ht = normalize(cos(_Anisotropy) * Hn + sin(_Anisotropy) * cross(normal, Hn));
    144.  
    145.                 // Calculate the tangent vector
    146.                 half3 tangent = normalize(i.worldTangent);
    147.  
    148.                 // Calculate the dot products ("saturate" = clamp between 0 and 1)
    149.                 half dotNL = saturate(dot(normal, lightDir));
    150.                 half dotNV = saturate(dot(normal, i.viewDir));
    151.                 half dotHT = saturate(dot(Ht, tangent)); // Calculate the dot product of the half vector and the tangent vector
    152.  
    153.                 // Calculate the anisotropic distribution function
    154.                 half alpha = _Smoothness * _Smoothness;
    155.                 half D = alpha / (PI * pow(dotHT * dotHT * (alpha - 1) + 1, 2));
    156.  
    157.                 // Calculate the fresnel term
    158.                 half F = lerp(0.04, 1, pow(1 - dotNV, 5));
    159.  
    160.                 // Calculate the geometric term
    161.                 half k = alpha * 0.5;
    162.                 half G = dotNL / (dotNL * (1 - k) + k) * dotNV / (dotNV * (1 - k) + k);
    163.  
    164.                 // Calculate the specular term
    165.                 half3 specular = D * F * G * _Anisotropy;
    166.  
    167.                 half3 uno = half3(1,1,1);
    168.                 half3 zero = half3 (0,0,0);
    169.                 // Calculate the diffuse term
    170.                 half3 diffuse = lerp(uno - F, zero, _Metallic) * albedo.rgb;
    171.  
    172.  
    173.                 // Calculate the final color
    174.                 half3 color = (diffuse + specular) * lightColor * dotNL;
    175.  
    176.                 // Apply ambient occlusion
    177.                 color *= ao;
    178.  
    179.                 // Output the color
    180.                 return half4(color, 1);
    181.  
    182.             }
    183.  
    184.             ENDHLSL
    185.      
    186.         }
    187.  
    188.     }
    189.  
    190. }
    191.  
     
    Last edited: May 9, 2023