关于程序员:Unity-Shader学习笔记

57次阅读

共计 2396 个字符,预计需要花费 6 分钟才能阅读完成。

 在 Unity5.2 及以上版本中,Unity 一共提供了 4 中 UnityShader 模板供咱们抉择,别离是:UnlitShader、SurfaceShader、ImageEffectShader、ComputeShader。

    SurfaceShader:会产生一个蕴含了规范光照模型的外表着色器模板,代码如下:

 

Shader “Custom/SurfaceShader” {

Properties {_Color ("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
}
SubShader {Tags { "RenderType"="Opaque"}
    LOD 200
    
    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;};

    half _Glossiness;
    half _Metallic;
    fixed4 _Color;

    void surf (Input IN, inout SurfaceOutputStandard o) {
        // Albedo comes from a texture tinted by color
        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
} 
FallBack "Diffuse"

}
Unlit Shader 会产生一个不蕴含光照(但蕴含雾效)的根本顶点 / 片元着色器,代码如下:

Shader “Unlit/UnlitShader”
{

Properties
{_MainTex ("Texture", 2D) = "white" {}}
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;
        
        v2f vert (appdata v)
        {
            v2f o;
            o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
            o.uv = TRANSFORM_TEX(v.uv, _MainTex);
            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
    }
}

}

Image Effect Shader 则为咱们实现各种屏幕后处理成果提供了一个模板,代码如下:

Shader “Hidden/ImageEffectShader”
{

Properties
{_MainTex ("Texture", 2D) = "white" {}}
SubShader
{
    // No culling or depth
    Cull Off ZWrite Off ZTest Always

    Pass
    {
        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        
        #include "UnityCG.cginc"

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

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

        v2f vert (appdata v)
        {
            v2f o;
            o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
            o.uv = v.uv;
            return o;
        }
        
        sampler2D _MainTex;

        fixed4 frag (v2f i) : SV_Target
        {fixed4 col = tex2D(_MainTex, i.uv);
            // just invert the colors
            col = 1 - col;
            return col;
        }
        ENDCG
    }
}

}
Compute Shader 会产生一张非凡的 Shader 文件,这类 Shader 旨在利用 GPU 的并行性来进行一些与惯例渲染流水线无关的计算,
与失常着色器相似,Compute 着色器是我的项目中的资源文件,具备 * .compute 文件扩展名。它们是用 DirectX 11 格调的 HLSL 语言编写的,用最大量的 #pragma 编译指令来批示哪些函数要编译为计算着色器内核。

正文完
 0