[Unity 5.xShaders]Diffuse Shader--創(chuàng)建你的第一個(gè)shader


創(chuàng)建一個(gè)基本的 Standard Shader

關(guān)系圖.png

作為一個(gè)unity的開(kāi)發(fā)者,大家都知道components,所有的objects都包含了一系列的components。scripts決定了objects的邏輯,renderers決定了表現(xiàn)。而所有的renderers都包含幾個(gè)materials。每個(gè)material都包含一個(gè)獨(dú)立的shader。他們的關(guān)系如上圖。


準(zhǔn)備階段

  1. 運(yùn)行untiy5并打開(kāi)一個(gè)新的工程
  2. Assets目錄下新建文件夾:Shaders
  3. Assets目錄下新建文件夾:Materials

如何實(shí)現(xiàn)

  1. 在Shaders文件夾下右鍵Create/Shader/Standard Surface Shader新建一個(gè)shader;
  2. 在Materials文件夾下右鍵Create/Material新建一個(gè)material;
  3. 把剛新建的shader和material重命名為StandardDiffuse;
  4. 把新建的shader拖給新建的material;
  5. 在場(chǎng)景中新建一個(gè)Cube,把新建的material拖給Cube
  6. 可以添加一個(gè)Texture給material

具體效果

效果.png

解釋說(shuō)明

打開(kāi)的shader像這個(gè)樣子:

Shader "Custom/StandardDiffuse" {
    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"
}

我們可以發(fā)現(xiàn)unity已經(jīng)幫我們寫(xiě)了很多內(nèi)容,初步需要知道:
1.第一行表明了這個(gè)shader在Unity中的路徑,它會(huì)出現(xiàn)在你選擇某個(gè)Material的shader時(shí)的下拉列表里,并且可以隨時(shí)更改;
2.最后一行表明,當(dāng)這個(gè)shader在當(dāng)前環(huán)境中運(yùn)行失敗后,會(huì)默認(rèn)調(diào)用Unity自帶的Diffuse Shader;

這就是一個(gè)簡(jiǎn)單基于physically-based rendering的Surface Shaer,你會(huì)發(fā)現(xiàn)它和Unity4有很大的不同。在引入physically-based shaders前,Unity4使用了更復(fù)雜的方法。而Unity內(nèi)部使用Cg語(yǔ)言實(shí)現(xiàn),在Unity\Editor\Data\CGIncludes可以查看。

欲知后事如何,且聽(tīng)下回分解

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容