Search Unity

Question shader tiling and offset not working help please

Discussion in 'General Graphics' started by kashifrazzaq20, Jan 19, 2023.

  1. kashifrazzaq20

    kashifrazzaq20

    Joined:
    Aug 15, 2019
    Posts:
    15
    hi every one i am facing problem in this shader all things working but tiling and offset parameter not working please solve my problem

    shader code

    Code (CSharp):
    1. Shader "TF/Cylindrical"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Base (RGB)", 2D) = "white" {}
    6.         _Bump ("Bump (RGB)", 2D) = "white" {}
    7.         _ShadowTex ("Shadow (RGB)", 2D) = "white" {}
    8.         _Cutout ("Cutout", Range(0,1)) = 0.5
    9.         _SaturationRandomize ("Saturation randomize", Range(0,1)) = 0.5
    10.         _BrightnessRandomize ("Brightness randomize", Range(0,1)) = 0.5
    11.         _Ambient ("Ambient Color", Color) = (.5,.5,.5,1)
    12.         _LightMult ("Light multiplier", FLOAT) = 1.0
    13. _Speed("Speed",Range(0.1,4)) = 1
    14.  
    15. _Amount("Amount", Range(0.1,10)) = 3
    16.  
    17. _Distance("Distance", Range( 0, 2 )) = 0.3
    18.  
    19.     }  
    20.     SubShader
    21.     {
    22.         Tags {
    23.             "Queue"="Transparent-1"
    24.             "DisableBatching" = "True"
    25.             }
    26.        
    27.         Pass
    28.         {
    29.             Tags { "LightMode" = "Always" }
    30.            
    31.             Cull Back
    32.            
    33.             CGPROGRAM
    34.  
    35.             #include "UnityCG.cginc"
    36.             #pragma vertex vert
    37.             #pragma fragment frag
    38.             #pragma multi_compile_fog
    39.             #pragma exclude_renderers xbox360
    40.            
    41.             #define TF_CYLINDRICAL_MODE
    42.            
    43.             #include "tf.cginc"
    44.  
    45.             ENDCG
    46.         } // pass
    47.        
    48.         Pass
    49.         {
    50.             Name "ShadowCaster"
    51.             Tags { "LightMode" = "ShadowCaster" }
    52.             Fog {Mode Off}
    53.             ZWrite On ZTest LEqual Cull Back
    54.             Offset 1, 1
    55.            
    56.             CGPROGRAM
    57.             #pragma vertex vert
    58.             #pragma fragment frag
    59.             #pragma multi_compile_shadowcaster
    60.             #pragma fragmentoption ARB_precision_hint_fastest
    61.             #include "UnityCG.cginc"
    62.             #pragma exclude_renderers xbox360
    63.            
    64.             #define TF_SHADOW_TEXTURE _ShadowTex
    65.            
    66.             #include "tfShadow.cginc"
    67.  
    68.             ENDCG
    69.         }      
    70.     } // subshader
    71.    
    72.     FallBack "Diffuse"
    73. }
    74.  

    cginc file

    Code (CSharp):
    1. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
    2. // Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
    3. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    4.  
    5.            
    6.            
    7.            
    8.             uniform sampler2D _MainTex;      
    9.             uniform float _SaturationRandomize;
    10.             uniform float _BrightnessRandomize;
    11.             uniform float _Cutout;
    12. half _Speed;
    13.  
    14. half _Amount;
    15.  
    16. half _Distance;
    17.            
    18.     #ifndef TF_DYNAMIC          
    19.             uniform sampler2D _Bump;
    20.             uniform fixed4 _LightColor0;
    21.             uniform float _LightMult;
    22.     #endif
    23.     #ifdef TF_DISTANCE_CULL
    24.             uniform float _CullingNear;
    25.     #endif
    26.             struct vertexInput
    27.             {
    28.                 float4 center : POSITION;
    29.                 float4 corner : TEXCOORD1;
    30.                 float3 normal : NORMAL;
    31.                 float4 tex : TEXCOORD0;
    32.                 float4 color : COLOR;
    33.             };
    34.  
    35.             struct vertexOutput
    36.             {
    37.                 float4 pos : SV_POSITION;
    38.                 float4 tex : TEXCOORD0;
    39.             #ifndef TF_DYNAMIC  
    40.                 fixed3 lightDirection : TEXCOORD1;
    41.             #endif
    42.             #ifdef TF_DISTANCE_CULL
    43.                 half distanceCull : TEXCOORD2;
    44.             #endif
    45.                 fixed3 color : TEXCOORD3;
    46.                
    47.                 fixed3 N: TEXCOORD4;
    48.                 fixed3 B: TEXCOORD5;
    49.             #ifndef TF_DISABLE_FOG  
    50.                 UNITY_FOG_COORDS(6)
    51.             #endif
    52.             };
    53.            
    54.             inline fixed rand(float2 co)
    55.             {
    56.                 return frac(sin(dot(co ,half2(12.9898, 78.233))) * 43758.5453);
    57.             }
    58.            
    59.             vertexOutput vert(vertexInput i)
    60.             {
    61.                 vertexOutput o;
    62.                 UNITY_INITIALIZE_OUTPUT(vertexOutput,o);
    63.                
    64. #ifndef TF_DYNAMIC          
    65.    
    66.     #ifndef TF_AMBIENT_ONLY
    67.    
    68.         #ifdef TF_MESH
    69.                
    70.             #ifdef TF_BACK_PASS
    71.                 o.N = -i.normal;
    72.                 o.B = cross(o.N, fixed3(0,1,0));
    73.             #else
    74.                 o.N = i.normal;
    75.                 o.B = -cross(o.N, fixed3(0,1,0));
    76.             #endif
    77.                
    78.                 o.lightDirection = _WorldSpaceLightPos0.xyz;
    79.  
    80.         #else
    81.            
    82.                 o.lightDirection.xyz =  normalize( mul(mul(UNITY_MATRIX_MV, unity_WorldToObject), _WorldSpaceLightPos0).xyz );
    83.        
    84.         #endif
    85.    
    86.     #endif
    87.                
    88. #endif
    89.  
    90. #ifdef TF_DISTANCE_CULL
    91.                 o.distanceCull = length( _WorldSpaceCameraPos.xz - mul(unity_ObjectToWorld, i.center).xz );
    92. #endif  
    93.                 o.tex = i.tex;
    94.  
    95. #ifdef TF_MESH
    96.                 o.pos = UnityObjectToClipPos(i.center);
    97. #else
    98.    
    99.     #ifdef TF_CYLINDRICAL_MODE
    100.  
    101.                 o.pos = mul(UNITY_MATRIX_MV, float4(i.center.x, i.center.y + i.corner.y, i.center.z, 1));
    102.                 o.pos = mul(UNITY_MATRIX_P, o.pos + float4(-i.corner.x, 0, 0, 0) );
    103. //o.pos += float4 (sin(_Time.y* _Frequency) * _Wave, 0, sin(_Time.y) * _Wave,0);
    104.  
    105. o.pos.x += sin( _Time.y * _Speed + o.pos.y * _Amount ) * o.tex.y;
    106.  
    107.     #else
    108.                
    109.                 o.pos = mul(UNITY_MATRIX_MV, float4(i.center.x, i.center.y, i.center.z, 1));
    110.                 o.pos = mul(UNITY_MATRIX_P, o.pos + float4(-i.corner.x, i.corner.y, 0, 0));
    111.                
    112.         #ifndef TF_DYNAMIC
    113.                 fixed3 dir = normalize(_WorldSpaceCameraPos.xyz - mul(unity_ObjectToWorld, i.center).xyz);
    114.                
    115.                 fixed d = saturate(dot(dir, fixed3(0, 1, 0)));
    116.                
    117.                 if (dot(UNITY_MATRIX_IT_MV[1].xyz, fixed3(0, 1, 0))<0)
    118.                     d=.99;
    119.                
    120.                 o.tex.xy /= 4.0;
    121.                 o.tex.y -= .25;
    122.            
    123.                 int row = (int)(d * 4);
    124.                 int col = (int)(d * 16);
    125.  
    126.                 o.tex.y -= (1.0 / 4) * row;
    127.                 o.tex.x += (1.0 / 4) * (col - row * 4);
    128.         #endif
    129.    
    130.     #endif
    131.    
    132. #endif
    133.                 o.tex.z = rand(i.center.xz);
    134.                 o.tex.w = rand(i.center.zx);
    135.                
    136.                 o.color = i.color.rgb;
    137.                
    138.             #ifndef TF_DISABLE_FOG
    139.                 UNITY_TRANSFER_FOG(o,o.pos);
    140.             #endif
    141.            
    142.                 return o;
    143.             }
    144.            
    145.             float4 frag(vertexOutput i) : COLOR
    146.             {
    147.  
    148.                 fixed4 c = tex2D(_MainTex, i.tex.xy);
    149.                
    150. #ifdef TF_DISTANCE_CULL
    151.                 if (i.distanceCull - rand(i.pos.xy) * 10 < _CullingNear) discard;
    152. #endif              
    153.             #ifndef TF_DISABLE_ALPHATEST
    154.                 clip(c.a - _Cutout);
    155.             #endif
    156.            
    157.                 c.rgb *= i.color;
    158.                 c.rgb = lerp(c.rgb, Luminance(c.rgb), _SaturationRandomize * i.tex.z);
    159.                 c.rgb *= 1.0 - _BrightnessRandomize * i.tex.w;
    160.  
    161.     #ifndef TF_DYNAMIC
    162.            
    163.         #ifndef TF_AMBIENT_ONLY
    164.                 fixed3 bump = (tex2D(_Bump, i.tex.xy).xyz - 0.5) * 2;
    165.                
    166.             #ifdef TF_MESH
    167.                 bump = bump.x * i.B + fixed3(0,bump.y,0) + bump.z * i.N;
    168.             #endif
    169.            
    170.                 c.rgb = c.rgb * UNITY_LIGHTMODEL_AMBIENT.rgb +
    171.                         c.rgb *  ( (dot( bump, i.lightDirection ) + 1.0) * 0.5 )  *
    172.                         _LightColor0.rgb * _LightMult;
    173.                        
    174.         #else
    175.                 c.rgb = c.rgb * _LightMult;
    176.         #endif
    177.     #endif
    178.    
    179.             #ifndef TF_DISABLE_FOG
    180.                 UNITY_APPLY_FOG(i.fogCoord, c);
    181.             #endif
    182.                
    183.                 return c;
    184.             }