Unity Shader 5 Fixed Function Shader 固定管線Shader

是相對於可編程的shader而言的。所謂固定管線,是說芯片上一組電路已經固定實現了特定的運算功能,程序能做的只是提供場景數據以及微調運算功能的參數。它適用於所有的顯卡,常用於高級Shader在老顯卡無法顯示時的Fallback。Unity中的固定管線shader使用ShaderLab語言。
包括了

  • properties 屬性
  • material 材質
  • lighting 光照
  • settexture 設置紋理
  • pass 通道

1 示例

1.1 VS2019 Shader 插件

打開VS2019 點擊 擴展>管理擴展 搜索 ShaderVS插件,安裝即可

1.2 打開Unity,新建一個Unity 3D項目

在Project欄目中,新建一個Shader 右鍵>Create>Shader 第一個
默認如下:已經包括了屬性 Properties SubShader FallBack

Shader "Custom/ff3"
{
    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;

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        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"
}

1.3 刪除默認代碼

Shader "Custom/ff3"
{
    SubShader{
        
    }
}

 

1.4 增加Color屬性

  • 新建一個材質 FF1
    在右邊的窗口選擇 Customer/FF1

  • 新建一個物體 Sphere

  • 把材質拖到 Sphere上

  • 修改Shader代碼

Shader "Custom/ff3"
{
     Properties
    {
        _Color("Main Color",color)=(1,1,1,1) 
    }
   
    SubShader
    {
        pass { 
            material{
                diffuse[_Color] 
            }
            lighting on
                separateSpecular on
        }
}

語法:屬性[Properties]

  • 增加 diffuse屬性
    diffuse[_color]

  • 增加 Ambinent 環境光
    Ambinent[_Ambinent]

  • 增加 Specular 鏡面反射
    Specular[]

  • 增加 光澤度 Shininess
    Shininess[]

  • 增加自發光
    Emission[]

注意,記住一些名詞即可 Diffuse 散射 Ambinent 環境光 Specular 鏡面反射,增加此屬性,必須增加 separateSpecular on Shininess 光澤度 Emission 自發光

最後完整代碼

Shader "Custom/ff1"
{
    Properties
    {
        _Color("Main Color",color)=(1,1,1,1)
        _Ambient("Ambient",color)=(0.3,0.3,0.3,0.3)
        _Specular("Specular",color)=(1,1,1,1)
        _Shininess("Shininess",range(0,9))=4
        _Emission("Emission",color)=(1,1,1,1)
    }
   
    SubShader
    {
        pass {
            //color(1,0,0,1)
            //color[_Color]
            material{
                Diffuse[_Color]//散射
                Ambient[_Ambient]//環境光
                Specular[_Specular]//增加鏡面反射 specular 必須增加 separateSpecular
                Shininess[_Shininess]//光澤,光澤
                Emission[_Emission]//自發光 
            }
            lighting on
            separateSpecular on
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章