SlideShare une entreprise Scribd logo
1  sur  16
ShaderX5 2.6
Normal Mapping without
Precomputed Tangents
Christian Schuler, Phenomic
ShaderStudy 2013.03.18 황규석
http://cafe.naver.com/shader
Introduction
 TBN Matrix(Tangent Frames)를 Pixel Shader에서 실시간으로
계산하는 방법
 장점
- Vertex attributes에 Tangent Frames를 넣어 줄 필요가 없다
• Band width, Memory, Complexity 감소
• 그래픽 디자이너가 툴에서 이것을 신경 쓸 필요가 없다
 단점
- Pixel Shader에서의 부하, Optimization 필수
 Cotangent Frames
- More optimization
TBN matrix
Normal Texture Mapping
©http://marupeke296.com
Normal 벡터를 Texture 좌표계(공간)에서 Local 좌표계(공간)로 변환
new = [dot(old, X), dot(old, Y), dot(old, Z)]
(X, Y, Z는 new 좌표계에서 표현되는 old 좌표계 basis vectors, e.g. T, B, N)
Tangents to a surface
Position 벡터(p1, p2)와 Texture 좌표 벡터(u1, u2, v1, v2)를 이용해 Tangent
벡터(T, B) 계산
// A code snippet to perform normal perturbation using a tangent-space normal map
float3 perturb_normal(float3 T, float3 B, float3 N, float2 texcoord)
{
// Build a tangent frame as float3x3 for convenience
float3x3 tangent_frame = float3x3(T, B, N);
// Read the perturbed normal from the normal map
float3 perturbed_normal = tex2D(normalmap, texcoord);
// Sign-expand (for a normal map in unsigned RGB format)
perturbed_normal = 2 * perturbed_normal – 1;
// And transform the perturbed normal out of tangent space
// (into whatever space T, B and N were originally expressed in, usually world).
return normalize( mul( perturbed_normal, tangent_frame));
}
Tangent space Normal mapping
// A function that takes an interpolated surface normal N, a position vector p,
// and texture coordinates u and v and returns a complete tangent frame that is
// ready to be used in lighting calculations (46 pixel shader instructions)
float3x3 compute_tangent_frame(float3 N, float3 p, float2 uv)
{
// Get edge vectors of the pixel triangle
float3 dp1 = ddx(p); // 가로 방향의 옆 픽셀과의 Position 변화량
float3 dp2 = ddy(p); // 세로 방향의 옆 픽셀과의 Position 변화량
float2 duv1 = ddx(uv); // 가로 방향의 옆 픽셀과의 UV값 변화량
float2 duv2 = ddy(uv); // 세로 방향의 옆 픽셀과의 UV값 변화량
// Solve the linear system
float3x3 M = float3x3(dp1, dp2, cross(dp1, dp2));
float3x3 inverseM = invert_3x3(M);
float3 T = mul(inverseM, float3(duv1.x, duv2.x, 0));
float3 B = mul(inverseM, float3(duv1.y, duv2.y, 0));
// Construct tangent frame
// (* see discussion regarding the square patch assumption)
float maxLength = max(length(T), length(B));
return float3x3 (T / maxLength, B / maxLength, N);
}
float3x3 invert_3x3(float3x3 M)
{
float det = dot( cross(M[0], M[1]), M[2] );
float3x3 T = transpose(M);
return float3x3( cross(T[1], T[2]), cross(T[2], T[0]), cross(T[0], T[1]) ) / det;
}
Moving to the Pixel Sahder
// Optimization 1:
// normalize T and B and spare the determinant.
// (31 pixel shader instructions)
float3x3 compute_tangent_frame_01(float3 N, float3 p, float2 uv)
{
// Get edge vectors of the pixel triangle
float3 dp1 = ddx(p);
float3 dp2 = ddy(p);
float2 duv1 = ddx(uv);
float2 duv2 = ddy(uv);
// Solve the linear system
float3x3 M = float3x3(dp1, dp2, cross(dp1, dp2));
float3x3 inverseM = invert_3x3_nodet(M);
float3 T = mul(inverseM, float3(duv1.x, duv2.x, 0));
float3 B = mul(inverseM, float3(duv1.y, duv2.y, 0));
// Construct tangent frame
return float3x3(normalize(T), normalize(B), N);
}
float3x3 invert_3x3_nodet(float3x3 M)
{
float3x3 T = transpose(M);
return float3x3( cross(T[1], T[2]), cross(T[2], T[0]), cross(T[0], T[1]) );
}
Optimization 1
// Optimization 2:
// exploits the zero components found in the solution vectors and collapses
// a hidden double transpose
// (17 pixel shader instructions)
float3x3 compute_tangent_frame_02(float3 N, float3 p, float2 uv)
{
// Get edge vectors of the pixel triangle
float3 dp1 = ddx(p);
float3 dp2 = ddy(p);
float2 duv1 = ddx(uv);
float2 duv2 = ddy(uv);
// Solve the linear system
float3x3 M = float3x3(dp1, dp2, cross(dp1, dp2));
float3x3 inversetransposeM = float2x3( cross(M[1], M[2]), cross(M[2], M[0]) ); //inversetranspose = original
float3 T = mul(float2(duv1.x, duv2.x), inversetransposeM);
float3 B = mul(float2(duv1.y, duv2.y), inversetransposeM);
// Construct tangent frame
return float3x3(normalize(T), normalize(B), N);
}
Optimization 2
// Optimization 3:
// assume M is orthogonal and exploits the fact that we have made an inverse transpose explicit.
// (14 pixel shader instructions)
float3x3 compute_tangent_frame_03(float3 N, float3 p, float2 uv)
{
// Get edge vectors of the pixel triangle
float3 dp1 = ddx(p);
float3 dp2 = ddy(p);
float2 duv1 = ddx(uv);
float2 duv2 = ddy(uv);
// Solve the linear system
// (not much solving is left going here)
float2x3 M = float2x3(dp1, dp2);
float3 T = mul(float2(duv1.x, duv2.x), M);
float3 B = mul(float2(duv1.y, duv2.y), M);
// Construct tangent frame
return float3x3(normalize(T), normalize(B), N);
}
Optimization 3
Conclusion
 TBN matrix 계산을 Pixel shader에서 낮은 비용으로 하는 방법 제
