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 Liquid Shader based on Quake

Discussion in 'Shaders' started by a4chteam, Sep 19, 2021.

  1. a4chteam

    a4chteam

    Joined:
    Dec 22, 2020
    Posts:
    19
    Hi. I don't know how to write a shader myself, but I need liquid emulation for the game.
    In this topic https://forum.unity.com/threads/quake-1-liquid-shader.873616/ I found the easiest solution to my problem. But I'm stuck on the error invalid subscript 'uv' at line 39 (on d3d11)

    Shader

    Code (CSharp):
    1. Shader "Custom/Water"
    2. {
    3.     //Based on bgolus QuakeLiquid shader;
    4.     Properties
    5.     {
    6.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    7.         _Color("Color", Color) = (1,1,1,1)
    8.         _Amplitude("Amplitude", Float) = 0.12
    9.         _Scale("Scale", Float) = 0.7
    10.         _Frequency("Frequency", Float) = 0.2
    11.     }
    12.     SubShader
    13.     {
    14.         Tags { "RenderType"="Opaque" }
    15.         LOD 200
    16.  
    17.         CGPROGRAM
    18.         #pragma surface surf Lambert
    19.         #pragma vertex vert
    20.  
    21.         sampler2D _MainTex;
    22.  
    23.         struct Input
    24.         {
    25.             float2 uv_MainTex;
    26.             float4 vertex : POSITION;
    27.             float2 uv : TEXCOORD0;
    28.         };
    29.  
    30.         fixed4 _Color;
    31.         float _Amplitude;
    32.         float _Scale;
    33.         float _Frequency;
    34.  
    35.         void vert(inout appdata_full v, out Input o)
    36.         {
    37.             UNITY_INITIALIZE_OUTPUT(Input, o);
    38.             o.vertex = UnityObjectToClipPos(v.vertex);
    39.             o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    40.         }
    41.  
    42.         void surf (Input IN, inout SurfaceOutput o)
    43.         {
    44.             // Albedo comes from a texture tinted by color
    45.             float2 DeltaTime = (IN.uv.yx * _Scale + _Time.y * _Frequency) * UNITY_PI;
    46.             float2 uv = IN.uv + float2(sin(DeltaTime.x), sin(DeltaTime.y)) * _Amplitude;
    47.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    48.             o.Albedo = c.rgb;
    49.         }
    50.         ENDCG
    51.     }
    52.     FallBack "Diffuse"
    53. }
    Please tell me where I might have made a mistake and how it can be resolved.
     
  2. Shane_Michael

    Shane_Michael

    Joined:
    Jul 8, 2013
    Posts:
    157
    The appdata_full struct uses "texcoord" as a subscript for the texture coordinates not "uv" like you used in your Input struct.
     
    a4chteam likes this.
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,236
    That’s technically the error the compiler is complaining about, but that’s not actually the issue in the shader.

    The issue is you don’t need a
    vert
    function in a surface shader to pass the UV along, and thus you shouldn’t be using the
    .uv
    in the
    surf
    function. With surface shaders the UV is automatically handled with the
    .uv_MainTex
    variable passed to the
    surf
    function. All that math being done to
    .uv
    in the surf function should be applied to the
    .uv_MainTex
    instead.
     
    a4chteam likes this.
  4. a4chteam

    a4chteam

    Joined:
    Dec 22, 2020
    Posts:
    19
    Thank you so much for the information!