fixed function shader | 固定機能シェーダー |
vertex and fragment shader | 頂点ごとの処理、ピクセルごとの処理 |
surface shader | ライティング、シャドー、マテリアルなどの表現 |
Shader "VertexLit" { Properties { _Color ("Main Color", Color) = (1,1,1,0.5) _SpecColor ("Spec Color", Color) = (1,1,1,1) _Emission ("Emmisive Color", Color) = (0,0,0,0) _Shininess ("Shininess", Range (0.01, 1)) = 0.7 _MainTex ("Base (RGB)", 2D) = "white" { } } SubShader { Pass { Material { Diffuse [_Color] Ambient [_Color] Shininess [_Shininess] Specular [_SpecColor] Emission [_Emission] } Lighting On SeparateSpecular On SetTexture [_MainTex] { constantColor [_Color] Combine texture * primary DOUBLE, texture * constant } } } }
_Color ("Main Color", Color) = (1,1,1,0.5)
_Color | シェーダー内の名称 |
"Main Color" | インスペクタで表示される名称 |
Color | プロパティの型 |
(1,1,1,0.5) | 初期値 |
name ("display name", Range (min, max)) = number // floatのみ name ("display name", Float) = number name ("display name", Int) = number
name ("display name", Color) = (number,number,number,number) name ("display name", Vector) = (number,number,number,number)
name ("display name", 2D) = "defaulttexture" {} name ("display name", 3D) = "defaulttexture" {} name ("display name", Cube) = "defaulttexture" {} // キューブマップ
Combine ColorPart, AlphaPartカラー、アルファそれぞれのブレンド計算。
Shader "MyShaderName" { Properties { // ... properties here ... } SubShader { // ... subshader for graphics hardware A ... Pass { // ... pass commands ... } // ... more passes if needed ... } SubShader { // ... subshader for graphics hardware B ... } // ... Optional fallback ... FallBack "VertexLit" }
FallBack "VertexLit"ターゲットプラットフォームがどのSubShaderもサポートしていない場合に、動作させるシェーダーを指定する。
Pass{ // ... the usual pass state setup ... CGPROGRAM #pragma vertex vert // 頂点シェーダーの関数名(この例では vert) #pragma fragment frag // フラグメントシェーダーの関数名(この例では frag) // ここに Cg コードを記述 ENDCG // ... the rest of pass setup ... }
SubShader{ Blend SrcAlpha OneMinusSrcAlpha Pass{ } }
SubShader{ Pass{ Blend SrcAlpha OneMinusSrcAlpha CGPROGRAM ENDCG } }
Blend SrcAlpha OneMinusSrcAlpha // Alpha blending Blend One One // Additive Blend OneMinusDstColor One // Soft Additive Blend DstColor Zero // Multiplicative Blend DstColor SrcColor // 2x Multiplicative
Shader "Tutorial/Textured Colored" { Properties { _Color ("Main Color", Color) = (1,1,1,0.5) _MainTex ("Texture", 2D) = "white" { } } SubShader { Pass { CGPROGRAM //---- 略 ---- float4 _Color; // プロパティと同じ名前で定義する sampler2D _MainTex; // プロパティと同じ名前で定義する //---- 略 ---- half4 frag (v2f i) : COLOR { half4 texcol = tex2D (_MainTex, i.uv); // _MainTexを参照 return texcol * _Color; // _Colorを参照 } ENDCG } } Fallback "VertexLit" }
struct SurfaceOutput { half3 Albedo; half3 Normal; half3 Emission; half Specular; half Gloss; half Alpha; };※Albedo:入射した光をどれだけ反射するか、という値。Diffuseとほぼ同じっぽい。
#pragma surface surfaceFunction lightModel [optionalparams]
alpha | |
alphatest | |
vertex | 頂点シェーダーを指定 |
finalcolor | ピクセルシェーダーを指定 |
exclude_path | |
addshadow | |
dualforward | |
fullforwardshadows | |
decal | |
softvegetation | |
noambient | |
novertexlights | |
nolightmap | |
nodirlightmap | |
noforwardadd | |
approxview | |
halfsview |
float3 | viewDir | 視線方向 |
float4 | color(COLORセマンティックを付ける) | 頂点カラー |
float4 | screenPos | スクリーン上の座標 |
float3 | worldPos | ワールド座標 |
float3 | worldRefl | ワールドリフレクションベクトル |
float3 | worldNormal | ワールド法線ベクトル |
float3 | worldRefl | |
float3 | worldNormal |
(Unityインストールフォルダ)/Editor/Data/CGIncludes/UnityCG.cginc
struct appdata_base { float4 vertex : POSITION; float3 normal : NORMAL; float4 texcoord : TEXCOORD0; };
struct appdata_tan { float4 vertex : POSITION; float4 tangent : TANGENT; float3 normal : NORMAL; float4 texcoord : TEXCOORD0; };
struct appdata_full { float4 vertex : POSITION; float4 tangent : TANGENT; float3 normal : NORMAL; float4 texcoord : TEXCOORD0; float4 texcoord1 : TEXCOORD1; fixed4 color : COLOR; #if defined(SHADER_API_XBOX360) half4 texcoord2 : TEXCOORD2; half4 texcoord3 : TEXCOORD3; half4 texcoord4 : TEXCOORD4; half4 texcoord5 : TEXCOORD5; #endif };
float3 WorldSpaceViewDir (float4 v) | ワールド空間の,特定のオブジェクト空間の頂点位置からカメラに向かった,(正規化されていない)方向。 |
float3 ObjSpaceViewDir (float4 v) | オブジェクト空間の,特定のオブジェクト空間の頂点位置からカメラに向かった,(正規化されていない)方向。 |
float2 ParallaxOffset (half h, half height, half3 viewDir) | 視差法線マッピングのUVオフセットを計算。 |
fixed Luminance (fixed3 c) | 色を輝度(グレースケール)に変換します |
fixed3 DecodeLightmap (fixed4 color) | Unityライトマップの色をデコードします(プラットフォームにより,RGBMあるいはdLDR) |
float4 EncodeFloatRGBA (float v) | 精度の低いレンダリングターゲットを格納するため,0〜1の範囲の浮動小数点をRGBAcolorにエンコードします。 |
float DecodeFloatRGBA (float4 enc) | RGBA colorをfloatにデコードします。 |
float2 EncodeFloatRG (float v) | float4 EncodeFloatRGBA (float v) のカラーチャンネル2つ版 |
float DecodeFloatRG (float2 enc) | float DecodeFloatRGBA (float4 enc) のカラーチャンネル2つ版 |
float2 EncodeViewNormalStereo (float3 n) | ビュー空間法線を0〜1の範囲の二つの数字にエンコードします。 |
float3 DecodeViewNormalStereo (float4 enc4) | enc4.xyからビュー空間法線をデコードします。 |
ret floor(x) | 指定された値以下の最大の整数を返します。 |
ret saturate(x) | 指定された値を 0 〜 1 の範囲にクランプします。 |
ret distance(x, y) | 2 つのベクトルの間の距離スカラーを返します。 |
Queue種類 | 説明 | 優先度 |
---|---|---|
Background | 一番最初に描画されるキュー。スカイボックスなど。 | 1000 |
Geometry | デフォルト | 2000 |
AlphaTest | αテストを行うオブジェクト | 2450 |
Transparent | アルファブレンドを行うオブジェクト デプスバッファに書き込まないものはこれを推奨 | 3000 |
Overlay | ポストエフェクト、UIなど | 4000 |
Tags { "Queue" = "Geometry+1" }という使い方も可能。
フルパス | 効果 | Queue | RenderType |
---|---|---|---|
Diffuse | ライトあり(Lambert) カラーあり | Opaque | |
Diffuse Detail | Diffuseに加えて、 _MainTexと_Detail(2倍)の掛けあわせ | Opaque | |
Decal | Diffuseの上に_DecalTexを重ねる感じ | Opaque | |
Specular | _SpecColor, _Shininessで、光の強い反射を指定 | Opaque | |
VertexLit | Diffuse, Ambient,Shininess Specular, Emission (ライトモードによって計算が違う) | Opaque | |
Bumped Diffuse | Diffuse バンプマッピング | Opaque | |
Bumped Specular | Specular バンプマッピング | Opaque | |
Parallax Diffuse | Diffuse パララックスマッピング | Opaque | |
Parallax Specular | Specular パララックスマッピング | Opaque |
#pragma surface surf BlinnPhong
フルパス | 効果 | Queue | RenderType |
---|---|---|---|
Unlit/Texture | テクスチャカラーそのまま | Transparent | Opaque |
Unlit/Transparent | アルファブレンド ZWrite Off | Transparent | Transparent |
Unlit/Transparent Cutout | _Cutoffより小さいα値のピクセルを破棄する | AlphaTest | TransparentCutout |