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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Liquid in Bottle Shader Giving Error in URP

Discussion in 'Shaders' started by cihatozdemir, May 2, 2023.

  1. cihatozdemir

    cihatozdemir

    Joined:
    Oct 23, 2020
    Posts:
    5
    Hello everyone, first of all, I apologize for any mistakes as this is my first post on this forum. I want to use a water shader that I saw on YouTube in my own URP project. However, I'm getting an error message saying: "Shader properties can't be added to this global property sheet. Trying to add _GrabTexture_HDR". I tried options like converting to URP from the Edit tab, but I couldn't get any results. Where exactly is the problem?

    Code (CSharp):
    1. Shader "TNTC/LiquidInBottle"{
    2.     Properties{
    3.         _MainTex ("Texture", 2D) = "white" {}
    4.         _Color ("Color", Color) = (1,1,1,1)
    5.         _Glossiness("Smoothness", Range(0,1)) = 0.5
    6.         _Metallic ("Metallic", Range(0,1)) = 0.0
    7.  
    8.         _BumpMap ("Top Normal Map", 2D) = "bump" {}
    9.         _BumpStrenght("Top Normal Strenght", float) = 1
    10.         _BumpMapInner ("Inner Normal Map", 2D) = "bump" {}
    11.         _BumpStrenghtInner("Inner Normal Strenght", float) = 1
    12.         _TopColor("Top Section Color", Color) = (1,1,1,1)
    13.  
    14.         _Ruffle("Ruffle", float) = 10
    15.         _PlanePosition("PlanePosition", Vector) = (0,0,0,1)
    16.         _PlaneNormal("PlaneNormal", Vector) = (0,1,0,0)
    17.  
    18.  
    19.         [IntRange] _StencilValue ("Stencil Value", Range(0,255)) = 255
    20.     }
    21.  
    22.     SubShader {
    23.         Tags {"Queue"="Geometry"}
    24.  
    25.         CGINCLUDE
    26.  
    27.         float3 _PlaneNormal;
    28.         float3 _PlanePosition;
    29.         float3 _Inertia;
    30.         float _Ruffle;
    31.  
    32.         bool checkVisibility(float3 worldPos){
    33.             float dotProd = dot(worldPos - _PlanePosition, _PlaneNormal);
    34.             float activityX = clamp(_Inertia.x, 0, 1);
    35.             float activityZ = clamp(_Inertia.z, 0, 1);
    36.             dotProd += (.5/_Ruffle * sin(activityX * _Ruffle * (worldPos.x + _Time.x)));
    37.             dotProd += (.5/_Ruffle * cos(activityZ * _Ruffle * (worldPos.z + _Time.x)));
    38.             return dotProd > 0;
    39.         }
    40.  
    41.         ENDCG
    42.  
    43.         GrabPass{
    44.             "_GrabTexture"
    45.         }
    46.  
    47.         Stencil{
    48.             Ref [_StencilValue]
    49.             CompBack Always
    50.             PassBack Replace
    51.  
    52.             CompFront Always
    53.             PassFront Zero
    54.         }
    55.  
    56.      
    57.         Cull Front
    58.         CGPROGRAM
    59.         #pragma surface surf Standard addshadow
    60.         #pragma vertex vert
    61.         #pragma target 3.0
    62.  
    63.         struct Input {
    64.             half2 uv_MainTex;
    65.             float3 worldPos;
    66.             float4 screenPos;
    67.             float3 normal;
    68.             float3 viewDir;
    69.             float3 worldNormal;
    70.             float4 grabUV;
    71.             INTERNAL_DATA
    72.         };
    73.  
    74.         sampler2D _MainTex;
    75.         sampler2D _BumpMap;
    76.         sampler2D _GrabTexture;
    77.         fixed4 _Color;
    78.         fixed4 _TopColor;
    79.         float _BumpStrenght;
    80.         half _Glossiness;
    81.         half _Metallic;
    82.         float _RotationSpeed;
    83.  
    84.         void vert (inout appdata_full v, out Input o) {
    85.             UNITY_INITIALIZE_OUTPUT(Input,o);
    86.             float4 hpos = UnityObjectToClipPos (v.vertex);
    87.             o.grabUV = ComputeGrabScreenPos(hpos);
    88.         }
    89.  
    90.         void surf(Input IN, inout SurfaceOutputStandard o){
    91.             if (checkVisibility(IN.worldPos))discard;
    92.          
    93.             float2 screenUV = IN.screenPos.xy / IN.screenPos.w;
    94.             screenUV *= float2(8,6);
    95.  
    96.             float4 background = tex2Dproj( _GrabTexture, IN.grabUV) * _TopColor;
    97.             o.Albedo = lerp(background.rgb, _TopColor.rgb, _TopColor.a);
    98.  
    99.             half3 worldT = WorldNormalVector(IN, half3(1,0,0));
    100.             half3 worldB = WorldNormalVector(IN, half3(0,1,0));
    101.             half3 worldN = WorldNormalVector(IN, half3(0,0,1));
    102.             half3x3 world2Tangent = half3x3(worldT, worldB, worldN);
    103.      
    104.             float3 n = UnpackScaleNormal(tex2D (_BumpMap, screenUV), _BumpStrenght);
    105.             n = mul(world2Tangent, n);
    106.             o.Normal = mul(world2Tangent, _PlaneNormal) + n;
    107.             o.Metallic = _Metallic;
    108.             o.Smoothness = _Glossiness;
    109.         }
    110.         ENDCG
    111.      
    112.         Cull Back
    113.  
    114.         CGPROGRAM
    115.         #pragma surface surf Standard addshadow
    116.         #pragma vertex vert
    117.         #pragma target 3.0
    118.  
    119.         sampler2D _MainTex;
    120.         sampler2D _BumpMapInner;
    121.         sampler2D _GrabTexture;
    122.         float _BumpStrenghtInner;
    123.  
    124.         struct Input {
    125.             float2 uv_MainTex;
    126.             float2 uv_BumpMapInner;
    127.             float3 worldPos;
    128.             float4 screenPos;
    129.             float4 grabUV;
    130.         };
    131.  
    132.         half _Glossiness;
    133.         half _Metallic;
    134.         fixed4 _Color;
    135.         fixed4 _TopColor;
    136.  
    137.         void vert (inout appdata_full v, out Input o) {
    138.             UNITY_INITIALIZE_OUTPUT(Input,o);
    139.             float4 hpos = UnityObjectToClipPos (v.vertex);
    140.             o.grabUV = ComputeGrabScreenPos(hpos);
    141.         }
    142.  
    143.  
    144.         void surf(Input IN, inout SurfaceOutputStandard o) {
    145.             if (checkVisibility(IN.worldPos)){
    146.                 discard;
    147.             }
    148.  
    149.             float4 background = tex2Dproj( _GrabTexture, IN.grabUV) * _Color;
    150.             o.Albedo = lerp(background.rgb, _Color.rgb, _Color.a);
    151.          
    152.             o.Metallic = _Metallic;
    153.             o.Smoothness = _Glossiness;
    154.             o.Normal = UnpackScaleNormal (tex2D (_BumpMapInner, IN.uv_BumpMapInner), _BumpStrenghtInner);
    155.         }
    156.         ENDCG
    157.  
    158.     }
    159. }
    160.  
    161.  
     
  2. adamgryu

    adamgryu

    Joined:
    Mar 1, 2014
    Posts:
    171
    I noticed that this is a surface shader (see #pragma surface) which isn't compatible with URP.
    I'm not directly sure what the error message is, but you might have better luck rebuilding the shader manually in the Shader Graph.
     
  3. cihatozdemir

    cihatozdemir

    Joined:
    Oct 23, 2020
    Posts:
    5
    I can’t rebuild manually in shader graph,
    do you have any other advice?
     
  4. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,455
    Grap pass isn't in URP. Opaque texture is. Not sure how to implement it in text shaders
     
  5. wwWwwwW1

    wwWwwwW1

    Joined:
    Oct 31, 2021
    Posts:
    637
    Since the shader queue is set to geometry (2000), I think you can replace the grab pass texture with URP's opaque texture.

    URP opaque texture can be sampled with scene color node in shader graph or a global texture named "_CameraOpaqueTexture" after opaque rendering, so the material should be set to transparent if you'd like to sample it.

    There two passes in the original shader. If you'd like to recreate it in shader graph, you'll need to assign two materials to the mesh. For text shaders, you can use
    Tags { "LightMode" = "UniversalForwardOnly" }
    for the first pass, and
    Tags { "LightMode" = "SRPDefaultUnlit" }
    for the second.
     
    DevDunk likes this.