美文网首页Unity Shader分享
Shader- MultiPass(转)

Shader- MultiPass(转)

作者: 树上的cat_ee3c | 来源:发表于2017-09-28 23:17 被阅读10次

    实现高级效果时,往往需要用到多个Pass.

    用AlphaBlend混合

    例1:

    将一张图 分别用红色 和 绿色渲染并混合

    (1)新建场景新建一个plane

    (2)新建一个material并随便赋张贴图,并让plane使用该material

    material所使用的shader如下

    Shader "Custom/MyMultiPass" {

    Properties {

    _MainTex ("Base (RGB)", 2D) = "white" {}

    }

    SubShader

    {

    Tags{"Queue"="Transparent"}

    pass

    {

    Name "pass1"

    CGPROGRAM

    #pragma vertex vert

    #pragma fragment frag

    #include "UnityCG.cginc"

    sampler2D _MainTex;

    float4 _MainTex_ST;

    struct v2f {

    float4pos : SV_POSITION;

    float2uv : TEXCOORD0;

    } ;

    v2f vert (appdata_base v)

    {

    v2f o;

    o.pos = mul(UNITY_MATRIX_MVP,v.vertex);

    o.uv =TRANSFORM_TEX(v.texcoord,_MainTex);

    return o;

    }

    float4 frag (v2f i) : COLOR

    {

    float4 texCol = tex2D(_MainTex,i.uv+0.1);

    float4 outp;

    outp = texCol * float4(1,0,0,1);

    return outp;

    }

    ENDCG

    }

    pass

    {

    Blend one one

    Name "pass2"

    CGPROGRAM

    #pragma vertex vert

    #pragma fragment frag

    #include "UnityCG.cginc"

    sampler2D _MainTex;

    float4 _MainTex_ST;

    struct v2f {

    float4pos : SV_POSITION;

    float2uv : TEXCOORD0;

    } ;

    v2f vert (appdata_base v)

    {

    v2f o;

    o.pos = mul(UNITY_MATRIX_MVP,v.vertex);

    o.uv =TRANSFORM_TEX(v.texcoord,_MainTex);

    return o;

    }

    float4 frag (v2f i) : COLOR

    {

    float4 texCol = tex2D(_MainTex,i.uv);

    float4 outp;

    outp = texCol * float4(0,1,0,1);

    return outp;

    }

    ENDCG

    }

    }

    }

    效果如下,红色渲出来的图像 和 绿色渲出来的图像 混合。

    如果把AlphaBlend的指令 Blend one one去掉,那么效果如下,即显示的是最后一个pass的效果。

    相关文章

      网友评论

        本文标题:Shader- MultiPass(转)

        本文链接:https://www.haomeiwen.com/subject/rjhbextx.html