Soumettre la recherche
Mettre en ligne
シェーダ勉強会 第2回 Phong モデルとテクスチャマッピング
•
0 j'aime
•
113 vues
Y
Yusuke Nojima
Suivre
社内で開催したシェーダ勉強会の第2回目です。Unity で Phong を使ったマテリアルを作ったりテクスチャを貼ったりする方法を説明しました。
Lire moins
Lire la suite
Technologie
Signaler
Partager
Signaler
Partager
1 sur 29
Télécharger maintenant
Télécharger pour lire hors ligne
Recommandé
[DL輪読会]Unsupervised Learning of Probably Symmetric Deformable 3D Objects from...
[DL輪読会]Unsupervised Learning of Probably Symmetric Deformable 3D Objects from...
Deep Learning JP
シェーダ勉強会(第1回) Unityでシェーダを書いてみる回
シェーダ勉強会(第1回) Unityでシェーダを書いてみる回
Yusuke Nojima
Kubernetes で実現するインフラ自動構築パイプライン
Kubernetes で実現するインフラ自動構築パイプライン
Yusuke Nojima
kintone on EKS ― EKS で実現するインフラ自動構築パイプライン
kintone on EKS ― EKS で実現するインフラ自動構築パイプライン
Yusuke Nojima
2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
Recommandé
[DL輪読会]Unsupervised Learning of Probably Symmetric Deformable 3D Objects from...
[DL輪読会]Unsupervised Learning of Probably Symmetric Deformable 3D Objects from...
Deep Learning JP
シェーダ勉強会(第1回) Unityでシェーダを書いてみる回
シェーダ勉強会(第1回) Unityでシェーダを書いてみる回
Yusuke Nojima
Kubernetes で実現するインフラ自動構築パイプライン
Kubernetes で実現するインフラ自動構築パイプライン
Yusuke Nojima
kintone on EKS ― EKS で実現するインフラ自動構築パイプライン
kintone on EKS ― EKS で実現するインフラ自動構築パイプライン
Yusuke Nojima
2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
博三 太田
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
Yuki Kikuchi
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
Hiroshi Tomioka
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
akihisamiyanaga1
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
UEHARA, Tetsutaro
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
FumieNakayama
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
FumieNakayama
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
NTT DATA Technology & Innovation
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
Skeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
Introduction to Data Science
Introduction to Data Science
Christy Abraham Joy
Time Management & Productivity - Best Practices
Time Management & Productivity - Best Practices
Vit Horky
Contenu connexe
Dernier
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
博三 太田
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
Yuki Kikuchi
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
Hiroshi Tomioka
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
akihisamiyanaga1
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
UEHARA, Tetsutaro
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
FumieNakayama
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
FumieNakayama
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
NTT DATA Technology & Innovation
Dernier
(8)
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
En vedette
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
Skeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
Introduction to Data Science
Introduction to Data Science
Christy Abraham Joy
Time Management & Productivity - Best Practices
Time Management & Productivity - Best Practices
Vit Horky
The six step guide to practical project management
The six step guide to practical project management
MindGenius
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
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...
Applitools
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
GetSmarter
ChatGPT webinar slides
ChatGPT webinar slides
Alireza Esmikhani
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 Routes
Project for Public Spaces & National Center for Biking and Walking
En vedette
(20)
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
Skeleton Culture Code
Skeleton Culture Code
PEPSICO 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)
How 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 Insights
Trends 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 summary
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
Google'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
Introduction to Data Science
Introduction to Data Science
Time Management & Productivity - Best Practices
Time Management & Productivity - Best Practices
The 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...
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 Work
ChatGPT 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 Routes
シェーダ勉強会 第2回 Phong モデルとテクスチャマッピング
1.
シェーダ勉強会 第2回 @nojima
2.
今⽇やること ■ 前回の復習 ■ Phong
の反射モデル ■ テクスチャマッピング
3.
前回の復習
4.
オブジェクトを描画するプログラム マテリアル =描画に必要なパラメータ+ シェーダ と呼ぶ 基本⾊、滑らかさ、⾦属っぽさ など
5.
頂点シェーダ クリッピング カリング ラスタライズ フラグメント シェーダ アルファテスト 深度テスト ステンシルテスト アルファブレンド 各頂点を画⾯のどこに 描画するかを計算する 画⾯外のポリゴンや 裏向きのポリゴンを破棄 ポリゴンをピクセルに 分割する 各ピクセルの⾊を計算する ピクセルシェーダともいう ピクセルを出⼒するか決める アルファブレンドを⾏う ⼊⼒ 出⼒(画像) レンダリングパイプライン
6.
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 } } } 最⼩限のシェーダ
7.
#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); // 翡翠色 } 頂点 シェーダ フラグメント シェーダ
8.
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; } オブジェクト座標系 (モデル座標系、ローカル座標系ともいう) ワールド座標系
9.
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軸になるように変換)
10.
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に変換する。
11.
Lambert 反射モデル 物体の表⾯に⼊射した光が あらゆる⽅向に同じ強さで反射するとしたモデル 𝑰" =
𝝆 max(𝑳 * 𝑵, 0) 𝑰/ ⼊射光の強度物体の⾊反射光の強度 ⼊射光の⽅向 表⾯の法線
12.
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) 𝑰/ ⼊射光の強度物体の⾊反射光の強度 ⼊射光の⽅向 表⾯の法線 ↑ 以下の式にしたがって反射光を計算している(コードの簡略化のために 𝑰/ = 𝟏 とした)
13.
復習ここまで
14.
Phong 反射モデル Diffuse 拡散光 Specular 鏡⾯反射光 Ambient 環境光 ■ 物質の表⾯の反射光を 拡散光、鏡⾯反射光、環境光の和 として表現するモデル ■
物理ベースモデルではなく、経験ベースの モデル ■ 昔のゲームなどでよく使われている 実⽤性の⾼いモデル 𝑳𝑵 𝑹 𝑽 𝑳𝑵
15.
Diffuse (拡散光) ■ 拡散光には
Lambert モデルと同じ式を 使う ■ つまり、物質の表⾯に⼊射した光が すべての⽅向に同じ強さで反射する 𝑳 𝑰" = 𝒌 𝑫 max(𝑳 * 𝑵, 0) 𝑰 𝑫 ⼊射光の強度物体の⾊反射光の強度 ⼊射光の⽅向 表⾯の法線 𝑵
16.
Specular (鏡⾯反射光) ■ ハイライトの部分 ■
⼊射⾓ = 反射⾓ となる⽅向に強く光が 反射する ■ shiness というパラメータを持つ – shiness を⼤きくするほど ハイライトが鋭くなる ■ ⾮⾦属の場合、物質の⾊とは関係なく 鏡⾯反射の⾊は⽩になる ■ ⾦属の場合、鏡⾯反射の⾊が⽩でない 場合がある 𝑹 𝑽 𝑳 𝑰 𝑺 = 𝒌 𝑺 max 𝑹 * 𝑽, 0 6 𝑰 𝑺 ⼊射光の強度鏡⾯反射光の⾊反射光の強度 反射光の⽅向 カメラの⽅向 shiness 𝑹 = 𝟐 𝑵 * 𝑳 𝑵 − 𝑳 𝑵
17.
Ambient (環境光) ■ 拡散光と鏡⾯反射光だけだと、物体の陰 の部分は真っ暗になる –
宇宙空間っぽくなる ■ ⽇常的な状況の場合、陰が真っ暗になる ことはあまりない – 光がいろんな場所で反射して多少は 明るくなる ■ 環境光はこのような雑多な反射を定数で 近似したもの ⼊射光の強度環境光の⾊反射光の強度 𝑰 𝑨 = 𝒌 𝑨 𝑰:
18.
Phong 反射モデル 𝑰 𝑹
= 𝐈 𝐃 + 𝐈 𝐒 + 𝐈 𝐀 ■ 最終的な⾊は単純に3つの反射光を ⾜したもの
19.
Phong Shader のサンプルコード ■
https://gist.github.com/nojima/f75674c8de7a2c3e1275e36bb93d3747
20.
テクスチャマッピング ■ オブジェクトの表⾯にテクスチャを貼りたい ■ やりかた –
各頂点がテクスチャ上のどこに位置するかを あらかじめ割り当てておく – フラグメントシェーダで、その位置のテクス チャの⾊をサンプリングする – サンプリングした⾊を Phong Shader の BaseColor として扱ってレンダリングする
21.
𝒖 𝒗 (𝟎, 𝟎) (𝟏,
𝟎) (𝟎, 𝟏) UV座標系 それぞれの頂点に、その頂点がテクスチャ上のどこに位置すのかという 情報を持たせておく(予めモデルに埋め込んでおく)
22.
テクスチャのサンプリング ■ tex2D(sampler, uv)
という関数を使うと、位置 uv の⾊を sampler を使って サンプリングできる ■ sampler はテクスチャからサンプリングするための情報をまとめたもの – どのテクスチャを使うか – 補間のアルゴリズム – クリッピングのアルゴリズム ■ tex2D() は単にテクスチャのピクセルの⾊を返すのではなく、適切に補間した りクリッピングしてくれる
23.
テクスチャマッピングの サンプルコード ■ https://gist.github.com/nojima/2c0410773f18e444a4311c8188310496
24.
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 }
25.
#include "UnityCG.cginc" #include "UnityLightingCommon.cginc"
// _LightColor0 のため sampler2D _Texture; float4 _Texture_ST; // *_ST にはタイリングとオフセットの値が⼊る float _AmbientReflectance; float _DiffuseReflectance; float _SpecularReflectance; float _Shininess;
26.
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 };
27.
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; }
28.
// Transforms 2D
UV by scale/bias property #define TRANSFORM_TEX(tex,name) (tex.xy * name##_ST.xy + name##_ST.zw) ちなみに TRANSFORM_TEX の定義はこれ︓
29.
float4 frag(VertexOutput i)
: SV_TARGET { float3 baseColor = tex2D(_Texture, i.uv).rgb; float3 color = Phong(i, baseColor); return float4(color, 1.0); }
Télécharger maintenant