在增强现实中用平面裁剪的表面着色器问题

问题描述 投票:0回答:0

我想在 Unity 中用飞机剪辑 3D 模型,找到了这个很棒的教程,并让它在通常的 Unity 环境中轻松工作。本教程使用表面着色器剪辑平面上方的所有部分,只显示平面下方的部分,这样您就会有切开 3D 模型的感觉(请参阅链接教程中的 GIF)。表面着色器的代码是这样的:

Shader "Clippingplane" {
    Properties {
        _Color ("Tint", Color) = (0, 0, 0, 1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Smoothness ("Smoothness", Range(0, 1)) = 0
        _Metallic ("Metalness", Range(0, 1)) = 0
        [HDR] _Emission ("Emission", color) = (0,0,0)

        [HDR]_CutoffColor("Cutoff Color", Color) = (1,0,0,0)
    }
    SubShader {
        Tags{ "RenderType"="Opaque" "Queue"="Geometry"}

        // render faces regardless if they point towards the camera or away from it
        Cull Off

        CGPROGRAM

        #pragma surface surf Standard fullforwardshadows
        #pragma target 3.0

        sampler2D _MainTex;
        fixed4 _Color;

        half _Smoothness;
        half _Metallic;
        half3 _Emission;

        float4 _Plane;

        float4 _CutoffColor;

        struct Input {
            float2 uv_MainTex;
            float3 worldPos;
            float facing : VFACE;
        };

        void surf (Input i, inout SurfaceOutputStandard o) {
            //calculate signed distance to plane
            float distance = dot(i.worldPos, _Plane.xyz);
            distance = distance + _Plane.w;
            //discard surface above plane
            clip(-distance);

            float facing = i.facing * 0.5 + 0.5; //convert facing from -1/1 to 0/1 for linear interpolation

            //normal color stuff
            fixed4 col = tex2D(_MainTex, i.uv_MainTex);
            col *= _Color;
            o.Albedo = col.rgb * facing;
            o.Metallic = _Metallic * facing;
            o.Smoothness = _Smoothness * facing;
            o.Emission = lerp(_CutoffColor, _Emission, facing); // lerp = linear interpolation

        }
        ENDCG
    }
    FallBack "Standard"
}

现在我想将通过 3D 模型移动裁剪平面的整个交互转换为 增强现实,为此我将 ARFoundation 与 ARCore 结合使用。

出于某种原因,着色器现在不再像预期的那样在 AR 中工作。 “内部颜色”(红色)覆盖了整个模型本身,而不仅仅是模型被切开的部分。似乎着色器无法区分外部和内部?然而,剪辑部分有效。

Screenshot of the whole 3D model showing in red

我玩了一下,但只是让它在没有剪裁部分工作的情况下显示正确的颜色。尤其是带有 facing 变量及其转换的部分看起来像是在搞乱结果。我真的不太了解着色器,所以我想知道是否有人可以指出正确的方向法线和其他东西发生了什么? 当省略“显示内部”部分时,教程中的表面着色器在 AR 中工作正常。 超级奇怪,着色器在通常的 Unity 环境中工作,而不是在 AR 中工作。任何帮助表示赞赏!

unity3d shader arcore
© www.soinside.com 2019 - 2024. All rights reserved.