Search Unity

Shader 2nd Texture Offset and Tilliing not Working

Discussion in 'Shaders' started by Dark_Seth, Jun 18, 2019.

  1. Dark_Seth

    Dark_Seth

    Joined:
    May 28, 2014
    Posts:
    140
    Hi All.

    I am learning some basic of shaders. Trying to create one for my water. What I got is a Texture with 2 Displacement Maps.

    The First one works as expected but adding the Second Displacement Texture it just doesn't offset at all.
    Can someone please have a look at my shader for me.

    Regards

    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2.  
    3. Shader "Setti/Water"
    4. {
    5.    Properties
    6.    {
    7.       _MainTex ("MainTex", 2D) = "white" {}
    8.       _DisplTex ("DisplTex", 2D) = "white" {}
    9.       _DisplScale("DisplScale", Range(0, 1)) = 0.25
    10.        _DisplTex1 ("DisplTex1", 2D) = "white" {}
    11.      
    12.       _DisplScale("DisplScale1", Range(0, 1)) = 0.25
    13.  
    14.    }
    15.    SubShader
    16.    {
    17.         Tags { "Queue" = "Transparent" }
    18.         Blend SrcAlpha OneMinusSrcAlpha
    19.         ZWrite Off
    20.       Pass
    21.       {  
    22.          CGPROGRAM
    23.          #pragma vertex vert
    24.          #pragma fragment frag
    25.          uniform sampler2D _MainTex;  
    26.          uniform sampler2D _DisplTex;  
    27.          uniform sampler2D _DisplTex1;
    28.          uniform float4 _MainTex_ST;          
    29.          uniform float4 _DisplTex_ST;
    30.          uniform float4 _DisplTex_ST1;        
    31.          uniform float _DisplScale;
    32.          uniform float _DisplScale1;          
    33.          struct vertexInput
    34.          {
    35.             float4 vertex : POSITION;
    36.             float4 texcoord : TEXCOORD0;
    37.          };
    38.          struct vertexOutput
    39.          {
    40.             float4 pos : SV_POSITION;
    41.             float4 tex : TEXCOORD0;
    42.          };
    43.          vertexOutput vert(vertexInput input)
    44.          {
    45.             vertexOutput output;
    46.             output.tex = input.texcoord;
    47.             output.pos = UnityObjectToClipPos(input.vertex);
    48.             return output;
    49.          }
    50.          float4 frag(vertexOutput input) : COLOR
    51.          {
    52.             float dipl = (tex2D(_DisplTex, input.tex.xy *_DisplTex_ST.xy + _DisplTex_ST.zw).a-0.5)*2*_DisplScale;
    53.             float dipl2 = (tex2D(_DisplTex1, input.tex.xy *_DisplTex_ST1.xy + _DisplTex_ST1.zw).a-0.5)*2*_DisplScale1;
    54.             fixed4 col = tex2D(_MainTex, _MainTex_ST.xy * input.tex.xy + _MainTex_ST.zw + float2(dipl.x, 0));
    55.             fixed4 col1 = tex2D(_MainTex, _MainTex_ST.xy * input.tex.xy + _MainTex_ST.zw + float2(dipl2.x, 0));
    56.             return (col+col1)/2;
    57.          }
    58.          ENDCG
    59.       }
    60.    }
    61. }
     
  2. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    Your scale offset variable is not named correctly . You have a '1' at the end, not in the middle where it belongs.
     
  3. Dark_Seth

    Dark_Seth

    Joined:
    May 28, 2014
    Posts:
    140

    Thanks Fixed this below

    Code (CSharp):
    1.  _DisplScale("DisplScale1", Range(0, 1)) = 0.25
    to
    Code (CSharp):
    1.  _DisplScale1("DisplScale1", Range(0, 1)) = 0.25
    But still the offset of the second image is not working. The the tilling
     
  4. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    Not that. Line 30 of your code and then where you calculate your uvs. The format of the ST property is wrong.
     
    Dark_Seth likes this.
  5. Dark_Seth

    Dark_Seth

    Joined:
    May 28, 2014
    Posts:
    140
    Thanks. Got it. Thank you for your reply.