Upload
remibodin
View
231
Download
3
Embed Size (px)
Citation preview
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