Wiki Shield shader in 3.0?

I’m in a pinch and I realize everyone is scrambling to get their projects up to speed in 3.0.
I’ve gleaned much useful info from this community over the years just by searching and this is my first post.

Was wondering if anyone has converted the Shield shader from the Wiki to 3.0 spec, as it doesn’t seem to auto-convert and I haven’t s clue where to begin- I think it’s just one line in the code according to the debugger.

Much thanks for any insight.

Shader "Shield" {
    Properties {
        _Offset ("Time", Range (0, 1)) = 0.0
        _Color ("Tint (RGB)", Color) = (1,1,1,1)
        _SurfaceTex ("Texture (RGB)", 2D) = "white" {}
        _RampTex ("Facing Ratio Ramp (RGB)", 2D) = "white" {}
    }
    SubShader {
        ZWrite Off
        Tags { "Queue" = "Transparent" }
        Blend One One
        Cull Off

        Pass { 
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma fragmentoption ARB_fog_exp2
            #include "UnityCG.cginc"
 
            struct v2f {
                V2F_POS_FOG;
                float2 uv : TEXCOORD0;
                float2 uv2 : TEXCOORD1;
                float3 normal : TEXCOORD2;
            };
           
            uniform float _Offset;
           
            v2f vert (appdata_base v) {
                v2f o;
                PositionFog( v.vertex, o.pos, o.fog );
               
                float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
                v.texcoord.x = v.texcoord.x;
                v.texcoord.y = v.texcoord.y + _Offset;
                o.uv = TRANSFORM_UV (1);
                o.uv2 = float2( abs (dot (viewDir, v.normal)), 0.5);
                o.normal = v.normal;
                return o;
            }
           
            uniform float4 _Color;
            uniform sampler2D _RampTex : register(s0);
            uniform sampler2D _SurfaceTex : register(s1);
               
            half4 frag (v2f f) : COLOR
            {
                f.normal = normalize (f.normal);
               
                half4 ramp = tex2D (_RampTex, f.uv2) * _Color.a;
                half4 thisTex = tex2D (_SurfaceTex, f.uv) * ramp * _Color;
               
                return half4 (thisTex.r, thisTex.g, thisTex.b, ramp.r);
            }
           
            ENDCG
 
            SetTexture [_RampTex] {combine texture}
            SetTexture [_SurfaceTex] {combine texture}
        }
    }
    Fallback "Transparent/VertexLit"
}

Had a similar issue and the following appears to be working now…

Shader "Shield" {
    Properties {
        _Offset ("Time", Range (0, 1)) = 0.0
        _Color ("Tint (RGB)", Color) = (1,1,1,1)
        _SurfaceTex ("Texture (RGB)", 2D) = "white" {}
        _RampTex ("Facing Ratio Ramp (RGB)", 2D) = "white" {}
    }
    SubShader {
        ZWrite Off
        Tags { "Queue" = "Transparent" }
        Blend One One
        Cull Off

        Pass { 
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma fragmentoption ARB_fog_exp2
            #include "UnityCG.cginc"
 
            struct v2f {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
                float2 uv2 : TEXCOORD1;
                float3 normal : TEXCOORD2;
            };
           
            uniform float _Offset;
           
            v2f vert (appdata_base v) {
                v2f o;
                o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
               
                float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
                v.texcoord.x = v.texcoord.x;
                v.texcoord.y = v.texcoord.y + _Offset;
                o.uv = TRANSFORM_UV (1);
                o.uv2 = float2( abs (dot (viewDir, v.normal)), 0.5);
                o.normal = v.normal;
                return o;
            }
           
            uniform float4 _Color;
            uniform sampler2D _RampTex : register(s0);
            uniform sampler2D _SurfaceTex : register(s1);
               
            half4 frag (v2f f) : COLOR
            {
                f.normal = normalize (f.normal);
               
                half4 ramp = tex2D (_RampTex, f.uv2) * _Color.a;
                half4 tex = tex2D (_SurfaceTex, f.uv) * ramp * _Color;
               
                return half4 (tex.r, tex.g, tex.b, ramp.r);
            }
           
            ENDCG
 
            SetTexture [_RampTex] {combine texture}
            SetTexture [_SurfaceTex] {combine texture}
        }
    }
    Fallback "Transparent/VertexLit"
}

It does- much appreciated.

I had downloaded the Unity 3 Node Based Shader Editor and adapted one of the shader graphs to get a similar result, but this one seems more streamlined with sharper results.

Thanks again- hopefully the Wiki will gain these updates soon.

Greetings! I just used the node based shader editor to create a Shield shader, and it offers a couple new features over the original in addition to Unity 3 support. You can find it here:
http://www.unifycommunity.com/wiki/index.php?title=Shield

Happy coding,

-Aubrey

robur, I’m trying to use your new shader. I created a shader with your code. Then I created a material, gave it the ForceField shader and set a texture on it. I see a preview when I look at the Material and it looks good. However, when I add the material to a sphere in the scene, I see nothing. Any ideas?

Thanks,

Old Monk

Hmm - is it a really tiny sphere near to another object? I have soft particle edge detection built into the shader and you can tweak the _SoftParticlesFactor to customize it’s aggressiveness.

Other than that, the only issue I can think of is if your sphere isn’t UV mapped.
Has anyone else had success with my shader?

No, it isn’t a really small sphere next to another object. How do I tell if it is UV mapped? I just add a default sphere and drag the material over. The strange thing is that in the material preview, it appears that it would work b/c I see a preview.

Hmm - after further testing, it looks looks like this shader doesn’t work quite right unless you are in deferred rendering mode.
Here’s the Sgraph file, you are welcome to try tweaking it with Strumpy’s shader editor:
http://dl.dropbox.com/u/4922306/Snippets/ForceField.sgraph

http://forum.unity3d.com/threads/56180-Strumpy-Shader-Editor-Beta-3.1-released-(performance-usability-upgrade)?highlight=shader+editor

By removing the depth testing node, you should be able to get it working fine in forward rendering.