4
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Unityシェーダーの覚書1

Last updated at Posted at 2018-04-24

最近シェーダーの勉強を始めたので学んだことの自分用メモ。
unityでのシェーダーについて自分なりにまとめていくつもりである。
考え方についてはUE4にも応用できたらいいなと。

Unityではマテリアルとシェーダーが1対1で結びついています。

・処理の流れ
図にすると下記のような感じです。
資料.png

Unityのサーフェイスシェーダーには3つの工程があります。
・Vertex関数で頂点情報の処理
・Surf関数でオブジェクトの表面の色のを定義
・Lightingでオブジェクトの陰影を決定

・シェーダーの中身
unityでシェーダーファイルを作成した初期状態のものです。

sample.shader
Shader "Custom/sample" {

//Parametersパート
	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
	}

//Shader Settingsパート
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		CGPROGRAM
		// Physically based Standard lighting model, and enable shadows on all light types
		#pragma surface surf Standard fullforwardshadows

//Surface Shaderパート
		// 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 = fixed4(0.1f, 0.1f, 0.1f, 1);
			// Metallic and smoothness come from slider variables
			o.Metallic = _Metallic;
			o.Smoothness = _Glossiness;
			o.Alpha = c.a;
		}
		ENDCG
	}
	FallBack "Diffuse"
}

シェーダーは大きく分けて3つのパートに分かれています。
・Parameters
・Shader Settings
・Surface Shader

では上記の3つのパートでは何をしているのでしょうか?
Parametersのパートではインスペクタに公開する変数を記述します。プログラムでいう変数の定義のようなものです。
Shader Settingsのパートではライティングや透明度などのシェーダーの設定項目を記述します。
Surface Shaderのパートにはシェーダ本体のプログラムを記述します。この部分を修正して目的のシェーダを作ります。

試しにo.Albedoのパラメータの数値を変えてみてください。するとモデルの表面の色が変わると思います。
のっぺりした色でなく立体的な色になっているのはSurfで色を決定した後にLightingの工程で光を当てることにより陰影がつくからです。

下記のサイトにて勉強させていただきました。
こちらにより詳しく記載されています。
Unityシェーダー入門

4
8
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?