在单个网格上渲染多种材质

Fre*_*ave 5 shader unity-game-engine

在 Unity3D 中,我尝试渲染一个生物并在选择它时显示轮廓。

仅生物就被渲染得很好:

生物的形象

在 Github 上下载了一个轮廓着色器,并将其作为第二种材质应用到我的网格中:

渲染器设置图像

展开后的材料如下所示:

膨胀材料的图像

然而,结果却完全不符合预期:

材料的错误组合

在对材质和着色器了解不多的情况下,我尝试摆弄并发现如果我将标准材质的渲染模式更改为透明,结果看起来不错:

正确的渲染结果

但现在该生物单独以一种奇怪的方式呈现,四肢与身体重叠:

生物的错误渲染

实现我想要做的事情的正确方法是什么?您有资源可以让我阅读更多内容吗?

Plu*_*uto 2

您的设置问题是渲染队列。透明物体在不透明物体之后渲染,因此您的轮廓仅绘制在生物的顶部。如果要更改渲染顺序,则必须将具有轮廓的对象视为“特殊”不透明对象(例如,绘制普通对象、绘制轮廓、绘制生物)。

这里有几个替代方案:

  1. 使用Cull Front- 该着色器基本上是在原始对象之上绘制对象的更大副本,例如贝壳。Cull Front 会绘制对象后面的外壳的背面,而不是正面。
  2. 使用模板缓冲区标记绘制原始对象的区域,并在绘制轮廓时跳过它。

下面是着色器的修改版本(删除了第二个颜色通道和表面着色器通道,因为您不使用它们)。这是模板缓冲区选项。如果您想尝试另一个,请删除第一遍、第二遍中的模板块并替换Cull BackCull Front

Shader "Outlined/UltimateOutline"
{
    Properties
    {
        _Color("Main Color", Color) = (0.5,0.5,0.5,1)       

        _FirstOutlineColor("Outline color", Color) = (1,0,0,0.5)
        _FirstOutlineWidth("Outlines width", Range(0.0, 2.0)) = 0.15        

        _Angle("Switch shader on angle", Range(0.0, 180.0)) = 89
    }
    CGINCLUDE
    #include "UnityCG.cginc"

    struct appdata {
        float4 vertex : POSITION;
        float4 normal : NORMAL;
    };

    uniform float4 _FirstOutlineColor;
    uniform float _FirstOutlineWidth;

    uniform float4 _Color;
    uniform float _Angle;

    ENDCG

    SubShader{              
        Pass {
            Tags{ "Queue" = "Transparent-1" "IgnoreProjector" = "True" }
            ZWrite Off
            Stencil {
                Ref 1
                Comp always
                Pass replace
            }
            ColorMask 0
        }

        //First outline
        Pass{
            Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
            Stencil {
                Ref 1
                Comp NotEqual
            }

            Blend SrcAlpha OneMinusSrcAlpha
            ZWrite Off
            Cull Back      //Replace this with Cull Front for option 1
            CGPROGRAM

            struct v2f {
                float4 pos : SV_POSITION;
            };

            #pragma vertex vert
            #pragma fragment frag

            v2f vert(appdata v) {
                appdata original = v;

                float3 scaleDir = normalize(v.vertex.xyz - float4(0,0,0,1));
                //This shader consists of 2 ways of generating outline that are dynamically switched based on demiliter angle
                //If vertex normal is pointed away from object origin then custom outline generation is used (based on scaling along the origin-vertex vector)
                //Otherwise the old-school normal vector scaling is used
                //This way prevents weird artifacts from being created when using either of the methods
                if (degrees(acos(dot(scaleDir.xyz, v.normal.xyz))) > _Angle) {
                    v.vertex.xyz += normalize(v.normal.xyz) * _FirstOutlineWidth;
                }
                else {
                   v.vertex.xyz += scaleDir * _FirstOutlineWidth;
                }

                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                return o;
            }

            half4 frag(v2f i) : COLOR{
                return _FirstOutlineColor;
            }
        ENDCG
        }
    }
    Fallback "Diffuse"
}
Run Code Online (Sandbox Code Playgroud)