使用CG编程和Shaderlab在Unity中合并2个着色器

时间:2017-10-22 23:45:18

标签: unity3d merge shader cg shaderlab

我想为Unity合并这两个着色器的功能。 第一个着色器是主要着色器,可添加毛刺全息图效果。 第二个着色器使表面纹理更加细腻,以增加视觉效果。

我正在尝试合并我的代码版本,或者不同的传递允许我在相同的材料上实现这两个功能,如果可能的话。

我已尝试过shaderpass功能,但无法使其正常工作。

有可能吗?

第一个shadercode

    Shader "Custom/Cool Hologram Original"
{

    Properties
    {
    //This program takes an existing shader script and builds upon it, by adding the features :
    //Tintcolor - allowing for manipulation of colors
    //Transparency - Changing opaque in tags to transparency so that the model becomes transparent
    //Properties allow defining public manipulative variables

        _MainTex ("AlbedoTexture", 2D) = "white" {} //braces are useless leftover braces no longer needed according to Unity developer
        _TintColor("Tint Color", Color) = (1,1,1,1)     //Public variable appearing in inspector
        _Transparency("Transparency", Range(0.0,0.5)) = 0.25
        _CutoutTresh("Cutout Threshold", Range(0.0,1.0)) = 0.2
        _Distance("Distance", Float) = 1
        _Amplitude("Amplitude", Float) = 1
        _Speed("Speed", Float) = 1
        _Amount("Amount", Range(0.0,1.0)) = 1
    }   
    //The actual shader program
    SubShader
    {
        Tags {"Queue"="Transparent" "RenderType"="Transparent" }  //Added queue and transparent elements because order matters (see render order queu tag)
        LOD 100     //LOD means level of details and used for when player is close or far away

        ZWrite Off      //This is related to culling and depth testing, controlling if something is written to depth buffer. Zwrite off is for transparent objects      
        Blend SrcAlpha OneMinusSrcAlpha             //See blend factors in unity doc. It is about render order and how they should blend

        Pass            //A pass can be made for each type of situation, like Virtual Reality etc

        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"        //Shaders does not use inheritance, it has got monobehaviour instead, so it must be defined as #included 

            struct appdata                  //Structs are objects with 2 objects each. appdata is passed in to v2f vert as argument
            {
                float4 vertex : POSITION;   //Variable with 4 floating point numbers x,y,z,w. POSITION is a semantic binding telling where it is to be used
                float2 uv : TEXCOORD0;      
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;    //SV_Position corresponds to screen position in Unity
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            float4 _TintColor;
            float _Transparency;
            float _CutoutThresh;
            float _Distance;
            float _Amplitude;
            float _Speed;
            float _Amount;

            v2f vert (appdata v)
            {
                v2f o;
                v.vertex.x += sin(_Time.y * _Speed + v.vertex.y * _Amplitude) * _Distance * _Amount; //Allows sinusoidal movement to vertices in object space b4 translation to clip
                o.vertex = UnityObjectToClipPos(v.vertex);      //See learnopengl.com to see 5 spaces: local space, world space, view space, clip space, screen space
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);           //Taking uv data from model and data from maintexture (see the shader texture in inspector)
                return o;                                       //Returns struct after it has been build in coordinate system, it will then be passed to fragment function
            }

            fixed4 frag (v2f i) : SV_Target                      //Takes in v2f struct calling it i and then bind it to render target, which is frame buffer for screen
            {
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv) + _TintColor; //fixed 4 col is color. Can be fixed rgbA for 3 colors and an alpha channel. tex2D reads in colors from maintex and struct data from i
                col.a = _Transparency;                          //The colors are what actually drawed in this feature with tex3D
                clip(col.r - _CutoutThresh);                    //Not drawing pixels with certain amount of red color
                return col;
            }
            ENDCG
        }
    }
}

第二个shadercode

    Shader "Custom/Offset 1"
{
    Properties {
    _MainTex ("Texture", 2D) = "white" {}
    _ScrollSpeeds ("Scroll Speeds", vector) = (0, -3, 0, 0)
    }

    SubShader
    {
        Tags { "RenderType"="Opaque" }


        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

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

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            // Declare our new parameter here so it's visible to the CG shader
            float4 _ScrollSpeeds;

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

                 // Shift the uvs over time.
                o.uv += _ScrollSpeeds * _Time.x;

                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv);
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, col);
                return col;
            }
            ENDCG
        }
    }
}
  

第三个shadercode

Shader "Custom/Combined" 
{

Properties 
{
_MainTex ("AlbedoTexture", 2D) = "white" {}
_HoloColor("Hologram Color", Color) = (1,1,1,1)
_Transparency("Transparency", Range(0.0,0.5)) = 0.25
_CutoutTresh("Cutout Threshold", Range(0.0,1.0)) = 0.2
_Distance("Distance", Float) = 1
_Amplitude("Amplitude", Float) = 1
_Speed("Speed", Float) = 1
_ScrollSpeeds("Scroll Speeds", vector) = (0,-3,0,0) //added from offset shader
_Amount("Amount", Range(0.0,1.0)) = 1

}

SubShader {

    Tags {"Queue"="Transparent" "RenderType"="Transparent" }
    LOD 100     
    ZWrite Off
    Blend SrcAlpha OneMinusSrcAlpha

    Pass 

    {

        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag

        #include "UnityCG.cginc"

        struct appdata {

            float4 vertex : POSITION;
            float2 uv : TEXCOORD0;
        };

        struct v2f {

            float2 uv : TEXCOORD0;
            UNITY_FOG_COORDS(1) //added from offset shader
            float4 vertex : SV_POSITION;

        };

        sampler2D _MainTex;
        float4 _MainTex_ST;
        float4 _HoloColor;
        float4 _ScrollSpeeds;
        float _Transparency;
        float _CutoutThresh;
        float _Distance;
        float _Amplitude;
        float _Speed;
        float _Amount;


        v2f vert (appdata v) 
        {
            v2f o;
            v.vertex.x += sin(_Time.y * _Speed + v.vertex.y * _Amplitude) * _Distance * _Amount;
            o.vertex = UnityObjectToClipPos(v.vertex);      
            o.uv = TRANSFORM_TEX(v.uv, _MainTex);   
            o.uv += _ScrollSpeeds * _Time.x;    //Added from offset shader
            UNITY_TRANSFER_FOG(o,o.vertex);     //added from offset shader

            return o;                                       
        }


        fixed4 frag (v2f i) : SV_Target 
        {
            fixed4 col = tex2D(_MainTex, i.uv) + _HoloColor; 
            col.a = _Transparency;                          
            clip(col.r - _CutoutThresh);    
            UNITY_APPLY_FOG(i.fogCoord, col);   //Added from offset shader
            return col;

        }

        ENDCG
    }
}

Fallback" Diffuse" }

0 个答案:

没有答案