SlideShare a Scribd company logo
1 of 16
Download to read offline
My Standard Shader
Introduction
/me
Rémi Bodin – 28 ans
Ingénieur développeur Unity3D chez Persistant Studios.
Premiers pas sur Unity3D en 2009
STANDARD SHADER
Standard Shader
Avant
Un Shader différent par « type » de rendu
Par exemple :
Main texture  Diffuse
Main texture + Normal map  Bumped Diffuse
Main texture + Normal map + Height map  Parallax Diffuse
Standard Shader
Maintenant
Un seul Shader qui fait la vie o/
Génial mais comment ca marche ?
LES BASES
Les Bases
Un shader est un programme exécuté par la carte graphique pour paramétrer une
partie du processus de rendu.
Un shader est un mini programme
sampler2D _MainTex;
fixed4 _Color;
void surf (Input IN, inout SurfaceOutput o)
{
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * _Color;
}
sampler2D _MainTex;
sampler2D _BumpMap;
fixed4 _Color;
void surf (Input IN, inout SurfaceOutput o)
{
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * _Color;
}
Les Bases
Son temps d’exécution dépend de sa complexité.
Un shader est un mini programme
Les Bases
Il est donc conseillé de bien choisir le shader utilisé pour ne pas exécuter des
instructions inutiles.
Un shader est un mini programme
sampler2D _MainTex;
sampler2D _BumpMap;
sampler2D _ParallaxMap;
fixed4 _Color;
float _Parallax;
void surf (Input IN, inout SurfaceOutput o)
{
half h = tex2D (_ParallaxMap, IN.uv_BumpMap).w;
float2 offset = ParallaxOffset (h, _Parallax, IN.viewDir);
IN.uv_MainTex += offset;
IN.uv_BumpMap += offset;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * _Color;
}
Les bases
Un shader est un mini programme
sampler2D _MainTex;
sampler2D _BumpMap;
sampler2D _ParallaxMap;
fixed4 _Color;
float _Parallax;
void surf (Input IN, inout SurfaceOutput o)
{
half h = tex2D (_ParallaxMap, IN.uv_BumpMap).w;
float2 offset = ParallaxOffset (h, _Parallax, IN.viewDir);
IN.uv_MainTex += offset;
IN.uv_BumpMap += offset;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * _Color;
}
Diffuse
Bump diffuse
Parallax diffuse
COMMENT CA MARCHE ALORS ?
Comment ca marche alors ?
Les valeurs par defaut
sampler2D _MainTex;
sampler2D _BumpMap;
sampler2D _ParallaxMap;
fixed4 _Color;
float _Parallax;
void surf (Input IN, inout SurfaceOutput o)
{
half h = tex2D (_ParallaxMap, IN.uv_BumpMap).w;
float2 offset = ParallaxOffset (h, _Parallax, IN.viewDir);
IN.uv_MainTex += offset;
IN.uv_BumpMap += offset;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * _Color;
}
Comment ca marche alors ?
Shader variants
sampler2D _MainTex;
sampler2D _BumpMap;
sampler2D _ParallaxMap;
fixed4 _Color;
float _Parallax;
#pragma shader_feature _NO_BUMP _BUMP
#pragma shader_feature _NO_PARALLAX _PARALLAX
void surf (Input IN, inout SurfaceOutput o)
{
#ifdef _PARALLAX
half h = tex2D (_ParallaxMap, IN.uv_BumpMap).w;
float2 offset = ParallaxOffset (h, _Parallax, IN.viewDir);
IN.uv_MainTex += offset;
IN.uv_BumpMap += offset;
#endif
#ifdef _BUMP
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
#endif
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * _Color;
}
_NO_BUMP _NO_PARALLAX
_BUMP _NO_PARALLAX
_BUMP _PARALLAX
_NO_BUMP _PARALLAX
Comment ca marche alors ?
CustomEditor
public class MyStandardShaderGUI : ShaderGUI
{
public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
{
Material targetMat = materialEditor.target as Material;
base.OnGUI(materialEditor, properties);
if (targetMat.GetTexture("_BumpMap"))
targetMat.EnableKeyword("_BUMP");
else
targetMat.DisableKeyword("_BUMP");
}
}
Comment ca marche alors ?
Et voila
_NO_BUMP _NO_PARALLAX _BUMP _NO_PARALLAX _BUMP _PARALLAX
QUESTIONS

