著色器分析
首先看一下create shader時,unity的幾種著色器
前三個都可以直接拖到material上俩由,圖標也相同苦银。
查了一下發(fā)現(xiàn)
Standard surface shader會產生一個包含了標準光照模型(使用了unity5中新添加的基于物理的渲染方法)的表面著色器模板(為我們提供了典型的表面著色器的實現(xiàn)方法)
Unlit shader則會產生一個不包含關照(但包含霧效)的基本的頂點/片元著色器
Image effect shader則為我們實現(xiàn)各種屏幕后處理效果提供了一個基本的模板
Compute shader會產生一種特殊的shader文件莹妒,這類shader目的是利用GPU的并行性來進行一些與常規(guī)渲染流水線無關的計算红碑。
ShaderVariantCollection是Unity在5.x后提供的一個著色器預編譯的方案舞吭,使用它可以輕松的指定需要著色器變體,在需要的時候加載、解析镣典、編譯等一條龍服務。 其實就是一個著色器資源列表唾琼,是一個由通道類型+著色器關鍵字組合的列表
似懂非懂……看一下代碼吧
Standard surface shader
Shader "Custom/1" {
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_CBUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_CBUFFER_END
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"
}
可以看到可以改變的有顏色兄春、貼圖、平滑度和金屬度(自己定義的float類型)
#pragma surface surf Standard fullforwardshadows
看起來這是一個表面著色器
著色器名稱 | 含義 |
---|---|
surface shaders | 指的是表面著色器锡溯,據說本質上也是頂點和片段著色器 |
vertex and fragment shaders | 頂點著色器主要是處理頂點的平移赶舆,縮放等位置或者大小相關的函數。片段著色器主要處理的是像素的顏色祭饭。 |
fixed function shaders | 指固定功能著色器 |
代碼主體部分可以看到芜茵。void surf (Input IN, inout SurfaceOutputStandard o)
有這樣的一個輸入。然后這個著色器把材質和顏色數據簡單相乘得到的rgb值作為了o的Albedo(反照率倡蝙,=diffuse)九串,其他的值就直接賦給了光照模型。
Input是之前定義的結構體(據說是用于計算中的信息傳遞)寺鸥,SurfaceOutputStandard是標準光照模型猪钮,inout指的是可以輸入也可以輸出
一點疑問:fixed4 c
中fixed4這個變量是用rgba來指代思維的數據嗎?網上查到的定義是:
fixed:11位低精度浮點數。范圍是[-2, 2]胆建,精度是1/256烤低。
Unlit shader
Shader "Unlit/US"
{
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 = UnityObjectToClipPos(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
}
}
}
#pragma vertex vert
說明了這是頂點著色器弊攘。#pragma fragment frag
應該是片段著色器的意思仔涩。在Pass中也能看到對應兩者的函數。
#pragma multi_compile_fog
開啟了霧效
float4 vertex : POSITION;
':' 后面的是語義黍翎,代表這個變量的意義
Tiling表示UV坐標的縮放倍數凉驻,Offset表示UV坐標的起始位置腻要。
ShaderVariantCollection不能用VS打開,嗯……以后研究
今日參考資料:
【淺墨Unity3D Shader編程】之一 夏威夷篇:游戲場景的創(chuàng)建 & 第一個Shader的書寫
yejianyong:unity shader入門指南與總結
Unity3D Shader加載時機和預編譯
Unity5中新的Shader