【问题标题】:Combining Two Shaders into One Shader将两个着色器合并为一个着色器
【发布时间】:2019-05-14 02:40:58
【问题描述】:

Unity 项目,想要将这两个着色器组合成一个着色器以同时获得它们的功能。一个着色器用于照明,另一个着色器用于更好地渲染。如何结合?

Shader "Transparent/Cutout/Lit3dSprite" {
Properties{
    _MainCol("Main Tint", Color) = (1,1,1,1)
    _MainTex("Main Texture", 2D) = "white" {}
    _Cutoff("Alpha cutoff", Range(0,1)) = 0.5
}

SubShader{
    Tags {"Queue" = "AlphaTest" "IgnoreProjector" = "True" "RenderType" = "TransparentCutout" "PreviewType" = "Plane"}
    Cull Off
    LOD 200

    CGPROGRAM
    #pragma surface surf SimpleLambert alphatest:_Cutoff addshadow fullforwardshadows
    #pragma target 3.0

    sampler2D _MainTex;
    fixed4 _MainCol;

    half4 LightingSimpleLambert(SurfaceOutput s, half3 lightDir, half atten) 

{
            half4 c;
            c.rgb = s.Albedo * _MainCol.rgb * (atten)* _LightColor0.rgb;
            c.a = s.Alpha;
            return c;
        }

        struct Input {
            float2 uv_MainTex;
        };

        void surf(Input IN, inout SurfaceOutput o) {
            fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _MainCol;
            o.Albedo = lerp(c.rgb, c.rgb, c.a);
            o.Alpha = c.a;
        }
        ENDCG
    }

    Fallback "Transparent/Cutout/VertexLit"
}

