16
My Standard Shader

[Unity3D] My standard shader

Embed Size (px)

Citation preview

Page 1: [Unity3D] My standard shader

My Standard Shader

Page 2: [Unity3D] My standard shader

Introduction /me

Rémi Bodin – 28 ans

Ingénieur développeur Unity3D chez Persistant Studios.

Premiers pas sur Unity3D en 2009

Page 3: [Unity3D] My standard shader

STANDARD SHADER

Page 4: [Unity3D] My 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

Page 5: [Unity3D] My standard shader

Standard Shader Maintenant

Un seul Shader qui fait la vie \o/

Génial mais comment ca marche ?

Page 6: [Unity3D] My standard shader

LES BASES

Page 7: [Unity3D] My standard shader

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; }

Page 8: [Unity3D] My standard shader

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

Page 9: [Unity3D] My standard shader

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; }

Page 10: [Unity3D] My standard shader

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

Page 11: [Unity3D] My standard shader

COMMENT CA MARCHE ALORS ?

Page 12: [Unity3D] My standard shader

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; }

Page 13: [Unity3D] My standard shader

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

Page 14: [Unity3D] My standard shader

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"); } }

Page 15: [Unity3D] My standard shader

Comment ca marche alors ? Et voila

_NO_BUMP _NO_PARALLAX _BUMP _NO_PARALLAX _BUMP _PARALLAX

Page 16: [Unity3D] My standard shader

QUESTIONS