尝试合并两个着色器(双面和横截面),但语法错误

时间:2019-05-22 09:59:39

标签: unity3d glsl shader hololens mrtk

我正在尝试“合并”两个着色器的代码:

第一个是Microsoft Holotoolkit中Hololens的双面着色器:

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

Shader "MixedRealityToolkit/Obsolete/Fast Configurable 2 Sided"
{
    Properties
    {
        _Mode("Rendering Mode", Float) = 0.0

        [Toggle] _UseVertexColor("Vertex Color Enabled?", Float) = 0
        [Toggle] _UseMainColor("Main Color Enabled?", Float) = 0
        _Color("Main Color", Color) = (1,1,1,1)
        [Toggle] _UseMainTex("Main Texture Enabled?", Float) = 0
        [NoScaleOffset]_MainTex("Main Texture", 2D) = "red" {}

        [Toggle] _UseOcclusionMap("Occlusion/Detail Texture Enabled?", Float) = 0
        [NoScaleOffset]_OcclusionMap("Occlusion/Detail Texture", 2D) = "blue" {}

        [Toggle] _UseAmbient("Ambient Lighting Enabled?", Float) = 1
        [Toggle] _UseDiffuse("Diffuse Lighting Enabled?", Float) = 1

        [Toggle] _SpecularHighlights("Specular Lighting Enabled?", Float) = 0
        [Toggle] _Shade4("Use additional lighting data? (Expensive!)", Float) = 0

        [Toggle] _ForcePerPixel("Light per-pixel (always on if a map is set)", Float) = 0

        _SpecColor("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
        [PowerSlider(5.0)]_Specular("Specular (Specular Power)", Range(1.0, 100.0)) = 10.0
        [Toggle] _UseSpecularMap("Use Specular Map? (per-pixel)", Float) = 0
        [NoScaleOffset]_SpecularMap("Specular Map", 2D) = "white" {}

        _Gloss("Gloss (Specular Scale)", Range(0.1, 10.0)) = 1.0
        [Toggle] _UseGlossMap("Use Gloss Map? (per-pixel)", Float) = 0
        [NoScaleOffset]_GlossMap("Gloss Map", 2D) = "white" {}

        [Toggle] _UseBumpMap("Normal Map Enabled? (per-pixel)", Float) = 0
        [NoScaleOffset][Normal] _BumpMap("Normal Map", 2D) = "bump" {}

        [Toggle] _UseReflections("Reflections Enabled?", Float) = 0
        [NoScaleOffset]_CubeMap("CubeMap", Cube) = "" {}
        _ReflectionScale("Reflection Scale", Range(0.01, 3.0)) = 2.0
        [Toggle]_CalibrationSpaceReflections("Reflect in calibration space?", Float) = 0

        [Toggle] _UseRimLighting("Rim Lighting Enabled?", Float) = 0
        [PowerSlider(.6)]_RimPower("Power", Range(0.1, 1.0)) = 0.7
        _RimColor("Color", Color) = (1,1,1,1)

        [Toggle] _UseEmissionColor("Emission Color Enabled?", Float) = 0
        _EmissionColor("Emission Color", Color) = (1,1,1,1)
        [Toggle] _UseEmissionMap("Emission Map Enabled?", Float) = 0
        [NoScaleOffset] _EmissionMap("Emission Map", 2D) = "blue" {}

        _TextureScaleOffset("Texture Scale (XY) and Offset (ZW)", Vector) = (1, 1, 0, 0)

        [Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend("SrcBlend", Float) = 1 //"One"
        [Enum(UnityEngine.Rendering.BlendMode)] _DstBlend("DestBlend", Float) = 0 //"Zero"
        [Enum(UnityEngine.Rendering.BlendOp)] _BlendOp("BlendOp", Float) = 0 //"Add"

        [Toggle] _AlphaTest("Alpha test enabled?", Float) = 0
        _Cutoff("Alpha Cutoff", Range(-0.1, 1.0)) = -0.1

        [Enum(UnityEngine.Rendering.CompareFunction)] _ZTest("ZTest", Float) = 4 //"LessEqual"
        [Enum(Off,0,On,1)] _ZWrite("ZWrite", Float) = 1 //"On"
        [Enum(UnityEngine.Rendering.ColorWriteMask)] _ColorWriteMask("ColorWriteMask", Float) = 15 //"All"
    }

    SubShader
    {
        Tags { "RenderType"="Opaque" "PerformanceChecks"="False" }
        LOD 100
        Blend[_SrcBlend][_DstBlend]
        BlendOp[_BlendOp]
        ZTest[_ZTest]
        ZWrite[_ZWrite]

        Pass
        {
            Name "FRONT"
            Tags { "LightMode" = "ForwardBase" }
            Cull Back
            ColorMask[_ColorWriteMask]

            CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag

                //compiles all variants needed by ForwardBase (forward rendering base) pass type. The variants deal with different lightmap types and main directional light having shadows on or off.
                #pragma multi_compile_fwdbase

                //expands to several variants to handle different fog types
                #pragma multi_compile_fog

                //We only target the HoloLens (and the Unity editor), so take advantage of shader model 5.
                #pragma target 5.0
                #pragma only_renderers d3d11

                //shader features are only compiled if a material uses them
                #pragma shader_feature _USEVERTEXCOLOR_ON
                #pragma shader_feature _USEMAINCOLOR_ON
                #pragma shader_feature _USEMAINTEX_ON
                #pragma shader_feature _USESOCCLUSIONMAP_ON
                #pragma shader_feature _USEBUMPMAP_ON
                #pragma shader_feature _USEAMBIENT_ON
                #pragma shader_feature _USEDIFFUSE_ON
                #pragma shader_feature _USESPECULAR_ON
                #pragma shader_feature _USEGLOSSMAP_ON
                #pragma shader_feature _SHADE4_ON
                #pragma shader_feature _USEREFLECTIONS_ON
                #pragma shader_feature _USERIMLIGHTING_ON
                #pragma shader_feature _USEEMISSIONCOLOR_ON
                #pragma shader_feature _USEEMISSIONTEX_ON
                #pragma shader_feature _ALPHATEST_ON

                //scale and offset will apply to all
                #pragma shader_feature _MainTex_SCALE_ON
                #pragma shader_feature _MainTex_OFFSET_ON

                //may be set from script so generate both paths
                #pragma multi_compile __ _NEAR_PLANE_FADE_ON

                #include "FastConfigurable.cginc"           
            ENDCG
        }

        Pass
        {
            Name "BACK"
            Tags{ "LightMode" = "ForwardBase" }
            Cull Front
            ColorMask[_ColorWriteMask]

            CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag

                //compiles all variants needed by ForwardBase (forward rendering base) pass type. The variants deal with different lightmap types and main directional light having shadows on or off.
                #pragma multi_compile_fwdbase

                //expands to several variants to handle different fog types
                #pragma multi_compile_fog

                //We only target the HoloLens (and the Unity editor), so take advantage of shader model 5.
                #pragma target 5.0
                #pragma only_renderers d3d11

                //shader features are only compiled if a material uses them
                #pragma shader_feature _USEMAINCOLOR_ON
                #pragma shader_feature _USEMAINTEX_ON
                #pragma shader_feature _USESOCCLUSIONMAP_ON
                #pragma shader_feature _USEBUMPMAP_ON
                #pragma shader_feature _USEAMBIENT_ON
                #pragma shader_feature _USEDIFFUSE_ON
                #pragma shader_feature _USESPECULAR_ON
                #pragma shader_feature _USEGLOSSMAP_ON
                #pragma shader_feature _SHADE4_ON
                #pragma shader_feature _USEEMISSIONCOLOR_ON
                #pragma shader_feature _USEEMISSIONTEX_ON

                //scale and offset will apply to all
                #pragma shader_feature _MainTex_SCALE_ON
                #pragma shader_feature _MainTex_OFFSET_ON

                //may be set from script so generate both paths
                #pragma multi_compile __ _NEAR_PLANE_FADE_ON

                #define FLIP_NORMALS 1
                #include "FastConfigurable.cginc"
            ENDCG
        }
    } 

    CustomEditor "HoloToolkit.Unity.FastConfigurable2SidedGUI"
    Fallback "VertexLit" //for shadows
}

第二个是Unity资产商店的横截面着色器:

Shader "CrossSection/OnePlaneBSP" {
    Properties{
        _Color("Color", Color) = (1,1,1,1)
        _CrossColor("Cross Section Color", Color) = (1,1,1,1)
        _MainTex("Albedo (RGB)", 2D) = "white" {}
        _Glossiness("Smoothness", Range(0,1)) = 0.5
        _Metallic("Metallic", Range(0,1)) = 0.0
        _PlaneNormal("PlaneNormal",Vector) = (0,1,0,0)
        _PlanePosition("PlanePosition",Vector) = (0,0,0,1)
        _StencilMask("Stencil Mask", Range(0, 255)) = 255
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        //LOD 200
        Stencil
        {
            Ref [_StencilMask]
            CompBack Always
            PassBack Replace

            CompFront Always
            PassFront Zero
        }
        Cull Back
            CGPROGRAM
            // Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows

            // Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0

            sampler2D _MainTex;

        struct Input {
            float2 uv_MainTex;

            float3 worldPos;
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;
        fixed4 _CrossColor;
        fixed3 _PlaneNormal;
        fixed3 _PlanePosition;
        bool checkVisability(fixed3 worldPos)
        {
            float dotProd1 = dot(worldPos - _PlanePosition, _PlaneNormal);
            return dotProd1 > 0  ;
        }
        void surf(Input IN, inout SurfaceOutputStandard o) {
            if (checkVisability(IN.worldPos))discard;
            fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb;
            // Metallic and smoothness come from slider variables
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;
        }
        ENDCG

            Cull Front
            CGPROGRAM
#pragma surface surf NoLighting  noambient

        struct Input {
            half2 uv_MainTex;
            float3 worldPos;

        };
        sampler2D _MainTex;
        fixed4 _Color;
        fixed4 _CrossColor;
        fixed3 _PlaneNormal;
        fixed3 _PlanePosition;
        bool checkVisability(fixed3 worldPos)
        {
            float dotProd1 = dot(worldPos - _PlanePosition, _PlaneNormal);
            return dotProd1 >0 ;
        }
        fixed4 LightingNoLighting(SurfaceOutput s, fixed3 lightDir, fixed atten)
        {
            fixed4 c;
            c.rgb = s.Albedo;
            c.a = s.Alpha;
            return c;
        }

        void surf(Input IN, inout SurfaceOutput o)
        {
            if (checkVisability(IN.worldPos))discard;
            o.Albedo = _CrossColor;

        }
            ENDCG

    }
    //FallBack "Diffuse"
}

我尝试从双面着色器开始,并在第一个PASS的末尾从第二个着色器添加pragma冲浪表面,如下所示:

...
//End of the first PASS from the first shader


        #include "FastConfigurable.cginc"       



#pragma surface surf NoLighting  noambient


        struct Input {
            half2 uv_MainTex;
            float3 worldPos;

        };
        fixed4 _CrossColor;
        fixed3 _PlaneNormal;
        fixed3 _PlanePosition;
        bool checkVisability(fixed3 worldPos)
        {
            float dotProd1 = dot(worldPos - _PlanePosition, _PlaneNormal);
            return dotProd1 > 0;
        }
        fixed4 LightingNoLighting(SurfaceOutput s, fixed3 lightDir, fixed atten)
        {
            fixed4 c;
            c.rgb = s.Albedo;
            c.a = s.Alpha;
            return c;
        }

        void surf(Input IN, inout SurfaceOutput o)
        {
            if (checkVisability(IN.worldPos))discard;
            o.Albedo = _CrossColor;

        }
            ENDCG
        }


        Pass
        {
            Name "BACK"
            Tags{ "LightMode" = "ForwardBase" }
            Cull Front
            ColorMask[_ColorWriteMask]

            CGPROGRAM
//Next of the second PASS of the first shader
...

但是我遇到以下错误:

  

Shader Compiler套接字异常:终止着色器编译器进程

     

着色器编译器:预处理DoubleSidedStandardCrossCut.shader:   与着色器编译器进程通信时发生内部错误。请   报告包含此着色器和编辑器日志的错误。

     

“ DoubleSidedStandardCrossCut”中的着色器错误:解析错误:语法   错误,意外的$ end,期望TOK_SHADER位于第1行

如果您有任何建议或建议,我将非常感谢,谢谢!

1 个答案:

答案 0 :(得分:1)

通常,这归结为您想要实现的目标,并了解哪一部分着色器可以实现最终结果。

这不是很清楚,我只能做一些假设。看来您有给定的网格;您希望根据平面的位置和法线将其( 1 切出;并希望( 2 正面(F)和背面(B)具有不同的渲染属性。

编写多遍着色器通常只是您可以通过设置完成的事情的简写,尽管可能会增加一些开销:每遍本质上是另一个绘制调用,它也可能是另一个对象。

选项:

  1. 编写多遍着色器
  2. 将2个对象嵌套在一个公共变换中,或者将对象F或B嵌套在另一个对象中。
  3. 如果您的网格物体没有子网格物体,则将2种材质分配给MeshRenderer组件。此列表通常为每个子网格分配不同的材质,但是如果不存在,它将再次绘制网格。

在任一情况下 2 3 ,您需要 2种材料。它们都可以使用相同的着色器,或者为简单起见,您可以首先编写2个具有烘焙属性的着色器:

  • 材料B(ack)的渲染队列较低(也许是Geometry-1,因为它总是需要用F(ront)遮盖。 (请参阅https://docs.unity3d.com/Manual/SL-SubShaderTags.html
  • 材料B使用Cull Front标志,材料F使用Cull Back标志(默认标志)
  • 它们两者都使用checkVisibility(IN.worldPos)来丢弃雕刻出来的像素。

附带说明:由于您似乎要进行VR操作,因此此操作必须很快,因此我主张使用clip() hlsl指令并让checkVisibility返回一个float,以摆脱它完全有条件分支。