공함
 Vertex 속성에서 Tangent frames를 제거할 수 있지만 픽셀셰이더
에서 부하가 증가
 텍스처 좌표 공간에서의 procedural 처리도 가능한 방법
 추가적으로 Cotangent frames를 이용한 방법을 블로그에 소개하
고 있음 
A vector v (red) represented by tangent basis vectors (yellow, left: e1, e2, e3) to the coordinate
curves (black), · dual basis, covector basis, or cobasis (blue, right: e1, e2, e3 ), normal vectors to
coordinate surfaces (grey), in 3D general curvilinear coordinates (q1, q2, q3), a tuple of numbers
to define point in a position space.
Note: the basis and cobasis do not coincide unless the basis is orthogonal.
Tangent and Contangent basis vectors
©wikipedia
// compute contangent frames
mat3 contangent_frame(vec3 N, vec3 p, vec2 uv)
{
// Get edge vectors of the pixel triangle
vec3 dp1 = dFdx(p);
vec3 dp2 = dFdy(p);
vec2 duv1 = dFdx(uv);
vec2 duv2 = dFdy(uv);
// Solve the linear system
vec3 dp2perp = cross(dp2, N);
vec3 dp1perp = cross(N, dp1);
vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;
vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;
// Construct a scale-invariant frame
float invmax = inversesqrt(max(dot(T,T), dot(B,B));
return mat3(T * invmax, B * invmax, N);
}
vec3 pertub_normal(vec3 N, vec3 V, vec2 texcoord)
{
// assume N, the interpolated vertex normal and
// V, the view vector(vertex to eye)
vec3 map = texture2D(mapBump, texcoord).xyz;
#ifdef WITH_NORMALMAP_UNSIGNED map = map * 255./127. – 128./127.; #endif
#ifdef WITH_NORMALMAP_2CHANNEL map.z = sqrt(1. – dot(map.xy, map.xy)); #endif
#ifdef WITH_NORMALMAP_GREEN_UP map.y = -map.y; #endif
mat3 TBN = cotangent_frame(N, -V, texcoord);
return normalize(TBN * map);
}
Contangent Frames
varying vec3 g_vertexnormal;
varying vec3 g_viewvector; // camera pos – vertex pos
varying vec2 g_texcoord;
void main()
{
vec3 N = normalize(g_vertexnormal);
vec3 V = normalize(g_viewvector);
#ifdef WITH_NORMALMAP
N = perturb_normal(N, V, g_texcoord);
#endif
// ...
}
Contangent Frames (cont.)
Reference
(Followup) Normal mapping without precomputed tangents
http://www.thetenthplanet.de/archives/1180
Covariance and contravariance of vectors
http://en.wikipedia.org/wiki/Covariance_and_contravariance_of_vectors
Fast inverse square root
http://en.wikipedia.org/wiki/Fast_inverse_square_root

Contenu connexe

Tendances

統計的学習手法による物体検出の高精度化と効率化 -人検出の実用化に向けて-
統計的学習手法による物体検出の高精度化と効率化 -人検出の実用化に向けて-統計的学習手法による物体検出の高精度化と効率化 -人検出の実用化に向けて-
統計的学習手法による物体検出の高精度化と効率化 -人検出の実用化に向けて-Hironobu Fujiyoshi
 
新數學UPUP學測講義
新數學UPUP學測講義新數學UPUP學測講義
新數學UPUP學測講義lungtengtech
 
自作LSIコミュニティの可能性
自作LSIコミュニティの可能性自作LSIコミュニティの可能性
自作LSIコミュニティの可能性Junichi Akita
 
えっ今日はハッキングしてもいいのか?(CTF Web入門)
えっ今日はハッキングしてもいいのか?(CTF Web入門)えっ今日はハッキングしてもいいのか?(CTF Web入門)
えっ今日はハッキングしてもいいのか?(CTF Web入門)otya mura
 
多チャンネルバイラテラルフィルタの高速化
多チャンネルバイラテラルフィルタの高速化多チャンネルバイラテラルフィルタの高速化
多チャンネルバイラテラルフィルタの高速化Norishige Fukushima
 
【展開用】日曜数学会 Sinc関数の積分について
【展開用】日曜数学会 Sinc関数の積分について【展開用】日曜数学会 Sinc関数の積分について
【展開用】日曜数学会 Sinc関数の積分について和人 桐ケ谷
 
Suite exercice
Suite exerciceSuite exercice
Suite exercicehassan1488
 
Best Practices for Shader Graph
Best Practices for Shader GraphBest Practices for Shader Graph
Best Practices for Shader GraphUnity Technologies
 
キムワイプ卓球における得点の複素数への拡張
キムワイプ卓球における得点の複素数への拡張キムワイプ卓球における得点の複素数への拡張
キムワイプ卓球における得点の複素数への拡張Kento Maeda
 
VR for Cinema 4D 세미나 (2016) - C4D를 활용한 Full-CG VR 영상 콘텐츠 제작 워크플로우
VR for Cinema 4D 세미나 (2016)  - C4D를 활용한 Full-CG VR 영상 콘텐츠 제작 워크플로우VR for Cinema 4D 세미나 (2016)  - C4D를 활용한 Full-CG VR 영상 콘텐츠 제작 워크플로우
VR for Cinema 4D 세미나 (2016) - C4D를 활용한 Full-CG VR 영상 콘텐츠 제작 워크플로우HYEONSU BAE
 
ニューラルネットワークの理論
ニューラルネットワークの理論ニューラルネットワークの理論
ニューラルネットワークの理論Kazuma Komiya
 
Shadow mapping 정리
Shadow mapping 정리Shadow mapping 정리
Shadow mapping 정리changehee lee
 
クリーンランゲージについて
クリーンランゲージについてクリーンランゲージについて
クリーンランゲージについてnishio
 
FPGAスタートアップ資料
FPGAスタートアップ資料FPGAスタートアップ資料
FPGAスタートアップ資料marsee101
 
2章グラフ理論スピード入門
2章グラフ理論スピード入門2章グラフ理論スピード入門
2章グラフ理論スピード入門Teruo Kawasaki
 
Datamining 8th Hclustering
Datamining 8th HclusteringDatamining 8th Hclustering
Datamining 8th Hclusteringsesejun
 

Tendances (20)

統計的学習手法による物体検出の高精度化と効率化 -人検出の実用化に向けて-
統計的学習手法による物体検出の高精度化と効率化 -人検出の実用化に向けて-統計的学習手法による物体検出の高精度化と効率化 -人検出の実用化に向けて-
統計的学習手法による物体検出の高精度化と効率化 -人検出の実用化に向けて-
 
Chapter2.3.6
Chapter2.3.6Chapter2.3.6
Chapter2.3.6
 
新數學UPUP學測講義
新數學UPUP學測講義新數學UPUP學測講義
新數學UPUP學測講義
 
CUDAメモ
CUDAメモCUDAメモ
CUDAメモ
 
自作LSIコミュニティの可能性
自作LSIコミュニティの可能性自作LSIコミュニティの可能性
自作LSIコミュニティの可能性
 
えっ今日はハッキングしてもいいのか?(CTF Web入門)
えっ今日はハッキングしてもいいのか?(CTF Web入門)えっ今日はハッキングしてもいいのか?(CTF Web入門)
えっ今日はハッキングしてもいいのか?(CTF Web入門)
 
多チャンネルバイラテラルフィルタの高速化
多チャンネルバイラテラルフィルタの高速化多チャンネルバイラテラルフィルタの高速化
多チャンネルバイラテラルフィルタの高速化
 
【展開用】日曜数学会 Sinc関数の積分について
【展開用】日曜数学会 Sinc関数の積分について【展開用】日曜数学会 Sinc関数の積分について
【展開用】日曜数学会 Sinc関数の積分について
 
Suite exercice
Suite exerciceSuite exercice
Suite exercice
 
Best Practices for Shader Graph
Best Practices for Shader GraphBest Practices for Shader Graph
Best Practices for Shader Graph
 
キムワイプ卓球における得点の複素数への拡張
キムワイプ卓球における得点の複素数への拡張キムワイプ卓球における得点の複素数への拡張
キムワイプ卓球における得点の複素数への拡張
 
VR for Cinema 4D 세미나 (2016) - C4D를 활용한 Full-CG VR 영상 콘텐츠 제작 워크플로우
VR for Cinema 4D 세미나 (2016)  - C4D를 활용한 Full-CG VR 영상 콘텐츠 제작 워크플로우VR for Cinema 4D 세미나 (2016)  - C4D를 활용한 Full-CG VR 영상 콘텐츠 제작 워크플로우
VR for Cinema 4D 세미나 (2016) - C4D를 활용한 Full-CG VR 영상 콘텐츠 제작 워크플로우
 
Lista matematica 07
Lista matematica 07Lista matematica 07
Lista matematica 07
 
ニューラルネットワークの理論
ニューラルネットワークの理論ニューラルネットワークの理論
ニューラルネットワークの理論
 
Shadow mapping 정리
Shadow mapping 정리Shadow mapping 정리
Shadow mapping 정리
 
クリーンランゲージについて
クリーンランゲージについてクリーンランゲージについて
クリーンランゲージについて
 
FPGAスタートアップ資料
FPGAスタートアップ資料FPGAスタートアップ資料
FPGAスタートアップ資料
 
2章グラフ理論スピード入門
2章グラフ理論スピード入門2章グラフ理論スピード入門
2章グラフ理論スピード入門
 
Pietのエディタを作った話
Pietのエディタを作った話Pietのエディタを作った話
Pietのエディタを作った話
 
Datamining 8th Hclustering
Datamining 8th HclusteringDatamining 8th Hclustering
Datamining 8th Hclustering
 

Similaire à Shaderx5 2.6normalmappingwithoutprecomputedtangents 130318 (1)

[shaderx5] 4.2 Multisampling Extension for Gradient Shadow Maps
[shaderx5] 4.2 Multisampling Extension for Gradient Shadow Maps[shaderx5] 4.2 Multisampling Extension for Gradient Shadow Maps
[shaderx5] 4.2 Multisampling Extension for Gradient Shadow Maps종빈 오
 
From RNN to neural networks for cyclic undirected graphs
From RNN to neural networks for cyclic undirected graphsFrom RNN to neural networks for cyclic undirected graphs
From RNN to neural networks for cyclic undirected graphstuxette
 
Trident International Graphics Workshop 2014 4/5
Trident International Graphics Workshop 2014 4/5Trident International Graphics Workshop 2014 4/5
Trident International Graphics Workshop 2014 4/5Takao Wada
 
CS 354 Graphics Math
CS 354 Graphics MathCS 354 Graphics Math
CS 354 Graphics MathMark Kilgard
 
Line drawing algo.
Line drawing algo.Line drawing algo.
Line drawing algo.Mohd Arif
 
Performance Analysis of Image Enhancement Using Dual-Tree Complex Wavelet Tra...
Performance Analysis of Image Enhancement Using Dual-Tree Complex Wavelet Tra...Performance Analysis of Image Enhancement Using Dual-Tree Complex Wavelet Tra...
Performance Analysis of Image Enhancement Using Dual-Tree Complex Wavelet Tra...IJERD Editor
 
Problem Solving by Computer Finite Element Method
Problem Solving by Computer Finite Element MethodProblem Solving by Computer Finite Element Method
Problem Solving by Computer Finite Element MethodPeter Herbert
 
Beginning direct3d gameprogramming10_shaderdetail_20160506_jintaeks
Beginning direct3d gameprogramming10_shaderdetail_20160506_jintaeksBeginning direct3d gameprogramming10_shaderdetail_20160506_jintaeks
Beginning direct3d gameprogramming10_shaderdetail_20160506_jintaeksJinTaek Seo
 
Parallel Evaluation of Multi-Semi-Joins
Parallel Evaluation of Multi-Semi-JoinsParallel Evaluation of Multi-Semi-Joins
Parallel Evaluation of Multi-Semi-JoinsJonny Daenen
 
Fractal Rendering in Developer C++ - 2012-11-06
Fractal Rendering in Developer C++ - 2012-11-06Fractal Rendering in Developer C++ - 2012-11-06
Fractal Rendering in Developer C++ - 2012-11-06Aritra Sarkar
 
Natural and Clamped Cubic Splines
Natural and Clamped Cubic SplinesNatural and Clamped Cubic Splines
Natural and Clamped Cubic SplinesMark Brandao
 
MTH 2001 Project 2Instructions• Each group must choos.docx
MTH 2001 Project 2Instructions• Each group must choos.docxMTH 2001 Project 2Instructions• Each group must choos.docx
MTH 2001 Project 2Instructions• Each group must choos.docxgilpinleeanna
 
Digital Distance Geometry
Digital Distance GeometryDigital Distance Geometry
Digital Distance Geometryppd1961
 
TAO Fayan_X-Ray and MIP volume rendering
TAO Fayan_X-Ray and MIP volume renderingTAO Fayan_X-Ray and MIP volume rendering
TAO Fayan_X-Ray and MIP volume renderingFayan TAO
 
Slides: Perspective click-and-drag area selections in pictures
Slides: Perspective click-and-drag area selections in picturesSlides: Perspective click-and-drag area selections in pictures
Slides: Perspective click-and-drag area selections in picturesFrank Nielsen
 
Transformations advanced
Transformations advancedTransformations advanced
Transformations advancedAVINASH JURIANI
 
OUTPUT PRIMITIVES.pptx
OUTPUT PRIMITIVES.pptxOUTPUT PRIMITIVES.pptx
OUTPUT PRIMITIVES.pptxIndhuMcamphil
 
OUTPUT PRIMITIVES.pptx
OUTPUT PRIMITIVES.pptxOUTPUT PRIMITIVES.pptx
OUTPUT PRIMITIVES.pptxAlamelu
 

Similaire à Shaderx5 2.6normalmappingwithoutprecomputedtangents 130318 (1) (20)

[shaderx5] 4.2 Multisampling Extension for Gradient Shadow Maps
[shaderx5] 4.2 Multisampling Extension for Gradient Shadow Maps[shaderx5] 4.2 Multisampling Extension for Gradient Shadow Maps
[shaderx5] 4.2 Multisampling Extension for Gradient Shadow Maps
 
From RNN to neural networks for cyclic undirected graphs
From RNN to neural networks for cyclic undirected graphsFrom RNN to neural networks for cyclic undirected graphs
From RNN to neural networks for cyclic undirected graphs
 
Trident International Graphics Workshop 2014 4/5
Trident International Graphics Workshop 2014 4/5Trident International Graphics Workshop 2014 4/5
Trident International Graphics Workshop 2014 4/5
 
CS 354 Graphics Math
CS 354 Graphics MathCS 354 Graphics Math
CS 354 Graphics Math
 
Line drawing algo.
Line drawing algo.Line drawing algo.
Line drawing algo.
 
Performance Analysis of Image Enhancement Using Dual-Tree Complex Wavelet Tra...
Performance Analysis of Image Enhancement Using Dual-Tree Complex Wavelet Tra...Performance Analysis of Image Enhancement Using Dual-Tree Complex Wavelet Tra...
Performance Analysis of Image Enhancement Using Dual-Tree Complex Wavelet Tra...
 
Problem Solving by Computer Finite Element Method
Problem Solving by Computer Finite Element MethodProblem Solving by Computer Finite Element Method
Problem Solving by Computer Finite Element Method
 
Beginning direct3d gameprogramming10_shaderdetail_20160506_jintaeks
Beginning direct3d gameprogramming10_shaderdetail_20160506_jintaeksBeginning direct3d gameprogramming10_shaderdetail_20160506_jintaeks
Beginning direct3d gameprogramming10_shaderdetail_20160506_jintaeks
 
Parallel Evaluation of Multi-Semi-Joins
Parallel Evaluation of Multi-Semi-JoinsParallel Evaluation of Multi-Semi-Joins
Parallel Evaluation of Multi-Semi-Joins
 
Fractal Rendering in Developer C++ - 2012-11-06
Fractal Rendering in Developer C++ - 2012-11-06Fractal Rendering in Developer C++ - 2012-11-06
Fractal Rendering in Developer C++ - 2012-11-06
 
Natural and Clamped Cubic Splines
Natural and Clamped Cubic SplinesNatural and Clamped Cubic Splines
Natural and Clamped Cubic Splines
 
MTH 2001 Project 2Instructions• Each group must choos.docx
MTH 2001 Project 2Instructions• Each group must choos.docxMTH 2001 Project 2Instructions• Each group must choos.docx
MTH 2001 Project 2Instructions• Each group must choos.docx
 
Digital Distance Geometry
Digital Distance GeometryDigital Distance Geometry
Digital Distance Geometry
 
TAO Fayan_X-Ray and MIP volume rendering
TAO Fayan_X-Ray and MIP volume renderingTAO Fayan_X-Ray and MIP volume rendering
TAO Fayan_X-Ray and MIP volume rendering
 
Slides: Perspective click-and-drag area selections in pictures
Slides: Perspective click-and-drag area selections in picturesSlides: Perspective click-and-drag area selections in pictures
Slides: Perspective click-and-drag area selections in pictures
 
Transformations advanced
Transformations advancedTransformations advanced
Transformations advanced
 
Problems 2
Problems 2Problems 2
Problems 2
 
Lect5 v2
Lect5 v2Lect5 v2
Lect5 v2
 
OUTPUT PRIMITIVES.pptx
OUTPUT PRIMITIVES.pptxOUTPUT PRIMITIVES.pptx
OUTPUT PRIMITIVES.pptx
 
OUTPUT PRIMITIVES.pptx
OUTPUT PRIMITIVES.pptxOUTPUT PRIMITIVES.pptx
OUTPUT PRIMITIVES.pptx
 

Dernier

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 

Dernier (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 

Shaderx5 2.6normalmappingwithoutprecomputedtangents 130318 (1)

  • 1. ShaderX5 2.6 Normal Mapping without Precomputed Tangents Christian Schuler, Phenomic ShaderStudy 2013.03.18 황규석 http://cafe.naver.com/shader
  • 2. Introduction  TBN Matrix(Tangent Frames)를 Pixel Shader에서 실시간으로 계산하는 방법  장점 - Vertex attributes에 Tangent Frames를 넣어 줄 필요가 없다 • Band width, Memory, Complexity 감소 • 그래픽 디자이너가 툴에서 이것을 신경 쓸 필요가 없다  단점 - Pixel Shader에서의 부하, Optimization 필수  Cotangent Frames - More optimization
  • 4. Normal Texture Mapping ©http://marupeke296.com Normal 벡터를 Texture 좌표계(공간)에서 Local 좌표계(공간)로 변환 new = [dot(old, X), dot(old, Y), dot(old, Z)] (X, Y, Z는 new 좌표계에서 표현되는 old 좌표계 basis vectors, e.g. T, B, N)
  • 5. Tangents to a surface Position 벡터(p1, p2)와 Texture 좌표 벡터(u1, u2, v1, v2)를 이용해 Tangent 벡터(T, B) 계산
  • 6. // A code snippet to perform normal perturbation using a tangent-space normal map float3 perturb_normal(float3 T, float3 B, float3 N, float2 texcoord) { // Build a tangent frame as float3x3 for convenience float3x3 tangent_frame = float3x3(T, B, N); // Read the perturbed normal from the normal map float3 perturbed_normal = tex2D(normalmap, texcoord); // Sign-expand (for a normal map in unsigned RGB format) perturbed_normal = 2 * perturbed_normal – 1; // And transform the perturbed normal out of tangent space // (into whatever space T, B and N were originally expressed in, usually world). return normalize( mul( perturbed_normal, tangent_frame)); } Tangent space Normal mapping
  • 7. // A function that takes an interpolated surface normal N, a position vector p, // and texture coordinates u and v and returns a complete tangent frame that is // ready to be used in lighting calculations (46 pixel shader instructions) float3x3 compute_tangent_frame(float3 N, float3 p, float2 uv) { // Get edge vectors of the pixel triangle float3 dp1 = ddx(p); // 가로 방향의 옆 픽셀과의 Position 변화량 float3 dp2 = ddy(p); // 세로 방향의 옆 픽셀과의 Position 변화량 float2 duv1 = ddx(uv); // 가로 방향의 옆 픽셀과의 UV값 변화량 float2 duv2 = ddy(uv); // 세로 방향의 옆 픽셀과의 UV값 변화량 // Solve the linear system float3x3 M = float3x3(dp1, dp2, cross(dp1, dp2)); float3x3 inverseM = invert_3x3(M); float3 T = mul(inverseM, float3(duv1.x, duv2.x, 0)); float3 B = mul(inverseM, float3(duv1.y, duv2.y, 0)); // Construct tangent frame // (* see discussion regarding the square patch assumption) float maxLength = max(length(T), length(B)); return float3x3 (T / maxLength, B / maxLength, N); } float3x3 invert_3x3(float3x3 M) { float det = dot( cross(M[0], M[1]), M[2] ); float3x3 T = transpose(M); return float3x3( cross(T[1], T[2]), cross(T[2], T[0]), cross(T[0], T[1]) ) / det; } Moving to the Pixel Sahder
  • 8.
  • 9. // Optimization 1: // normalize T and B and spare the determinant. // (31 pixel shader instructions) float3x3 compute_tangent_frame_01(float3 N, float3 p, float2 uv) { // Get edge vectors of the pixel triangle float3 dp1 = ddx(p); float3 dp2 = ddy(p); float2 duv1 = ddx(uv); float2 duv2 = ddy(uv); // Solve the linear system float3x3 M = float3x3(dp1, dp2, cross(dp1, dp2)); float3x3 inverseM = invert_3x3_nodet(M); float3 T = mul(inverseM, float3(duv1.x, duv2.x, 0)); float3 B = mul(inverseM, float3(duv1.y, duv2.y, 0)); // Construct tangent frame return float3x3(normalize(T), normalize(B), N); } float3x3 invert_3x3_nodet(float3x3 M) { float3x3 T = transpose(M); return float3x3( cross(T[1], T[2]), cross(T[2], T[0]), cross(T[0], T[1]) ); } Optimization 1
  • 10. // Optimization 2: // exploits the zero components found in the solution vectors and collapses // a hidden double transpose // (17 pixel shader instructions) float3x3 compute_tangent_frame_02(float3 N, float3 p, float2 uv) { // Get edge vectors of the pixel triangle float3 dp1 = ddx(p); float3 dp2 = ddy(p); float2 duv1 = ddx(uv); float2 duv2 = ddy(uv); // Solve the linear system float3x3 M = float3x3(dp1, dp2, cross(dp1, dp2)); float3x3 inversetransposeM = float2x3( cross(M[1], M[2]), cross(M[2], M[0]) ); //inversetranspose = original float3 T = mul(float2(duv1.x, duv2.x), inversetransposeM); float3 B = mul(float2(duv1.y, duv2.y), inversetransposeM); // Construct tangent frame return float3x3(normalize(T), normalize(B), N); } Optimization 2
  • 11. // Optimization 3: // assume M is orthogonal and exploits the fact that we have made an inverse transpose explicit. // (14 pixel shader instructions) float3x3 compute_tangent_frame_03(float3 N, float3 p, float2 uv) { // Get edge vectors of the pixel triangle float3 dp1 = ddx(p); float3 dp2 = ddy(p); float2 duv1 = ddx(uv); float2 duv2 = ddy(uv); // Solve the linear system // (not much solving is left going here) float2x3 M = float2x3(dp1, dp2); float3 T = mul(float2(duv1.x, duv2.x), M); float3 B = mul(float2(duv1.y, duv2.y), M); // Construct tangent frame return float3x3(normalize(T), normalize(B), N); } Optimization 3
  • 12.
  • 13. Conclusion  TBN matrix 계산을 Pixel shader에서 낮은 비용으로 하는 방법 제 공함  Vertex 속성에서 Tangent frames를 제거할 수 있지만 픽셀셰이더 에서 부하가 증가  텍스처 좌표 공간에서의 procedural 처리도 가능한 방법  추가적으로 Cotangent frames를 이용한 방법을 블로그에 소개하 고 있음 
  • 14. A vector v (red) represented by tangent basis vectors (yellow, left: e1, e2, e3) to the coordinate curves (black), · dual basis, covector basis, or cobasis (blue, right: e1, e2, e3 ), normal vectors to coordinate surfaces (grey), in 3D general curvilinear coordinates (q1, q2, q3), a tuple of numbers to define point in a position space. Note: the basis and cobasis do not coincide unless the basis is orthogonal. Tangent and Contangent basis vectors ©wikipedia
  • 15. // compute contangent frames mat3 contangent_frame(vec3 N, vec3 p, vec2 uv) { // Get edge vectors of the pixel triangle vec3 dp1 = dFdx(p); vec3 dp2 = dFdy(p); vec2 duv1 = dFdx(uv); vec2 duv2 = dFdy(uv); // Solve the linear system vec3 dp2perp = cross(dp2, N); vec3 dp1perp = cross(N, dp1); vec3 T = dp2perp * duv1.x + dp1perp * duv2.x; vec3 B = dp2perp * duv1.y + dp1perp * duv2.y; // Construct a scale-invariant frame float invmax = inversesqrt(max(dot(T,T), dot(B,B)); return mat3(T * invmax, B * invmax, N); } vec3 pertub_normal(vec3 N, vec3 V, vec2 texcoord) { // assume N, the interpolated vertex normal and // V, the view vector(vertex to eye) vec3 map = texture2D(mapBump, texcoord).xyz; #ifdef WITH_NORMALMAP_UNSIGNED map = map * 255./127. – 128./127.; #endif #ifdef WITH_NORMALMAP_2CHANNEL map.z = sqrt(1. – dot(map.xy, map.xy)); #endif #ifdef WITH_NORMALMAP_GREEN_UP map.y = -map.y; #endif mat3 TBN = cotangent_frame(N, -V, texcoord); return normalize(TBN * map); } Contangent Frames
  • 16. varying vec3 g_vertexnormal; varying vec3 g_viewvector; // camera pos – vertex pos varying vec2 g_texcoord; void main() { vec3 N = normalize(g_vertexnormal); vec3 V = normalize(g_viewvector); #ifdef WITH_NORMALMAP N = perturb_normal(N, V, g_texcoord); #endif // ... } Contangent Frames (cont.) Reference (Followup) Normal mapping without precomputed tangents http://www.thetenthplanet.de/archives/1180 Covariance and contravariance of vectors http://en.wikipedia.org/wiki/Covariance_and_contravariance_of_vectors Fast inverse square root http://en.wikipedia.org/wiki/Fast_inverse_square_root