Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Unityのシェーダーについて自分なりにまとめてみた

 Unityのシェーダーについて自分なりにまとめてみた

Unityのシェーダーについて自分なりにまとめてみました。
まだまだ書ききれていない項目が多いです。
Cg/HLSL部分などはリファレンスに丸投げしてます。

Es_Program

March 06, 2016
Tweet

More Decks by Es_Program

Other Decks in Programming

Transcript

  1. “ ➤ UnityΤϯδχΞ ➤ C#Ͱ৭ʑ࡞ͬͯ·͢ ➤ ࠷ۙγΣʔμʔͷษڧ͸͡ Ί·ͨ͠ɻ Es About

    Me • http://esbrog.hatenablog.com/ • https://github.com/EsProgram • https://twitter.com/Es_Program
  2. γΣʔμʔͷجຊ ➤ UnityͷγΣʔμʔʹ͓͚Δج ຊతͳ߲໨ʹ͍ͭͯݟ͍ͯ͘ɻ ➤ ͜͜Ͱ͸UnityͷϨϯμϦϯά ύΠϓϥΠϯ΍ΧϝϥɺuGUI ʹ͍ͭͯܰ͘৮ΕɺγΣʔμʔ ͷجຊߏจʹ͍ͭͯղઆ͢Δɻ •

    ʲUnityϚχϡΞϧʳShaderLab ϦϑΝϨϯε • ʲԜTipsʳUnity ͷγΣʔμͷجૅΛษڧͯ͠ΈͨͷͰ΍Δ ؾग़ͯ͠·ͱΊͯΈͨ • ʲΏʹͯ͘උ๨࿥ʳUnity Shader ·ͱΊ
  3. ShaderLabجຊߏจ Shader "name" { [Properties] Subshaders [Fallback] [CustomEditor] } ShaderLabγϯλοΫε

    Properties γΣʔμʔ͸ Properties ͷϦετΛ΋ͭ͜ͱ͕Ͱ͖ΔɻγΣʔμʔͰఆٛ͞ΕͨϓϩύςΟʔ͸ Unity ͷ ϚςϦ ΞϧΠϯεϖΫλʔ Ͱදࣔ͞ΕΔɻ௨ৗͷϓϩύςΟʔ͸ɺΦϒδΣΫτͷΧϥʔɺςΫενϟɺ͋Δ͍͸ γΣʔμʔʹΑΓ࢖༻͞ΕΔ೚ҙͷ஋ɻ SubShadersͱFallback ֤γΣʔμʔ͸ SubShader ͷϦετ͔Βߏ੒͞ΕΔɻগͳ͘ͱ΋ͻͱ͕ͭඞਢɻγΣʔμʔΛϩʔυ͢Δͱ͖ɺ Unity ͸αϒγΣʔμʔͷϦετ͔ΒɺΤϯυϢʔβʔͷϚγϯͰαϙʔτ͞ΕΔ࠷ॳͷαϒγΣʔμʔΛબ୒͢ Δɻ΋͠αϙʔτ͞ΕΔαϒγΣʔμʔ͕ͳ͍৔߹ɺUnity ͸ Fallback γΣʔμʔ ͷ࢖༻ΛࢼΈΔɻ γΣʔμʔͷ “level of detail” (LOD) ͱɺ“shader replacement” ͷೋͭ΋·ͨɺαϒγΣʔμʔͷ࡞੒ʹ࢖༻Ͱ͖ ΔςΫχοΫɻ name γΣʔμʹ෇Ճ͢Δ໊લͰ͢ɻϚςϦΞϧΠϯεϖΫλʔʹදࣔ͞ΕΔ͔ name ҎԼͰϦετʹهड़͞ΕΔɻ ͨͱ͑͹ Custom/MyShader ͱ͢Ε͹ΠϯεϖΫλʔ্ͰγΣʔμʔΛબ୒͢Δ࣌ɺCustomϦετʹMyShader͕දࣔ͞ΕΔΑ͏ʹͳΔɻ CustomEditor γΣʔμʔʹର͢Δ CustomEditor Λఆٛ͢Δ͜ͱ͕Ͱ͖ΔɻUnity ͸͜ͷ໊લͰ ShaderGUI Λ֦ுͨ͠Ϋϥε Λ୳͢ɻ΋͠ݟ͔ͭͬͨ৔߹ɺ͜ͷγΣʔμʔΛ࢖༻͢Δ͢΂ͯͷϚςϦΞϧͷΠϯεϖΫλʔ͕͜ͷ ShaderGUI Λ࢖༻͢Δɻ
  4. جຊߏจ࣮૷ྫ Shader "Structure Example" { Properties { // ΠϯεϖΫλʹදࣔ͞ΕΔϓϩύςΟʢ৭΍ςΫενϟ౳ʣ }

    SubShader { // ...௖఺/ϑϥάϝϯτϓϩάϥϜΛ࢖༻͢ΔαϒγΣʔμ... } SubShader { // ...ύε͝ͱʹ 4 ͭͷςΫενϟΛ࢖༻͢ΔαϒγΣʔμ... } SubShader { // ...ύε͝ͱʹ 2 ͭͷςΫενϟΛ࢖༻͢ΔαϒγΣʔμ... } SubShader { // …ݟͨ໨͸ѱ͍͕ɺ͍ͣΕͰ΋࣮ߦ͢ΔαϒγΣʔμ… } Fallback "Other Shader Name" // SubShader ͕͍ͣΕ΋μϝͩͬͨͱ͖ʹࢼ͢ผͷγΣʔμ }
  5. Category߲໨ ΧςΰϦ͸࿦ཧతάϧʔϐϯάɻ͜ Ε͸ଟ͘ͷ৔߹ɺϨϯμϦϯάঢ়ଶ Λܧঝ͢Δͷʹ࢖༻͞ΕΔɻྫ͑͹ɺ γΣʔμʔʹෳ਺ͷSubshader͕͋ Δ৔߹ɺͦΕͧΕ͕Fog ΛΦϑʹ ͠ɺBlendingΛ௥Ճʹࢦఆ͢Δඞཁ ͕͋Δ৔߹ʹ͸ɺΧςΰϦΛ࢖ͬͯ ̍ͭͷهड़ͰࢦఆΛߦ͏͜ͱ͕Ͱ͖

    Δɻ Shader "example" { Category { Fog { Mode Off } Blend One One SubShader { // ... } SubShader { // ... } // ... } } ΧςΰϦϒϩοΫ͸ɺγΣʔμʔͷߏจղੳʹͷΈӨڹ͢Δ͕ɺԼͷ͢΂ͯͷϒ ϩοΫʹΧςΰϦ಺Ͱઃఆͨ͠ঢ়ଶΛߏจղੳ͢Δͷͱ·ͬͨ͘ಉ͡ʹͳΔɻ γΣʔμʔͷ࣮ߦ଎౓ʹ͸Ұ੾Өڹ͠ͳ͍ɻ
  6. Propertiesͱ͸ ➤ γΣʔμʔʹΑΓɺUnity ͷ ϚςϦΞϧΠϯεϖΫλʔ ্ ͰɺΞʔςΟετ͕ηοτͰ͖Δม਺ͷҰཡΛఆٛͰ͖Δɻ ➤ ϓϩύςΟʔ͸ γΣʔμʔϑΝΠϧͷ

    Properties ϒϩοΫͰ ఆٛ͢Δɻ Tint͔ΒColor Mask·ͰશͯϓϩύςΟɻ ϓϩύςΟ͸ϚςϦΞϧΠϯεϖΫλʔͰ ஋Λฤू͢Δ͜ͱ͕Մೳɻ
  7. Propertiesߏจ Properties { Property [Property ...] } ྫ Properties {

    _WaveScale ("Wave scale", Range (0.02,0.15)) = 0.07 // sliders _ReflDistort ("Reflection distort", Range (0,1.5)) = 0.5 _RefrDistort ("Refraction distort", Range (0,1.5)) = 0.4 _RefrColor ("Refraction color", Color) = (.34, .85, .92, 1) // color _ReflectionTex ("Environment Reflection", 2D) = "" {} // textures _RefractionTex ("Environment Refraction", 2D) = "" {} _Fresnel ("Fresnel (A) ", 2D) = "" {} _BumpMap ("Bumpmap (RGB) ", 2D) = "" {} } Propertiesʹ͸ม਺ͱͯ͠ѻ͏PropertyΛෳ਺ఆٛ͢Δ͜ͱ͕Ͱ͖Δɻ
  8. Property name ("display name",type) = value Propertyͷߏจ ➤ name
 γΣʔμʔϓϩάϥϜ্Ͱར༻͢Δม਺໊ɻΞϯμʔείΞ

    (_)Ͱ։࢝͢Δɻ ➤ display name
 ϚςϦΞϧΠϯεϖΫλʔͰදࣔ͢Δจࣈྻ ➤ type
 ม਺ͷܕ ➤ value
 ॳظ஋
  9. ܕ͝ͱͷϓϩύςΟهड़ྫ name("display name", Range( min, max)) = number floatϓϩύςΟΛఆٛ͠ɺΠϯεϖΫλ্ͰεϥΠυόʔ͕ min͔Β

    maxͱͯ͠දݱɻ name("display name", Color) = ( number, number, number, number) colorϓϩύςΟΛఆٛɻ name("display name", 2D) = "name" { options} 2DςΫενϟϓϩύςΟΛఆٛɻ name("display name", Rect) = "name" { options} ௕ํܗʢ2ͷ΂͖৐Ͱͳ͍ʣςΫενϟͷϓϩύςΟΛఆٛɻ name("display name", Cube) = "name" { options} ΩϡʔϒϚοϓςΫενϟͷϓϩύςΟΛఆٛɻ name("display name", Float) = number floatϓϩύςΟΛఆٛɻ name("display name", Vector) = ( number, number, number, number) 4ίϯϙʔωϯτɹϕΫτϧͷϓϩύςΟΛఆٛɻ ςΫενϟܥϓϩύςΟʔͷoptions͸Unity 5 Ͱ͸࡟আ͞ΕͨͷͰɺۭͷ{}Λఆٛ͢Δ
  10. ࣗಈ࣮૷ͷϓϩύςΟ ➤ ϓϩύςΟͱͯ͠ઃఆ͞Ε֤ͨςΫενϟͷͨΊʹɺUnity͸௥ՃͷϓϩύςΟͱͯ͠༨෼ ͳ৘ใΛࣗಈతʹઃఆ͢Δɻ ➤ ࣗಈతʹఆٛ͞ΕΔͷͰɺγΣʔμʔ͔Β͸Propertiesʹఆٛ͠ͳͯ͘΋ΞΫηε͕Մೳɻ {TextureName}_ST - float4ϓϩύςΟɻλΠϦϯά/Φϑηοτͷ৘ใؚ͕·Ε͍ͯΔɻ •

    x͸ XλΠϦϯά஋ • y͸ YλΠϦϯά஋ • z͸ XΦϑηοτ஋ • w͸ YΦϑηοτ஋ ͨͱ͑͹ɺ_MainTexΛPropertiesʹఆٛͨ͠৔߹_MainTex_ST͕ࣗಈఆٛ͞ΕΔɻ {TextureName}_TexelSize - float4ϓϩύςΟɻςΫενϟαΠζͷ৘ใؚ͕·Ε͍ͯΔɻ • x͸ 1.0 /ςΫενϟͷ෯ • y͸ 1.0 /ςΫενϟͷߴ͞ • z͸ςΫενϟͷ෯ • w͸ςΫενϟͷߴ͞ {TextureName}_HDR - float4 ϓϩύςΟɻ࢖༻͞ΕΔ৭ۭؒʹԠͯ͡ɺજࡏతʹHDRʢྫ͑͹RGBMͰΤϯίʔυ ͞ΕͨʣςΫενϟΛσίʔυ͢Δํ๏ʹ͍ͭͯͷ৘ใؚ͕·ΕΔɻ σίʔυʹ༻͍ΔDecodeHDRؔ਺͸ UnityCG.cgincͰఆٛ͞Ε͍ͯΔɻ
  11. γΣʔμʔ͔ΒϓϩύςΟʹΞΫηε ➤ ShaderLabߏจ಺Ͱ͸ɺϓϩύςΟʔͷ஋͸ׅހ [] ͰϓϩύςΟʔωʔϜΛ هड़ͯ͠ࢀর͢Δɻ
 Blend [_SrcBlend] [_DstBlend] ྫ

    Properties { _StencilComp ("Stencil Comparison", Float) = 8 _Stencil ("Stencil ID", Float) = 0 _StencilOp ("Stencil Operation", Float) = 0 _StencilWriteMask ("Stencil Write Mask", Float) = 255 _StencilReadMask ("Stencil Read Mask", Float) = 255 } SubShader { Stencil { Ref [_Stencil] Comp [_StencilComp] Pass [_StencilOp] ReadMask [_StencilReadMask] WriteMask [_StencilWriteMask] } Pass {...} } ྫ
  12. γΣʔμʔ͔ΒϓϩύςΟʹΞΫηε ➤ Cg/HLSLهड़ՕॴͰγΣʔμʔϓϩύςΟʔΛࢀর͢Δ৔߹ɺCg/HLSLม਺Λಉ໊͡લͱϚον ϯά͢ΔλΠϓͰએݴ͢Δඞཁ͕͋Δɻ • Color ͱ Vector ϓϩύςΟʔ͸ɺfloat4ɺhalf4͋Δ͍͸fixed4ม਺ʹରԠ •

    Range ͱ Float ϓϩύςΟʔ͸ɺfloatɺhalf͋Δ͍͸fixedม਺ʹରԠ • Texture ϓϩύςΟʔ͸ɺ௨ৗʢ 2D ʣͷςΫενϟͷ৔߹͸sampler2Dม਺ʹରԠɺ3D ς Ϋενϟͷ৔߹͸sampler3Dม਺ʹରԠ Shader “Sample”{ Properties{ _MyColor ("Some Color", Color) = (1,1,1,1) _MyVector ("Some Vector", Vector) = (0,0,0,0) _MyFloat ("My float", Float) = 0.5 _MyTexture ("Texture", 2D) = "white" {} _MyCubemap ("Cubemap", CUBE) = "" {} } SubShader{ Pass{ …. fixed4 _MyColor; // low precision type is usually enough for colors float4 _MyVector; float _MyFloat; sampler2D _MyTexture; samplerCUBE _MyCubemap; …. } } }
  13. Property Attributes • [HideInInspector]
 
 ϓϩύςΟΛΠϯεϖΫλʔʹදࣔ͠ͳ͍ • [NoScaleOffset]
 
 ςΫενϟͷTiling/OffsetϑΟʔϧυΛΠϯεϖΫλʔʹදࣔ͠ͳ͍ɻ

    • [Normal]
 
 ςΫενϟϓϩύςΟ͕๏ઢϚοϐϯάʹ༻͍Δ͜ͱΛظ଴͍ͯ͠Δ͜ͱΛࣔ͠·͢ɻ • [HDR]
 
 ςΫενϟϓϩύςΟ͕HDRςΫενϟΛظ଴͍ͯ͠Δ͜ͱΛࣔ͠·͢ɻ • [Gamma]
 
 float/vector ϓϩύςΟ͕sRGB஋Ͱ͋Δ͜ͱΛࣔ͠·͢ɻ࢖༻͢ΔΧϥʔεϖʔεʹԠͨ͡ม׵ॲཧ͕ߦΘΕ ΔՄೳੑ͕͋Δ͜ͱΛ͍ࣔͯ͠·͢ɻ • [PerRendererData]
 
 indicates that a texture property will be coming from per-renderer data in the form of a MaterialPropertyBlock. Material inspector changes the texture slot UI for these properties. • [Toggle]
 
 floatͷΠϯεϖΫλʔදࣔΛτάϧʹ͠·͢ɻfloat͸0͔1ͷ஋ΛͱΔΑ͏ʹͳΓ·͢ɻ
 // Will set "_INVERT_ON" shader keyword when set
 [Toggle] _Invert ("Invert?", Float) = 0
 // Will set "ENABLE_FANCY" shader keyword when set
 [Toggle(ENABLE_FANCY)] _Fancy ("Fancy?", Float) = 0 σϑΥϧτͰ࢖༻Ͱ͖Δ΋ͷ(Ұ෦)
  14. Property Attributes • [Enum]
 
 floatϓϩύςΟʔʹϙοϓΞοϓϝχϡʔΛදࣔ͠·͢ɻྻڍମͷܕ໊ɺ·ͨ͸໌ࣔతͳ໊લ / ஋ͷϖΞΛࢦ ఆͰ͖·͢ɻ࠷େͰ 7

    ͭͷ໊લ / ஋ͷϖΞΛࢦఆͰ͖·͢ɻ
 // Blend mode values
 [Enum(UnityEngine.Rendering.BlendMode)] _Blend ("Blend mode", Float) = 1
 // A subset of blend mode values, just "One" (value 1) and "SrcAlpha" (value 5)
 [Enum(One,1,SrcAlpha,5)] _Blend2 ("Blend mode subset", Float) = 1
 • [KeywordEnum]
 
 floatϓϩύςΟʹϙοϓΞοϓϝχϡʔΛදࣔ͠·͢ɻShaderͷΩʔϫʔυͱͯ͠஋Λ࢖༻ग़དྷ·͢ɻΩʔ ϫʔυʹ͸ʮ”ϓϩύςΟ໊”+”_”+”ྻڍ໊”ʯΛ࢖༻͠·͢ɻ࠷େ9ͭͷ஋ΛྻڍͰ͖·͢ɻ
 // Display a popup with None,Add,Multiply choices.
 // Each option will set _OVERLAY_NONE, _OVERLAY_ADD, _OVERLAY_MULTIPLY shader keywords.
 [KeywordEnum(None, Add, Multiply)] _Overlay ("Overlay mode", Float) = 0
 // ...later on in CGPROGRAM code:
 #pragma multi_compile _OVERLAY_NONE, _OVERLAY_ADD, _OVERLAY_MULTIPLY
 // .. σϑΥϧτͰ࢖༻Ͱ͖Δ΋ͷ(Ұ෦)
  15. Property Attributes • [PowerSlider]
 
 Range γΣʔμʔϓϩύςΟʔʹର͢ΔઢܗͰͳ͍൓ԠۂઢͷεϥΠμʔΛදࣔ͠·͢ɻ
 // A slider

    with 3.0 response curve
 [PowerSlider(3.0)] _Shininess ("Shininess", Range (0.01, 1)) = 0.08
 • [Space]
 
 γΣʔμʔϓϩύςΟʔͷલʹਨ௚ํ޲ͷεϖʔεΛ࡞੒͠·͢ɻ
 // Default small amount of space
 [Space] _Prop1 ("Prop1", Float) = 0
 // Large amount of space
 [Space(50)] _Prop2 ("Prop2", Float) = 0
 • [Header]
 
 γΣʔμʔϓϩύςΟʔͷલʹϔομʔςΩετΛ࡞੒͠·͢ɻ
 [Header(A group of things)] _Prop1 ("Prop1", Float) = 0 σϑΥϧτͰ࢖༻Ͱ͖Δ΋ͷ(Ұ෦)
  16. Property Drawer Property Attributeͱͯ͠ར༻ग़དྷΔϨΠΞ΢τଐੑΛࣗ࡞Ͱ͖Δɻ
 ࣗ਎ͷ Drawer Λ࣮૷͢Δͱ͖ɺOnGUI ؔ਺ΛΦʔόʔϥΠυ͢Δɻ // The

    property drawer class should be placed in an editor script, inside a folder called Editor. // Use with "[MyToggle]" before a float shader property class MyToggleDrawer extends MaterialPropertyDrawer { // Draw the property inside the given rect function OnGUI (position : Rect, prop : MaterialProperty, label : String, editor : MaterialEditor) { // Setup EditorGUI.BeginChangeCheck (); var value : boolean = prop.floatValue != 0.0f; EditorGUI.showMixedValue = prop.hasMixedValue; // Show the toggle control value = EditorGUI.Toggle (position, label, value); EditorGUI.showMixedValue = false; if (EditorGUI.EndChangeCheck ()) { // Set the new value if it has changed prop.floatValue = value ? 1.0f : 0.0f; } } }
  17. SubShaderͱ͸ ➤ γΣʔμʔ̍ͭͷߏ੒୯Ґ ➤ αϒγΣʔμʔͰ͸ϨϯμϦϯάύεͷҰཡΛఆٛ͠ɺ೚ҙͷΦϓγϣϯͱ ͯ͢͠΂ͯͷύεʹڞ௨ͷ State ʢঢ়ଶʣΛઃఆ͢Δɻ ➤ ύεఆٛͰهड़Ͱ͖ΔϨϯμϦϯάεςʔτ͸αϒγΣʔμʔͷهड़ϒϩο

    ΫͰ΋ॻ͘͜ͱ͕Ͱ͖Δɻ͜ΕʹΑΓ͢΂ͯͷύε͕”ڞ༗ͷ” State (ঢ়ଶ) Λ࢖༻͢Δ͜ͱ͕Ͱ͖Δ(྆ํʹهड़ͨ͠৔߹͸Pass಺ͷState͕༏ઌ)ɻ ➤ ֤ύεͷఆٛ͸ Regular PassɺUse PassɺGrab Pass ͷ͍ͣΕ͔ɻ ➤ SubShader಺ͷTag ΛઃఆͰ͖Δɻ
  18. SubShaderߏจ Subshader { [Tags] [CommonState] Passdef [Passdef ...] } ߏจ

    ➤ Tags • SubShader಺ͷTagsɻͲͷΑ͏ʹͯ͠ϨϯμϦϯάΤϯδϯͰϨϯμϦϯά͢Δ ͔Λࢦఆ͢Δɻ ➤ CommonState • ͞·͟·ͳάϥϑΟοΫεϋʔυ΢ΣΞͷ State ʢঢ়ଶʣΛઃఆͰ͖ΔɻPass Ͱهड़Ͱ͖ΔεςʔτϝϯτͱಉҰ(εςʔτ͕༗ޮͳείʔϓ͕ҧ͏)ɻ ➤ Passdef • Regular PassɺUse Passɺ·ͨ͸ Grab Pass ͷ͍ͣΕ͔
  19. SubShader಺ͷTags SubShader͸TagsΛ࢖༻͍ͯͭ͠ͲͷΑ͏ʹͯ͠ϨϯμϦϯάΤϯδϯʹΑ ΓϨϯμϦϯά͢΂͖͔ΛࢦఆͰ͖Δɻ Tagsͷߏจ Tags { "TagName1" = "Value1" "TagName2"

    = "Value2" } λάͷ TagName1 ͕஋ͱͯ͠ Value1ɺλάͷ TagName2 ͕஋ͱͯ͠ Value2 Ͱ͋Δ͜ͱΛࢦఆ͍ͯ͠ Δɻλά͸޷͖ͳ͚ͩࢦఆͰ͖Δɻλά͸جຊతʹ Key-Value ϖΞɻSubShaderͷதͰλά͸ɺ SubShader࣮ߦॱং΍ଞͷύϥϝʔλʔΛ൑ఆ͢Δͷʹ࢖༻͢Δɻ Tags͸SubShaderɺPass಺ͷͲͪΒʹͰ΋ఆٛ͢Δ͜ͱ͕Ͱ͖Δ͕ɺ࣍ͷλά͸ Unity ʹΑΓ ೝࣝ͞ΕɺPass಺ͷTagsͰͳ͘ඞͣSubShaderηΫγϣϯͷதʹఆٛ͢Δɻ
 • Queue λά • RenderType λά • DisableBatching λά • ForceNoShadowCasting λά • IgnoreProjector λά • CanUseSpriteAtlas λά • RenderType λά
  20. SubShader಺ͷTags Queueλά QueueʹΑΓΦϒδΣΫτΛඳը͢Δॱ൪ΛࢦఆͰ͖ΔɻγΣʔμʔ͸ΦϒδΣΫτ͕ଐ͢ΔϨϯμϦϯά ΩϡʔΛ൑ఆ͠ɺॱʹϨϯμϦϯάΛߦ͍·͢ɻྫ͑͹ɺಁ໌ͳΦϒδΣΫτ͸ෆಁ໌ͳΦϒδΣΫτͷޙ ʹඳը͞ΕΔ΂͖ͳͷͰQueueλάͰॱ൪Λࢦఆ͢Δඞཁ͕͋Γ·͢ɻ ఆٛࡁΈͷϨϯμϦϯάΩϡʔ ➤ Background
 
 ͜ͷϨϯμϦϯάΩϡʔ͸ଞΑΓલʹϨϯμϦϯά͞ΕΔɻSkybox

    ͳͲʹ࢖༻͞ΕΔɻ ➤ Geometry (default)
 
 ΄ͱΜͲͷΦϒδΣΫτʹ࢖༻͞ΕΔɻෆಁ໌ͳδΦϝτϦ͸͜ͷΩϡʔΛ࢖༻͢Δɻ ➤ AlphaTest
 
 ΞϧϑΝςετ͢Δܗঢ়Ͱ͜ͷΩϡʔΛ࢖༻͢ΔɻGeometry ͱ͸ผͷΩϡʔͰ͋Δͷ͸ΞϧϑΝςετ ͷΦϒδΣΫτΛ͢΂ͯͷෆಁ໌ͷΦϒδΣΫτͷޙʹඳը͢Δ΄͏͕ޮ཰తͰ͋ΔͨΊɻ ➤ Transparent
 
 ͜ͷϨϯμϦϯάΩϡʔ͸Geometry ͱAlphaTest ͷޙʹɺޙΖʹ͋Δ΋ͷ͔Βઌʹॱ൪ͰϨϯμϦϯά͞ ΕΔɻΞϧϑΝϒϨϯσΟϯά͢Δ΋ͷʢ͢ͳΘͪɺσϓεόοϑΝʹॻ͖ࠐΈ͠ͳ͍γΣʔμʔʣ͸͢ ΂ͯɺ͜͜ʹ͋Δ΂͖ʢΨϥεɺύʔςΟΫϧΤϑΣΫτʣɻ ➤ Overlay
 
 ͜ͷϨϯμϦϯάΩϡʔ͸ΦʔόʔϨΠΤϑΣΫτͷͨΊͷ΋ͷɻ࠷ޙʹϨϯμϦϯά͢Δ΋ͷ͸͜͜ʹ ͋Δ΂͖ʢྫ͑͹ɺϨϯζϑϨΞʣɻ
  21. SubShader಺ͷTags Queueλά ಛผͳ༻్ʹ͓͍ͯɺؒʹΩϡʔΛ࢖༻͢Δ͜ͱ͕Ͱ͖Δɻ֤Ωϡʔ͸ ಺෦Ͱ੔਺ΠϯσοΫεʹΑΓදݱ͞ΕɺBackground ͸ 1000ɺ Geometry ͸ 2000ɺAlphaTest ͸

    2450ɺTransparent ͸ 3000ɺͦͯ͠ Overlay ͸ 4000 ͱ͍͏੔਺஋Ͱදݱ͞ΕΔɻ΋͠γΣʔμʔ͕ͭ͗ͷΑ ͏ͳΩϡʔΛ࢖༻͢Δ৔߹ Tags { "Queue" = "Geometry+1" } ͜ΕʹΑΓΦϒδΣΫτ͸͢΂ͯͷෆಁ໌ͷΦϒδΣΫτͷޙʹϨϯμϦ ϯά͞ΕΔ͕ɺϨϯμϦϯάΩϡʔ͕ 2001 ʢGeometry ʹ 1 ΛՃࢉʣͷͨ ΊɺಁաΦϒδΣΫτΑΓઌʹϨϯμϦϯά͞ΕΔɻ
  22. SubShader಺ͷTags DisableBatchingλά ͍͘Β͔ͷγΣʔμʔ(େ఍͸ΦϒδΣΫτεϖʔεͰ௖఺Λมܗͤ͞Δ΋ ͷ)͸ɺυϩʔίʔϧόονϯά Λ࢖༻͍ͯ͠Δͱɺಈ࡞͠ͳ͍ɻ
 
 υϩʔίʔϧόονϯά͸ɺ͢΂ͯͷδΦϝτϦΛϫʔϧυεϖʔεʹม ׵͢ΔͷͰɺ“object space” ͕ࣦΘΕΔͨΊͰ͋Δɻ


    DisableBatching λάΛ࢖ͬͯυϩʔίʔϧόονϯάͷ༗ޮ/ແޮΛࢦࣔ Ͱ͖ΔɻઃఆՄೳͳͷ͸ҎԼ3ͭɻ ➤ True
 
 ͜ͷγΣʔμʔͰ͸ৗʹόονϯάΛແޮʹ͢Δɻ ➤ False
 
 όονϯάΛແޮʹ͠ͳ͍ɻσϑΥϧτ͸͜Εɻ ➤ LODFading
 
 LOD fading ͕ΞΫςΟϒͳ࣌͸όονϯάΛແޮʹ͢Δɻେ఍͸थ໦ Ͱ࢖ΘΕΔɻ
  23. Fallback ͢΂ͯͷ Subshader ͷࢼߦޙɺFallback Λఆٛ͢Δ͜ͱ͕Ͱ͖Δɻجຊత ͳҙຯͱͯ͠͸“͜ͷϋʔυ΢ΣΞͰ࣮ߦͰ͖ΔαϒγΣʔμʔ͕ͳ͍৔ ߹ɺผͷγΣʔμʔ͔Βࢼ͢”ɻ Fallback "name" ߏจ

    nameͰࢦఆ͞ΕͨShaderΛFallbackͱͯ͠ఆٛ͢Δɻ OffΛࢦఆ͢Δ͜ͱ΋Ͱ͖Δɻ Fallback Off ͜ͷ৔߹͸໌ࣔతʹ Fallback ͕ͳ͘ɺͨͱ͑͜ͷϋʔυ΢ΣΞͰ࣮ߦͰ ͖ΔαϒγΣʔμʔ͕ͳ͔ͬͨͱͯ͠΋ɺܯࠂΛग़ྗ͠ͳ͍ͱ͍͏ҙຯ ʹͳΔɻ
  24. Passͷ֓ཁ ➤ Pass ϒϩοΫ͸ΦϒδΣΫτܗঢ়ΛҰճͷΈϨϯμϦϯά͢ΔΑ͏ʹ͢Δɻ ➤ ෳ਺ύεͷϨϯμϦϯάΛ͢Δ৔߹͸ɺ̍ͭͷSubShaderʹෳ਺ͷPassΛهड़͢Δɻ ➤ PassϒϩοΫ಺Ͱར༻Ͱ͖Δεςʔτ͸ϨϯμϦϯάεςʔτΛࢀরɻ ➤ PassϒϩοΫ಺Ͱར༻Ͱ͖Δλά͸Pass಺ͷTagsΛࢀরɻ

    ➤ PassϒϩοΫ಺Ͱར༻Ͱ͖Δεςϯγϧʹ͍ͭͯ͸StencilΛࢀরɻ ➤ PassϒϩοΫ಺Ͱར༻Ͱ͖ΔϒϨϯσΟϯάʹ͍ͭͯ͸BlendingΛࢀরɻ ➤ Render Setup͸ݻఆؔ਺γΣʔμʔ/αʔϑΣεγΣʔμʔ/௖఺ɾϑϥάϝϯτγΣ ʔμʔΛࢀরɻ Pass { [Name and Tags] [RenderSetup] } ߏจ
  25. GrabPass ➤ GrabPass͸ಛผͷύελΠϓͰɺGrabPass࣮ߦ࣌ʹطʹϨϯμϦϯά͞Ε͍ͯΔը૾ΛςΫενϟͱ ͯ͠࢖༻Ͱ͖ΔΑ͏ʹ͢Δ(ݱࡏ·ͰͷϨϯμϦϯά݁ՌΛ௫ΈऔΔ)ɻ ➤ ௫ΈऔͬͨςΫενϟ͸ޙଓͷPassͰར༻͢Δ͜ͱ͕Ͱ͖Δɻ ➤ ͍ͣΕ͔ͷॻࣜΛར༻͢Δɻ • GrabPass

    { } ͱࢦఆ͠ɺ୯ʹݱࡏͷը໘ίϯςϯπΛςΫενϟ಺ʹ௫ΈͱΔɻ͜ͷςΫενϟ ͸ޙଓͷύεͰ _GrabTexture ͰΞΫηε͢Δ͜ͱ͕Ͱ͖Δɻ஫ҙɿ͜ͷܗࣜͷGrap Pass͸ը໘ ͷ௫ΈͱΓॲཧΛ࢖༻͢Δ֤ΦϒδΣΫτ͝ͱʹߦ͏ɻྫ͑͹͜ͷܗࣜͷGrabPassΛ࣋ͭಉ͡ ϚςϦΞϧΛద༻ͨ͠2ͭͷΦϒδΣΫτΛϨϯμϦϯά͢Δ৔߹ɺ̎ͭͷ_GrabTexture͕ੜ੒ ͞ΕΔ͜ͱʹͳΔɻ • GrabPass { "TextureName" } ʹΑΓը໘ίϯςϯπΛςΫενϟ಺ʹ௫ΈऔΔɻϑϨʔϜ͝ͱʹ 1౓͚ͩɺࢦఆͷςΫενϟ໊Λ࢖༻͢Δ࠷ॳͷΦϒδΣΫτ෼ʹͷΈ࣮ࢪ͢ΔɻςΫενϟ͸ ޙଓͷύεͰࢦఆͷςΫενϟ໊ͰΞΫηε͢Δ͜ͱ͕Ͱ͖ΔɻγʔϯͰ Grab Pass Λ࢖༻͢Δ ΦϒδΣΫτ͕ෳ਺͋Δ৔߹ʹޮ཰తͳํ๏ɻ ➤ GrabPass಺Ͱ͸Name΍Tags ίϚϯυΛ࢖༻͢Δ͜ͱ͕Ͱ͖ΔɻRegular PassͰͷར༻ͱಉ͡༻్ɻ
  26. GrabPassͷྫ Shader "GrabPassInvert" { SubShader { // શͯͷෆಁ໌ͳܗঢ়ͷޙʹɺࣗ਎Λඳը͠·͢ Tags {

    "Queue" = "Transparent" } // ΦϒδΣΫτΑΓޙΖͷγʔϯΛ_GrabTextureʹ௫ΈऔΓ·͢ GrabPass { } // ্Ͱੜ੒ͨ͠ςΫενϟͰΦϒδΣΫτΛϨϯμϦϯά͠ɺΧϥʔΛ൓స͠·͢ɻ Pass { SetTexture [_GrabTexture] { combine one-texture } } } } લʹϨϯμϦϯά͞ΕͨΧϥʔΛ൓స͢Δ ͨΊʹGrabPassΛར༻͢Δྫ
  27. ݻఆؔ਺γΣʔμʔͱ͸ ➤ ݻఆؔ਺γΣʔμʔ͸ɺݹ͍ϋʔυ΢ΣΞͷͨΊʹهड़͢Δɻ ➤ ݻఆؔ਺γΣʔμʔ͸ϑϥάϝϯτγΣʔμ·ͨ͸αʔϑΣΠ εγΣʔμͷޙͷ n ൪໨ͷ fallback ͱͯ͠هड़ͯ͠ɺݹ͍ϋʔ

    υ΢ΣΞͰ࣮ߦͨ͠ͱ͖΋ϨϯμϦϯάΛߦ͑ΔΑ͏ʹ͢Δɻ ➤ ݻఆؔ਺γΣʔμʔ͸͢΂ͯ ShaderLab Ͱॻ͘ɻ ➤ ݻఆؔ਺γΣʔμʔ͸ݻఆػೳͱݺ͹ΕΔίϚϯυͷΈͰه ड़͞ΕͨγΣʔμʔͷ͜ͱɻ ➤ ݻఆػೳ͸ଞͷγΣʔμʔͰ΋ར༻Ͱ͖Δɻ
  28. ݻఆػೳϥΠςΟϯάͱϚςϦΞϧ ➤ Lighting On | Off
 • ϚςϦΞϧϒϩοΫͰఆٛ͞ΕͨઃఆͰɺ೚ҙͷޮՌΛ࣋ͭΑ͏ʹ͢Δʹ͸ɺLighting ON ί

    ϚϯυΛ࢖༻ͯ͠ϥΠςΟϯάΛ༗ޮʹ͢ΔɻϥΠςΟϯά͕Φϑʹͳ͍ͬͯΔ৔߹͸ɺ৭͕ Color ίϚϯυ͔Β௚઀औಘ͞ΕΔɻ ➤ Material { Material Block }
 • Diffuse color: ΦϒδΣΫτͷϕʔεΧϥʔɻ • Ambient color: ΞϯϏΤϯτ(؀ڥ)ޫ͕ΦϒδΣΫτʹরࣹͨ͠ͱ͖ͷ৭ɻ • Specular color: ΦϒδΣΫτͷεϖΩϡϥ(ڸ໘൓ࣹޫͷ৭)ɻ • Shininess number: 0 ͱ 1 ͷؒͷϋΠϥΠτͷγϟʔϓωε஋ɻ0 Ͱ֦ࢄর໌ͷΑ͏ͳͨ͘͞Μ ͷڊେͳϋΠϥΠτΛಘɺ1 Ͱখ͞ͳൗ఺ΛಘΔɻ • Emission color: ޫ͕౰͍ͨͬͯͳ͍ΦϒδΣΫτͷ৭ɻ ➤ SeparateSpecular On | Off
 • εϖΩϡϥʔϥΠςΟϯά͕γΣʔμʔύεͷ࠷ޙʹ௥Ճ͞ΕΔɻεϖΩϡϥʔϥΠςΟϯά͸ ςΫενϟϦϯάͷӨڹΛड͚ͳ͍ɻLighting ON ͕࢖༻͞Ε͍ͯΔ৔߹ʹͷΈޮՌ͕͋Δɻ ➤ Color Color-value
 • ΦϒδΣΫτͷιϦουΧϥʔΛઃఆɻ৭͸ׅހͰׅͬͨ4 ͭͷ RGBA ஋ɺ·ͨ͸֯Χοίͷ தͷ৭ͷϓϩύςΟʔ໊ͷ͍ͣΕ͔ɻ ➤ ColorMaterial AmbientAndDiffuse | Emission
 • ϚςϦΞϧʹͯઃఆ͞Εͨ৭ͷ୅ΘΓʹ௖఺৭Λ࢖༻͢ΔɻAmbientAndDiffuse͸ɺϚςϦΞ ϧͷ Ambient ͓Αͼ Diffuse ஋Λஔ͖׵͑ΔɻEmission͸ϚςϦΞϧͷ Emission ஋Λஔ͖׵ ͑Δɻ
  29. ݻఆػೳϑΥάͷߏจ Fog {Fog Commands} ߏจ Fog Commands ➤ Mode Off

    | Global | Linear | Exp | Exp2
 • ϑΥάϞʔυΛఆٛ͢ΔɻσϑΥϧτ͸ Global ͰɺϑΥά͕ϨϯμϦ ϯάઃఆͰ༗ޮԽ͞Ε͍ͯΔ͔ʹΑΓ Off ͔ Exp2 ʹม׵͞ΕΔɻ ➤ Color ColorValue
 • ϑΥάͷ৭Λઃఆ͢Δɻ ➤ Density FloatValue
 • Exp ʢࢦ਺ʣϞʔυͷϑΥάͷີ౓Λઃఆ͢Δɻ ➤ Range FloatValue, FloatValue
 • Linear ʢϦχΞʣϞʔυͷϑΥάͷ Near ͓Αͼ Far ͷൣғΛઃఆ͢ Δɻ
  30. ݻఆػೳςΫενϟ߹੒ͷߏจ Combine ➤ combine src1 * src2 : src1 ͱ

    src2 Λ৐ࢉɻ ➤ combine src1 + src2 : src1 ͱ src2 ΛՃࢉɻ ➤ combine src1 - src2 : src2 Λ src1 ΑΓݮࢉɻ ➤ combine src1 lerp ( src2 ) src3 : src3 ͱ src1 ͷิؒΛ src2 ͷΞϧϑΝΛ༻͍ͯߦ͏ɻิؒͷ޲͖͕ٯͰ ͋Δ͜ͱʹ஫ҙɻΞϧϑΝ͕ 1 ͷͱ͖ʹ src1ɺΞϧϑΝ͕ 0 ͷͱ͖ʹ src3 ͱͳΔɻ ➤ combine src1 * src2 + src3 : src1 Λ src2 ͷΞϧϑΝͰ৐ࢉ͠ src3 ΛՃࢉɻ ͢΂ͯͷ src ϓϩύςΟʔ͸ previousɺconstantɺprimaryɺ͋Δ͍͸ texture ͷ͍ͣΕ͔Ͱ͢ɻ previous ͸௚લͷ SetTexture ͷ݁Ռɻ primary ͸ lighting calculation ͷ৭·ͨ͸ bound Ͱ͋Δ৔߹͸௖఺Χϥʔɻ texture ͸ SetTexture Ͱࢦఆ͞Εͨ [TextureName] ɻ constant ͸ constantColor Ͱࢦఆͨ͠৭ɻ म০ࢠɿ ্Ͱࣔͨ͠ܭࢉࣜ͸ Double ·ͨ͸ Quad ΩʔϫʔυΛ೚ҙͰࢦఆ͢Δ͜ͱͰ݁ՌͷΧϥʔΛ 2 ഒ·ͨ͸ 4 ഒ ໌Δ͘Ͱ͖Δɻ lerp จݴΛআ͍ͨɺ͢΂ͯͷ src ϓϩύςΟʔ͸ɺ೚ҙͰ one - Λࢦఆ͢Δ͜ͱͰɺ݁Ռͷ৭Λωήʔτ͢Δ ͜ͱ͕Ͱ͖Δɻ ͢΂ͯͷ src ϓϩύςΟʔ͸ɺalpha Λࢦఆ͢Δ͜ͱͰΞϧϑΝͷΈΛऔಘͤ͞Δ͜ͱ͕Ͱ͖Δɻ
  31. 2 ͭͷςΫενϟͷΞϧϑΝϒϨϯσΟϯά Shader "Examples/2 Alpha Blended Textures" { Properties {

    _MainTex ("Base (RGB)", 2D) = "white" {} _BlendTex ("Alpha Blended (RGBA) ", 2D) = "white" {} } SubShader { Pass { // Apply base texture SetTexture [_MainTex] { combine texture } // Blend in the alpha texture using the lerp operator SetTexture [_BlendTex] { combine texture lerp (texture) previous } } } }
  32. ΞϧϑΝ੍ޚͨࣗ͠ݾর໌ Shader "Examples/Self-Illumination" { Properties { _MainTex ("Base (RGB) Self-Illumination

    (A)", 2D) = "white" {} } SubShader { Pass { // Set up basic white vertex lighting Material { Diffuse (1,1,1,1) Ambient (1,1,1,1) } Lighting On // Use texture alpha to blend up to white (= full illumination) SetTexture [_MainTex] { constantColor (1,1,1,1) combine constant lerp(texture) previous } // Multiply in texture SetTexture [_MainTex] { combine previous * texture } } } }
  33. ΞϧϑΝ੍ޚͨࣗ͠ݾর໌(৭ࢦఆ) Shader "Examples/Self-Illumination 2" { Properties { _IlluminCol ("Self-Illumination color

    (RGB)", Color) = (1,1,1,1) _MainTex ("Base (RGB) Self-Illumination (A)", 2D) = "white" {} } SubShader { Pass { // Set up basic white vertex lighting Material { Diffuse (1,1,1,1) Ambient (1,1,1,1) } Lighting On // Use texture alpha to blend up to white (= full illumination) SetTexture [_MainTex] { // Pull the color property into this blender constantColor [_IlluminCol] // And use the texture's alpha to blend between it and // vertex color combine constant lerp(texture) previous } // Multiply in texture SetTexture [_MainTex] { combine previous * texture } } } }
  34. ௖఺γΣʔμʔͷϥΠςΟϯάϓϩύςΟʔΛ׆༻ Shader "Examples/Self-Illumination 3" { Properties { _IlluminCol ("Self-Illumination color

    (RGB)", Color) = (1,1,1,1) _Color ("Main Color", Color) = (1,1,1,0) _SpecColor ("Spec Color", Color) = (1,1,1,1) _Emission ("Emmisive Color", Color) = (0,0,0,0) _Shininess ("Shininess", Range (0.01, 1)) = 0.7 _MainTex ("Base (RGB)", 2D) = "white" {} } SubShader { Pass { // Set up basic vertex lighting Material { Diffuse [_Color] Ambient [_Color] Shininess [_Shininess] Specular [_SpecColor] Emission [_Emission] } Lighting On // Use texture alpha to blend up to white (= full illumination) SetTexture [_MainTex] { constantColor [_IlluminCol] combine constant lerp(texture) previous } // Multiply in texture SetTexture [_MainTex] { combine previous * texture } } } }
  35. αʔϑΣΠεγΣʔμʔͱ͸ ➤ ௖఺γΣʔμʔͱ Fragment γΣʔμʔͷϓϩάϥϛϯά Λ࢖༻ ͨ͠৔߹ΑΓ΋ɺϦοτγΣʔμʔͷهड़Λίʔυੜ੒ʹΑͬ ͯང͔ʹ؆୯ʹ͢Δख๏ɻ ➤ Surface

    Shaders͸खಈͰهड़͢ΔඞཁͳΔ܁Γฦ͠ίʔυΛ͢ ΂ͯੜ੒͍ͯ͠Δ͚ͩɻ ➤ Cg/HLSL ͰγΣʔμʔίʔυΛهड़͢Δɻ ➤ Surface Shaders ίϯύΠϥ͸ɺͲΜͳೖྗ͕ඞཁͰɺͲΜͳग़ ྗΛग़͔͢ͱ͍͏͜ͱΛܾఆ͠ɺ௖఺γΣʔμʔͱϑϥάϝϯ τγΣʔμʔ Λੜ੒͢ΔɻForward ϨϯμϦϯά΍ Deferred Ϩ ϯμϦϯάΛॲཧ͢ΔϨϯμϦϯάύε΋ੜ੒͢Δɻ
  36. αʔϑΣΠεγΣʔμʔίϯύΠϧ໋ྩ #pragma surface surfaceFunction lightModel [optionalparams] ➤ surfaceFunction
 • Cg

    Ͱهड़͞ΕͨαʔϑΣΠεγΣʔμʔͷؔ਺໊Λࢦఆ͢Δɻ
 surfΛࢦఆͨ͠৔߹ɺvoid surf(Input IN, inout <SurfaceOutput> o) ͱ͍͏ؔ਺Λ࣋ͭ͜ͱΛҙ ຯ͢Δɻ͜͜ͷ Input ͸લ΋ͬͯఆٛ͢Δඞཁͷ͋Δೖྗߏ଄ମɻInput ͸ɺsurface ؔ਺͕ඞཁ ͱ͢ΔςΫενϟ࠲ඪͱ௥Ճͷࣗಈม਺ΛؚΉඞཁ͕͋Δ(ৄࡉ͸ޙड़)ɻ<SurfaceOutput>͸ αʔϑΣΠεγΣʔμʔ͕ఆ͍ٛͯ͠Δೖग़ྗߏ଄ମɻ࢖༻͢ΔlightModelʹΑͬͯ࢖༻͢΂͖ ೖग़ྗߏ଄ମ͕มΘΔ(ͭ·Γ<SurfaceOutput>͸਺छྨ͋Δɻৄࡉ͸ޙड़)ɻ ➤ lightModel
 • ࢖༻͢ΔϥΠςΟϯάϞσϧɻࣗલͷϥΠςΟϯάϞσϧΛهड़͢Δํ๏ʹؔͯ͠͸ɺαʔϑΣ ΠεγΣʔμʔͰͷΧελϜϥΠςΟϯάϞσϧ Λࢀরɻ
 ✴ Standard 
 ✴ ۚଐΛදݱ͢ΔϥΠςΟϯάϞσϧΛར༻͢ΔɻαʔϑΣΠεγΣʔμʔͷೖग़ྗߏ଄ମ ʹ͸SurfaceOutputStandardΛ࢖͏͜ͱɻ ✴ StandardSpecular 
 ✴ ڸ໘Λදݱ͢ΔϥΠςΟϯάϞσϧΛར༻͢ΔɻαʔϑΣΠεγΣʔμʔͷೖग़ྗߏ଄ମ ʹ͸SurfaceOutputStandardSpecularΛ࢖͏͜ͱɻ ✴ Lambert
 ✴ Lambert (σϑϡʔζ)Λར༻͢Δɻ ✴ BlinnPhong
 ✴ BlinnPhong (εϖΩϡϥ) Λར༻͢Δɻ
  37. αʔϑΣΠεγΣʔμʔίϯύΠϧ໋ྩ #pragma surface surfaceFunction lightModel [optionalparams] ➤ [optionalparams]
 • alpha/decalΦϓγϣϯ

    ★ alpha or alpha:auto
 ✴ ΞϧϑΝςετΛߦ͏Α͏ࢦఆ͢Δɻ ★ alpha:fade
 ✴ ैདྷͷΞϧϑΝϒϨϯσΟϯά(ΦϒδΣΫτͷϑΣʔυΛߦ͏Α͏ͳϒϨϯυϞʔυ)Λ ࢖༻͢Δɻ ★ alpha:premul
 ✴ ΞϧϑΝϒϨϯσΟϯάʹϓϨϚϧνϓϥΠυ ϒϨϯσΟϯάΛར༻͢Δɻ ★ alphatest:VariableFloat
 ✴ ΞϧϑΝςετΛ༗ޮʹ͢ΔɻςετͰΧοτ͢ΔΞϧϑΝͷᮢ஋ΛVariableFloatʹࢦ ఆ͢Δɻ ★ keepalpha
 ✴ ೖྗ͞ΕͨΞϧϑΝ஋Λग़ྗʹΩʔϓ͢Δɻ ★ decal:add
 ✴ Additive decal shader (e.g. terrain AddPass). This is meant for objects that lie atop of 
 
 other surfaces, and use additive blending. See Surface Shader Examples
 ★ decal:blend
 ✴ Semitransparent decal shader. This is meant for objects that lie atop of other surfaces, 
 
 and use alpha blending. See Surface Shader Examples ಁ໌౓ͱΞϧϑΝςετ ͸ɺσΟϨΫςΟϒ alpha ͓Αͼ alphatest ʹΑ੍ͬͯޚ͞ΕΔɻ ಁ໌౓ʹ͸௨ৗɺೋछྨ͋Δɻͻͱͭ͸ैདྷܕ ͷΞϧϑΝ ϒϨϯσΟϯάʢΦϒδΣΫτͷϑΣʔυΞ΢τʹ࢖ΘΕΔʣɻ ΋͏ͻͱͭ͸ɺΑΓ෺ཧతʹઆಘྗͷ͋ΔʮϓϨϚϧνϓϥ Πυ ϒϨϯσΟϯάʯʢ൒ಁ໌ͷαʔϑΣʔε͕ద੾ͳڸ໘൓ࣹΛอͯΔΑ͏ʹ͢Δʣɻ ※
  38. αʔϑΣΠεγΣʔμʔίϯύΠϧ໋ྩ #pragma surface surfaceFunction lightModel [optionalparams] ➤ [optionalparams]
 • ௥Ճؔ਺Φϓγϣϯ

    ★ vertex:VertexFunction
 ✴ ௖఺γΣʔμʔΛఆ͍ٛͨ͠৔߹ʹࢦఆ͢ΔɻVertexFunction͸௖఺γΣʔμʔؔ਺໊Λ ࢦఆ͢Δɻ௖఺γΣʔμʔؔ਺ͷҾ਺ʹ͸inout appdata_fullߏ଄ମΛ࢖͏ඞཁ͕͋Δɻ ೚ҙͰୈೋҾ਺ʹout InputܕΛར༻Ͱ͖Δɻ ★ finalcolor:ColorFunction
 ✴ ࠷ऴతͳϐΫηϧ৭Λௐઅ͢ΔͨΊͷؔ਺Λఆ͍ٛͨ͠৔߹ʹࢦఆ͢ΔɻColorFunction ʹ͸ఆٛ͢Δؔ਺໊Λࢦఆ͢Δɻ͜ͷؔ਺ͷҾ਺ʹ͸(Input IN, SurfaceOutput o, inout fixed4 color)Λར༻͢Δ(ม਺໊͸ԿͰ΋ྑ͍)ɻ࠷ऴతͳग़ྗ৭Λfixed4 colorʹ֨ೲ͢ Δ͜ͱͰϐΫηϧ৭Λௐ੔Ͱ͖Δɻ ★ finalgbuffer:ColorFunction
 ✴ Custom deferred path for altering gbuffer content.
 ★ finalprepass:ColorFunction
 ✴ Custom prepass base path.
  39. αʔϑΣΠεγΣʔμʔίϯύΠϧ໋ྩ #pragma surface surfaceFunction lightModel [optionalparams] ➤ [optionalparams]
 • ӨͱςοηϨʔγϣϯʹؔ͢ΔΦϓγϣϯ

    ★ addshadow
 ✴ γϟυ΢Ωϟελ͓ΑͼίϨΫλύεΛ௥Ճ͢Δɻ௨ৗɺΧελϜ௖఺ฤूͰ࢖༻͞Ε ΔͷͰɺγϟυ΢ΩϟεςΟϯά΋खଓ͖త௖఺ΞχϝʔγϣϯΛऔಘ͠·͢ɻଟ͘ͷ৔ ߹ɺγΣʔμʔ͸ϑΥʔϧόοΫ͔Βγϟυ΢ΩϟελʔύεΛ࢖༻͢Δ͜ͱ͕Ͱ͖Δͨ Ίɺಛผͳγϟυ΢ͷॲཧΛߦ͏ඞཁ͸ͳ͍ɻ ★ fullforwardshadows
 ✴ Support all light shadow types in Forward rendering path. By default shaders only 
 
 support shadows from one directional light in forward rendering (to save on internal 
 
 shader variant count). If you need point or spot light shadows in forward rendering, use
 
 this directive.
 ★ tessellate:TessFunction
 ✴ CDX11 GPU ςοηϨʔγϣϯΛ࢖͏ɻৄ͘͠͸ DX11 / OpenGL ίΞςοηϨʔγϣ ϯͷαʔϑΣʔεγΣʔμʔΛࢀরɻ

  40. αʔϑΣΠεγΣʔμʔίϯύΠϧ໋ྩ #pragma surface surfaceFunction lightModel [optionalparams] ➤ [optionalparams]
 • ίʔυੜ੒໋ྩΦϓγϣϯ

    ★ exclude_path:deferred, exclude_path:forward, exclude_path:prepass
 ✴ ͦΕͧΕDeferred ShadingɺForward RenderingɺLegacy Deferred ShadingͷPassੜ੒Λແޮʹ͢ ΔίϚϯυɻ ★ noshadow
 ✴ ͜ͷγΣʔμʔͰγϟυ΢ͷαϙʔτΛແޮʹ͢Δɻ
 ★ noambient
 ✴ ΞϯϏΤϯτϥΠτ΍ϥΠτϓϩʔϒΛద༻͠ͳ͍ɻ ★ novertexlights
 ✴ Forward ϨϯμϦϯάͰɺϥΠτϓϩʔϒ΍௖఺͝ͱͷϥΠτΛద༻͠ͳ͍ɻ ★ nolightmap
 ✴ ϥΠτϚοϓͷαϙʔτΛແޮʹ͢Δɻ ★ nodynlightmap
 ✴ ϥϯλΠϜͷμΠφϛοΫͳάϩʔόϧΠϧϛωʔγϣϯͷαϙʔτΛແޮʹ͢Δɻ ★ nodirlightmap
 ✴ σΟϨΫγϣφϧϥΠτϚοϓΛແޮʹ͢Δɻ ★ nofog
 ✴ ϑΥάͷαϙʔτΛແޮʹ͢Δɻ ★ nometa
 ✴ Does not generate a “meta” pass (that’s used by lightmapping & dynamic global illumination to extract surface information).
 ★ noforwardadd
 ✴ ɻ Forward ϨϯμϦϯά௥ՃύεΛແޮʹ͢Δɻ͜ΕʹΑΓɺγΣʔμʔ͸׬શͳσΟϨΫγϣφ ϧϥΠταϙʔτ͠ɺͦͷଞ͢΂ͯͷϥΠτ͸ɺ௖఺͝ͱ/SH Ͱܭࢉ͞ΕΔɻ
  41. ೖྗߏ଄ମInput ҎԼͷ஋͕ೖྗߏ଄ମInputʹೖྗͰ͖Δɻ ➤ float2 uvNXXX
 • PropertiesͰࢦఆͨ͠ςΫενϟͷςΫενϟ࠲ඪɻXXX͸ม਺໊ʹஔ͖׵͑ΔɻNʹ͸Կݸ໨ͷuvม਺ͳͷ͔ͱ͍͏஋͕ೖΔ(1 ݸ໨ͷ৔߹͸ͳ͠ɺ2ݸ໨͸uv2XXXɺ…)ɻ ➤ float3

    viewDir
 • Ϗϡʔํ޲ɻࢹࠩޮՌɺϦϜϥΠςΟϯάͳͲͷܭࢉʹ࢖༻Ͱ͖Δɻ ➤ float4 XXXColor
 • ิؒ͞Εͨ௖఺͝ͱͷ৭ɻXXXʹ͸PropertiesͰࢦఆͨ͠ม਺໊Λ͚ͭΔ͜ͱ͕Ͱ͖Δɻ ➤ float4 screenPos
 • εΫϦʔϯ࠲ඪͷҐஔɻ ➤ float3 worldPos
 • ϫʔϧυ࠲ඪͷҐஔɻ ➤ float3 worldRefl
 • Surface Shaders ͕ o.Normal ʹॻ͖ࠐ·ͳ͍৔߹ ͷϫʔϧυ࠲ඪͷ൓ࣹϕΫτϧɻ ➤ float3 worldNormal
 • Surface Shaders ͕ o.Normal ʹॻ͖ࠐ·ͳ͍৔߹ ͷϫʔϧυ࠲ඪͷ๏ઢϕΫτϧɻ ➤ float3 worldRefl; INTERNAL_DATA
 • Surface Shaders ͕ o.Normal ʹॻ͖ࠐΉ৔߹ ͷϫʔϧυ࠲ඪͷ൓ࣹϕΫτϧɻϐΫηϧ͝ͱͷ๏ઢϚοϓʹج͍ͮͯɺ൓ࣹϕΫτ ϧΛऔಘ͢Δʹ͸ɺWorldReflectionVector (IN, o.Normal) Λ࢖༻͢Δɻ ➤ float3 worldNormal; INTERNAL_DATA
 • Surface Shaders ͕ o.Normal ʹॻ͖ࠐΉ৔߹ ͷϫʔϧυ࠲ඪͷ൓ࣹϕΫτϧɻϐΫηϧ͝ͱͷ๏ઢϚοϓʹج͍ͮͯɺ๏ઢϕΫτ ϧΛऔಘ͢Δʹ͸ɺWorldNormalVector (IN, o.Normal) Λ࢖༻͢Δɻ
  42. ೖग़ྗߏ଄ମ struct SurfaceOutput { fixed3 Albedo; // diffuse color fixed3

    Normal; // tangent space normal, if written fixed3 Emission; half Specular; // specular power in 0..1 range fixed Gloss; // specular intensity fixed Alpha; // alpha for transparencies }; struct SurfaceOutputStandard { fixed3 Albedo; // base (diffuse or specular) color fixed3 Normal; // tangent space normal, if written half3 Emission; half Metallic; // 0=non-metal, 1=metal half Smoothness; // 0=rough, 1=smooth half Occlusion; // occlusion (default 1) fixed Alpha; // alpha for transparencies }; struct SurfaceOutputStandardSpecular { fixed3 Albedo; // diffuse color fixed3 Specular; // specular color fixed3 Normal; // tangent space normal, if written half3 Emission; half Smoothness; // 0=rough, 1=smooth half Occlusion; // occlusion (default 1) fixed Alpha; // alpha for transparencies };
  43. Lambert (σϑϡʔζ) Shader "Example/Diffuse Simple" { SubShader { Tags {

    "RenderType" = "Opaque" } CGPROGRAM #pragma surface surf Lambert struct Input { float4 color : COLOR; }; void surf (Input IN, inout SurfaceOutput o) { o.Albedo = 1; } ENDCG } Fallback "Diffuse" }
  44. ςΫενϟ Shader "Example/Diffuse Texture" { Properties { _MainTex ("Texture", 2D)

    = "white" {} } SubShader { Tags { "RenderType" = "Opaque" } CGPROGRAM #pragma surface surf Lambert struct Input { float2 uv_MainTex; }; sampler2D _MainTex; void surf (Input IN, inout SurfaceOutput o) { o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb; } ENDCG } Fallback "Diffuse" }
  45. ๏ઢϚοϐϯά Shader "Example/Diffuse Bump" { Properties { _MainTex ("Texture", 2D)

    = "white" {} _BumpMap ("Bumpmap", 2D) = "bump" {} } SubShader { Tags { "RenderType" = "Opaque" } CGPROGRAM #pragma surface surf Lambert struct Input { float2 uv_MainTex; float2 uv_BumpMap; }; sampler2D _MainTex; sampler2D _BumpMap; void surf (Input IN, inout SurfaceOutput o) { o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb; o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap)); } ENDCG } Fallback "Diffuse" }
  46. ϦϜϥΠςΟϯά Shader "Example/Rim" { Properties { _MainTex ("Texture", 2D) =

    "white" {} _BumpMap ("Bumpmap", 2D) = "bump" {} _RimColor ("Rim Color", Color) = (0.26,0.19,0.16,0.0) _RimPower ("Rim Power", Range(0.5,8.0)) = 3.0 } SubShader { Tags { "RenderType" = "Opaque" } CGPROGRAM #pragma surface surf Lambert struct Input { float2 uv_MainTex; float2 uv_BumpMap; float3 viewDir; }; sampler2D _MainTex; sampler2D _BumpMap; float4 _RimColor; float _RimPower; void surf (Input IN, inout SurfaceOutput o) { o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb; o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap)); half rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal)); o.Emission = _RimColor.rgb * pow (rim, _RimPower); } ENDCG } Fallback "Diffuse" }
  47. ࡉ෦ςΫενϟ Shader "Example/Detail" { Properties { _MainTex ("Texture", 2D) =

    "white" {} _BumpMap ("Bumpmap", 2D) = "bump" {} _Detail ("Detail", 2D) = "gray" {} } SubShader { Tags { "RenderType" = "Opaque" } CGPROGRAM #pragma surface surf Lambert struct Input { float2 uv_MainTex; float2 uv_BumpMap; float2 uv_Detail; }; sampler2D _MainTex; sampler2D _BumpMap; sampler2D _Detail; void surf (Input IN, inout SurfaceOutput o) { o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb; o.Albedo *= tex2D (_Detail, IN.uv_Detail).rgb * 2; o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap)); } ENDCG } Fallback "Diffuse" }
  48. ը໘ۭؒ಺ͷࡉ෦ςΫενϟ Shader "Example/ScreenPos" { Properties { _MainTex ("Texture", 2D) =

    "white" {} _Detail ("Detail", 2D) = "gray" {} } SubShader { Tags { "RenderType" = "Opaque" } CGPROGRAM #pragma surface surf Lambert struct Input { float2 uv_MainTex; float4 screenPos; }; sampler2D _MainTex; sampler2D _Detail; void surf (Input IN, inout SurfaceOutput o) { o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb; float2 screenUV = IN.screenPos.xy / IN.screenPos.w; screenUV *= float2(8,6); o.Albedo *= tex2D (_Detail, screenUV).rgb * 2; } ENDCG } Fallback "Diffuse" }
  49. ΩϡʔϒϚοϓ൓ࣹ Shader "Example/WorldRefl" { Properties { _MainTex ("Texture", 2D) =

    "white" {} _Cube ("Cubemap", CUBE) = "" {} } SubShader { Tags { "RenderType" = "Opaque" } CGPROGRAM #pragma surface surf Lambert struct Input { float2 uv_MainTex; float3 worldRefl; }; sampler2D _MainTex; samplerCUBE _Cube; void surf (Input IN, inout SurfaceOutput o) { o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * 0.5; o.Emission = texCUBE (_Cube, IN.worldRefl).rgb; } ENDCG } Fallback "Diffuse" }
  50. ๏ઢϚοϓʹӨڹ͞ΕΔΩϡʔϒϚοϓ൓ࣹ Shader "Example/WorldRefl Normalmap" { Properties { _MainTex ("Texture", 2D)

    = "white" {} _BumpMap ("Bumpmap", 2D) = "bump" {} _Cube ("Cubemap", CUBE) = "" {} } SubShader { Tags { "RenderType" = "Opaque" } CGPROGRAM #pragma surface surf Lambert struct Input { float2 uv_MainTex; float2 uv_BumpMap; float3 worldRefl; INTERNAL_DATA }; sampler2D _MainTex; sampler2D _BumpMap; samplerCUBE _Cube; void surf (Input IN, inout SurfaceOutput o) { o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * 0.5; o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap)); o.Emission = texCUBE (_Cube, WorldReflectionVector (IN, o.Normal)).rgb; } ENDCG } Fallback "Diffuse" }
  51. ੈքۭؒͷҐஔΛհͨ͠εϥΠε Shader "Example/Slices" { Properties { _MainTex ("Texture", 2D) =

    "white" {} _BumpMap ("Bumpmap", 2D) = "bump" {} } SubShader { Tags { "RenderType" = "Opaque" } Cull Off CGPROGRAM #pragma surface surf Lambert struct Input { float2 uv_MainTex; float2 uv_BumpMap; float3 worldPos; }; sampler2D _MainTex; sampler2D _BumpMap; void surf (Input IN, inout SurfaceOutput o) { clip (frac((IN.worldPos.y+IN.worldPos.z*0.1) * 5) - 0.5); o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb; o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap)); } ENDCG } Fallback "Diffuse" }
  52. ๏ઢԡ͠ग़͠ Shader "Example/Normal Extrusion" { Properties { _MainTex ("Texture", 2D)

    = "white" {} _Amount ("Extrusion Amount", Range(-1,1)) = 0.5 } SubShader { Tags { "RenderType" = "Opaque" } CGPROGRAM #pragma surface surf Lambert vertex:vert struct Input { float2 uv_MainTex; }; float _Amount; void vert (inout appdata_full v) { v.vertex.xyz += v.normal * _Amount; } sampler2D _MainTex; void surf (Input IN, inout SurfaceOutput o) { o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb; } ENDCG } Fallback "Diffuse" }
  53. ௖఺͝ͱʹܭࢉ͞ΕͨΧελϜͷσʔλ Shader "Example/Custom Vertex Data" { Properties { _MainTex ("Texture",

    2D) = "white" {} } SubShader { Tags { "RenderType" = "Opaque" } CGPROGRAM #pragma surface surf Lambert vertex:vert struct Input { float2 uv_MainTex; float3 customColor; }; void vert (inout appdata_full v, out Input o) { UNITY_INITIALIZE_OUTPUT(Input,o); o.customColor = abs(v.normal); } sampler2D _MainTex; void surf (Input IN, inout SurfaceOutput o) { o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb; o.Albedo *= IN.customColor; } ENDCG } Fallback "Diffuse" }
  54. ࠷ऴతͳΧϥʔम০ࢠʢColor Modifierʣ Shader "Example/Tint Final Color" { Properties { _MainTex

    ("Texture", 2D) = "white" {} _ColorTint ("Tint", Color) = (1.0, 0.6, 0.6, 1.0) } SubShader { Tags { "RenderType" = "Opaque" } CGPROGRAM #pragma surface surf Lambert finalcolor:mycolor struct Input { float2 uv_MainTex; }; fixed4 _ColorTint; void mycolor (Input IN, SurfaceOutput o, inout fixed4 color) { color *= _ColorTint; } sampler2D _MainTex; void surf (Input IN, inout SurfaceOutput o) { o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb; } ENDCG } Fallback "Diffuse" }
  55. Final Color Modifier ʹΑΔΧελϜϑΥά Shader "Example/Fog via Final Color" {

    Properties { _MainTex ("Texture", 2D) = "white" {} _FogColor ("Fog Color", Color) = (0.3, 0.4, 0.7, 1.0) } SubShader { Tags { "RenderType" = "Opaque" } CGPROGRAM #pragma surface surf Lambert finalcolor:mycolor vertex:myvert struct Input { float2 uv_MainTex; half fog; }; void myvert (inout appdata_full v, out Input data) { UNITY_INITIALIZE_OUTPUT(Input,data); float4 hpos = mul (UNITY_MATRIX_MVP, v.vertex); hpos.xy/=hpos.w; data.fog = min (1, dot (hpos.xy, hpos.xy)*0.5); } fixed4 _FogColor; void mycolor (Input IN, SurfaceOutput o, inout fixed4 color) { fixed3 fogColor = _FogColor.rgb; #ifdef UNITY_PASS_FORWARDADD fogColor = 0; #endif color.rgb = lerp (color.rgb, fogColor, IN.fog); } sampler2D _MainTex; void surf (Input IN, inout SurfaceOutput o) { o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb; } ENDCG } Fallback "Diffuse" }
  56. ௖఺ɾϑϥάϝϯτγΣʔμʔͱ͸ ➤ ͦΕͧΕ௖఺ɾϐΫηϧσʔλॲཧ࣌ʹݺͼग़͢͜ͱͷͰ͖Δϓϩάϥ Ϝɻ ➤ ௖఺γΣʔμʔؔ਺͸௖఺̍ͭ̍ͭΛॲཧ͢Δࡍʹݺ͹ΕΔɻ ➤ ϑϥάϝϯτγΣʔμʔؔ਺͸̍ͭ̍ͭͷϐΫηϧΛॲཧ͢Δࡍʹݺ͹ ΕΔɻ ➤

    Cg/HLSLͰهड़͢Δɻ ➤ UnityͰ͸ϥΠςΟϯάʹ൓Ԡ͢ΔγΣʔμʔΛॻ͘৔߹͸αʔϑΣΠε γΣʔμʔΛॻ͘΂͖ɻ௖఺ɾϑϥάϝϯτγΣʔμʔͰϥΠςΟϯά Λܭࢉ͢Δ৔߹શͯࣗ෼Ͱ࣮૷͢Δඞཁ͕͋Δɻ ➤ ϥΠςΟϯάͷܭࢉ͕ඞཁͳ͍ΤϑΣΫτʹར༻͢Δ͜ͱ͕ଟ͍(Π ϝʔδΤϑΣΫτ౳)ɻ
  57. ௖఺ɾϑϥάϝϯτγΣʔμʔίϯύΠϧ໋ྩ γΣʔμʔؔ਺ࢦఆ໋ྩ ➤ #pragma vertex name
 • ௖఺γΣʔμʔͱͯ͠ name ͱ͍͏໊લͷؔ਺Λఆٛ

    ➤ #pragma fragment name
 • ϑϥάϝϯτγΣʔμʔͱͯ͠ name ͱ͍͏໊લͷؔ਺Λఆٛ ➤ #pragma geometry name
 • DX10 ͷδΦϝτϦγΣʔμʔͱͯ͠ name ͱ͍͏໊લͷؔ਺Λఆٛɻ͜ͷΦϓγϣϯ͸ࣗಈత ʹ #pragma target 4.0 ͷίϯύΠϧ໋ྩΛΦϯʹ͢Δɻ ➤ #pragma hull name
 • DX11 ͷ Hull γΣʔμʔͱͯ͠ name ͱ͍͏໊લͷؔ਺Λఆٛɻ͜ͷΦϓγϣϯ͸ࣗಈతʹ #pragma target 5.0 ͷίϯύΠϧ໋ྩΛΦϯʹ͢Δɻ ➤ #pragma domain name
 • DX11 ͷ Domein γΣʔμʔͱͯ͠ name ͱ͍͏໊લͷؔ਺Λఆٛɻ͜ͷΦϓγϣϯ͸ࣗಈతʹ #pragma target 5.0 ͷίϯύΠϧ໋ྩΛΦϯʹ͢Δɻ ͦΕͧΕͷεχϖοτʹ͸࠷௿Ͱ΋௖఺ϓϩάϥϜͱϑϥάϝϯτϓϩάϥϜؚ͕·Ε͍ͯͳ͚Ε ͹ͳΒͳ͍ɻͦΕΏ͑ʹ #pragma vertex ͱ #pragma fragment ໋ྩ͸ඞͣඞཁɻ
  58. ௖఺ɾϑϥάϝϯτγΣʔμʔίϯύΠϧ໋ྩ ͦͷଞͷίϯύΠϧ໋ྩ ➤ #pragma target requirements
 • ίϯύΠϧ͢ΔγΣʔμʔλʔήοτͷࢦఆɻৄࡉ͸ Shader Compilation

    Target Levels Λࢀরɻ ➤ #pragma only_renderers space_separated_names
 • ࢦఆͷϨϯμϦϯάϓϥοτϑΥʔϜͷΈγΣʔμʔΛίϯύΠϧɻσϑΥϧτͰ͸͢΂ͯͷϨϯμ ϥʔͷγΣʔμʔ͕ίϯύΠϧ͞ΕΔɻ ➤ #pragma exclude_renderers space_separated_names
 • ࢦఆͷϨϯμϦϯάϓϥοτϑΥʔϜͷΈγΣʔμʔΛίϯύΠϧ͠ͳ͍ɻσϑΥϧτͰ͸͢΂ͯͷ ϨϯμϥʔͷγΣʔμʔ͕ίϯύΠϧ͞ΕΔɻ ➤ #pragma multi_compile …_
 • ϚϧνγΣʔμʔόϦΞϯτͱͱ΋ʹಈ࡞͢Δɻ ➤ #pragma enable_d3d11_debug_symbols
 • DirectX 11 ༻ʹίϯύΠϧ͞ΕͨγΣʔμʔͷσόοά৘ใΛੜ੒͢Δɻ͜ΕʹΑΓɺVisual Studio 2012 (·ͨ͸ͦΕҎ߱ͷόʔδϣϯ)ͷάϥϑΟοΫεσόοΨʔΛܦ༝ͯ͠γΣʔμʔͷσ όοά͕ՄೳʹͳΔɻ
  59. ௖఺ɾϑϥάϝϯτγΣʔμʔίϯύΠϧ໋ྩ ϨϯμϦϯάϓϥοτϑΥʔϜࢦఆ໋ྩ ➤ d3d9 - Direct3D 9.
 ➤ d3d11 -

    Direct3D 11/12
 ➤ glcore - OpenGL 3.x/4.x
 ➤ gles - OpenGL ES 2.0
 ➤ gles3 - OpenGL ES 3.x
 ➤ metal - iOS/Mac Metal
 ➤ d3d11_9x - Ұൠతʹ͸ WSA/WP8 ϓϥοτϑΥʔϜ্Ͱ࢖༻͞ΕΔ Direct3D 11 9.x ϑΟʔνϟʔϨϕϧɻ ➤ xbox360 - Xbox 360
 ➤ xboxone - Xbox One
 ➤ ps3 - PlayStation 3
 ➤ ps4 - PlayStation 4
 ➤ psp2 - PlayStation Vita
 ➤ n3ds - Nintendo 3DS
 ➤ wiiu - Nintendo Wii U Unity ͸͍͔ͭ͘ͷϨϯμϦϯά API ʢ͢ͳΘͪ Direct3D 9 ͱ OpenGL ʣΛαϙʔτ͠ɺσϑΥϧτͰ͢΂ͯͷγΣʔμʔϓϩάϥϜ͸ αϙʔτ͞Ε͍ͯΔϨϯμϥʔʹίϯύΠϧ͞ΕΔɻͲͷϨϯμϥʔʹίϯύΠϧ͢Δ͔͸ #pragma only_renderers ͋Δ͍͸ #pragma exclude_renderers σΟϨΫςΟϒͰࢦఆͰ͖Δɻ͜Ε͸ɺಛఆͷγΣʔμʔ͕͋ΔϨϯμϥʔͰ͔͠࢖༻Ͱ͖ͣଞͰ͸࢖༻Ͱ͖ͳ͍৔߹ ʹར༻͢Δɻ
  60. ௖఺γΣʔμʔηϚϯςΟοΫ ೖྗύϥϝʔλʔηϚϯςΟοΫ ೖྗ આ໌ λΠϓ BINORMAL[n] ै๏ઢ float4 BLENDINDICES[n] ϒϨϯυࢦ਺

    uint BLENDWEIGHT[n] ϒϨϯυ΢ΣΠτ float COLOR[n] σΟϑϡʔζͱεϖΩϡϥ৭ float4 NORMAL[n] ๏ઢϕΫτϧ float4 POSITION[n] ΦϒδΣΫτۭؒ಺ͷ௖఺Ґஔɻ float4 POSITIONT ม׵͞Εͨ௖఺Ґஔɻ float4 PSIZE[n] ϙΠϯταΠζ float TANGENT[n] ઀ઢϕΫτϧ float4 TEXCOORD[n] ςΫενϟ࠲ඪ float4
  61. ௖఺γΣʔμʔηϚϯςΟοΫ ग़ྗύϥϝʔλʔηϚϯςΟοΫ ग़ྗ આ໌ λΠϓ COLOR[n] ֦ࢄ·ͨ͸εϖΩϡϥ৭ float4 FOG ௖఺ϑΥά

    float POSITION[n] ۉ࣭ͳۭؒͰͷ௖఺ͷҐஔɻWͰʢxɺyɺzʣΛ෼ׂ͢Δ͜ͱʹΑΓɺ εΫϦʔϯۭؒ಺ͷҐஔΛܭࢉ͢Δɻ͢΂ͯͷ௖఺γΣʔμ͸ɺ͜ͷη ϚϯςΟοΫΛ࣋ͭύϥϝʔλΛॻ͖ग़͢ඞཁ͕͋ΔɻDirect3D 10Ҏ ߱ͷ৔߹Ͱ͋Ε͹SV_POSITIONΛར༻ՄೳɻDirect3D 10ҎલͷγΣʔ μʔίʔυͰSV_POSITIONηϚϯςΟοΫΛར༻ͨ͠৔߹ɺ୯ʹ POSITIONηϚϯςΟοΫͱͯ͠ղऍ͞ΕΔɻ float4 PSIZE ϙΠϯταΠζ float TESSFACTOR[n] ςοηϨʔγϣϯ܎਺ float TEXCOORD[n] ςΫενϟ࠲ඪ float4
  62. ϑϥάϝϯτγΣʔμʔηϚϯςΟοΫ ೖྗύϥϝʔλʔηϚϯςΟοΫ ೖྗ આ໌ λΠϓ COLOR[n] ֦ࢄ·ͨ͸ڸ໘৭ɻ float4 TEXCOORD[n] ςΫενϟ࠲ඪ

    float4 VFACE ϓϦϛςΟϒ͕ޙΖ޲͖͔Ͳ͏͔Λ͍ࣔͯ͠Δුಈখ਺఺εΧϥʔɻਖ਼ͷ ஋͸ɺΧϝϥʹ௚໘͍ͯ͠Δɻෛͷ஋͸ɺޙํʹ௚໘͍ͯ͠Δɻ ʢ஫ʣ ͜ͷηϚϯςΟοΫ͕Ͱར༻Ͱ͖ΔͷDirect3D 9γΣʔμʔϞσ ϧ3.0ɻDirect3Dͷ10Ҏ߱Ͱ͸ɺ࢖༻SV_IsFrontFaceΛ୅ΘΓʹ࢖༻͢ Δɻ float VPOS εΫϦʔϯۭؒ಺ͷϐΫηϧҐஔʢXɺYʣɻDirect3Dͷ10Ҏ߱ͷγΣʔμ ʔͰ༻͍Δ৔߹͸SV_VPOSΛ༻͍Δɻৄࡉ͸Direct3D 9 VPOSͱDirect3D 10 SV_PositionΛࢀরɻ float2
  63. Cg/HLSL ͰγΣʔμʔϓϩύςΟʔΛࢀর͢Δ ྫ͑͹࣍ͷγΣʔμʔϓϩύςΟʔ
 _MyColor ("Some Color", Color) = (1,1,1,1) 


    _MyVector ("Some Vector", Vector) = (0,0,0,0) 
 _MyFloat ("My float", Float) = 0.5 
 _MyTexture ("Texture", 2D) = "white" {} 
 _MyCubemap ("Cubemap", CUBE) = "" {} ʹ͍ͭͯɺCg/HLSL Ͱࢀর͢ΔͨΊʹએݴ͢Δίʔυ͸ fixed4 _MyColor; // low precision type is usually enough for colors
 float4 _MyVector; 
 float _MyFloat; 
 sampler2D _MyTexture; 
 samplerCUBE _MyCubemap; 
 
 Cg/HLSL ͸uniformΩʔϫʔυΛهड़Ͱ͖Δ͕ɺඞਢͰ͸ͳ͍ɻ uniform float4 _MyColor;
  64. ShaderLabͱCg/HLSLͷม਺ܕͷϚοϐϯά ShaderLab ͷϓϩύςΟʔλΠϓ͸ Cg/HLSL ม਺λΠϓͱͷରԠ͸࣍ͷΑ͏ʹ Ϛοϐϯά͞ΕΔɻ ➤ Color ͱ Vector

    ϓϩύςΟʔ͸ɺfloat4ɺhalf4͋Δ͍͸fixed4ม਺ʹରԠɻ
 ➤ Range ͱ Float ϓϩύςΟʔ͸ɺfloatɺhalf͋Δ͍͸fixedม਺ʹରԠɻ
 ➤ Texture ϓϩύςΟʔ͸ɺ௨ৗʢ 2D ʣͷςΫενϟͷ৔߹͸sampler2Dม਺ ʹରԠɺ3D ςΫενϟͷ৔߹͸sampler3Dม਺ʹରԠɻ
  65. UnityCG.cginc ͷσʔλߏ଄ ➤ struct appdata_base
 • Ґஔɺ๏ઢɺςΫενϟ࠲ඪΛ΋ͭɻ ➤ struct appdata_tan


    • Ґஔɺ๏ઢɺ઀ઢɺςΫενϟ࠲ඪΛ΋ͭɻ ➤ struct appdata_full
 • Ґஔɺ๏ઢɺ઀ઢɺ௖఺Χϥʔɺ;ͨͭͷςΫενϟ࠲ඪ Λ΋ͭɻ ➤ struct appdata_img
 • ҐஔɺςΫενϟ࠲ඪΛ΋ͭɻ ௖఺γΣʔμʔೖྗߏ଄ମఆٛ
  66. UnityCG.cginc ͷ൚༻ϔϧύʔؔ਺ ➤ float3 WorldSpaceViewDir (float4 v)
 • ϫʔϧυۭؒͷಛఆͷΦϒδΣΫτۭؒͷ௖఺Ґஔ͔ΒΧϝϥʹ޲͔ͬͨ(ਖ਼نԽ͞Ε͍ͯͳ͍) ํ޲Λऔಘ͢Δɻ

    ➤ float3 ObjSpaceViewDir (float4 v)
 • ΦϒδΣΫτۭؒͷಛఆͷΦϒδΣΫτۭؒͷ௖఺Ґஔ͔ΒΧϝϥʹ޲͔ͬͨʢਖ਼نԽ͞Ε͍ͯ ͳ͍ʣํ޲Λऔಘ͢Δɻ ➤ float2 ParallaxOffset (half h, half height, half3 viewDir)
 • ࢹࠩ๏ઢϚοϐϯάͷ UV ΦϑηοτΛܭࢉɻ ➤ fixed Luminance (fixed3 c)
 • ৭Λً౓ʢάϨʔεέʔϧʣʹม׵͢Δɻ ➤ fixed3 DecodeLightmap (fixed4 color)
 • Unity ϥΠτϚοϓͷ৭Λσίʔυ͢ΔʢϓϥοτϑΥʔϜʹΑΓɺRGBM ͋Δ͍͸ dLDR ʣɻ ➤ float4 EncodeFloatRGBA (float v)
 • ਫ਼౓ͷ௿͍ϨϯμϦϯάλʔήοτΛ֨ೲ͢ΔͨΊɺ0 ʙ 1 ͷൣғͷුಈখ਺఺Λ RGBAcolor ʹ Τϯίʔυ͢Δɻ ➤ float DecodeFloatRGBA (float4 enc)
 • RGBA color Λ float ʹσίʔυ͢Δɻಉ༷ʹɺfloat2 EncodeFloatRG (float v) ͓Αͼ float DecodeFloatRG (float2 enc) ͸ೋͭͷΧϥʔνϟωϧΛ࢖༻͢Δɻ ➤ float2 EncodeViewNormalStereo (float3 n)
 • Ϗϡʔۭؒ๏ઢΛ 0 ʙ 1 ͷൣғͷೋͭͷ਺ࣈʹΤϯίʔυ͢Δɻ ➤ float3 DecodeViewNormalStereo (float4 enc4)
 • enc4.xy ͔ΒϏϡʔۭؒ๏ઢΛσίʔυ͢Δɻ
  67. UnityCG.cginc ͷ Forward Rendering ϔϧύʔؔ਺ Forward Rendering ༻ͷؔ਺ɻ( ForwardBase ·ͨ͸

    ForwardAdd ͷύελΠϓ) ➤ float3 WorldSpaceLightDir (float4 v)
 • ϫʔϧυۭؒͷΦϒδΣΫτۭؒͷ௖఺Ґஔ͔ΒϥΠτ΁޲͚ͨʢਖ਼نԽ͞Εͯͳ͍ʣํ޲Λܭ ࢉ͢Δɻ ➤ float3 ObjSpaceLightDir (float4 v)
 • ΦϒδΣΫτۭؒͷΦϒδΣΫτۭؒͷ௖఺Ґஔ͔ΒϥΠτ΁޲͚ͨʢਖ਼نԽ͞Εͯͳ͍ʣํ޲ Λܭࢉ͢Δɻ ➤ float3 Shade4PointLights (...)
 • ࢛ͭͷϙΠϯτϥΠτ͔ΒɺϥΠτͷσʔλΛ vector ʹ֨ೲͯ͠ɺً౓Λࢉग़͢ΔɻForward Rendering ͸֤௖఺͝ͱͷϥΠςΟϯάʹ͜ͷ৘ใΛ࢖༻͢Δɻ Shade4PointLights ( unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0, unity_LightColor0, unity_LightColor1, unity_LightColor2, unity_LightColor3, unity_4LightAtten0, worldPos, worldN ); 
 ͷΑ͏ͳܗͰར༻͢Δɻ ֤ύϥϝʔλʔ͸UnityShaderVariables.cgincͷϥΠςΟϯάม਺Λࢀর
  68. UnityCG.cginc ͷ Vertex-lit ϔϧύʔؔ਺ ௖఺ϥΠςΟϯάΛߦ͏৔߹ʹͷΈศར ➤ float3 ShadeVertexLights (float4 vertex,

    float3 normal)
 • 4 ͭͷ֤௖఺ϥΠτ͔ΒΦϒδΣΫτۭؒͷҐஔͱ๏ઢΛࢦఆͯ͠ɺً౓ͱ؀ڥޫΛࢉग़͢Δɻ
  69. ୯৭ Shader "Unlit/SingleColor" { Properties { // Color property for

    material inspector, default to white _Color ("Main Color", Color) = (1,1,1,1) } SubShader { Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag // vertex shader // this time instead of using "appdata" struct, just spell inputs manually, // and instead of returning v2f struct, also just return a single output // float4 clip position float4 vert (float4 vertex : POSITION) : SV_POSITION { return mul(UNITY_MATRIX_MVP, vertex); } // color from the material fixed4 _Color; // pixel shader, no inputs needed fixed4 frag () : SV_Target { return _Color; // just return it } ENDCG } } }
  70. ๏ઢํ޲Ͱ৭෇͚ Shader "Unlit/WorldSpaceNormals" { // no Properties block this time!

    SubShader { Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag // include file that contains UnityObjectToWorldNormal helper function #include "UnityCG.cginc" struct v2f { // we'll output world space normal as one of regular ("texcoord") interpolators half3 worldNormal : TEXCOORD0; float4 pos : SV_POSITION; }; // vertex shader: takes object space normal as input too v2f vert (float4 vertex : POSITION, float3 normal : NORMAL) { v2f o; o.pos = mul(UNITY_MATRIX_MVP, vertex); // UnityCG.cginc file contains function to transform // normal from object to world space, use that o.worldNormal = UnityObjectToWorldNormal(normal); return o; } fixed4 frag (v2f i) : SV_Target { fixed4 c = 0; // normal is a 3D vector with xyz components; in -1..1 // range. To display it as color, bring the range into 0..1 // and put into red, green, blue components c.rgb = i.worldNormal*0.5+0.5; return c; } ENDCG } } }
  71. ๏ઢʹΑΔ৭෇͚ Shader "Debug/Normals" { SubShader { Pass { CGPROGRAM #pragma

    vertex vert #pragma fragment frag // vertex input: position, normal struct appdata { float4 vertex : POSITION; float3 normal : NORMAL; }; struct v2f { float4 pos : SV_POSITION; fixed4 color : COLOR; }; v2f vert (appdata v) { v2f o; o.pos = mul( UNITY_MATRIX_MVP, v.vertex ); o.color.xyz = v.normal * 0.5 + 0.5; o.color.w = 1.0; return o; } fixed4 frag (v2f i) : SV_Target { return i.color; } ENDCG } } }
  72. UV஋Ͱ৭෇͚ Shader "Unlit/Show UVs" { SubShader { Pass { CGPROGRAM

    #pragma vertex vert #pragma fragment frag struct v2f { float2 uv : TEXCOORD0; float4 pos : SV_POSITION; }; v2f vert ( float4 vertex : POSITION, // vertex position input float2 uv : TEXCOORD0 // first texture coordinate input ) { v2f o; o.pos = mul(UNITY_MATRIX_MVP, vertex); o.uv = uv; return o; } fixed4 frag (v2f i) : SV_Target { return fixed4(i.uv, 0, 0); } ENDCG } } }
  73. ௖఺৭Λදࣔ Shader "Debug/Vertex color" { SubShader { Pass { CGPROGRAM

    #pragma vertex vert #pragma fragment frag // vertex input: position, color struct appdata { float4 vertex : POSITION; fixed4 color : COLOR; }; struct v2f { float4 pos : SV_POSITION; fixed4 color : COLOR; }; v2f vert (appdata v) { v2f o; o.pos = mul( UNITY_MATRIX_MVP, v.vertex ); o.color = v.color; return o; } fixed4 frag (v2f i) : SV_Target { return i.color; } ENDCG } } }
  74. ઀ઢʹΑΔ৭෇͚ Shader "Debug/Tangents" { SubShader { Pass { CGPROGRAM #pragma

    vertex vert #pragma fragment frag // vertex input: position, tangent struct appdata { float4 vertex : POSITION; float4 tangent : TANGENT; }; struct v2f { float4 pos : SV_POSITION; fixed4 color : COLOR; }; v2f vert (appdata v) { v2f o; o.pos = mul( UNITY_MATRIX_MVP, v.vertex ); o.color = v.tangent * 0.5 + 0.5; return o; } fixed4 frag (v2f i) : SV_Target { return i.color; } ENDCG } } }
  75. SkyBoxΛCubeMapͱͯ͠๏ઢΛ࢖ͬͨಁաॲཧ Shader "Unlit/SkyReflection" { SubShader { Pass { CGPROGRAM #pragma

    vertex vert #pragma fragment frag #include "UnityCG.cginc" struct v2f { half3 worldRefl : TEXCOORD0; float4 pos : SV_POSITION; }; v2f vert (float4 vertex : POSITION, float3 normal : NORMAL) { v2f o; o.pos = mul(UNITY_MATRIX_MVP, vertex); // compute world space position of the vertex float3 worldPos = mul(_Object2World, vertex).xyz; // compute world space view direction float3 worldViewDir = normalize(UnityWorldSpaceViewDir(worldPos)); // world space normal float3 worldNormal = UnityObjectToWorldNormal(normal); // world space reflection vector o.worldRefl = reflect(-worldViewDir, worldNormal); return o; } fixed4 frag (v2f i) : SV_Target { // sample the default reflection cubemap, using the reflection vector half4 skyData = UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, i.worldRefl); // decode cubemap data into actual color half3 skyColor = DecodeHDR (skyData, unity_SpecCube0_HDR); // output it! fixed4 c = 0; c.rgb = skyColor; return c; } ENDCG } } }
  76. ଞͷςΫενϟΛՃࢉ Shader "Unlit/More Textures" { Properties { // three textures

    we'll use in the material _MainTex("Base texture", 2D) = "white" {} _OcclusionMap("Occlusion", 2D) = "white" {} _BumpMap("Normal Map", 2D) = "bump" {} } SubShader { Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" // exactly the same as in previous shader struct v2f { float3 worldPos : TEXCOORD0; half3 tspace0 : TEXCOORD1; half3 tspace1 : TEXCOORD2; half3 tspace2 : TEXCOORD3; float2 uv : TEXCOORD4; float4 pos : SV_POSITION; }; v2f vert (float4 vertex : POSITION, float3 normal : NORMAL, float4 tangent : TANGENT, float2 uv : TEXCOORD0) { v2f o; o.pos = mul(UNITY_MATRIX_MVP, vertex); o.worldPos = mul(_Object2World, vertex).xyz; half3 wNormal = UnityObjectToWorldNormal(normal); half3 wTangent = UnityObjectToWorldDir(tangent.xyz); half tangentSign = tangent.w * unity_WorldTransformParams.w; half3 wBitangent = cross(wNormal, wTangent) * tangentSign; o.tspace0 = half3(wTangent.x, wBitangent.x, wNormal.x); o.tspace1 = half3(wTangent.y, wBitangent.y, wNormal.y); o.tspace2 = half3(wTangent.z, wBitangent.z, wNormal.z); o.uv = uv; return o; } // textures from shader properties sampler2D _MainTex; sampler2D _OcclusionMap; sampler2D _BumpMap; fixed4 frag (v2f i) : SV_Target { // same as from previous shader... half3 tnormal = UnpackNormal(tex2D(_BumpMap, i.uv)); half3 worldNormal; worldNormal.x = dot(i.tspace0, tnormal); worldNormal.y = dot(i.tspace1, tnormal); worldNormal.z = dot(i.tspace2, tnormal); half3 worldViewDir = normalize(UnityWorldSpaceViewDir(i.worldPos)); half3 worldRefl = reflect(-worldViewDir, worldNormal); half4 skyData = UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, worldRefl); half3 skyColor = DecodeHDR (skyData, unity_SpecCube0_HDR); fixed4 c = 0; c.rgb = skyColor; // modulate sky color with the base texture, and the occlusion map fixed3 baseColor = tex2D(_MainTex, i.uv).rgb; fixed occlusion = tex2D(_OcclusionMap, i.uv).r; c.rgb *= baseColor; c.rgb *= occlusion; return c; } ENDCG } } }
  77. Checkerboardύλʔϯ Shader "Unlit/Checkerboard" { Properties { _Density ("Density", Range(2,50)) =

    30 } SubShader { Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag struct v2f { float2 uv : TEXCOORD0; float4 vertex : SV_POSITION; }; float _Density; v2f vert (float4 pos : POSITION, float2 uv : TEXCOORD0) { v2f o; o.vertex = mul(UNITY_MATRIX_MVP, pos); o.uv = uv * _Density; return o; } fixed4 frag (v2f i) : SV_Target { float2 c = i.uv; c = floor(c) / 2; float checker = frac(c.x + c.y) * 2; return checker; } ENDCG } } }
  78. ֨ࢠঢ়ʹ੾Γൈ͘ Shader "Unlit/Screen Position" { Properties { _MainTex ("Texture", 2D)

    = "white" {} } SubShader { Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma target 3.0 // note: no SV_POSITION in this struct struct v2f { float2 uv : TEXCOORD0; }; v2f vert ( float4 vertex : POSITION, // vertex position input float2 uv : TEXCOORD0, // texture coordinate input out float4 outpos : SV_POSITION // clip space position output ) { v2f o; o.uv = uv; outpos = mul(UNITY_MATRIX_MVP, vertex); return o; } sampler2D _MainTex; fixed4 frag (v2f i, UNITY_VPOS_TYPE screenPos : VPOS) : SV_Target { // screenPos.xy will contain pixel integer coordinates. // use them to implement a checkerboard pattern that skips rendering // 4x4 blocks of pixels // checker value will be negative for 4x4 blocks of pixels // in a checkerboard pattern screenPos.xy = floor(screenPos.xy * 0.25) * 0.5; float checker = -frac(screenPos.r + screenPos.g); // clip HLSL instruction stops rendering a pixel if value is negative clip(checker); // for pixels that were kept, read the texture and output it fixed4 c = tex2D (_MainTex, i.uv); return c; } ENDCG } } }
  79. Diffuse Light Shader "Lit/Simple Diffuse" { Properties { [NoScaleOffset] _MainTex

    ("Texture", 2D) = "white" {} } SubShader { Pass { // indicate that our pass is the "base" pass in forward // rendering pipeline. It gets ambient and main directional // light data set up; light direction in _WorldSpaceLightPos0 // and color in _LightColor0 Tags {"LightMode"="ForwardBase"} CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" // for UnityObjectToWorldNormal #include "UnityLightingCommon.cginc" // for _LightColor0 struct v2f { float2 uv : TEXCOORD0; fixed4 diff : COLOR0; // diffuse lighting color float4 vertex : SV_POSITION; }; v2f vert (appdata_base v) { v2f o; o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); o.uv = v.texcoord; // get vertex normal in world space half3 worldNormal = UnityObjectToWorldNormal(v.normal); // dot product between normal and light direction for // standard diffuse (Lambert) lighting half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz)); // factor in the light color o.diff = nl * _LightColor0; return o; } sampler2D _MainTex; fixed4 frag (v2f i) : SV_Target { // sample texture fixed4 col = tex2D(_MainTex, i.uv); // multiply by lighting col *= i.diff; return col; } ENDCG } } }
  80. ؀ڥޫΛߟྀͨ͠Diffuse Light Shader "Lit/Diffuse With Ambient" { Properties { [NoScaleOffset]

    _MainTex ("Texture", 2D) = "white" {} } SubShader { Pass { Tags {"LightMode"="ForwardBase"} CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" #include "UnityLightingCommon.cginc" struct v2f { float2 uv : TEXCOORD0; fixed4 diff : COLOR0; float4 vertex : SV_POSITION; }; v2f vert (appdata_base v) { v2f o; o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); o.uv = v.texcoord; half3 worldNormal = UnityObjectToWorldNormal(v.normal); half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz)); o.diff = nl * _LightColor0; // the only difference from previous shader: // in addition to the diffuse lighting from the main light, // add illumination from ambient or light probes // ShadeSH9 function from UnityCG.cginc evaluates it, // using world space normal o.diff.rgb += ShadeSH9(half4(worldNormal,1)); return o; } sampler2D _MainTex; fixed4 frag (v2f i) : SV_Target { fixed4 col = tex2D(_MainTex, i.uv); col *= i.diff; return col; } ENDCG } } }
  81. Shadow Shader "Lit/Shadow Casting" { SubShader { // very simple

    lighting pass, that only does non-textured ambient Pass { Tags {"LightMode"="ForwardBase"} CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct v2f { fixed4 diff : COLOR0; float4 vertex : SV_POSITION; }; v2f vert (appdata_base v) { v2f o; o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); half3 worldNormal = UnityObjectToWorldNormal(v.normal); // only evaluate ambient o.diff.rgb = ShadeSH9(half4(worldNormal,1)); o.diff.a = 1; return o; } fixed4 frag (v2f i) : SV_Target { return i.diff; } ENDCG } // shadow caster rendering pass, implemented manually // using macros from UnityCG.cginc Pass { Tags {"LightMode"="ShadowCaster"} CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_shadowcaster #include "UnityCG.cginc" struct v2f { V2F_SHADOW_CASTER; }; v2f vert(appdata_base v) { v2f o; TRANSFER_SHADOW_CASTER_NORMALOFFSET(o) return o; } float4 frag(v2f i) : SV_Target { SHADOW_CASTER_FRAGMENT(i) } ENDCG } } }
  82. DiffuseͱShadow Shader "Lit/Diffuse With Shadows" { Properties { [NoScaleOffset] _MainTex

    ("Texture", 2D) = "white" {} } SubShader { Pass { Tags {"LightMode"="ForwardBase"} CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" #include "Lighting.cginc" // compile shader into multiple variants, with and without shadows // (we don't care about any lightmaps yet, so skip these variants) #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight // shadow helper functions and macros #include "AutoLight.cginc" struct v2f { float2 uv : TEXCOORD0; SHADOW_COORDS(1) // put shadows data into TEXCOORD1 fixed3 diff : COLOR0; fixed3 ambient : COLOR1; float4 pos : SV_POSITION; }; v2f vert (appdata_base v) { v2f o; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); o.uv = v.texcoord; half3 worldNormal = UnityObjectToWorldNormal(v.normal); half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz)); o.diff = nl * _LightColor0.rgb; o.ambient = ShadeSH9(half4(worldNormal,1)); // compute shadows data TRANSFER_SHADOW(o) return o; } sampler2D _MainTex; fixed4 frag (v2f i) : SV_Target { fixed4 col = tex2D(_MainTex, i.uv); // compute shadow attenuation (1.0 = fully lit, 0.0 = fully shadowed) fixed shadow = SHADOW_ATTENUATION(i); // darken light's illumination with shadow, keep ambient intact fixed3 lighting = i.diff * shadow + i.ambient; col.rgb *= lighting; return col; } ENDCG } // shadow casting support UsePass "Legacy Shaders/VertexLit/SHADOWCASTER" } }
  83. Fog Shader "Custom/TextureCoordinates/Fog" { SubShader { Pass { CGPROGRAM #pragma

    vertex vert #pragma fragment frag //Needed for fog variation to be compiled. #pragma multi_compile_fog #include "UnityCG.cginc" struct vertexInput { float4 vertex : POSITION; float4 texcoord0 : TEXCOORD0; }; struct fragmentInput{ float4 position : SV_POSITION; float4 texcoord0 : TEXCOORD0; //Used to pass fog amount around number should be a free texcoord. UNITY_FOG_COORDS(1) }; fragmentInput vert(vertexInput i){ fragmentInput o; o.position = mul (UNITY_MATRIX_MVP, i.vertex); o.texcoord0 = i.texcoord0; //Compute fog amount from clip space position. UNITY_TRANSFER_FOG(o,o.position); return o; } fixed4 frag(fragmentInput i) : SV_Target { fixed4 color = fixed4(i.texcoord0.xy,0,0); //Apply fog (additive pass are automatically handled) UNITY_APPLY_FOG(i.fogCoord, color); //to handle custom fog color another option would have been //#ifdef UNITY_PASS_FORWARDADD // UNITY_APPLY_FOG_COLOR(i.fogCoord, color, float4(0,0,0,0)); //#else // fixed4 myCustomColor = fixed4(0,0,1,0); // UNITY_APPLY_FOG_COLOR(i.fogCoord, color, myCustomColor); //#endif return color; } ENDCG } } }
  84. ΧϦϯάͷߏจ Cull Back | Front | Off ߏจ ϙϦΰϯͷͲͪΒଆΛΧϦϯά͢Δʢඳը͠ͳ͍ʣ੍͔ޚɻ
 ➤

    Back • ࢹ఺ͱ൓ରଆͷϙϦΰϯΛϨϯμϦϯά͠ͳ͍Α͏ʹ͢Δ(σϑΥϧ τ) ɻ ➤ Front • ࢹ఺ͱಉ͡ଆͷϙϦΰϯΛϨϯμϦϯά͠ͳ͍ɻΦϒδΣΫτΛ൓ స͢Δͷʹ࢖༻͢Δɻ ➤ Off • ΧϦϯάΛແޮʹͯ͠ɺ͢΂ͯͷ໘Λඳը͢Δɻ
  85. ਂ౓ςετͷߏจ ZTest Less | Greater | LEqual | GEqual |

    Equal | NotEqual | Always ZTest σϓεςετͷ࣮ߦํ๏ɻσϑΥϧτ͸ LEqual ʢ͢Ͱʹඳը͞Ε͍ͯΔΦϒδΣΫτͱڑ཭͕౳͍͠ ͔ɺΑΓ͍ۙ৔߹ʹඳըʣɻ Offset Offset Factor, Units σϓεΦϑηοτΛೋͭͷύϥϝʔλʔɺFactor ͓Αͼ UnitsɺͰࢦఆͰ͖ΔɻFactor ͸ɺϙϦΰϯͷ X ͋Δ͍͸ Y ʹؔ࿈ͯ͠ɺ࠷େͷ Z ޯ഑Λεέʔϧ͠ɺUnits ͸࠷খͷσϓεόοϑΝ஋Λεέʔϧ ͢Δɻྫ͑͹ɺOffset 0, –1 ʹΑΓɺϙϦΰϯͷޯ഑Λແࢹͯ͠ϙϦΰϯΛΧϝϥʹ͚ۙͮɺҰํͰ Offset –1, –1 ͸͞Βʹݟ্͛Δ֯౓ͰϙϦΰϯΛ͚ۙͮΔɻZϑΝΠςΟϯά(ಉ͡Ґஔʹ2ͭͷϙϦΰ ϯ͕ଘࡏ͢Δ࣌ʹϨϯμϦϯά͞ΕΔϐΫηϧ͕2ͭͷϙϦΰϯ͕ࠞͬͯ͟͠·͏ݱ৅)ͷղܾͰར༻͢ Δɻ ZWrite On | Off ZWrite ϨϯμϦϯάλʔήοτͷਂ౓৘ใΛDepth Bufferʹॻ͖ࠐΉ͔Ͳ͏͔ɻσϑΥϧτ͸Onɻ
  86. εςϯγϧόοϑΝʹॻ͖ࠐΉ Stencil { Ref 1 // εςϯγϧ஋(όοϑΝ΁ͷॻ͖ࠐΈɾಡΈग़͠) Comp Always //

    ࣮ߦ͞ΕΔ৚݅͸ʢৗʹʣ Pass Replace // ࣮ߦ͞ΕΔίϚϯυ͸ʢ্ॻ͖ʣ } εςϯγϧόοϑΝ΁ͷॻ͖ࠐΈɻྫͷ৔߹Comp (࣮ߦ৚݅)ΛAlways ʹઃఆ͠ɺ৚͕݅ਅͩͬͨ৔߹(͜ͷ৔߹͸ৗʹਅ)࣮ߦ͢Δಈ࡞Ͱ͋Δ PassʹReplace(Refͷ஋Ͱ্ॻ͖)Λࢦఆ͍ͯ͠Δɻ ͭ·Γ͜ͷࢦఆํ๏ͷ৔߹ɺϑϥάϝϯτඳը࣌ʹৗʹεςϯγϧόοϑΝ ʹ1ͱ͍͏੔਺஋Λॻ͖ࠐΉ͜ͱʹͳΔɻ ߏจʹ͍ͭͯ͸ޙड़
  87. ஫ҙ Stencil { Ref 1 // εςϯγϧ஋(όοϑΝ΁ͷॻ͖ࠐΈɾಡΈग़͠) Comp Always //

    ࣮ߦ͞ΕΔ৚݅͸ʢৗʹʣ Pass Replace // ࣮ߦ͞ΕΔίϚϯυ͸ʢ্ॻ͖ʣ } ઌ΄Ͳͷ͜ͷΑ͏ͳεςϯγϧόοϑΝ΁ͷॻ͖ࠐΈΛ3DϞσϧͷඳըʹ ߦͬͯ΋໰୊͸ͳ͍ɻ ͔͠͠2DεϓϥΠτ౳ʹߦ͏ͱɺϙϦΰϯͰεςϯγϧ͕࡞੒͞Εͯ͠·͏ɻ ͜ΕΛճආ͢ΔͨΊʹ͸ if (color.a < 0.001) discard; ͷΑ͏ʹɺεϓϥΠτͷΞϧϑΝ஋͕ҰఆҎԼͳΒഁغ͢Δͱ͍͏ॲཧ͕ඞཁɻ εϓϥΠτΛඳը͢Δࡍʹ ϙϦΰϯͰඳը͞ΕΔͨΊʹ εςϯγϧ΁ͷॻ͖ࠐΈ͕ ΩϟϥΫλʔͷܗʹͳΒͳ͍ ಁ໌౓ʹΑΔDiscardॲཧΛ ϑϥάϝϯτγΣʔμʔͷॲཧʹ ௥Ճ͢Δ͜ͱͰɺΩϟϥΫλʔͷ ܗʹεςϯγϧόοϑΝΛॻ͖ࠐΊΔ
  88. εςϯγϧςετ Stencil { Ref 1 // εςϯγϧ஋(όοϑΝ΁ͷॻ͖ࠐΈɾಡΈग़͠) Comp Equal //

    ࣮ߦͷ৚݅͸ʢόοϑΝͷ஋͕Refͷ࣌ʣ } εςϯγϧςετɻྫͷ৔߹͸ɺεςϯγϧόοϑΝ ͷ஋ͱൺֱͯ͠ɺ஋͕̍ʹ౳͚͠Ε͹ϐΫηϧͷඳը ͕ߦΘΕΔɻ ߏจʹ͍ͭͯ͸ޙड़
  89. εςϯγϧߏจ SubShader { … Stencil { Ref 1 Comp always

    Pass replace } … } SubShader { Pass{ … Stencil { Ref 1 Comp always Pass replace } } … } SubShaderҎԼʹॻ͖ࠐΉ͜ͱ͕Ͱ͖Δ PassҎԼʹॻ͖ࠐΉ͜ͱ͕Ͱ͖Δ Stencil{…} ͱ͍͏ܗࣜͰهड़͍ͯ͘͠ɻ ಺༰͸ޙड़ ྫ ྫ
  90. ߏจ Ref [੔਺஋] Comp [ൺֱؔ਺] Pass [߹֨࣌ͷεςϯγϧૢ࡞] Fail [ෆ߹֨࣌ͷεςϯγϧૢ࡞] ZFail

    [߹͕֨ͩਂ౓ςετෆ߹֨࣌ͷεςϯγϧૢ࡞] ReadMask [ಡΈࠐΈϚεΫ஋] WriteMask [ॻ͖ࠐΈϚεΫ஋]
  91. εςϯγϧόοϑΝ΁ͷॻ͖ࠐΈ Shader "Red" { SubShader { Tags { "RenderType"="Opaque" "Queue"="Geometry"}

    Pass { Stencil { Ref 2 Comp always Pass replace ZFail decrWrap } CGPROGRAM #pragma vertex vert #pragma fragment frag struct appdata { float4 vertex : POSITION; }; struct v2f { float4 pos : SV_POSITION; }; v2f vert(appdata v) { v2f o; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); return o; } half4 frag(v2f i) : COLOR { return half4(1,0,0,1); } ENDCG } } } εςϯγϧόοϑΝ΁ͷॻ͖ࠐΈ Λߦ͏γΣʔμʔϓϩάϥϜྫɻ ॻ͖ࠐΜͩεςϯγϧͷ಺༰Λ Θ͔Γ΍͘͢͢ΔͨΊʹɺ ϑϥάϝϯτγΣʔμʔͰ͸ ॻ͖ࠐΜͩ಺༰Λ੺Ͱදࣔ͢Δ Α͏ʹͳ͍ͬͯΔɻ
  92. εςϯγϧόοϑΝͷར༻ Shader "Custom/SpineShader"{ SubShader { Tags {"Queue"="Transparent+2" "IgnoreProjector"="True" "RenderType"="Transparent"} ZWrite

    Off Blend SrcAlpha OneMinusSrcAlpha Stencil { Ref 1 Comp Equal } Pass{…} } Shader "Custom/SpineSpriteMask"{ SubShader { Tags {"Queue"="Transparent+1" "IgnoreProjector"="True"} ZWrite Off AlphaTest Greater 0.5 ColorMask 0 ZTest Always Stencil { Ref 1 Comp always Pass replace } Pass{…} } όοϑΝॻ͖ࠐΈ εςϯγϧςετ
  93. Propertiesͷ஋ΛStencilʹར༻͢Δ Shader "UI/Fade" { Properties { _StencilComp ("Stencil Comparison", Float)

    = 8 _Stencil ("Stencil ID", Float) = 0 _StencilOp ("Stencil Operation", Float) = 0 _StencilWriteMask ("Stencil Write Mask", Float) = 255 _StencilReadMask ("Stencil Read Mask", Float) = 255 } SubShader { Stencil { Ref [_Stencil] Comp [_StencilComp] Pass [_StencilOp] ReadMask [_StencilReadMask] WriteMask [_StencilWriteMask] } Pass {...} } } Propertiesͷ஋ΛShaderLab ಛ༗ͷ߲໨Ͱར༻͢Δʹ͸ []ͰϓϩύςΟΛғΉɻ
  94. Blendingͷߏจ ➤ Blend Off • ϒϨϯσΟϯάΛແޮʹ͢Δɻ ➤ Blend SrcFactor DstFactor

    • ੜ੒͞ΕͨΧϥʔʢ͜Ε͔ΒృΔ৭ʣʹSrcFactor Λ৐ࢉ͠ը໘ʹ͢Ͱʹ͋ΔΧϥʔʢϑϨʔϜόο ϑΝʹ͋Δ৭ʣʹDstFactor Λ৐ࢉͯ͠ɺ2ͭΛՃࢉ͢Δɻͭ·Γɺݟ͑Δ৭ʹ͜Ε͔ΒృΔ৭ *SrcFactorʴݱࡏͷ৭*DstFactor ͱͳΔΑ͏ʹϒϨϯυ͢Δɻ ➤ Blend SrcFactor DstFactor, SrcFactorA DstFactorA • ্هͱಉ༷͕ͩɺΞϧϑΝ஋ΛϒϨϯυ͢Δͷʹผͳεέʔϧ஋ʢSrcFactorA DstFactorAʣΛ࢖͏ɻ ➤ BlendOp BlendOp • ϒϨϯσΟϯά͞ΕͨΧϥʔΛ࢖༻͢Δ୅ΘΓʹɺBlendOpͰࢦఆ͞ΕΔผͷॲཧΛߦ͏ɻ ➤ BlendOp OpColor, OpAlpha • ্هͱಉ༷͕ͩɺΞϧϑΝνϟωϧΛϒϨϯσΟϯά͢ΔͷʹҟͳΔૢ࡞Λ࢖༻͢Δɻ ੨ࣈɾɾɾBlend ܎਺ ੺ࣈɾɾɾBlend ૢ࡞ ͱͯ͠આ໌͍ͯ͘͠ɻ
  95. ҰൠతͳϒϨϯσΟϯάͷλΠϓ Blend SrcAlpha OneMinusSrcAlpha // Alpha blending Blend One One

    // Additive Blend OneMinusDstColor One // Soft Additive Blend DstColor Zero // Multiplicative Blend DstColor SrcColor // 2x Multiplicative
  96. ShaderLabৄࡉτϐοΫ ➤ ShaderLabͷৄࡉͳτϐοΫʹ ͍ͭͯղઆ͢Δɻ • ʲUnityϚχϡΞϧʳৄࡉͳ ShaderLab τϐοΫ • ʲϝϞϒϩάʳUnity

    5ͰG-Bufferͷςετ • ʲଠ࿠WorkʳSetReplacementShaderͷ࢖͍ํ Unity • ʲQiitaʳσϓεόοϑΝΛར༻͢Δ • ʲCross RoadʳUnity࢖༻தʹֶΜͩύϑΥʔϚϯενϡʔ χϯάํ๏:LOD • ʲͱ·ͱɾιϑτ΢ΣΞ੡࡞ॴʳDirect3D9΍ϨϯμϦϯά ύΠϓϥΠϯؔ࿈༻ޠʢ3DCGશൠͷجૅ஌ࣝΊ΋ʣ • ʲচҪݚڀࣨʳϨϯμϦϯάύΠϓϥΠϯ
  97. Pass಺ͷTags Pass ͸TagsΛ࢖༻͍ͯͭ͠ͲͷΑ͏ʹͯ͠ϨϯμϦϯάΤϯδϯʹΑΓϨ ϯμϦϯά͢΂͖͔ΛࢦఆͰ͖Δɻ Tagsͷߏจ Tags { "TagName1" = "Value1"

    "TagName2" = "Value2" } λάͷ TagName1 ͕஋ͱͯ͠ Value1ɺλάͷ TagName2 ͕஋ͱͯ͠ Value2 Ͱ͋Δ͜ͱΛࢦఆ͍ͯ͠ Δɻλά͸޷͖ͳ͚ͩࢦఆͰ͖Δɻλά͸جຊతʹ Key-Value ϖΞɻPass ͷதͰλά͸ɺ౰֘ύε͕ϥ ΠςΟϯάύΠϓϥΠϯʢ؀ڥޫɺ௖఺ϥΠςΟϯάɺϐΫηϧϥΠςΟϯάʣͷதͰͲͷ໾ׂͰ͋Δ͔ Λ੍ޚͨ͠ΓɺଞͷύϥϝʔλʔΛ൑ఆ͢Δͷʹ࢖༻͢Δɻ Tags͸SubShaderɺPass಺ͷͲͪΒʹͰ΋ఆٛ͢Δ͜ͱ͕Ͱ͖Δ͕ɺ࣍ ͷλά͸ Unity ʹΑΓೝࣝ͞ΕɺSubShader಺ͷTagsͰͳ͘ඞͣPassη Ϋγϣϯͷதʹఆٛ͢Δɻ
 • LightMode λά • RequireOptions λά
  98. Pass಺ͷTags LightModeλά LightMode λά͸ϥΠςΟϯάύΠϓϥΠϯͰͷύεͷ໾ׂΛఆٛ͢Δɻ͜ΕΒͷλά͸खಈͰ࢖༻͞ΕΔ͜ͱ ͸كͰɺϥΠςΟϯάͱ૬ޓ࡞༻ͷ͋Δ΄ͱΜͲͷγΣʔμʔ͸ Surface Shader Ͱهड़͞Εɺͦ͏͍ͬͨৄࡉ͸ ͦͷதͰॲཧ͞ΕΔɻ LightMode

    Ͱར༻Մೳͳ஋ Always ৗʹϨϯμϦϯά͞ΕɺϥΠςΟϯά͸ద༻͞Εͳ͍ɻ ForwardBase Forward Rendering Ͱ࢖༻͞Εɺ؀ڥޫɺϝΠϯͷσΟϨΫγϣφϧϥΠτɺ௖఺/SH ϥ Πτ͕ద༻͞ΕΔɻ ForwardAdd Forward Rendering Ͱ࢖༻͞ΕɺϥΠτ͝ͱʹҰͭͷύεͰɺAdditive ϐΫηϧϥΠςΟ ϯά͕ద༻͞ΕΔɻ Deferred Deferred Shading Ͱ࢖༻͞Εɺg-όοϑΝΛඳը͠·͢ɻ ShadowCaster ΦϒδΣΫτͷσϓεΛγϟυ΢Ϛοϓ΍ਂ౓ςΫενϟʹඳը͠·͢ɻ
  99. Forward Renderingύε Forward Rendering ύε͸֤ΦϒδΣΫτΛɺΦϒδΣΫτʹ࡞༻͢ΔϥΠτʹ΋ͱ͍ͮͯϨϯμϦϯά͢Δɻϥ Πτͦͷ΋ͷ͸ɺઃఆ͓Αͼڧ౓ʹΑͬͯɺForward Rendering ʹΑΓผʑʹѻΘΕΔɻ Forward RenderingͰѻ͏ύεʹ͸


    ➤ ʲForwardBaseʳ LightModeͰࢦఆͰ͖ΔBase Pass • ΦϒδΣΫτͷϐΫηϧΛͻͱͭͷࢦ޲ੑϥΠτͱ࢒Γ͢΂ͯͷٿ໘ௐ ࿨Ͱܭࢉ͞ΕͨϥΠτͰϨϯμϦϯά͢Δɻ͜ͷύεͰ͸͞Βʹଞͷϥ ΠτϚοϓɺ؀ڥޫϥΠςΟϯάɺ͓ΑͼࣗݾൃޫϥΠςΟϯάΛ௥Ճ ͢Δɻ ➤ ʲForwardAddʳLightModeͰࢦఆͰ͖ΔAdditional Passes • ΦϒδΣΫτʹ࡞༻͢Δɺ֤ʑͷ௥Ճ͞ΕͨϐΫηϧϥΠτͰϨϯμϦ ϯά͞ΕΔɻ͜ΕΒͷύεͷϥΠτ͸ӨΛ࣋ͯͳ͍ʢ݁Ռͱͯ͠ Forward Rendering ͕αϙʔτ͢Δͷ͸ͻͱͭͷࢦ޲ੑϥΠτͱӨʣɻ 
 ͕͋Δɻ
  100. Deferred Renderingύε Deferred Shading (஗ԆγΣʔσΟϯά)Λ࢖༻͢Δ৔߹ɺΦϒδΣΫτʹӨڹΛ༩͑Δ͜ͱ͕Ͱ͖Δޫͷ਺ ʹ੍ݶ͸ͳ͍ɻ͢΂ͯͷޫ͸ɺͦΕΒ͕͢΂ͯ౳๏Ϛοϓ౳ͱਖ਼͘͠૬ޓ࡞༻͢Δ͜ͱΛҙຯ͠ɺϐΫηϧ ͝ͱʹධՁ͞ΕΔɻ͞Βʹɺ͢΂ͯͷޫ͸ɺΫοΩʔͱγϟυ΢Λ࣋ͭ͜ͱ͕Ͱ͖Δɻ
 Deferred Shading ͸ɺΞϯνΤΠϦΞγϯάͷͨΊͷ࣮ࡍͷαϙʔτ͕ͳ͘ɺ൒ಁ໌ͷΦϒδΣΫτΛॲཧ

    ͢Δ͜ͱ͸Ͱ͖ͳ͍ʢ͜ΕΒ͸ɺForward ϨϯμϦϯάΛ࢖༻ͯ͠ϨϯμϦϯά͞ΕΔʣɻ
 
 Deferred Shading Λ࢖༻͢Δ৔߹ɺUnity ͰͷϨϯμϦϯάϓϩηε͸2ͭͷύεͰߦΘΕΔɻ ➤ G-buffer Pass
 ΦϒδΣΫτ͸ɺdiffuse colorɺspecular colorɺsmoothnessɺ world space normalɺemission ͓Αͼ depth ͱεΫϦʔϯεϖʔεόοϑΝΛੜ੒͢ΔͨΊʹϨϯμϦϯά͞ΕΔ(G-bufferΛੜ੒͢ΔͨΊͷ ϨϯμϦϯά޻ఔ)ɻ ➤ Lighting pass
 G-buffer ΍σϓεʹج͍ͮͯর໌Λܭࢉ͢Δɻ 
 ͜ͷϓϩηε͕׬ྃͨ͠ޙɺdeferred shading Λѻ͏͜ͱ͕Ͱ͖ͳ͍γΣʔμʔΛ࣋ͭΦϒδΣΫτ͸ɺ Forward Rendering ύεΛ࢖༻ͯ͠ϨϯμϦϯά͞ΕΔ͜ͱʹͳΔɻ
  101. ܭࢉͷਫ਼౓ ➤ ϫʔϧυۭؒҐஔͱςΫενϟ࠲ඪͷ৔߹͸ɺfloatΛ࢖༻͢Δɻ ➤ ଞͷ͢΂ͯʢϕΫτϧɺHDRͷ৭ͳͲʣͷ৔߹͸ɺhalfΛ࢖༻͢ΔɻΑΓଟ͘ͷਫ਼౓͕ඞ ཁͳ৔߹ʹͷΈfloatʹ͢Δɻ ➤ ςΫενϟσʔλ্ͷඇৗʹ؆୯ͳૢ࡞Ͱ͸ɺfixedΛ࢖༻͢Δɻ ➤ ͋ΒΏΔݱ୅ͷσεΫτοϓ

    GPU ͸ɺৗʹ͢΂ͯͷࣄΛ׬શͳුಈখ਺఺਺ਫ਼౓Ͱܭࢉ͠ ͍ͯͯɺfloat/half/fixed Ͱ͸ɺ·ͬͨ͘ಉ݁͡ՌͰऴΘΔɻ͜ͷࣄ͕ςετΛࠔ೉ʹͯ͠ ͓Γɺhalf/fixed ͷਫ਼౓Ͱ࣮ࡍ͸े෼ͳͷ͔Ͳ͏͔ɺPC্Ͱ͸൑Γʹ͍͘ɻ࡞੒ͨ͠ γΣʔμʔΛɺ࣮ࡍʹϞόΠϧσόΠεͰࢼ͢ඞཁ͕༗Δɻ ➤ ϞόΠϧ GPU ͸൒ਫ਼౓ුಈখ਺఺਺Λαϙʔτ͓ͯ͠Γɺେ఍͸ܭࢉ͕ΑΓૣ͘ɺΑΓল ΤωͰ͋Δɻ ➤ fixedͷਫ਼౓͸ɺҰൠతʹݹ͍ϞόΠϧ GPU ͰͷΈ࢖͑Δɻݱ୅ͷଟ͘ͷ GPU(OpenGL ES 3 ΍ Metal Λ࣮ߦ͢Δ͜ͱ͕Ͱ͖Δ΋ͷ)͸ɺݻఆখ਺఺਺ͱ൒ਫ਼౓ුಈখ਺఺਺Λɺ಺ ෦తʹ͸·ͬͨ͘ಉ͡΋ͷͱͯ͠औΓѻ͏ɻΑͬͯ࢖͏ػձ͸গͳ͍ɻ
  102. Replaced Shaderͷಈ࡞ Χϝϥ͸γʔϯΛ௨ৗͲ͓ΓʹϨϯμϦϯά͢ΔɻΦϒδΣΫτ͸ϚςϦΞϧΛ࢖༻͢Δ ͕ɺ࣮ࡍʹ࢖༻͞ΕΔ͜ͱʹͳΔγΣʔμʔ͕มߋ͞ΕΔɻ ➤ replacementTag ͕ۭͷ৔߹ɺγʔϯͷ͢΂ͯͷΦϒδΣΫτ͸ࢦఆ͞Εͨ replaced Shader ͰϨϯμϦϯά͞ΕΔɻ

    ➤ replacementTag ͕ۭͰͳ͍৔߹ɺϨϯμϦϯά͞ΕΔ֤ΦϒδΣΫτ͸ • ΦϒδΣΫτͷγΣʔμʔ͸SubShader಺ͷλάʹreplacementTagΛ΋ͭSubShader Λݕࡧ͢Δɻ • replacementTagΛ࣋ͨͳ͍৔߹ɺΦϒδΣΫτ͸ ϨϯμϦϯά͞Εͳ͍ɻ • replacementTagΛݟ͚ͭͨ৔߹ɺͦͷSubShaderΛ࢖༻ͯ͠ΦϒδΣΫτ͕ϨϯμϦ ϯά͞ΕΔɻ ΋͢͠΂ͯͷγΣʔμʔ͕ɺྫ͑͹ɺ“RenderType” λά͕ “Opaque”ɺ“Transparent”ɺ “Background”ɺ“Overlay” ͷ஋Ͱ͋ͬͨ৔߹ɺෆಁ໌ͳΦϒδΣΫτͷΈϨϯμϦϯά͢ Δ Replacement Shader ΛɺRenderType=“Solid”Ͱ͋ΔͻͱͭͷαϒγΣʔμʔΛ࢖༻͠ ͯɺهड़͢Δ͜ͱ͕Ͱ͖Δɻͳ͓ɺ͢΂ͯͷ Unity ϏϧτΠϯγΣʔμʔ͸ “RenderType” λάͷηοτΛ͍࣋ͬͯΔɻ
  103. ϏϧτΠϯγΣʔμʔͰͷReplacement Shaderλά ͢΂ͯͷ Unity ϏϧτΠϯγΣʔμʔ͸“RenderType” λάͷηοτΛ࣋ͪɺReplaced Shader Ͱ࢖༻͢Δ͜ͱ͕ Ͱ͖Δɻ ➤

    Opaque
 
 ΄ͱΜͲͷγΣʔμʔ(NormalɺSelf IlluminatedɺReflectiveɺTerrain γΣʔμʔ)ɻ ➤ Transparent
 
 ΄ͱΜͲͷ෦෼తʹಁաͳγΣʔμʔ(TransparentɺύʔςΟΫϧɺϑΥϯτɺTerrain ௥Ճύε γΣʔμʔ)ɻ ➤ TransparentCutout
 
 ϚεΩϯά͞ΕͨಁաγΣʔμʔ(Transparent Cutoutɺ2 ύε২ੜγΣʔμʔ)ɻ ➤ Background
 
 Skybox γΣʔμʔɻ ➤ Overlay
 
 GUITextureɺϋϩʔɺϑϨΞγΣʔμʔɻ ➤ TreeOpaque
 
 Terrain Τϯδϯ Tree ͷथൽɻ ➤ TreeTransparentCutout
 
 Terrain Τϯδϯ Tree Leavesɻ ➤ TreeBillboard
 
 Terrain ΤϯδϯϏϧϘʔυͷ Treeɻ ➤ Grass
 
 Terrain Τϯδϯͷ Grassɻ ➤ GrassBillboard
 
 Terrain ΤϯδϯϏϧϘʔυͷ Grassɻ
  104. ࢖༻ྫ camera.SetReplacementShader (shadow, "Shadow"); Tags { "RenderType"="Opaque" "Queue"="Geometry" "Shadow"="Character" }

    Tags { "RenderType"="Opaque" "Queue"="Geometry" "Shadow"="Character" } shadowcamera.cs shadow.shader character.shader Replace͠ͳ͍৔߹ɺΧϝϥ͸charactor.shaderΛ࢖ͬͯΩϟϥΫλʔΛϨϯμϦϯά͢Δɻ shadowcamera.csͰ͜ͷΑ͏ʹ͢ΔͱUnityଆ͕ShadowλάΛ֬ೝ͜͜͠Ͱ͸Character͕ڞ௨ͳͨΊɺ ΩϟϥΫλʔ͕shadow.shaderΛ࢖ͬͯඳը͞ΕΔ(charactor.shader͕shadow.shaderʹஔ͖׵ΘΔ)ɻ 
 ଞͷShadowλά͕ଘࡏ͠ͳ͍γΣʔμ͸ඳը͕ແࢹ͞Ε·͢ɻ
  105. ϚςϦΞϧΠϯεϖΫλʔͷΧελϚΠζ ΧελϜΤσΟλʔΛఆٛ͢Δʹ͸ɺ ShaderGUI ͷΫϥεΛܧঝ͠ɺΞηοτͷ֊૚಺ʹ͋Δ Editor ϑΥϧμʔͷԼʹɺεΫϦϓτΛஔ͘ɻ Shader "Custom/Redify" { Properties

    { _MainTex ("Base (RGB)", 2D) = "white" {} } SubShader { Tags { "RenderType"="Opaque" } LOD 200 CGPROGRAM #pragma surface surf Lambert addshadow #pragma shader_feature REDIFY_ON sampler2D _MainTex; struct Input { float2 uv_MainTex; }; void surf (Input IN, inout SurfaceOutput o) { half4 c = tex2D (_MainTex, IN.uv_MainTex); o.Albedo = c.rgb; o.Alpha = c.a; #if REDIFY_ON o.Albedo.gb *= 0.5; #endif } ENDCG } CustomEditor "CustomShaderGUI" } using UnityEngine; using UnityEditor; using System; public class CustomShaderGUI : ShaderGUI { public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties) { base.OnGUI(materialEditor, properties); Material targetMat = materialEditor.target as Material; // see if redify is set, and show a checkbox bool redify = Array .IndexOf(targetMat.shaderKeywords, "REDIFY_ON") != -1; EditorGUI.BeginChangeCheck(); redify = EditorGUILayout.Toggle("Redify material", redify); if (EditorGUI.EndChangeCheck()) { // enable or disable the keyword based on checkbox if (redify) targetMat.EnableKeyword("REDIFY_ON"); else targetMat.DisableKeyword("REDIFY_ON"); } } } γΣʔμʔͷઃఆʹ REDIFY_ON ͱ͍͏Ωʔϫʔυ͕͋Δɻ͜Ε͸ɺϚςϦΞϧͷΠϯεϖΫλʔΛ ֦ு͢Δ͜ͱͰϚςϦΞϧ͝ͱʹΩʔϫʔυͷ༗ޮ/ແޮΛ੾Γସ͑Δ͜ͱ͕Ͱ͖Δɻ ͨͩ͠ɺ্هͷ؆୯ͳྫ͸MaterialPropertyDrawersΛ࢖ͬͯɺ͞Βʹ୯७ԽͰ͖Δɻ
  106. Depth Texture࢖༻ྫ public class RenderDepthBuffer : MonoBehaviour { private RenderTexture

    m_colorTex; private RenderTexture m_depthTex; public Material m_postRenderMat; void Start () { Camera cam = GetComponent<Camera>(); cam.depthTextureMode = DepthTextureMode.Depth; // ΧϥʔόοϑΝ༻ RenderTexture m_colorTex = new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.ARGB32); m_colorTex.Create(); // σϓεόοϑΝ༻ RenderTexture m_depthTex = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.Depth); m_depthTex.Create(); // cameraʹΧϥʔόοϑΝͱσϓεόοϑΝΛηοτ͢Δ cam.SetTargetBuffers(m_colorTex.colorBuffer, m_depthTex.depthBuffer); } void OnPostRender() { // RenderTargetແ͠ɿը໘ʹग़ྗ͞ΕΔ Graphics.SetRenderTarget(null); // σϓεόοϑΝΛඳը͢Δ(m_postRenderMat͸ςΫενϟը૾Λͦͷ··ඳը͢ΔϚςϦΞϧ) Graphics.Blit(m_depthTex, m_postRenderMat); } }
  107. σϓεςΫενϟϔϧϓϚΫϩ ➤ UNITY_TRANSFER_DEPTH(o)
 
 ௖఺ͷ Eye Space Depth ʢࢹ఺ۭؒσϓεʣΛܭࢉ͠ɺo ʢ

    float2 Ͱ͋Δ͜ͱ͕ඞਢʣʹग़ྗ͢Δɻσϓε ςΫενϟʹରͯ͠ϨϯμϦϯά͢Δͱ͖ʹ௖఺ϓϩάϥϜͰ࢖༻͢ΔɻωΠςΟϒσϓεςΫενϟͷ ͋ΔϓϥοτϑΥʔϜͰ͸ɺZ όοϑΝͷ஋͸҉໧తʹϨϯμϦϯά͞ΕΔͨΊɺ͜ͷϚΫϩ͸Կ΋͠ͳ ͍ɻ ➤ UNITY_OUTPUT_DEPTH(i)
 
 ௖఺ͷ Eye Space Depth Λi (float2 Ͱ͋Δ͜ͱ͕ඞਢ)͔Βฦ͢ɻσϓεςΫενϟʹରͯ͠ϨϯμϦϯά͢ Δͱ͖ʹɺϑϥάϝϯτϓϩάϥϜͰ࢖༻͢ΔɻωΠςΟϒσϓεςΫενϟͷ͋ΔϓϥοτϑΥʔϜͰ ͸͜ͷϚΫϩ͸ɺZ όοϑΝ஋͕҉໧తʹϨϯμϦϯά͞ΕΔͨΊɺৗʹ 0 Λฦ͢ɻ ➤ COMPUTE_EYEDEPTH(o)
 
 ௖఺ͷ Eye Space Depth Λܭࢉ͠ɺoʹग़ྗ͢Δɻ௖఺ϓϩάϥϜͰɺσϓεςΫενϟʹ ϨϯμϦϯά͠ ͳ͍ͱ͖ʹɺ࢖༻͢Δɻ ➤ DECODE_EYEDEPTH(i)
 
 σϓεςΫενϟi ͔Βߴਫ਼౓ͷ஋Λड͚औΓɺ֘౰͢Δ Eye Space Depth Λฦ͢ɻ͜ͷϚΫϩ͸ Direct3D Ͱ͸୯ʹi*FarPlane Λฦ͢ɻωΠςΟϒσϓεςΫενϟͷ͋ΔϓϥοτϑΥʔϜͰ͸͜ͷϚΫϩ͸ɺΧ ϝϥͷൣғʹҰக͢ΔΑ͏ʹɺ஋ΛઢܗԽ֦͠ு͢Δɻ

  108. DepthΛϨϯμϦϯά͢ΔγΣʔμʔ Shader "Render Depth" { SubShader { Tags { "RenderType"="Opaque"

    } Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct v2f { float4 pos : SV_POSITION; float2 depth : TEXCOORD0; }; v2f vert (appdata_base v) { v2f o; o.pos = mul (UNITY_MATRIX_MVP, v.vertex); UNITY_TRANSFER_DEPTH(o.depth); return o; } half4 frag(v2f i) : SV_Target { UNITY_OUTPUT_DEPTH(i.depth); } ENDCG } } }
  109. LODͱ͸ ➤ Level of Detail ➤ ΧϝϥͱΦϒδΣΫτͷڑ཭͕཭Εͨ৔߹ʹϨϯμϦϯά͢Δ ϙϦΰϯͷ਺ΛݮΒ͢ࣄ͕Ͱ͖Δɻ ➤ Unity

    Ͱ͸ɺLOD Group ίϯϙʔωϯτΛ࢖͏ͱΦϒδΣΫ τʹରͯ͠ LOD ϨϯμϦϯάΛηοτΞοϓͰ͖Δɻ
  110. Shader LOD γΣʔμʔ Level of Detail (LOD) ͸ɺLOD ஋͕ܾΊΒΕ͖͍ͨ͠஋ҎԼͷ৔߹ʹɺγΣʔμʔ͋Δ͍͸ αϒγΣʔμʔΛ࢖༻ͯ͠ඳը͢Δɻ

    σϑΥϧτͰ͸ɺڐ༰͞ΕΔ LOD Ϩϕϧ͸ແݶͰ͋Γɺ͢ͳΘͪϢʔβʔͷϋʔυ΢ΣΞͰαϙʔτ͞ ΕΔ͢΂ͯͷγΣʔμʔΛ࢖༻͢Δ͜ͱ͕Ͱ͖Δɻ͔͠͠ɺ͍͔ͭ͘ͷέʔεͰ͸ɺͨͱ͑ϋʔυ΢ΣΞ ͕αϙʔτ͢Δ৔߹Ͱ͋ͬͯ΋ɺγΣʔμʔͷσΟςʔϧΛམͱ͍ͨ͠৔߹͕͋Δɻͦ͏͍ͬͨ৔߹ʹ LODΛར༻͢Δɻ 
 Shader LOD ͸εΫϦϓτʹΑͬͯݸผͷγΣʔμʔ͝ͱʹઃఆʢ Shader.maximumLOD ʣ͢Δ͜ͱ΋ɺ ͢΂ͯͷγΣʔμʔͰάϩʔόϧʹ࢖༻͢Δʢ Shader.globalMaximumLOD ʣ͜ͱ΋Մೳɻ
 
 Ϣʔβʔ࡞੒ͷΧελϜͷγΣʔμʔͰ͸ɺLOD ίϚϯυΛ࢖༻ͯ͠ͲͷΑ͏ͳαϒγΣʔμʔͰ΋ LOV ஋Λઃఆ͢Δ͜ͱ͕Ͱ͖Δɻ
 
 Unity ͷϏϧτΠϯγΣʔμʔ͸ LOD Λ࣍ͷΑ͏ʹઃఆ͞Ε͍ͯΔɻ ➤ VertexLit ͷΑ͏ͳγΣʔμʔ = 100 ➤ DecalɺReflective VertexLit = 150 ➤ Diffuse = 200
 ➤ Diffuse DetailɺReflective Bumped UnlitɺReflective Bumped VertexLit = 250 ➤ BumpedɺSpecular = 300 ➤ Bumped Specular = 400
 ➤ Parallax = 500
 ➤ Parallax Specular = 600