More Related Content

Viewers also liked

Mis viejitos...uff prohibido llorar
Mis viejitos...uff prohibido llorarMis viejitos...uff prohibido llorar
Mis viejitos...uff prohibido llorarAlex Granda
 
Faits saillants session_image_st-elie
Faits saillants session_image_st-elieFaits saillants session_image_st-elie
Faits saillants session_image_st-elieChemin de Saint-Élie
 
Carta irulla definitiva
Carta  irulla definitivaCarta  irulla definitiva
Carta irulla definitivauamericas
 
Poser une serrure
Poser une serrurePoser une serrure
Poser une serrureGENICIMO
 
Tema 24 la península ibérica hasta la dominación romana
Tema 24 la península ibérica hasta la dominación romanaTema 24 la península ibérica hasta la dominación romana
Tema 24 la península ibérica hasta la dominación romanapfresneda
 
Bloque cierre carlagomez
Bloque cierre carlagomezBloque cierre carlagomez
Bloque cierre carlagomezCarlita Gomez
 
En savoir plus sur Yves Doyon & Assoc
En savoir plus sur Yves Doyon & AssocEn savoir plus sur Yves Doyon & Assoc
En savoir plus sur Yves Doyon & AssocYves Doyon
 
Por El son todas las cosas
 Por El son todas las cosas Por El son todas las cosas
Por El son todas las cosasZoila Ibarra
 
Best Of Yummy Magazine
Best Of Yummy Magazine Best Of Yummy Magazine
Best Of Yummy Magazine YummyMagazine
 
Yummy magazine N°13
Yummy magazine N°13Yummy magazine N°13
Yummy magazine N°13YummyMagazine
 
Compañias de transporte francy
Compañias de transporte francyCompañias de transporte francy
Compañias de transporte francyfrancy
 

Viewers also liked (19)

Liste de figures
Liste de figuresListe de figures
Liste de figures
 
Mis viejitos...uff prohibido llorar
Mis viejitos...uff prohibido llorarMis viejitos...uff prohibido llorar
Mis viejitos...uff prohibido llorar
 
Faits saillants session_image_st-elie
Faits saillants session_image_st-elieFaits saillants session_image_st-elie
Faits saillants session_image_st-elie
 
COP CARMEN YELA
COP CARMEN YELACOP CARMEN YELA
COP CARMEN YELA
 
La magie des étoiles dévoilée
La magie des étoiles dévoiléeLa magie des étoiles dévoilée
La magie des étoiles dévoilée
 
Carta irulla definitiva
Carta  irulla definitivaCarta  irulla definitiva
Carta irulla definitiva
 
Poser une serrure
Poser une serrurePoser une serrure
Poser une serrure
 
Tema 24 la península ibérica hasta la dominación romana
Tema 24 la península ibérica hasta la dominación romanaTema 24 la península ibérica hasta la dominación romana
Tema 24 la península ibérica hasta la dominación romana
 
Bloque cierre carlagomez
Bloque cierre carlagomezBloque cierre carlagomez
Bloque cierre carlagomez
 
IES Cours De Stage
IES Cours De StageIES Cours De Stage
IES Cours De Stage
 
Pub
PubPub
Pub
 
En savoir plus sur Yves Doyon & Assoc
En savoir plus sur Yves Doyon & AssocEn savoir plus sur Yves Doyon & Assoc
En savoir plus sur Yves Doyon & Assoc
 
Por El son todas las cosas
 Por El son todas las cosas Por El son todas las cosas
Por El son todas las cosas
 
Cesar (1)
Cesar (1)Cesar (1)
Cesar (1)
 
Plan daction
Plan dactionPlan daction
Plan daction
 
Ley8835
Ley8835Ley8835
Ley8835
 
Best Of Yummy Magazine
Best Of Yummy Magazine Best Of Yummy Magazine
Best Of Yummy Magazine
 
Yummy magazine N°13
Yummy magazine N°13Yummy magazine N°13
Yummy magazine N°13
 
Compañias de transporte francy
Compañias de transporte francyCompañias de transporte francy
Compañias de transporte francy
 

