0
\$\begingroup\$

I'll preface this by saying I'm kinda new to the whole shaders thing, and I and mostly just trying to learn.

What I THINK I am doing in the below pass is extending the vertices along the normal and then adjusting the alpha down by distance to create a glow effect.

The issue is, I'd like to test the visibility of the vertex as if it has in fact not been transposed, to avoid shining glow through other meshes. That seems like it should be possible -- it is information the shader would normally have to not draw a pixel -- but I cannot find this particular use case anywhere online.

Please do tell me if I am approaching this completely wrong (as this could very well be the case of an X/Y question), but I'd like the actual question answered all the same :)

Pass{
            Cull Back
            ZWrite Off
            Blend SrcAlpha One
            CGPROGRAM

            struct v2f {
                float4 pos          : SV_POSITION;
                float3 normal       : TEXCOORD0;
                float3 viewDir      : TEXCOORD1;
                UNITY_FOG_COORDS(2)
            };

            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fog 

            v2f vert(appdata_tan v) {
                v.vertex.xyz += normalize(v.normal) * _GlowIntensity;

                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.normal = normalize(v.normal);
                o.viewDir = normalize(WorldSpaceViewDir(v.vertex));
                UNITY_TRANSFER_FOG(o, o.pos);
                return o;
            }


            half4 frag(v2f i) : COLOR{
                float3 worldNormal = mul(float4(i.normal.xyz, 0.0), unity_WorldToObject).xyz;
                float4 glow = _LightColor * pow(dot(i.viewDir, worldNormal), 2);
                glow.a = pow(glow.a, 1 / _GlowIntensity);
                glow.rgb = pow(glow.rgb, 0.5);
                UNITY_APPLY_FOG(i.fogCoord, glow);
                return glow;
            }

        ENDCG

        }
\$\endgroup\$
  • 1
    \$\begingroup\$ "Please do tell me if I am approaching this completely wrong" - You are approaching this completely wrong. The shader does not perform the visibility test. That's done by the depth buffer. While you can fudge the depth you pass down the line, there's no way to tell the pipeline "depth test this fragment as though it were at a different spot on the screen". So, to avoid the X/Y problem, always ask about the end result you want to achieve: "How do I avoid glow shining through other meshes" - and show us everything we need to understand and reproduce that artifact. \$\endgroup\$ – DMGregory Feb 14 at 23:28

Your Answer

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

Browse other questions tagged or ask your own question.