SebLague / Portals

Portals in Unity
https://www.youtube.com/watch?v=cWpFZbjtSQg
MIT License
788 stars 169 forks source link

Slice shader upgrade to URP? #17

Open slagatoras opened 2 years ago

slagatoras commented 2 years ago

Any luck with that? has anyone managed to convert it to URP please?

Rokukkkk commented 2 years ago

I think URP doesn't support Surface Shader anymore, you should either use Shader Graph or write a Lit Shader you own which clip the pixels manually. Check the code below, the important parts are the 2 Clip() functions. But I'm not sure is this the best way to achieve tho.

SubShader
{
    Tags { "RenderPipeline" = "UniversalPipeline" "IgnoreProjector" = "True" "Queue" = "Geometry" }

    HLSLINCLUDE
    // Includes & Structs
    ENDHLSL

    Pass
    {
        Tags { "LightMode" = "UniversalForward" }

        HLSLPROGRAM

        #pragma vertex vert
        #pragma fragment frag

        v2f vert(a2v v)
        {
            // Vertex calculations
            return o;
        }

        half4 frag(v2f i) : SV_TARGET
        {
            float3 adjustedCenter = sliceCenter + sliceNormal * sliceOffsetDst;
            float3 offsetToSliceCentre = adjustedCenter - i.worldPos;
            clip(dot(offsetToSliceCentre, sliceNormal));

            // Fragment calculations

            return color;
        }

        ENDHLSL
    }

    // Important! Have to use a custom shadercaster to clip out the shadow
    Pass
    {
        Tags { "LightMode" = "ShadowCaster" }
        ZWrite On
        ZTest LEqual
        Cull Off  // Set cull off to keep shadow casting from the inner clipped part

        HLSLPROGRAM

        #pragma vertex vert
        #pragma fragment frag

        v2f vert(a2v v)
        {
            // Same as forward pass
            return o;
        }

        half4 frag(v2f i) : SV_TARGET
        {
            float3 adjustedCenter = sliceCenter + sliceNormal * sliceOffsetDst;
            float3 offsetToSliceCentre = adjustedCenter - i.worldPos;
            clip(dot(offsetToSliceCentre, sliceNormal));

            return 0;
        }

        ENDHLSL
    }
}