[Unity3D] My standard shader

  • 2. Introduction /me Rémi Bodin – 28 ans Ingénieur développeur Unity3D chez Persistant Studios. Premiers pas sur Unity3D en 2009
  • 4. Standard Shader Avant Un Shader différent par « type » de rendu Par exemple : Main texture  Diffuse Main texture + Normal map  Bumped Diffuse Main texture + Normal map + Height map  Parallax Diffuse
  • 5. Standard Shader Maintenant Un seul Shader qui fait la vie o/ Génial mais comment ca marche ?
  • 7. Les Bases Un shader est un programme exécuté par la carte graphique pour paramétrer une partie du processus de rendu. Un shader est un mini programme sampler2D _MainTex; fixed4 _Color; void surf (Input IN, inout SurfaceOutput o) { o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * _Color; }
  • 8. sampler2D _MainTex; sampler2D _BumpMap; fixed4 _Color; void surf (Input IN, inout SurfaceOutput o) { o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap)); o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * _Color; } Les Bases Son temps d’exécution dépend de sa complexité. Un shader est un mini programme
  • 9. Les Bases Il est donc conseillé de bien choisir le shader utilisé pour ne pas exécuter des instructions inutiles. Un shader est un mini programme sampler2D _MainTex; sampler2D _BumpMap; sampler2D _ParallaxMap; fixed4 _Color; float _Parallax; void surf (Input IN, inout SurfaceOutput o) { half h = tex2D (_ParallaxMap, IN.uv_BumpMap).w; float2 offset = ParallaxOffset (h, _Parallax, IN.viewDir); IN.uv_MainTex += offset; IN.uv_BumpMap += offset; o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap)); o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * _Color; }
  • 10. Les bases Un shader est un mini programme sampler2D _MainTex; sampler2D _BumpMap; sampler2D _ParallaxMap; fixed4 _Color; float _Parallax; void surf (Input IN, inout SurfaceOutput o) { half h = tex2D (_ParallaxMap, IN.uv_BumpMap).w; float2 offset = ParallaxOffset (h, _Parallax, IN.viewDir); IN.uv_MainTex += offset; IN.uv_BumpMap += offset; o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap)); o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * _Color; } Diffuse Bump diffuse Parallax diffuse
  • 11. COMMENT CA MARCHE ALORS ?
  • 12. Comment ca marche alors ? Les valeurs par defaut sampler2D _MainTex; sampler2D _BumpMap; sampler2D _ParallaxMap; fixed4 _Color; float _Parallax; void surf (Input IN, inout SurfaceOutput o) { half h = tex2D (_ParallaxMap, IN.uv_BumpMap).w; float2 offset = ParallaxOffset (h, _Parallax, IN.viewDir); IN.uv_MainTex += offset; IN.uv_BumpMap += offset; o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap)); o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * _Color; }
  • 13. Comment ca marche alors ? Shader variants sampler2D _MainTex; sampler2D _BumpMap; sampler2D _ParallaxMap; fixed4 _Color; float _Parallax; #pragma shader_feature _NO_BUMP _BUMP #pragma shader_feature _NO_PARALLAX _PARALLAX void surf (Input IN, inout SurfaceOutput o) { #ifdef _PARALLAX half h = tex2D (_ParallaxMap, IN.uv_BumpMap).w; float2 offset = ParallaxOffset (h, _Parallax, IN.viewDir); IN.uv_MainTex += offset; IN.uv_BumpMap += offset; #endif #ifdef _BUMP o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap)); #endif o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * _Color; } _NO_BUMP _NO_PARALLAX _BUMP _NO_PARALLAX _BUMP _PARALLAX _NO_BUMP _PARALLAX
  • 14. Comment ca marche alors ? CustomEditor public class MyStandardShaderGUI : ShaderGUI { public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties) { Material targetMat = materialEditor.target as Material; base.OnGUI(materialEditor, properties); if (targetMat.GetTexture("_BumpMap")) targetMat.EnableKeyword("_BUMP"); else targetMat.DisableKeyword("_BUMP"); } }
  • 15. Comment ca marche alors ? Et voila _NO_BUMP _NO_PARALLAX _BUMP _NO_PARALLAX _BUMP _PARALLAX