oculus-samples / Unity-DepthAPI

Examples of using Depth API for real-time, dynamic occlusions
Other
167 stars 24 forks source link

"Redifinition of _Time" custom shader #8

Closed SashelI closed 8 months ago

SashelI commented 8 months ago

Hi, as I was trying to integrate occlusion into a custom URP shader using this repo, i've got this error (on a shader that worked perfectly before) : Shader error in 'Unlit/360ImagePortal_custom': redefinition of '_Time' at /DevPerso/Fenix/Demo/Library/PackageCache/com.unity.render-pipelines.universal@14.0.9/ShaderLibrary/UnityInput.hlsl(40) (on d3d11)

And I can't figure out why. Here's the shader.

Shader "Unlit/360ImagePortal_custom"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Stencil{
        Ref 2
        Comp Equal
        Pass Keep
        }

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            // DepthAPI Environment Occlusion
            #pragma multi_compile _ HARD_OCCLUSION SOFT_OCCLUSION

            #include "UnityCG.cginc"
            #include "Packages/com.meta.xr.depthapi.urp/Shaders/EnvironmentOcclusionURP.hlsl"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;

                float4 positionNDC : TEXCOORD0;

                UNITY_VERTEX_INPUT_INSTANCE_ID
                UNITY_VERTEX_OUTPUT_STEREO // required for stereo
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2f vert (appdata v)
            {
                v2f o;

                UNITY_SETUP_INSTANCE_ID(v);
                UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); // required to support stereo

                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                UNITY_TRANSFER_FOG(o,o.vertex);

                float4 ndc = o.vertex * 0.5f;
                o.positionNDC.xy = float2(ndc.x, ndc.y * _ProjectionParams.x) + ndc.w;
                o.positionNDC.zw = o.vertex.zw;

                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);

                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv);
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, col);

                // calculate UV for the depth texture lookup for occlusions
                float2 uv = i.positionNDC.xy / i.positionNDC.w;

                // pass UV and the current depth of the texel
                float occlusionValue = CalculateEnvironmentDepthOcclusion(uv, i.vertex.z);

                // consider early rejection to not write to depth if it's an opaque shader
                if (occlusionValue < 0.01)
                {
                    discard;
                }

                // premultiply color and alpha by occlusion value
                // when it's 1 - color is not affected - virtual covers real
                // when it's 0 - texel is invisible - virtual is under real
                // when it's in between - texel is semi transparent
                col *= occlusionValue;

                return col;
            }
            ENDCG
        }
    }
}
vasylbo commented 8 months ago

I expect this line to be the issue:

#include "UnityCG.cginc"

So URP occlusion include uses some URP functionality that collides with that BiRP included functionality, in this case _Time variable.

This is an example of how to write a simple, unlit URP shader: https://docs.unity3d.com/Packages/com.unity.render-pipelines.universal@14.0/manual/writing-shaders-urp-basic-unlit-structure.html It uses HLSLPROGRAM and not CGPROGRAM. And instead of #include "UnityCG.cginc" it uses URPs core include #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hls

SashelI commented 8 months ago

I expect this line to be the issue:

#include "UnityCG.cginc"

So URP occlusion include uses some URP functionality that collides with that BiRP included functionality, in this case _Time variable.

This is an example of how to write a simple, unlit URP shader: https://docs.unity3d.com/Packages/com.unity.render-pipelines.universal@14.0/manual/writing-shaders-urp-basic-unlit-structure.html It uses HLSLPROGRAM and not CGPROGRAM. And instead of #include "UnityCG.cginc" it uses URPs core include #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hls

Thanks, but the thing is, I need UnityCG.cginc for those variables : UNITY_APPLY_FOG(i.fogCoord, col); UNITY_FOG_COORDS(1) UnityObjectToClipPos(v.vertex);

vasylbo commented 8 months ago

UnityObjectToClipPos(v.vertex); - this has a URP equivalent, but I'm not aware of how to substitute fog functions from BiRP in URP.

Have you tried to use #include "Packages/com.meta.xr.depthapi/Runtime/BiRP/EnvironmentOcclusionBiRP.cginc" in this shader?

SashelI commented 8 months ago

UnityObjectToClipPos(v.vertex); - this has a URP equivalent, but I'm not aware of how to substitute fog functions from BiRP in URP.

Have you tried to use #include "Packages/com.meta.xr.depthapi/Runtime/BiRP/EnvironmentOcclusionBiRP.cginc" in this shader?

I just did : on the occluded part, the object turns white. I guess it is because BiRP isn't supposed to work in URP. I will try to find how to not include BiRP cginc.

SashelI commented 8 months ago

Now with this modified shader :

Shader "Unlit/360ImagePortal_custom"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Stencil{
        Ref 2
        Comp Equal
        Pass Keep
        }

        Pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            // DepthAPI Environment Occlusion
            #pragma multi_compile _ HARD_OCCLUSION SOFT_OCCLUSION

            #include "Packages/com.meta.xr.depthapi.urp/Shaders/EnvironmentOcclusionURP.hlsl"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                ////UNITY_FOG_COORDS(1)
                float fogCoord  : TEXCOORD2;
                float4 vertex : SV_POSITION;

                float4 positionNDC : TEXCOORD1;

                UNITY_VERTEX_INPUT_INSTANCE_ID
                UNITY_VERTEX_OUTPUT_STEREO // required for stereo
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2f vert (appdata v)
            {
                v2f o;

                UNITY_SETUP_INSTANCE_ID(v);
                UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); // required to support stereo

                o.vertex = TransformObjectToHClip(v.vertex.xyz);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                o.fogCoord = ComputeFogFactor(o.vertex.z);
                ////UNITY_TRANSFER_FOG(o,o.vertex);

                float4 ndc = o.vertex * 0.5f;
                o.positionNDC.xy = float2(ndc.x, ndc.y * _ProjectionParams.x) + ndc.w;
                o.positionNDC.zw = o.vertex.zw;

                return o;
            }

            float4 frag (v2f i) : SV_Target
            {
                UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);

                // sample the texture
                float4 col = tex2D(_MainTex, i.uv);
                // apply fog
                ////UNITY_APPLY_FOG(i.fogCoord, col);
                col.rgb = MixFogColor(col.rgb, half3(1,1,1), i.fogCoord);

                // calculate UV for the depth texture lookup for occlusions
                float2 uv = i.positionNDC.xy / i.positionNDC.w;

                // pass UV and the current depth of the texel
                float occlusionValue = CalculateEnvironmentDepthOcclusion(uv, i.vertex.z);

                // consider early rejection to not write to depth if it's an opaque shader
                if (occlusionValue < 0.01)
                {
                    discard;
                }

                // premultiply color and alpha by occlusion value
                // when it's 1 - color is not affected - virtual covers real
                // when it's 0 - texel is invisible - virtual is under real
                // when it's in between - texel is semi transparent
                col *= occlusionValue;

                return col;
            }
            ENDHLSL
        }
    }
}

The "to be occluded" part are showing white instead of passthrough layer

SashelI commented 8 months ago

Oh nvm the white part is on me. Thanks, this seems to be working then.