SlideShare a Scribd company logo
1 of 29
Download to read offline
シェーダ勉強会
第2回
@nojima
今⽇やること
■ 前回の復習
■ Phong の反射モデル
■ テクスチャマッピング
前回の復習
オブジェクトを描画するプログラム
マテリアル
=描画に必要なパラメータ+
シェーダ
と呼ぶ
基本⾊、滑らかさ、⾦属っぽさ
など
頂点シェーダ
クリッピング
カリング
ラスタライズ
フラグメント
シェーダ
アルファテスト
深度テスト
ステンシルテスト
アルファブレンド
各頂点を画⾯のどこに
描画するかを計算する
画⾯外のポリゴンや
裏向きのポリゴンを破棄
ポリゴンをピクセルに
分割する
各ピクセルの⾊を計算する
ピクセルシェーダともいう
ピクセルを出⼒するか決める
アルファブレンドを⾏う
⼊⼒
出⼒(画像)
レンダリングパイプライン
Shader "Unlit/FirstShader" {
SubShader {
Tags { "RenderType"="Opaque" }
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float4 vert(float4 pos : POSITION) : SV_POSITION {
return UnityObjectToClipPos(pos); // 座標変換
}
float4 frag() : SV_TARGET {
return float4(0.22, 0.71, 0.55, 1.0); // 翡翠色
}
ENDCG
}
}
}
最⼩限のシェーダ
#pragma vertex vert
#pragma fragment frag
float4 vert(float4 pos : POSITION) : SV_POSITION {
return UnityObjectToClipPos(pos); // 座標変換
}
float4 frag() : SV_TARGET {
return float4(0.22, 0.71, 0.55, 1.0); // 翡翠色
}
頂点
シェーダ
フラグメント
シェーダ
float4 vert(float4 pos : POSITION) : SV_POSITION {
pos = mul(UNITY_MATRIX_M, pos); // ワールド変換
pos = mul(UNITY_MATRIX_V, pos); // ビュー変換
pos = mul(UNITY_MATRIX_P, pos); // パースペクティブ変換
return pos;
}
オブジェクト座標系
(モデル座標系、ローカル座標系ともいう) ワールド座標系
float4 vert(float4 pos : POSITION) : SV_POSITION {
pos = mul(UNITY_MATRIX_M, pos); // ワールド変換
pos = mul(UNITY_MATRIX_V, pos); // ビュー変換
pos = mul(UNITY_MATRIX_P, pos); // パースペクティブ変換
return pos;
}
カメラから⾒た座標系に変換
(カメラを原点、カメラが向いている⽅向がz軸になるように変換)
float4 vert(float4 pos : POSITION) : SV_POSITION {
pos = mul(UNITY_MATRIX_M, pos); // ワールド変換
pos = mul(UNITY_MATRIX_V, pos); // ビュー変換
pos = mul(UNITY_MATRIX_P, pos); // パースペクティブ変換
return pos;
}
far plane
near plane
far plane
near plane
camera
ビュー座標系 正規化デバイス座標系(NDC)
※ 実際には、⼀次変換ではNDCに変換できないため、頂点シェーダはクリップ座標系に変換する。
あとでGPUがクリップ座標系からNDCに変換する。
Lambert 反射モデル
物体の表⾯に⼊射した光が
あらゆる⽅向に同じ強さで反射するとしたモデル
𝑰" = 𝝆 max(𝑳 * 𝑵, 0) 𝑰/
⼊射光の強度物体の⾊反射光の強度
⼊射光の⽅向 表⾯の法線
float4 frag(VertexOutput i) : SV_TARGET {
float3 lightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
float3 normal = normalize(i.normal);
float LN = max(dot(lightDir, normal), 0.0);
float3 color = _BaseColor * LN;
return float4(color, 1.0);
}
𝑰" = 𝝆 max(𝑳 * 𝑵, 0) 𝑰/
⼊射光の強度物体の⾊反射光の強度
⼊射光の⽅向 表⾯の法線
↑ 以下の式にしたがって反射光を計算している(コードの簡略化のために 𝑰/ = 𝟏 とした)
復習ここまで
Phong 反射モデル
Diffuse
拡散光
Specular
鏡⾯反射光
Ambient
環境光
■ 物質の表⾯の反射光を
拡散光、鏡⾯反射光、環境光の和
として表現するモデル
■ 物理ベースモデルではなく、経験ベースの
モデル
■ 昔のゲームなどでよく使われている
実⽤性の⾼いモデル
𝑳𝑵
𝑹
𝑽
𝑳𝑵
Diffuse (拡散光)
■ 拡散光には Lambert モデルと同じ式を
使う
■ つまり、物質の表⾯に⼊射した光が
すべての⽅向に同じ強さで反射する
𝑳
𝑰" = 𝒌 𝑫 max(𝑳 * 𝑵, 0) 𝑰 𝑫
⼊射光の強度物体の⾊反射光の強度
⼊射光の⽅向 表⾯の法線
𝑵
Specular (鏡⾯反射光)
■ ハイライトの部分
■ ⼊射⾓ = 反射⾓ となる⽅向に強く光が
反射する
■ shiness というパラメータを持つ
– shiness を⼤きくするほど
ハイライトが鋭くなる
■ ⾮⾦属の場合、物質の⾊とは関係なく
鏡⾯反射の⾊は⽩になる
■ ⾦属の場合、鏡⾯反射の⾊が⽩でない
場合がある
𝑹
𝑽
𝑳
𝑰 𝑺 = 𝒌 𝑺 max 𝑹 * 𝑽, 0 6
𝑰 𝑺
⼊射光の強度鏡⾯反射光の⾊反射光の強度
反射光の⽅向
カメラの⽅向
shiness
𝑹 = 𝟐 𝑵 * 𝑳 𝑵 − 𝑳
𝑵
Ambient (環境光)
■ 拡散光と鏡⾯反射光だけだと、物体の陰
の部分は真っ暗になる
– 宇宙空間っぽくなる
■ ⽇常的な状況の場合、陰が真っ暗になる
ことはあまりない
– 光がいろんな場所で反射して多少は
明るくなる
■ 環境光はこのような雑多な反射を定数で
近似したもの
⼊射光の強度環境光の⾊反射光の強度
𝑰 𝑨 = 𝒌 𝑨 𝑰:
Phong 反射モデル
𝑰 𝑹 = 𝐈 𝐃 + 𝐈 𝐒 + 𝐈 𝐀
■ 最終的な⾊は単純に3つの反射光を
⾜したもの
Phong Shader のサンプルコード
■ https://gist.github.com/nojima/f75674c8de7a2c3e1275e36bb93d3747
テクスチャマッピング
■ オブジェクトの表⾯にテクスチャを貼りたい
■ やりかた
– 各頂点がテクスチャ上のどこに位置するかを
あらかじめ割り当てておく
– フラグメントシェーダで、その位置のテクス
チャの⾊をサンプリングする
– サンプリングした⾊を Phong Shader の
BaseColor として扱ってレンダリングする
𝒖
𝒗
(𝟎, 𝟎) (𝟏, 𝟎)
(𝟎, 𝟏)
UV座標系
それぞれの頂点に、その頂点がテクスチャ上のどこに位置すのかという
情報を持たせておく(予めモデルに埋め込んでおく)
テクスチャのサンプリング
■ tex2D(sampler, uv) という関数を使うと、位置 uv の⾊を sampler を使って
サンプリングできる
■ sampler はテクスチャからサンプリングするための情報をまとめたもの
– どのテクスチャを使うか
– 補間のアルゴリズム
– クリッピングのアルゴリズム
■ tex2D() は単にテクスチャのピクセルの⾊を返すのではなく、適切に補間した
りクリッピングしてくれる
テクスチャマッピングの
サンプルコード
■ https://gist.github.com/nojima/2c0410773f18e444a4311c8188310496
Properties {
_Texture("Texture", 2D) = "white" {}
_AmbientReflectance("Ambient Reflection Constant", Range(0, 1)) = 0.1
_DiffuseReflectance("Diffuse Reflection Constant", Range(0, 1)) = 0.7
_SpecularReflectance("Specular Reflection Constant", Range(0, 1)) = 0.2
_Shininess("Shininess", Float) = 20.0
}
#include "UnityCG.cginc"
#include "UnityLightingCommon.cginc" // _LightColor0 のため
sampler2D _Texture;
float4 _Texture_ST; // *_ST にはタイリングとオフセットの値が⼊る
float _AmbientReflectance;
float _DiffuseReflectance;
float _SpecularReflectance;
float _Shininess;
struct VertexInput {
float4 objectPos : POSITION;
float3 objectNormal : NORMAL;
float2 uv : TEXCOORD0; // 元々の頂点のUV
};
struct VertexOutput {
float4 clipPos : SV_POSITION;
float3 worldPos : TEXCOORD0;
float3 worldNormal : TEXCOORD1;
float2 uv : TEXCOORD2; // タイリングとオフセットを考慮した頂点のUV
};
VertexOutput vert(VertexInput v) {
VertexOutput o;
o.clipPos = UnityObjectToClipPos(v.objectPos);
o.worldPos = mul(unity_ObjectToWorld, v.objectPos);
o.worldNormal = UnityObjectToWorldNormal(v.objectNormal);
o.uv = TRANSFORM_TEX(v.uv, _Texture); // タイリングとオフセットを適⽤
return o;
}
// Transforms 2D UV by scale/bias property
#define TRANSFORM_TEX(tex,name) (tex.xy * name##_ST.xy + name##_ST.zw)
ちなみに TRANSFORM_TEX の定義はこれ︓
float4 frag(VertexOutput i) : SV_TARGET {
float3 baseColor = tex2D(_Texture, i.uv).rgb;
float3 color = Phong(i, baseColor);
return float4(color, 1.0);
}