着色器 2:

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "RetroAA/Sprite"
{
    Properties
    {
        [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
        _Color ("Tint", Color) = (1,0,0,0)
    }
    SubShader
    {
        Tags {
            "Queue"="Transparent" 
            "IgnoreProjector"="True" 
            "RenderType"="Transparent" 
            "PreviewType"="Plane"
            "CanUseSpriteAtlas"="True"
        }
        LOD 100

        Cull Off
        Lighting Off
        ZWrite Off
        Blend SrcAlpha OneMinusSrcAlpha

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fog

            #include "RetroAA.cginc"

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

            struct v2f {
                float4 vertex : SV_POSITION;
                fixed4 color : COLOR;
                float2 uv : TEXCOORD0;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            float4 _MainTex_TexelSize;

            float4 _Color;

            v2f vert(appdata v){
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                o.color = v.color * _Color;

                return o;
            }

            fixed4 frag(v2f i) : SV_Target {
                fixed4 color = RetroAA(_MainTex, i.uv, _MainTex_TexelSize);
                return i.color*color*color.a;
            }
            ENDCG
        }
    }
}

【问题讨论】:

  • 你已经尝试了什么?
  • 尝试添加每个组件但没有用,虽然我不精通添加着色器
  • 我们无法知道RetroAA.cginc 中的内容,因此无法给出准确的答案。试试这个:使用你的第一个着色器,然后:1.#pragma 行之后,添加#include "RetroAA.cginc"float4 _MainTex_TexelSize;2.surf 中,将fixed4 c = ... 替换为fixed4 c = RetroAA(_MainTex, i.uv, _MainTex_TexelSize);。您可能需要更改一些标签和 LOD 等,但这两个步骤可能会将第二个步骤合并到第一个步骤中。
  • #include "UnityCG.cginc" #pragma target 3.0 fixed4 RetroAA(sampler2D tex, float2 uv, float4 texelSize){ float2 texelCoord = uv*texelSize.zw; float2 hfw = 0.5*fwidth(texelCoord); float2 fl = floor(texelCoord - 0.5) + 0.5; float2 uvaa = (fl + smoothstep(0.5 - hfw, 0.5 + hfw, texelCoord - fl))*texelSize.xy;返回 tex2D(tex, uvaa); }

标签: unity3d shader hlsl fragment-shader


【解决方案1】:

第二个着色器并不太复杂,只需使用第二个着色器的代码更改第一个着色器的surf 计算fixed4 c 的方式,就可以合并到第一个着色器中。

您还需要包含RetroAA 的定义,并在着色器变量中包含主纹理的 Texel Size。

但是,第一个着色器假定没有部分透明度,而第二个着色器需要它,所以你必须适应它。您需要使用 Alpha 混合,将 RenderTypeQueue 更改为 Transparent,并指明 ZWrite Off

这可能是所有的东西:

Shader "Transparent/Cutout/Lit3dSprite" {
    Properties{
        _MainCol("Main Tint", Color) = (1,1,1,1)
        _MainTex("Main Texture", 2D) = "white" {}
        _Cutoff("Alpha cutoff", Range(0,1)) = 0.5
    }

        SubShader{
            // change RenderType and Queue to Transparent
            Tags {"Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" "PreviewType" = "Plane"}
            Cull Off
            ZWrite Off // Add this
            LOD 200

            // Enable Alpha blending here
            Blend SrcAlpha OneMinusSrcAlpha

            CGPROGRAM
            // Enable Alpha blending here also
            #pragma surface surf SimpleLambert alphatest:_Cutoff addshadow fullforwardshadows alpha:blend
            #pragma target 3.0

            sampler2D _MainTex;
            float4 _MainTex_TexelSize; // Add this
            fixed4 _MainCol;

            // include this
            fixed4 RetroAA(sampler2D tex, float2 uv, float4 texelSize)
            {
                float2 texelCoord = uv * texelSize.zw;
                float2 hfw = 0.5*fwidth(texelCoord);
                float2 fl = floor(texelCoord - 0.5) + 0.5;
                float2 uvaa = (fl + smoothstep(0.5 - hfw, 0.5 + hfw, texelCoord - fl))*texelSize.xy;
                return tex2D(tex, uvaa);
            }

            half4 LightingSimpleLambert(SurfaceOutput s, half3 lightDir, half atten)
            {
                half4 c;

                // Fix the lambert lighting implementation here 
                half NdotL = dot(s.Normal, lightDir);

                // We set the surface rgba in surf, so don't need to do it again here.
                c.rgb = s.Albedo * (NdotL * atten) * _LightColor0.rgb;
                c.a = s.Alpha;
                return c;
            }

            struct Input {
                float2 uv_MainTex;
                float4 color: Color; // Add this to use SpriteRenderer color
            };

            void surf(Input IN, inout SurfaceOutput o) {
                // replace this line: 
                // fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _MainCol;

                // with this
                fixed4 c = RetroAA(_MainTex, IN.uv_MainTex, _MainTex_TexelSize);

                // factor in MainCol and SpriteRenderer color/tints
                o.Albedo = c.rgb * _MainCol.rgb * IN.color.rgb;
                o.Alpha = c.a * _MainCol.a * IN.color.a;
            }
            ENDCG
        }

            Fallback "Transparent/Cutout/VertexLit"
}

您可能需要将Alpha cutoff 调低至零或其他一些较低的数字,以使 AA 正常工作。

【讨论】:

  • 谢谢,您的代码添加了两个着色器的大部分功能,但添加的 retroAA 仍然存在一个问题,即它没有“完全”优化其消除锯齿边缘的功能.包含图片的右侧是一个仅添加了 retroAA 的精灵。左边是组合脚本的精灵。您可以看到边缘的差异。有任何想法吗? ibb.co/n3LgpWD
  • 我意识到第二个着色器有 alpha 混合,但第一个没有。我编辑了我的答案来解决这个问题。希望这会有所帮助。
  • 我尝试将 alpha cutoff 设置为 0,并将 rendertype 设置为透明,但不起作用。难道我们没有从 RetroAA 传输所有代码——比如顶点着色器、片段着色器和编译雾的东西,还有 appdata、v2f 等。我试着添加这些东西,但我不知道如何正确地做所以我要么得到错误,要么精灵变成白色或粉红色
  • @David 现在试试,我将alpha:blend 添加到第一个#pragma。如果这不能解决问题,请添加另一个屏幕截图,说明它与我的答案的当前代码的外观
  • 这是带有更新代码的图片 - 所以它看起来像修复了边缘,据我所知,但是它后面的精灵混合出现了问题。或排序或 w/e ibb.co/qJfFZNz
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多