1
\$\begingroup\$

I've been tasked with converting an older Unity shader which does Bumpy Glass with some alpha modification, to URP. I'm assuming that means using Shader Graph. I'm a bit new to shaders in general, but managed to find various clues here and there and have come to the conclusion that creating a Custom Function is probably the easiest way (I could be wrong). Did I mention, this is targeted for WebGL?

Here is the original shader:

Shader "Custom/Glass-Seethrough Alpha" {
Properties{
    _Color("Main Color", Color) = (1,1,1,1)
    _SpecColor("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    _Shininess("Shininess", Range(0.03, 1)) = 0.3
    _MainTex("Base (RGB)", 2D) = "white" {}
    _BumpMap("Normalmap", 2D) = "bump" {}
    _AlphaMap("Alphamap", 2D) = "black" {}
    _DistAmt("Distortion", range(0,256)) = 100
}
    SubShader{
        GrabPass { }

        Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Opaque" }
        LOD 200

        CGPROGRAM
        #pragma target 3.0
        #pragma exclude_renderers gles
        #pragma vertex vert
        #pragma surface surf BlinnPhong alpha
        #include "UnityCG.cginc"

        float4 _Color;
        float _Shininess;
        sampler2D _MainTex;
        sampler2D _BumpMap;
        sampler2D _AlphaMap;
        sampler2D _GrabTexture;
        float _DistAmt;
        float4 _GrabTexture_TexelSize;

        struct Input {
            float2 uv_MainTex;
            float2 uv_BumpMap;
            float2 uv_AlphaMap;
            float4 proj : TEXCOORD;
        };

        void vert(inout appdata_full v, out Input o) {
            UNITY_INITIALIZE_OUTPUT(Input, o);
            float4 oPos = UnityObjectToClipPos(v.vertex);
            #if UNITY_UV_STARTS_AT_TOP
                float scale = -1.0;
            #else
                float scale = 1.0;
            #endif
            o.proj.xy = (float2(oPos.x, oPos.y * scale) + oPos.w) * 0.5;
            o.proj.zw = oPos.zw;
        }

        void surf(Input IN, inout SurfaceOutput o) {
            half3 tint = tex2D(_MainTex, IN.uv_MainTex);
            half3 nor = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
            half3 alpha = tex2D(_AlphaMap, IN.uv_AlphaMap);

            float2 offset = nor * _DistAmt * _GrabTexture_TexelSize.xy;
            IN.proj.xy = offset * IN.proj.z + IN.proj.xy;
            half4 col = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(IN.proj));
            half3 tinted = col * tint * _Color;

            o.Albedo = lerp(col, tinted, alpha);//tex.rgb * _Color.rgb * col.rgb;
            o.Normal = nor.rgb;
            o.Specular = _Shininess;
            o.Gloss = col.a;
            o.Alpha = alpha.r;
        }
        ENDCG
    }
    FallBack "Diffuse"

}

The idea is to have the bump map animate on, say, uv.v, to simulate water flow through a pipe, and animate the alpha map to 'reveal' the pipe. Here's what it should look like: Supposed to look like

I found this article which helped (I THINK!) with the 'vert' part of the shader. Where I'm getting stuck is in the 'surf' part. I've set up a bunch of inputs, and maybe I've gone down the wrong rabbit hole, but I'm getting an error saying that uv_MainTex does not exist:

Here's a shot of the work in progress: glass shader conversion WIP

And the full text (admittedly incomplete):

half3 tint = SAMPLE_TEXTURE2D(MainTex, SS, uv_MainTex);
half3 nor = UnpackNormal(tex2D(BumpMap, uv_BumpMap));
half3 alpha = tex2D(AlphaMap, uv_AlphaMap);

float2 offset = BumpMap * DistAmt; * GrabTexture_TexelSize.xy;
proj.xy = offset * proj.z + proj.xy;
half4 col = tex2Dproj(GrabTexture, proj);
half3 tinted = col * tint * color;

Albedo = lerp(col, tinted, alpha);

But I'm getting this error that uv_MainTex does not exist. See error message

Am I even on the right track? Am I using the wrong macros? Do I need additional SamplerStates? Or hey, has someone already made such a shader I can get and call it a day? Thanks

New contributor
Dave is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
1
\$\begingroup\$

This should give you something fairly close to what you had. The proj data coming from the vertex shader wasn't doing anything geometrically meaningful, so I skipped that for simplicity. You can get a similar effect by tuning your Distortion parameter instead.

Shader Graph

\$\endgroup\$
1
  • \$\begingroup\$ Thank you! I came up with something similar, but your's is more accurate. \$\endgroup\$ – Dave yesterday

Your Answer

Dave is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.