Created
September 26, 2018 14:16
-
-
Save nabesi777/f16185e7b58e8d7be9042b65b211697b to your computer and use it in GitHub Desktop.
シェーダースクリプトの構造
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//シェーダー宣言、ファイルで一つだけ必要 /で階層化 以下ShaderLab言語 | |
Shader "Unlit/NewUnlitShader" | |
{ | |
//プロパティ宣言(インスペクター上で表示する項目を記述) | |
//テクスチャだけではなく数値やカラーも設定できる | |
Properties | |
{//左から、シェーダー内で使う変数(インスペクター上で表示する名称、受取リソースの種類) | |
//=初期値(テクスチャが指定されない時は白表示) {}は互換性の関係で記述 | |
_MainTex ("Texture", 2D) = "white" {} | |
} | |
//subshader、シェーダーの実行単位、Shader宣言内に複数記述できる | |
SubShader | |
{//Tags宣言、キー値=値 | |
//この設定はReplacementという機能を使用するために必要 | |
Tags { "RenderType"="Opaque" } | |
//LOD宣言、SubShaderを実行する条件設定として機能 | |
//この数値以下で最も大きな値が設定されたShaderが実行される | |
LOD 100 | |
//パス宣言、実行されるシェーダープログラムを記述。複数の場合はすべてを1回実行 | |
//*複数回実行される設定もある | |
Pass | |
{//CGPROGRAMからENDCGの範囲の文字列はHLSL言語 | |
//HLSL言語はC言語に似た仕様 | |
//各Pass内での宣言はプロパティで宣言された同名の値を参照できるようになる | |
CGPROGRAM | |
//頂点シェーダーメソッドの名前をvertに | |
#pragma vertex vert | |
//フラグメントシェーダーメソッドの名前をfragに | |
#pragma fragment frag | |
// make fog work | |
#pragma multi_compile_fog | |
#include "UnityCG.cginc" | |
//この中からシェーダープログラミングの中心 | |
struct appdata | |
{ //各変数には(:)の後に大文字のキーワード「セマンティクス名」が記載される | |
//これはセマンティクスというHLSL特有の概念 | |
float4 vertex : POSITION; | |
float2 uv : TEXCOORD0; | |
}; | |
struct v2f | |
{ | |
float2 uv : TEXCOORD0; | |
UNITY_FOG_COORDS(1) | |
float4 vertex : SV_POSITION; | |
}; | |
sampler2D _MainTex; | |
float4 _MainTex_ST; | |
//appdata構造体を受け取り、v2f構造体を返す | |
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 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment