Search Unity

Space Time Distortion: Please help with Normal Extrusion on iOS

Discussion in 'Shaders' started by Neon Genesis, Jan 29, 2015.

  1. Neon Genesis

    Neon Genesis

    Joined:
    Aug 18, 2013
    Posts:
    4
    I'm working on getting a Surface Shader example (Normal Extrusion with Vertex Modifier) to work on iOS, but am stuck with converting it.

    I want to achieve a space time distortion effect, so I adapted some code. The shader contracts/expands vertices in x,y based on a rendertexture that works like a normal map. It runs fine inside the Unity viewport (Mac), but doesn't show anything when I build to iPad.

    My understanding of shader programming is not that good, but as far as I've figured out, its because the tex2D lookup the shader is using is HLSL code, and that is not supported on OpenGL.

    My research results so far:
    (line 17) Inserting #pragma glsl makes the original shader work on OSX, but not on iOS
    (line 18, 19) I tried other statements (target 2.0 and target 3.0) with the same result.
    (line 32) I searched Google for the OpenGL equivalent of tex2D, found texture2D and tried to replace it, but with no luck since I run in other compiler errors then.

    Can anyone point me in the right direction please?

    Original Shader source: http://docs.unity3d.com/Manual/SL-SurfaceShaderExamples.html

    Code (CSharp):
    1.  
    2. Shader "CRX/Normal Extrusion" {
    3.     Properties
    4.     {
    5.         _Color ("Main Color", Color) = (1,1,1,1)
    6.         _MainTex ("Texture", 2D) = "white" {}
    7.         _DispTex ("Normal Map", 2D) = "white" {}
    8.         _Displacement ("Displacement Amount", Range(-4, 4)) = 4
    9.     }
    10.    
    11.     SubShader
    12.     {
    13.         Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
    14.         Blend SrcAlpha One
    15.         //ZWrite On
    16.      
    17.         CGPROGRAM
    18.         #pragma glsl
    19.         //#pragma target 2.0
    20.         //#pragma target 3.0
    21.         #pragma surface surf Lambert vertex:vert
    22.  
    23.         struct Input
    24.         {
    25.           float2 uv_MainTex;
    26.         };
    27.  
    28.         float _Displacement;
    29.         sampler2D _DispTex;
    30.  
    31.         void vert (inout appdata_full v)
    32.         {
    33.           float3 d = (tex2D (_DispTex, float4(v.texcoord.xy,0,0)) - float3(0.5, 0.5, 1)) * _Displacement;
    34.           //float3 d = (texture2D (_DispTex, float4(v.texcoord.xy,0,0)); - float3(0.5, 0.5, 1)) * _Displacement;
    35.           //float3 d = (texture2D (_DispTex, float4(v.texcoord.xy,0,0));
    36.           v.vertex.x += d.r;
    37.           v.vertex.y -= d.g;
    38.           //v.vertex.z -= d.b;
    39.         }
    40.  
    41.         sampler2D _MainTex;
    42.         fixed4 _Color;
    43.  
    44.         void surf (Input IN, inout SurfaceOutput o)
    45.         {
    46.             half4 c = tex2D (_MainTex, IN.uv_MainTex);
    47.             o.Albedo = c.rgb * _Color;
    48.             o.Alpha = c.a;
    49.         }
    50.         ENDCG
    51.     }
    52.     Fallback "Diffuse"
    53. }
    54.  
     

    Attached Files: