Search Unity

Question Parallax Shader On Android

Discussion in 'Shaders' started by Feral_Pug, Apr 8, 2021.

  1. Feral_Pug

    Feral_Pug

    Joined:
    Sep 29, 2019
    Posts:
    49
    I was trying to test some gyroscope input stuff on android and thought I'll make a cool cube to look at and rotate with the gyroscope. So a wrote a simple parallax shader to make the cube look cool. It works fine in the editor but on my phone the cube has no parallax, not cool. I messed around with build setting and stuff for a while and nothing seems to make it work. Can anyone point to what is maybe going wrong.

    Code (CSharp):
    1. Shader "CoolCube/CoolCube"
    2. {
    3.     Properties
    4.     {
    5.         _Color1("Color 1", Color) = (1, 0, 0, 1)
    6.         _Color2("Color 2", Color) = (0, 1, 0, 1)
    7.         _MainTex ("Texture", 2D) = "white" {}
    8.         _ParallaxStrength("Parallax Strength", float) = 1
    9.         _ParallaxColorPower("Parallax Color Power", Range(0, 1)) = 1
    10.     }
    11.     SubShader
    12.     {
    13.         Tags { "RenderType"="Opaque" }
    14.        
    15.  
    16.         Pass
    17.         {
    18.             CGPROGRAM
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.  
    22.             #include "UnityCG.cginc"
    23.  
    24.             struct appdata
    25.             {
    26.                 float4 vertex : POSITION;
    27.                 float2 uv : TEXCOORD0;
    28.                 float3 normal : Normal;
    29.                 float3 tangent : Tangent;
    30.             };
    31.  
    32.             struct v2f
    33.             {
    34.                 float2 uv : TEXCOORD0;
    35.                 float4 vertex : SV_POSITION;
    36.                 float3 tanViewDir : TEXCOORD1;
    37.                 float3 normal : TEXCOORD2;
    38.             };
    39.  
    40.             sampler2D _MainTex;
    41.             float4 _MainTex_ST;
    42.             float _ParallaxStrength;
    43.             float _ParallaxColorPower;
    44.             float4 _Color1, _Color2;
    45.  
    46.             #define ITERATIONS 10
    47.  
    48.             v2f vert (appdata v)
    49.             {
    50.                 v2f o;
    51.                 o.vertex = UnityObjectToClipPos(v.vertex);
    52.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    53.                 o.normal = UnityObjectToWorldNormal(v.normal);
    54.                 float3x3 objectToTangent = float3x3(v.tangent, cross(v.tangent, v.normal), v.normal);
    55.                 o.tanViewDir = mul(objectToTangent, ObjSpaceViewDir(v.vertex));
    56.  
    57.  
    58.  
    59.                 return o;
    60.             }
    61.  
    62.             fixed4 frag (v2f i) : SV_Target
    63.             {
    64.                 i.tanViewDir = normalize(i.tanViewDir);
    65.  
    66.                 float4 col = float4(0, 0, 0, 0);
    67.                 float stepSize = float(1) / ITERATIONS;
    68.  
    69.  
    70.                 [unroll]
    71.                 for(int p = 0; p < ITERATIONS; p++){
    72.                     float f = p;
    73.                     float effect = stepSize * f;
    74.                     float2 uv = i.uv - (i.tanViewDir.xy * _ParallaxStrength * effect);
    75.                     float value = tex2D(_MainTex, uv).r;
    76.                     float tempCol = value * smoothstep(effect, 1, value);  
    77.                     tempCol *= _ParallaxColorPower;
    78.                     col += lerp(_Color1, _Color2, effect) * tempCol;
    79.          
    80.                 }
    81.  
    82.  
    83.                 return col;
    84.             }
    85.             ENDCG
    86.         }
    87.     }
    88. }
    89.