Search Unity

Shader Texture Offset on Some Devices

Discussion in 'Shaders' started by SHiLLySiT, Sep 26, 2016.

  1. SHiLLySiT

    SHiLLySiT

    Joined:
    Feb 26, 2013
    Posts:
    12
    I combined a fresnel shader that I found on the wiki and the Unity fisheye shader to create a shader for bubble in my game. I also changed it so that the fisheye effect is only applied to the area behind the bubble, instead of the whole screen. As an optimization, the shader also does not use a grabpass, but instead uses the defined background texture. However, I think I've done this incorrectly or I'm not accounting for something on certain devices.

    On my windows machine the shader renders correctly:


    On my Macbook Air (and iPads):


    And here is the shader itself:
    Code (CSharp):
    1. Shader "Custom/BubbleFast" {
    2.     Properties {
    3.         _Scale("Fresnel Scale", Range(0.0, 1.0)) = 1.0
    4.         _Inside("Inside", Range(0.0, 1.0)) = 1.0
    5.         _Specular("Specular", Range(0.0, 1.0)) = 0.5
    6.         _IntensityX("Intensity X", Range(0.0, 1.0)) = 0.5
    7.         _IntensityY("Intensity Y", Range(0.0, 1.0)) = 0.5
    8.         _Tint("Tint", Color) = (1.0, 1.0, 1.0, 1.0)
    9.         [NoScaleOffset] _MainTex("Texture", 2D) = "white" {}
    10.         _HighlightIntensity("Highlight Intensity", Range(0.0, 1.0)) = 0.5
    11.         [NoScaleOffset] _HighlightTex("Highlight", 2D) = "white" {}
    12.         [NoScaleOffset] _BackgroundTex("Background", 2D) = "white" {}
    13.     }
    14.  
    15.     SubShader {
    16.         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    17.        
    18.         Pass // fisheye
    19.         {
    20.             CGPROGRAM
    21.  
    22.             #pragma vertex vert
    23.             #pragma fragment frag
    24.             #include "UnityCG.cginc"
    25.  
    26.             fixed _IntensityX;
    27.             fixed _IntensityY;
    28.             sampler2D _BackgroundTex;
    29.            
    30.             struct v2f
    31.             {
    32.                 fixed4 pos : SV_POSITION;
    33.                 fixed2 uv : TEXCOORD1;
    34.             };
    35.  
    36.             float4 ComputePos(float4 pos) {
    37.                 float scale = 1.0;
    38.                 float4 o = pos * 0.5f;
    39.                 o.xy = float2(o.x, o.y * scale) + o.w;
    40.                 o.zw = pos.zw;
    41.                 return o;
    42.             }
    43.  
    44.             v2f vert(appdata_base v) {
    45.                 v2f o;
    46.                
    47.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    48.  
    49.                 fixed4 texPos = ComputePos(o.pos);
    50.                 half2 coords = v.texcoord.xy;
    51.                 coords = (coords - 0.25) * 2.0;
    52.                
    53.                 fixed2 realCoordOffs;
    54.                 realCoordOffs.x = (1 - coords.y * coords.y) * _IntensityX * coords.x;
    55.                 realCoordOffs.y = (1 - coords.x * coords.x) * _IntensityY * coords.y;
    56.  
    57.                 o.uv = UnityStereoScreenSpaceUVAdjust(texPos - realCoordOffs, _BackgroundTex_ST);
    58.  
    59.                 return o;
    60.             }
    61.  
    62.             half4 frag(v2f i) : SV_Target {
    63.                 return tex2D(_BackgroundTex, i.uv);
    64.             }
    65.             ENDCG
    66.         }
    67.  
    68.         Pass // fresnel effect
    69.         {
    70.                ZWrite On
    71.             Lighting Off
    72.                Blend SrcAlpha OneMinusSrcAlpha
    73.  
    74.             CGPROGRAM
    75.             #pragma vertex vert
    76.             #pragma fragment frag
    77.             #include "UnityCG.cginc" // for UnityObjectToWorldNormal
    78.             #include "UnityLightingCommon.cginc" // for _LightColor0
    79.  
    80.             fixed4 _Tint;
    81.             fixed _Scale;
    82.             fixed _Inside;
    83.             fixed _HighlightIntensity;
    84.             sampler2D _MainTex;
    85.             sampler2D _HighlightTex;
    86.  
    87.             struct Input {
    88.                 fixed4 vertex : POSITION;
    89.                 fixed3 normal : NORMAL;
    90.                 fixed3 uv : TEXCOORD0;
    91.             };
    92.  
    93.             struct Output {
    94.                 fixed4 vertex  : SV_POSITION;
    95.                 fixed3 uv : TEXCOORD0;
    96.                 fixed3 normWorld : TEXCOORD1;
    97.             };
    98.  
    99.             Output vert(Input v) {
    100.                 Output o;
    101.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    102.                 o.uv = v.uv;
    103.                 o.normWorld = normalize(mul((fixed3x3)unity_ObjectToWorld, v.normal));
    104.                 return o;
    105.             }
    106.  
    107.             float4 frag(Output i) : COLOR {
    108.                 fixed3 I = normalize(i.uv - _WorldSpaceCameraPos.xyz);
    109.                 fixed refFactor = max(0.0, min(1.0, _Scale * pow(1.0 + dot(I, i.normWorld), _Inside)));
    110.                 fixed4 tex = lerp(fixed4(1.0, 1.0, 1.0, 0.0), tex2D(_MainTex, i.uv), dot(I, i.normWorld)) + (tex2D(_HighlightTex, i.uv) * _HighlightIntensity) * _Tint;
    111.                 return lerp(tex, _Tint, refFactor);
    112.             }
    113.  
    114.             ENDCG
    115.         }
    116.  
    117.        
    118.     }
    119.  
    120.     FallBack "Diffuse"
    121. }
    122.  
     
  2. SHiLLySiT

    SHiLLySiT

    Joined:
    Feb 26, 2013
    Posts:
    12