More Related Content

Recently uploaded

Recently uploaded (12)

知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx
知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx
知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx
 
新人研修 後半 2024/04/26の勉強会で発表されたものです。
新人研修 後半        2024/04/26の勉強会で発表されたものです。新人研修 後半        2024/04/26の勉強会で発表されたものです。
新人研修 後半 2024/04/26の勉強会で発表されたものです。
 
LoRaWANスマート距離検出センサー DS20L カタログ LiDARデバイス
LoRaWANスマート距離検出センサー  DS20L  カタログ  LiDARデバイスLoRaWANスマート距離検出センサー  DS20L  カタログ  LiDARデバイス
LoRaWANスマート距離検出センサー DS20L カタログ LiDARデバイス
 
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
 
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
 
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
 
Utilizing Ballerina for Cloud Native Integrations
Utilizing Ballerina for Cloud Native IntegrationsUtilizing Ballerina for Cloud Native Integrations
Utilizing Ballerina for Cloud Native Integrations
 
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
 
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアルLoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
 
Observabilityは従来型の監視と何が違うのか(キンドリルジャパン社内勉強会:2022年10月27日発表)
Observabilityは従来型の監視と何が違うのか(キンドリルジャパン社内勉強会:2022年10月27日発表)Observabilityは従来型の監視と何が違うのか(キンドリルジャパン社内勉強会:2022年10月27日発表)
Observabilityは従来型の監視と何が違うのか(キンドリルジャパン社内勉強会:2022年10月27日発表)
 
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
 
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
 

Featured

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 

シェーダ勉強会 第2回 Phong モデルとテクスチャマッピング