Share
Embed
Unityのシェーダーについて自分なりにまとめてみた
Unityのシェーダーについて自分なりにまとめてみました。
まだまだ書ききれていない項目が多いです。
Cg/HLSL部分などはリファレンスに丸投げしてます。
Es_Program
March 06, 2016
Transcript
228 slides
-
“ ➤ UnityΤϯδχΞ ➤ C#Ͱ৭ʑ࡞ͬͯ·͢ ➤ ࠷ۙγΣʔμʔͷษڧ͡ Ί·ͨ͠ɻ Es About
Me • http://esbrog.hatenablog.com/ • https://github.com/EsProgram • https://twitter.com/Es_Program -
γΣʔμʔͷجຊ ➤ UnityͷγΣʔμʔʹ͓͚Δج ຊతͳ߲ʹ͍ͭͯݟ͍ͯ͘ɻ ➤ ͜͜ͰUnityͷϨϯμϦϯά ύΠϓϥΠϯΧϝϥɺuGUI ʹ͍ͭͯܰ͘৮ΕɺγΣʔμʔ ͷجຊߏจʹ͍ͭͯղઆ͢Δɻ •
ʲUnityϚχϡΞϧʳShaderLab ϦϑΝϨϯε • ʲԜTipsʳUnity ͷγΣʔμͷجૅΛษڧͯ͠ΈͨͷͰΔ ؾग़ͯ͠·ͱΊͯΈͨ • ʲΏʹͯ͘උʳUnity Shader ·ͱΊ -
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 Λ༻͢Δɻ -
جຊߏจ࣮ྫ Shader "Structure Example" { Properties { // ΠϯεϖΫλʹදࣔ͞ΕΔϓϩύςΟʢ৭ςΫενϟʣ }
SubShader { // .../ϑϥάϝϯτϓϩάϥϜΛ༻͢ΔαϒγΣʔμ... } SubShader { // ...ύε͝ͱʹ 4 ͭͷςΫενϟΛ༻͢ΔαϒγΣʔμ... } SubShader { // ...ύε͝ͱʹ 2 ͭͷςΫενϟΛ༻͢ΔαϒγΣʔμ... } SubShader { // …ݟͨѱ͍͕ɺ͍ͣΕͰ࣮ߦ͢ΔαϒγΣʔμ… } Fallback "Other Shader Name" // SubShader ͕͍ͣΕμϝͩͬͨͱ͖ʹࢼ͢ผͷγΣʔμ } -
Category߲ ΧςΰϦཧతάϧʔϐϯάɻ͜ Εଟ͘ͷ߹ɺϨϯμϦϯάঢ়ଶ Λܧঝ͢Δͷʹ༻͞ΕΔɻྫ͑ɺ γΣʔμʔʹෳͷSubshader͕͋ Δ߹ɺͦΕͧΕ͕Fog ΛΦϑʹ ͠ɺBlendingΛՃʹࢦఆ͢Δඞཁ ͕͋Δ߹ʹɺΧςΰϦΛͬͯ ̍ͭͷهड़ͰࢦఆΛߦ͏͜ͱ͕Ͱ͖
Δɻ Shader "example" { Category { Fog { Mode Off } Blend One One SubShader { // ... } SubShader { // ... } // ... } } ΧςΰϦϒϩοΫɺγΣʔμʔͷߏจղੳʹͷΈӨڹ͢Δ͕ɺԼͷͯ͢ͷϒ ϩοΫʹΧςΰϦͰઃఆͨ͠ঢ়ଶΛߏจղੳ͢Δͷͱ·ͬͨ͘ಉ͡ʹͳΔɻ γΣʔμʔͷ࣮ߦʹҰӨڹ͠ͳ͍ɻ -
Propertiesͱ ➤ γΣʔμʔʹΑΓɺUnity ͷ ϚςϦΞϧΠϯεϖΫλʔ ্ ͰɺΞʔςΟετ͕ηοτͰ͖ΔมͷҰཡΛఆٛͰ͖Δɻ ➤ ϓϩύςΟʔ γΣʔμʔϑΝΠϧͷ
Properties ϒϩοΫͰ ఆٛ͢Δɻ Tint͔ΒColor Mask·ͰશͯϓϩύςΟɻ ϓϩύςΟϚςϦΞϧΠϯεϖΫλʔͰ Λฤू͢Δ͜ͱ͕Մೳɻ -
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Λෳఆٛ͢Δ͜ͱ͕Ͱ͖Δɻ -
Property name ("display name",type) = value Propertyͷߏจ ➤ name γΣʔμʔϓϩάϥϜ্Ͱར༻͢Δม໊ɻΞϯμʔείΞ
(_)Ͱ։࢝͢Δɻ ➤ display name ϚςϦΞϧΠϯεϖΫλʔͰදࣔ͢Δจࣈྻ ➤ type มͷܕ ➤ value ॳظ -
ܕ͝ͱͷϓϩύςΟهड़ྫ 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ίϯϙʔωϯτɹϕΫτϧͷϓϩύςΟΛఆٛɻ ςΫενϟܥϓϩύςΟʔͷoptionsUnity 5 Ͱআ͞ΕͨͷͰɺۭͷ{}Λఆٛ͢Δ -
ࣗಈ࣮ͷϓϩύςΟ ➤ ϓϩύςΟͱͯ͠ઃఆ͞Ε֤ͨςΫενϟͷͨΊʹɺ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Ͱఆٛ͞Ε͍ͯΔɻ -
γΣʔμʔ͔ΒϓϩύςΟʹΞΫηε ➤ 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 {...} } ྫ -
γΣʔμʔ͔ΒϓϩύςΟʹΞΫηε ➤ 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; …. } } } -
Property Attributes ➤ γΣʔμʔͷϓϩύςΟʹଐੑΛՃ͢Δ͜ͱ͕Ͱ͖Δɻ ➤ ͦͷଐੑʹैͬͯɺΠϯεϖΫλʔͷදࣔΛมߋͰ͖Δɻ Properties { [KeywordEnum(None, Add,
Multiply)] _Overlay ("Overlay mode", Float) = 0 [Toggle] _Invert ("Invert color?", Float) = 0 } ྫ -
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ͷΠϯεϖΫλʔදࣔΛτάϧʹ͠·͢ɻfloat0͔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 σϑΥϧτͰ༻Ͱ͖Δͷ(Ұ෦) -
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 // .. σϑΥϧτͰ༻Ͱ͖Δͷ(Ұ෦) -
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 σϑΥϧτͰ༻Ͱ͖Δͷ(Ұ෦) -
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; } } } -
SubShaderͱ ➤ γΣʔμʔ̍ͭͷߏ୯Ґ ➤ αϒγΣʔμʔͰϨϯμϦϯάύεͷҰཡΛఆٛ͠ɺҙͷΦϓγϣϯͱ ͯͯ͢͠ͷύεʹڞ௨ͷ State ʢঢ়ଶʣΛઃఆ͢Δɻ ➤ ύεఆٛͰهड़Ͱ͖ΔϨϯμϦϯάεςʔταϒγΣʔμʔͷهड़ϒϩο
ΫͰॻ͘͜ͱ͕Ͱ͖Δɻ͜ΕʹΑΓͯ͢ͷύε͕”ڞ༗ͷ” State (ঢ়ଶ) Λ༻͢Δ͜ͱ͕Ͱ͖Δ(྆ํʹهड़ͨ͠߹PassͷState͕༏ઌ)ɻ ➤ ֤ύεͷఆٛ Regular PassɺUse PassɺGrab Pass ͷ͍ͣΕ͔ɻ ➤ SubShaderͷTag ΛઃఆͰ͖Δɻ -
SubShaderߏจ Subshader { [Tags] [CommonState] Passdef [Passdef ...] } ߏจ
➤ Tags • SubShaderͷTagsɻͲͷΑ͏ʹͯ͠ϨϯμϦϯάΤϯδϯͰϨϯμϦϯά͢Δ ͔Λࢦఆ͢Δɻ ➤ CommonState • ͞·͟·ͳάϥϑΟοΫεϋʔυΣΞͷ State ʢঢ়ଶʣΛઃఆͰ͖ΔɻPass Ͱهड़Ͱ͖ΔεςʔτϝϯτͱಉҰ(εςʔτ͕༗ޮͳείʔϓ͕ҧ͏)ɻ ➤ Passdef • Regular PassɺUse Passɺ·ͨ Grab Pass ͷ͍ͣΕ͔ -
SubShaderͷTags SubShaderTagsΛ༻͍ͯͭ͠ͲͷΑ͏ʹͯ͠ϨϯμϦϯάΤϯδϯʹΑ ΓϨϯμϦϯά͖͔͢ΛࢦఆͰ͖Δɻ Tagsͷߏจ Tags { "TagName1" = "Value1" "TagName2"
= "Value2" } λάͷ TagName1 ͕ͱͯ͠ Value1ɺλάͷ TagName2 ͕ͱͯ͠ Value2 Ͱ͋Δ͜ͱΛࢦఆ͍ͯ͠ Δɻλά͖ͳ͚ͩࢦఆͰ͖Δɻλάجຊతʹ Key-Value ϖΞɻSubShaderͷதͰλάɺ SubShader࣮ߦॱংଞͷύϥϝʔλʔΛఆ͢Δͷʹ༻͢Δɻ TagsSubShaderɺPassͷͲͪΒʹͰఆٛ͢Δ͜ͱ͕Ͱ͖Δ͕ɺ࣍ͷλά Unity ʹΑΓ ೝࣝ͞ΕɺPassͷTagsͰͳ͘ඞͣSubShaderηΫγϣϯͷதʹఆٛ͢Δɻ • Queue λά • RenderType λά • DisableBatching λά • ForceNoShadowCasting λά • IgnoreProjector λά • CanUseSpriteAtlas λά • RenderType λά -
SubShaderͷTags Queueλά QueueʹΑΓΦϒδΣΫτΛඳը͢Δॱ൪ΛࢦఆͰ͖ΔɻγΣʔμʔΦϒδΣΫτ͕ଐ͢ΔϨϯμϦϯά ΩϡʔΛఆ͠ɺॱʹϨϯμϦϯάΛߦ͍·͢ɻྫ͑ɺಁ໌ͳΦϒδΣΫτෆಁ໌ͳΦϒδΣΫτͷޙ ʹඳը͞ΕΔ͖ͳͷͰQueueλάͰॱ൪Λࢦఆ͢Δඞཁ͕͋Γ·͢ɻ ఆٛࡁΈͷϨϯμϦϯάΩϡʔ ➤ Background ͜ͷϨϯμϦϯάΩϡʔଞΑΓલʹϨϯμϦϯά͞ΕΔɻSkybox
ͳͲʹ༻͞ΕΔɻ ➤ Geometry (default) ΄ͱΜͲͷΦϒδΣΫτʹ༻͞ΕΔɻෆಁ໌ͳδΦϝτϦ͜ͷΩϡʔΛ༻͢Δɻ ➤ AlphaTest ΞϧϑΝςετ͢Δܗঢ়Ͱ͜ͷΩϡʔΛ༻͢ΔɻGeometry ͱผͷΩϡʔͰ͋ΔͷΞϧϑΝςετ ͷΦϒδΣΫτΛͯ͢ͷෆಁ໌ͷΦϒδΣΫτͷޙʹඳը͢Δ΄͏͕ޮతͰ͋ΔͨΊɻ ➤ Transparent ͜ͷϨϯμϦϯάΩϡʔGeometry ͱAlphaTest ͷޙʹɺޙΖʹ͋Δͷ͔Βઌʹॱ൪ͰϨϯμϦϯά͞ ΕΔɻΞϧϑΝϒϨϯσΟϯά͢Δͷʢ͢ͳΘͪɺσϓεόοϑΝʹॻ͖ࠐΈ͠ͳ͍γΣʔμʔʣ͢ ͯɺ͜͜ʹ͋Δ͖ʢΨϥεɺύʔςΟΫϧΤϑΣΫτʣɻ ➤ Overlay ͜ͷϨϯμϦϯάΩϡʔΦʔόʔϨΠΤϑΣΫτͷͨΊͷͷɻ࠷ޙʹϨϯμϦϯά͢Δͷ͜͜ʹ ͋Δ͖ʢྫ͑ɺϨϯζϑϨΞʣɻ -
SubShaderͷTags Queueλά ಛผͳ༻్ʹ͓͍ͯɺؒʹΩϡʔΛ༻͢Δ͜ͱ͕Ͱ͖Δɻ֤Ωϡʔ ෦ͰΠϯσοΫεʹΑΓදݱ͞ΕɺBackground 1000ɺ Geometry 2000ɺAlphaTest
2450ɺTransparent 3000ɺͦͯ͠ Overlay 4000 ͱ͍͏Ͱදݱ͞ΕΔɻ͠γΣʔμʔ͕ͭ͗ͷΑ ͏ͳΩϡʔΛ༻͢Δ߹ Tags { "Queue" = "Geometry+1" } ͜ΕʹΑΓΦϒδΣΫτͯ͢ͷෆಁ໌ͷΦϒδΣΫτͷޙʹϨϯμϦ ϯά͞ΕΔ͕ɺϨϯμϦϯάΩϡʔ͕ 2001 ʢGeometry ʹ 1 ΛՃࢉʣͷͨ ΊɺಁաΦϒδΣΫτΑΓઌʹϨϯμϦϯά͞ΕΔɻ -
SubShaderͷTags DisableBatchingλά ͍͘Β͔ͷγΣʔμʔ(େΦϒδΣΫτεϖʔεͰΛมܗͤ͞Δ ͷ)ɺυϩʔίʔϧόονϯά Λ༻͍ͯ͠Δͱɺಈ࡞͠ͳ͍ɻ υϩʔίʔϧόονϯάɺͯ͢ͷδΦϝτϦΛϫʔϧυεϖʔεʹม ͢ΔͷͰɺ“object space” ͕ࣦΘΕΔͨΊͰ͋Δɻ
DisableBatching λάΛͬͯυϩʔίʔϧόονϯάͷ༗ޮ/ແޮΛࢦࣔ Ͱ͖ΔɻઃఆՄೳͳͷҎԼ3ͭɻ ➤ True ͜ͷγΣʔμʔͰৗʹόονϯάΛແޮʹ͢Δɻ ➤ False όονϯάΛແޮʹ͠ͳ͍ɻσϑΥϧτ͜Εɻ ➤ LODFading LOD fading ͕ΞΫςΟϒͳ࣌όονϯάΛແޮʹ͢Δɻେथ ͰΘΕΔɻ -
SubShaderͷTags ForceNoShadowCastingλά ͠ ForceNoShadowCasting ͕༻͞Ε͍ͯͯɺ͕ “True” Ͱ͋Δ ߹ɺ͜ͷαϒγΣʔμʔΛ༻͢ΔΦϒδΣΫτγϟυΛӨͳ͍ɻ ͜ΕɺಁաΦϒδΣΫτʹ Shader
Replacement Λ༻͍ͯ͠Δ߹ ɺผͷαϒγΣʔμʔ͔ΒγϟυύεΛܧঝͨ͘͠ͳ͍߹ʹศརɻ -
SubShaderͷTags IgnoreProjectorλά ͠ IgnoreProjector ͕༻͞Ε͍ͯͯɺ͕ “True” Ͱ͋Δ߹ɺ͜ͷ γΣʔμʔΛ༻͢ΔΦϒδΣΫτ ϓϩδΣΫλʔ ʹΑΓӨڹ͞Εͳ
͍ɻ͜ΕɺProjectors ͕Өڹ͢ΔͨΊͷΑ͍ํ๏͕ͳ͍ͨΊɺ෦తʹ ಁաͰ͋ΔΦϒδΣΫτͰศརɻ -
ϨϯμϦϯάεςʔτͱ ➤ ͞·͟·ͳάϥϑΟοΫεϋʔυΣΞͷ State ʢঢ়ଶʣΛϨ ϯμϦϯάεςʔτͱ͍͏ɻ ➤ ྫ͑ΞϧϑΝϒϨϯσΟϯάΛ༗ޮԽͨ͠ΓɺϑΥάΛ༻ ͨ͠Γͱ͍͏͜ͱ͕Ͱ͖Δɻ ➤
SubShaderɺ·ͨPassͰར༻Ͱ͖Δɻ ➤ ҎԼͰɺϨϯμϦϯάεςʔτΛઃఆͰ͖ΔίϚϯυΛղ આ͢Δɻ -
ϨϯμϦϯάεςʔτ ZTest ਂόοϑΝʔͷςετϞʔυΛઃఆ͢ΔɻৄࡉΧϦϯάͱਂςε τͰɻ ZTest (Less | Greater | LEqual
| GEqual | Equal | NotEqual | Always) -
ϨϯμϦϯάεςʔτ ColorMask Χϥʔͷॻ͖ࠐΈʹϚεΫΛઃఆ͢Δɻ”ColorMask 0”ͱهࡌ͢Δ͜ͱͰ ͯ͢ͷΧϥʔνϟωϧͷϨϯμϦϯάΛແޮԽ͢Δɻ σϑΥϧτϞʔ υRGBAͯ͢ʹॻ͖ࠐΈ͢Δɻ ϚεΫΛઃఆ͢Δ͜ͱͰɺઃఆͨ͠৭͕ϨϯμϦϯά͞ΕΔΑ͏ʹͳΔɻ ColorMask RGB
| A | 0 | any combination of R, G, B, A -
Fallback ͯ͢ͷ Subshader ͷࢼߦޙɺFallback Λఆٛ͢Δ͜ͱ͕Ͱ͖Δɻجຊత ͳҙຯͱͯ͠“͜ͷϋʔυΣΞͰ࣮ߦͰ͖ΔαϒγΣʔμʔ͕ͳ͍ ߹ɺผͷγΣʔμʔ͔Βࢼ͢”ɻ Fallback "name" ߏจ
nameͰࢦఆ͞ΕͨShaderΛFallbackͱͯ͠ఆٛ͢Δɻ OffΛࢦఆ͢Δ͜ͱͰ͖Δɻ Fallback Off ͜ͷ߹໌ࣔతʹ Fallback ͕ͳ͘ɺͨͱ͑͜ͷϋʔυΣΞͰ࣮ߦͰ ͖ΔαϒγΣʔμʔ͕ͳ͔ͬͨͱͯ͠ɺܯࠂΛग़ྗ͠ͳ͍ͱ͍͏ҙຯ ʹͳΔɻ -
Pass ➤ SubShaderλάͰఆٛ͢Δ Passʹ͍ͭͯղઆ͢Δɻ • ʲUnityϚχϡΞϧʳShaderLab ϦϑΝϨϯε • ʲKCSʳ۶ંޮՌΛUnityͰʂ •
ʲ͏ʹͳʳΤϑΣΫτ༻ͷγΣʔμʹؔ͢ΔΞΠσΞ • ʲMarveric ProjectʳϝϞॻ͖ -
Passͷ֓ཁ ➤ Pass ϒϩοΫΦϒδΣΫτܗঢ়ΛҰճͷΈϨϯμϦϯά͢ΔΑ͏ʹ͢Δɻ ➤ ෳύεͷϨϯμϦϯάΛ͢Δ߹ɺ̍ͭͷSubShaderʹෳͷPassΛهड़͢Δɻ ➤ PassϒϩοΫͰར༻Ͱ͖ΔεςʔτϨϯμϦϯάεςʔτΛࢀরɻ ➤ PassϒϩοΫͰར༻Ͱ͖ΔλάPassͷTagsΛࢀরɻ
➤ PassϒϩοΫͰར༻Ͱ͖Δεςϯγϧʹ͍ͭͯStencilΛࢀরɻ ➤ PassϒϩοΫͰར༻Ͱ͖ΔϒϨϯσΟϯάʹ͍ͭͯBlendingΛࢀরɻ ➤ Render SetupݻఆؔγΣʔμʔ/αʔϑΣεγΣʔμʔ/ɾϑϥάϝϯτγΣ ʔμʔΛࢀরɻ Pass { [Name and Tags] [RenderSetup] } ߏจ -
UsePass ➤ UsePassผͷγΣʔμʔͷPassΛ͏ͨΊͷίϚϯυɻ ➤ ผͷγΣʔμʔͰPass Name͕ఆٛ͞Ε͍ͯΔඞཁ͕͋Δɻ ➤ Shader/Name ʹɺεϥογϡͰ۠ΒΕͨγΣʔμʔͷ໊ લͱύεͷ໊લΛࢦఆ͢Δɻ
➤ ෦ͰPass Nameͯ͢େจࣈʹͳ͍ͬͯΔ͜ͱʹҙ͢ Δɻ UsePass "Shader/Name" ߏจ UsePass "VertexLit/SHADOWCASTER" ྫ -
GrabPass ➤ GrabPassಛผͷύελΠϓͰɺGrabPass࣮ߦ࣌ʹطʹϨϯμϦϯά͞Ε͍ͯΔը૾ΛςΫενϟͱ ͯ͠༻Ͱ͖ΔΑ͏ʹ͢Δ(ݱࡏ·ͰͷϨϯμϦϯά݁ՌΛ௫ΈऔΔ)ɻ ➤ ௫ΈऔͬͨςΫενϟޙଓͷPassͰར༻͢Δ͜ͱ͕Ͱ͖Δɻ ➤ ͍ͣΕ͔ͷॻࣜΛར༻͢Δɻ • GrabPass
{ } ͱࢦఆ͠ɺ୯ʹݱࡏͷը໘ίϯςϯπΛςΫενϟʹ௫ΈͱΔɻ͜ͷςΫενϟ ޙଓͷύεͰ _GrabTexture ͰΞΫηε͢Δ͜ͱ͕Ͱ͖Δɻҙɿ͜ͷܗࣜͷGrap Passը໘ ͷ௫ΈͱΓॲཧΛ༻͢Δ֤ΦϒδΣΫτ͝ͱʹߦ͏ɻྫ͑͜ͷܗࣜͷGrabPassΛ࣋ͭಉ͡ ϚςϦΞϧΛద༻ͨ͠2ͭͷΦϒδΣΫτΛϨϯμϦϯά͢Δ߹ɺ̎ͭͷ_GrabTexture͕ੜ ͞ΕΔ͜ͱʹͳΔɻ • GrabPass { "TextureName" } ʹΑΓը໘ίϯςϯπΛςΫενϟʹ௫ΈऔΔɻϑϨʔϜ͝ͱʹ 1͚ͩɺࢦఆͷςΫενϟ໊Λ༻͢Δ࠷ॳͷΦϒδΣΫτʹͷΈ࣮ࢪ͢ΔɻςΫενϟ ޙଓͷύεͰࢦఆͷςΫενϟ໊ͰΞΫηε͢Δ͜ͱ͕Ͱ͖ΔɻγʔϯͰ Grab Pass Λ༻͢Δ ΦϒδΣΫτ͕ෳ͋Δ߹ʹޮతͳํ๏ɻ ➤ GrabPassͰNameTags ίϚϯυΛ༻͢Δ͜ͱ͕Ͱ͖ΔɻRegular PassͰͷར༻ͱಉ͡༻్ɻ -
GrabPassͷྫ Shader "GrabPassInvert" { SubShader { // શͯͷෆಁ໌ͳܗঢ়ͷޙʹɺࣗΛඳը͠·͢ Tags {
"Queue" = "Transparent" } // ΦϒδΣΫτΑΓޙΖͷγʔϯΛ_GrabTextureʹ௫ΈऔΓ·͢ GrabPass { } // ্Ͱੜͨ͠ςΫενϟͰΦϒδΣΫτΛϨϯμϦϯά͠ɺΧϥʔΛస͠·͢ɻ Pass { SetTexture [_GrabTexture] { combine one-texture } } } } લʹϨϯμϦϯά͞ΕͨΧϥʔΛస͢Δ ͨΊʹGrabPassΛར༻͢Δྫ -
ݻఆؔγΣʔμʔ ➤ Ұ൪جຊతͳUnityγΣʔμʔ Ͱ͋ΔݻఆؔγΣʔμʔʹ ͍ͭͯݟ͍ͯ͘ɻ ➤ Ͱ͖Δ͜ͱগͳ͘ɺඪ४Ͱ උΘ͍ͬͯΔϚςϦΞϧͱಉ ఔͷදݱ͕ݶքͳͷͰɺج ຊతͳίʔυͷհʹͱͲΊ
͓ͯ͘ɻ • ʲUnityϚχϡΞϧʳShaderLab ϦϑΝϨϯε -
ݻఆؔγΣʔμʔͱ ➤ ݻఆؔγΣʔμʔɺݹ͍ϋʔυΣΞͷͨΊʹهड़͢Δɻ ➤ ݻఆؔγΣʔμʔϑϥάϝϯτγΣʔμ·ͨαʔϑΣΠ εγΣʔμͷޙͷ n ൪ͷ fallback ͱͯ͠هड़ͯ͠ɺݹ͍ϋʔ
υΣΞͰ࣮ߦͨ͠ͱ͖ϨϯμϦϯάΛߦ͑ΔΑ͏ʹ͢Δɻ ➤ ݻఆؔγΣʔμʔͯ͢ ShaderLab Ͱॻ͘ɻ ➤ ݻఆؔγΣʔμʔݻఆػೳͱݺΕΔίϚϯυͷΈͰه ड़͞ΕͨγΣʔμʔͷ͜ͱɻ ➤ ݻఆػೳଞͷγΣʔμʔͰར༻Ͱ͖Δɻ -
ݻఆػೳϥΠςΟϯάͱϚςϦΞϧ ➤ 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 Λஔ͖ ͑Δɻ -
ݻఆػೳϑΥάͷߏจ 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 ͷൣғΛઃఆ͢ Δɻ -
ݻఆػೳΞϧϑΝςετͱ ➤ ΞϧϑΝςετɺΞϧϑΝΛࢀরͯ͠ϐΫηϧ͕ը໘ʹඳ ը͞ΕΔ͔Ͳ͏͔Λ੍ޚ͢Δɻ ➤ ݻఆػೳΞϧϑΝςετɺ ϑϥάϝϯτγΣʔμʔ͕༻ ͞Ε͍ͯΔͱ͖ޮՌ͕ͳ͍ɻ ➤ ΄ͱΜͲͷϓϥοτϑΥʔϜͰɺΞϧϑΝςετγΣʔμʔ
Ͱ HLSL clip() ؔΛ༻ͯ͠ߦΘΕΔ(ύϑΥʔϚϯεͷ࠷ దԽ)ɻ -
ݻఆػೳςΫενϟ߹ͱ ➤ SetTextureίϚϯυΛ༻ͯ͠ ద༻͢ΔςΫενϟͱͦͷίϯ όΠϯϞʔυ(߹ػೳ)Λࢦఆ͢Δ͜ͱ͕Ͱ͖Δɻ ➤ SetTexture ίϚϯυ ϑϥάϝϯτϓϩάϥϜ͕༻͞Εͯ ͍Δͱ͖ޮՌ͕ͳ͍(ϐΫηϧॲཧ͕γΣʔμʔͰશʹද
ݱ͞Ε͍ͯΔͨΊ)ɻ ➤ ύεͷதʹෳͷ SetTexture Λͭ͜ͱ͕Ͱ͖Δɻ ➤ SetTexture ίϚϯυ Pass ͷऴΘΓʹஔ͢Δඞཁ͕͋Δɻ -
ݻఆػೳςΫενϟ߹ͷߏจ ߏจ SetTexture [TextureName] {Texture Block} TextureName ςΫενϟϓϩύςΟʔͱͯ͠ఆٛ͢Δɻ ςΫενϟϒϩοΫʹςΫενϟΛద༻͢Δํ๏Λهࡌ͢Δɻ ςΫενϟϒϩοΫʹ
ҎԼͷ2 ͭͷίϚϯυ·ͰؚΉ͜ͱ͕Ͱ͖ Δɻ ➤ combine ➤ constantColor -
ݻఆػೳςΫενϟ߹ͷߏจ 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 Λࢦఆ͢Δ͜ͱͰΞϧϑΝͷΈΛऔಘͤ͞Δ͜ͱ͕Ͱ͖Δɻ -
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 } } } } -
ΞϧϑΝ੍ޚͨࣗ͠ݾর໌ 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 } } } } -
ΞϧϑΝ੍ޚͨࣗ͠ݾর໌(৭ࢦఆ) 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 } } } } -
γΣʔμʔͷϥΠςΟϯάϓϩύςΟʔΛ׆༻ 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 } } } } -
αʔϑΣΠεγΣʔμʔͱ ➤ γΣʔμʔͱ Fragment γΣʔμʔͷϓϩάϥϛϯά Λ༻ ͨ͠߹ΑΓɺϦοτγΣʔμʔͷهड़ΛίʔυੜʹΑͬ ͯང͔ʹ؆୯ʹ͢Δख๏ɻ ➤ Surface
ShadersखಈͰهड़͢ΔඞཁͳΔ܁Γฦ͠ίʔυΛ͢ ͯੜ͍ͯ͠Δ͚ͩɻ ➤ Cg/HLSL ͰγΣʔμʔίʔυΛهड़͢Δɻ ➤ Surface Shaders ίϯύΠϥɺͲΜͳೖྗ͕ඞཁͰɺͲΜͳग़ ྗΛग़͔͢ͱ͍͏͜ͱΛܾఆ͠ɺγΣʔμʔͱϑϥάϝϯ τγΣʔμʔ Λੜ͢ΔɻForward ϨϯμϦϯά Deferred Ϩ ϯμϦϯάΛॲཧ͢ΔϨϯμϦϯάύεੜ͢Δɻ -
αʔϑΣΠεγΣʔμʔίϯύΠϧ໋ྩ #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 (εϖΩϡϥ) Λར༻͢Δɻ -
αʔϑΣΠεγΣʔμʔίϯύΠϧ໋ྩ #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 ʹΑ੍ͬͯޚ͞ΕΔɻ ಁ໌ʹ௨ৗɺೋछྨ͋Δɻͻͱͭैདྷܕ ͷΞϧϑΝ ϒϨϯσΟϯάʢΦϒδΣΫτͷϑΣʔυΞτʹΘΕΔʣɻ ͏ͻͱͭɺΑΓཧతʹઆಘྗͷ͋ΔʮϓϨϚϧνϓϥ Πυ ϒϨϯσΟϯάʯʢಁ໌ͷαʔϑΣʔε͕దͳڸ໘ࣹΛอͯΔΑ͏ʹ͢Δʣɻ ※ -
αʔϑΣΠεγΣʔμʔίϯύΠϧ໋ྩ #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. -
αʔϑΣΠεγΣʔμʔίϯύΠϧ໋ྩ #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 ίΞςοηϨʔγϣ ϯͷαʔϑΣʔεγΣʔμʔΛࢀরɻ -
αʔϑΣΠεγΣʔμʔίϯύΠϧ໋ྩ #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 Ͱܭࢉ͞ΕΔɻ -
ೖྗߏମ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) Λ༻͢Δɻ -
ೖग़ྗߏମ 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 }; -
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" } -
ςΫενϟ 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" } -
๏ઢϚοϐϯά 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" } -
ϦϜϥΠςΟϯά 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" } -
ࡉ෦ςΫενϟ 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" } -
ը໘ۭؒͷࡉ෦ςΫενϟ 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" } -
ΩϡʔϒϚοϓࣹ 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" } -
๏ઢϚοϓʹӨڹ͞ΕΔΩϡʔϒϚοϓࣹ 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" } -
ੈքۭؒͷҐஔΛհͨ͠εϥΠε 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" } -
๏ઢԡ͠ग़͠ 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" } -
͝ͱʹܭࢉ͞ΕͨΧελϜͷσʔλ 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" } -
࠷ऴతͳΧϥʔम০ࢠʢ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" } -
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" } -
ɾϑϥάϝϯτγΣʔμʔͱ ➤ ͦΕͧΕɾϐΫηϧσʔλॲཧ࣌ʹݺͼग़͢͜ͱͷͰ͖Δϓϩάϥ Ϝɻ ➤ γΣʔμʔؔ̍ͭ̍ͭΛॲཧ͢ΔࡍʹݺΕΔɻ ➤ ϑϥάϝϯτγΣʔμʔؔ̍ͭ̍ͭͷϐΫηϧΛॲཧ͢Δࡍʹݺ ΕΔɻ ➤
Cg/HLSLͰهड़͢Δɻ ➤ UnityͰϥΠςΟϯάʹԠ͢ΔγΣʔμʔΛॻ͘߹αʔϑΣΠε γΣʔμʔΛॻ͖͘ɻɾϑϥάϝϯτγΣʔμʔͰϥΠςΟϯά Λܭࢉ͢Δ߹શͯࣗͰ࣮͢Δඞཁ͕͋Δɻ ➤ ϥΠςΟϯάͷܭࢉ͕ඞཁͳ͍ΤϑΣΫτʹར༻͢Δ͜ͱ͕ଟ͍(Π ϝʔδΤϑΣΫτ)ɻ -
ɾϑϥάϝϯτγΣʔμʔίϯύΠϧ໋ྩ γΣʔμʔؔࢦఆ໋ྩ ➤ #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 ໋ྩඞͣඞཁɻ -
ɾϑϥάϝϯτγΣʔμʔίϯύΠϧ໋ྩ ͦͷଞͷίϯύΠϧ໋ྩ ➤ #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 (·ͨͦΕҎ߱ͷόʔδϣϯ)ͷάϥϑΟοΫεσόοΨʔΛܦ༝ͯ͠γΣʔμʔͷσ όοά͕ՄೳʹͳΔɻ -
ɾϑϥάϝϯτγΣʔμʔίϯύΠϧ໋ྩ ϨϯμϦϯάϓϥοτϑΥʔϜࢦఆ໋ྩ ➤ 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 σΟϨΫςΟϒͰࢦఆͰ͖Δɻ͜ΕɺಛఆͷγΣʔμʔ͕͋ΔϨϯμϥʔͰ͔͠༻Ͱ͖ͣଞͰ༻Ͱ͖ͳ͍߹ ʹར༻͢Δɻ -
γΣʔμʔηϚϯςΟοΫ ೖྗύϥϝʔλʔηϚϯςΟοΫ ೖྗ આ໌ λΠϓ 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 -
γΣʔμʔηϚϯςΟοΫ ग़ྗύϥϝʔλʔηϚϯςΟοΫ ग़ྗ આ໌ λΠϓ 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 -
ϑϥάϝϯτγΣʔμʔηϚϯςΟοΫ ೖྗύϥϝʔλʔηϚϯςΟοΫ ೖྗ આ໌ λΠϓ 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 -
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; -
ShaderLabͱCg/HLSLͷมܕͷϚοϐϯά ShaderLab ͷϓϩύςΟʔλΠϓ Cg/HLSL มλΠϓͱͷରԠ࣍ͷΑ͏ʹ Ϛοϐϯά͞ΕΔɻ ➤ Color ͱ Vector
ϓϩύςΟʔɺfloat4ɺhalf4͋Δ͍fixed4มʹରԠɻ ➤ Range ͱ Float ϓϩύςΟʔɺfloatɺhalf͋Δ͍fixedมʹରԠɻ ➤ Texture ϓϩύςΟʔɺ௨ৗʢ 2D ʣͷςΫενϟͷ߹sampler2Dม ʹରԠɺ3D ςΫενϟͷ߹sampler3DมʹରԠɻ -
UnityCG.cginc ͷσʔλߏ ➤ struct appdata_base • Ґஔɺ๏ઢɺςΫενϟ࠲ඪΛͭɻ ➤ struct appdata_tan
• Ґஔɺ๏ઢɺઢɺςΫενϟ࠲ඪΛͭɻ ➤ struct appdata_full • Ґஔɺ๏ઢɺઢɺΧϥʔɺ;ͨͭͷςΫενϟ࠲ඪ Λͭɻ ➤ struct appdata_img • ҐஔɺςΫενϟ࠲ඪΛͭɻ γΣʔμʔೖྗߏମఆٛ -
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 ͔ΒϏϡʔۭؒ๏ઢΛσίʔυ͢Δɻ -
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ͷϥΠςΟϯάมΛࢀর -
UnityCG.cginc ͷ Vertex-lit ϔϧύʔؔ ϥΠςΟϯάΛߦ͏߹ʹͷΈศར ➤ float3 ShadeVertexLights (float4 vertex,
float3 normal) • 4 ͭͷ֤ϥΠτ͔ΒΦϒδΣΫτۭؒͷҐஔͱ๏ઢΛࢦఆͯ͠ɺًͱڥޫΛࢉग़͢Δɻ -
UnityShaderVariables.cgincͷϥΠςΟϯά༻ม ϥΠτύϥϝʔλʔɺͲͷϨϯμϦϯάύε ͕༻͞Ε͍ͯΔ͔ɺ ·ͨͲͷ LightMode Pass λά ͕ γΣʔμʔͰ༻͞Ε͍ͯΔ͔ʹΑͬͯɺҟͳΔํ๏ͰγΣʔμʔʹύε͞ΕΔɻ Forward
RenderingͰ༻Ͱ͖ΔϥΠςΟϯά༻ม(ύελΠϓ ForwardBase ͱ ForwardAdd ) -
୯৭ 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 } } } -
๏ઢํͰ৭͚ 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 } } } -
๏ઢʹΑΔ৭͚ 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 } } } -
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 } } } -
৭Λදࣔ 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 } } } -
ઢʹΑΔ৭͚ 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 } } } -
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 } } } -
ଞͷςΫενϟΛՃࢉ 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 } } } -
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 } } } -
֨ࢠঢ়ʹΓൈ͘ 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 } } } -
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 } } } -
ڥޫΛߟྀͨ͠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 } } } -
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 } } } -
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" } } -
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 } } } -
ΧϦϯάͷߏจ Cull Back | Front | Off ߏจ ϙϦΰϯͷͲͪΒଆΛΧϦϯά͢Δʢඳը͠ͳ͍ʣ੍͔ޚɻ ➤
Back • ࢹͱରଆͷϙϦΰϯΛϨϯμϦϯά͠ͳ͍Α͏ʹ͢Δ(σϑΥϧ τ) ɻ ➤ Front • ࢹͱಉ͡ଆͷϙϦΰϯΛϨϯμϦϯά͠ͳ͍ɻΦϒδΣΫτΛ స͢Δͷʹ༻͢Δɻ ➤ Off • ΧϦϯάΛແޮʹͯ͠ɺͯ͢ͷ໘Λඳը͢Δɻ -
ਂςετͷߏจ 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ɻ -
Stencil ➤ εςϯγϧόοϑΝεςϯ γϧςετʹ͍ͭͯղઆ͢Δɻ • ʲςϥγϡʔϧϒϩάʳεςϯγϧόοϑΝΛͬͨ2D໘ന දݱΛ৭ʑͱࢼ͢ • ʲQiitaʳUnityͷShaderͰεςϯγϧόοϑΝΛࢼ͢ •
ʲwgld.orgʳεςϯγϧόοϑΝ • ʲUnityϚχϡΞϧʳεςϯγϧ -
εςϯγϧͱ ➤ ܕࢴɺݪࢴΛҙຯ͢ΔɻγΣʔμʔʹ͓͍ͯʮܕൈ͖ʯͷҙ ຯɻ • γϧΤοτΞτϥΠϯͷඳը • σΧʔϧ • ϑΣʔυΠϯϑΣʔυΞτԋग़
ͳͲ͕Ͱ͖Δɻ ➤ ར༻༻్ओʹϚεΩϯάɻ -
εςϯγϧόοϑΝʹॻ͖ࠐΉ Stencil { Ref 1 // εςϯγϧ(όοϑΝͷॻ͖ࠐΈɾಡΈग़͠) Comp Always //
࣮ߦ͞ΕΔ݅ʢৗʹʣ Pass Replace // ࣮ߦ͞ΕΔίϚϯυʢ্ॻ͖ʣ } εςϯγϧόοϑΝͷॻ͖ࠐΈɻྫͷ߹Comp (࣮ߦ݅)ΛAlways ʹઃఆ͠ɺ͕݅ਅͩͬͨ߹(͜ͷ߹ৗʹਅ)࣮ߦ͢Δಈ࡞Ͱ͋Δ PassʹReplace(RefͷͰ্ॻ͖)Λࢦఆ͍ͯ͠Δɻ ͭ·Γ͜ͷࢦఆํ๏ͷ߹ɺϑϥάϝϯτඳը࣌ʹৗʹεςϯγϧόοϑΝ ʹ1ͱ͍͏Λॻ͖ࠐΉ͜ͱʹͳΔɻ ߏจʹ͍ͭͯޙड़ -
ҙ Stencil { Ref 1 // εςϯγϧ(όοϑΝͷॻ͖ࠐΈɾಡΈग़͠) Comp Always //
࣮ߦ͞ΕΔ݅ʢৗʹʣ Pass Replace // ࣮ߦ͞ΕΔίϚϯυʢ্ॻ͖ʣ } ઌ΄Ͳͷ͜ͷΑ͏ͳεςϯγϧόοϑΝͷॻ͖ࠐΈΛ3DϞσϧͷඳըʹ ߦͬͯͳ͍ɻ ͔͠͠2DεϓϥΠτʹߦ͏ͱɺϙϦΰϯͰεςϯγϧ͕࡞͞Εͯ͠·͏ɻ ͜ΕΛճආ͢ΔͨΊʹ if (color.a < 0.001) discard; ͷΑ͏ʹɺεϓϥΠτͷΞϧϑΝ͕ҰఆҎԼͳΒഁغ͢Δͱ͍͏ॲཧ͕ඞཁɻ εϓϥΠτΛඳը͢Δࡍʹ ϙϦΰϯͰඳը͞ΕΔͨΊʹ εςϯγϧͷॻ͖ࠐΈ͕ ΩϟϥΫλʔͷܗʹͳΒͳ͍ ಁ໌ʹΑΔDiscardॲཧΛ ϑϥάϝϯτγΣʔμʔͷॲཧʹ Ճ͢Δ͜ͱͰɺΩϟϥΫλʔͷ ܗʹεςϯγϧόοϑΝΛॻ͖ࠐΊΔ -
ҙ ➤ ͭ·Γɺը૾ͷΞϧϑΝΛར༻ͯ͠εςϯγϧόοϑΝͷ ॻ͖ࠐΈΛߦ͍͍ͨ߹ɺϑϥάϝϯτγΣʔμʔͰΞϧϑΝ ͕ҰఆҎԼͳΒDiscard͢Δͱ͍͏Α͏ͳॲཧΛॻ͘ඞཁ͕ ͋Δɻ ➤ ϑϥάϝϯτγΣʔμʔͰDiscard͢ΕͦͷϐΫηϧʹؔ͠ ͯεςϯγϧόοϑΝͷॻ͖ࠐΈ͕࣮ߦ͞ΕΔ͜ͱͳ ͍ɻ
➤ ͜ΕΛར༻ͯ͠ɺϑϥάϝϯτγΣʔμʔͰ͋ΒΏΔ݅ͷ ͱεςϯγϧόοϑΝͷॻ͖ࠐΈΛ੍ݶ͢Δ͜ͱ͕Ͱ͖ Δɻ -
εςϯγϧςετͱ ➤ εςϯγϧόοϑΝͷͱɺࢦఆͨ͠ΛൺͯରͷϐΫ ηϧʹϨϯμϦϯάΛߦͳ͏͔൱͔Λܾఆ͢Δɻ ➤ ൺֱʹར༻͢ΔؔΛൺֱؔͱ͍͏ɻ ➤ ൺֱޭ࣌(Pass)ɺࣦഊ࣌(Fail)ɺޭ͕ͩਂςετʹෆ ߹֨ͩͬͨ߹(ZFail)ͷॲཧΛهड़Ͱ͖Δɻ ➤
͢ͰʹεςϯγϧόοϑΝʹԿ͔͠Βͷ͕ॻ͖ࠐ·Ε͍ͯ Δͱ͍͏લఏɻͳͷͰγΣʔμʔͷ࣮ߦॱʹҙ(εςϯγ ϧόοϑΝॻ͖ࠐ·ΕͨޙʹεςϯγϧςετΛߦ͏)ɻ -
εςϯγϧςετ Stencil { Ref 1 // εςϯγϧ(όοϑΝͷॻ͖ࠐΈɾಡΈग़͠) Comp Equal //
࣮ߦͷ݅ʢόοϑΝͷ͕Refͷ࣌ʣ } εςϯγϧςετɻྫͷ߹ɺεςϯγϧόοϑΝ ͷͱൺֱͯ͠ɺ͕̍ʹ͚͠ΕϐΫηϧͷඳը ͕ߦΘΕΔɻ ߏจʹ͍ͭͯޙड़ -
ҙ όοϑΝͷॻ͖ࠐΈͱςετͰߏจ͕ಉ͡Α͏ͳײ͡ʹͳ͍ͬͯ ΔͨΊɺࠞཚ͢Δ͜ͱ͕͋ΔɻجຊతʹεςϯγϧॲཧγΣʔ μʔͰෳͷύεΛ༻͍ͯϚεΩϯάॲཧΛ͢ΔͷʹΘΕΔ͜ͱΛ ͓֮͑ͯ͘ͱ͍͍ɻ ྫ͑ɺTags {“Queue”=“Transparent+1”}ͷPassͰεςϯγϧόο ϑΝͷॻ͖ࠐΈΛߦ͍ɺ Tags {“Queue”=“Transparent+2”}ͷPassͰεςϯγϧςετΛߦ
͏ɻͱ͍ͬͨΑ͏ʹར༻͢Δɻ ·ͨɺ2ύεͰಈ࡞͢ΔγΣʔμʔΛ࡞Γɺ1PassͰεςϯγϧ όοϑΝͷॻ͖ࠐΈɺ2ύεͰεςϯγϧςετΛ͢Δɻͱ͍ͬ ͨར༻Մೳɻ -
εςϯγϧߏจ SubShader { … Stencil { Ref 1 Comp always
Pass replace } … } SubShader { Pass{ … Stencil { Ref 1 Comp always Pass replace } } … } SubShaderҎԼʹॻ͖ࠐΉ͜ͱ͕Ͱ͖Δ PassҎԼʹॻ͖ࠐΉ͜ͱ͕Ͱ͖Δ Stencil{…} ͱ͍͏ܗࣜͰهड़͍ͯ͘͠ɻ ༰ޙड़ ྫ ྫ -
ߏจ Ref [] Comp [ൺֱؔ] Pass [߹֨࣌ͷεςϯγϧૢ࡞] Fail [ෆ߹֨࣌ͷεςϯγϧૢ࡞] ZFail
[߹͕֨ͩਂςετෆ߹֨࣌ͷεςϯγϧૢ࡞] ReadMask [ಡΈࠐΈϚεΫ] WriteMask [ॻ͖ࠐΈϚεΫ] -
εςϯγϧόοϑΝͷॻ͖ࠐΈ 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 } } } εςϯγϧόοϑΝͷॻ͖ࠐΈ Λߦ͏γΣʔμʔϓϩάϥϜྫɻ ॻ͖ࠐΜͩεςϯγϧͷ༰Λ Θ͔Γ͘͢͢ΔͨΊʹɺ ϑϥάϝϯτγΣʔμʔͰ ॻ͖ࠐΜͩ༰ΛͰදࣔ͢Δ Α͏ʹͳ͍ͬͯΔɻ -
εςϯγϧόοϑΝͷར༻ 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{…} } όοϑΝॻ͖ࠐΈ εςϯγϧςετ -
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 ಛ༗ͷ߲Ͱར༻͢Δʹ []ͰϓϩύςΟΛғΉɻ -
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 ૢ࡞ ͱͯ͠આ໌͍ͯ͘͠ɻ -
ҰൠతͳϒϨϯσΟϯάͷλΠϓ Blend SrcAlpha OneMinusSrcAlpha // Alpha blending Blend One One
// Additive Blend OneMinusDstColor One // Soft Additive Blend DstColor Zero // Multiplicative Blend DstColor SrcColor // 2x Multiplicative -
ShaderLabৄࡉτϐοΫ ➤ ShaderLabͷৄࡉͳτϐοΫʹ ͍ͭͯղઆ͢Δɻ • ʲUnityϚχϡΞϧʳৄࡉͳ ShaderLab τϐοΫ • ʲϝϞϒϩάʳUnity
5ͰG-Bufferͷςετ • ʲଠWorkʳSetReplacementShaderͷ͍ํ Unity • ʲQiitaʳσϓεόοϑΝΛར༻͢Δ • ʲCross RoadʳUnity༻தʹֶΜͩύϑΥʔϚϯενϡʔ χϯάํ๏:LOD • ʲͱ·ͱɾιϑτΣΞ࡞ॴʳDirect3D9ϨϯμϦϯά ύΠϓϥΠϯؔ࿈༻ޠʢ3DCGશൠͷجૅࣝΊʣ • ʲচҪݚڀࣨʳϨϯμϦϯάύΠϓϥΠϯ -
ϨϯμϦϯάύε ➤ ϥΠςΟϯά͕ͲͷΑ͏ʹద༻͞ΕΔ͔ɺ͓ ΑͼγΣʔμʔͷͲͷ Pass ͕༻͞ΕΔ͔ ɺͲͷϨϯμϦϯάύε͕༻͞ΕΔ͔Ͱ ܾ·ΔɻγΣʔμʔͷ֤ύεɺPassͷ Tags Λհͯ͠ɺͦͷϥΠςΟϯάλΠϓͱ௨
৴͢Δɻ ➤ Ͳͷ߹ͰɺӨΛϨϯμϦϯά͢ΔͨΊʹ ShadowCaster ύε͕ΘΕΔɻ -
ϨϯμϦϯάύε ➤ ͭ·ΓɺPassͷTagsʹΑͬͯɺ࣮ߦ͞ΕΔϨϯμϦϯάύε ΛࢦఆͰ͖Δɻ ➤ SurfaceγΣʔμʔͳΒࣗಈͰܾ·ΔΒ͍͠ͷͰߟྀ͠ͳ͘ ͍͍ͯɻ ➤ γΣʔμ͔ΒࢀরͰ͖ΔϥΠτྨͷσʔλ͕ϨϯμϦϯάύ εʹΑͬͯҟͳΔɻ
➤ ͪͳΈʹίϚϯυόοϑΝΛ͏ͱɺUnityͷϨϯμϦϯάύ ΠϓϥΠϯΛಠࣗʹ֦ு͢Δ͜ͱ͕Ͱ͖Δɻ -
PassͷTags Pass TagsΛ༻͍ͯͭ͠ͲͷΑ͏ʹͯ͠ϨϯμϦϯάΤϯδϯʹΑΓϨ ϯμϦϯά͖͔͢ΛࢦఆͰ͖Δɻ Tagsͷߏจ Tags { "TagName1" = "Value1"
"TagName2" = "Value2" } λάͷ TagName1 ͕ͱͯ͠ Value1ɺλάͷ TagName2 ͕ͱͯ͠ Value2 Ͱ͋Δ͜ͱΛࢦఆ͍ͯ͠ Δɻλά͖ͳ͚ͩࢦఆͰ͖Δɻλάجຊతʹ Key-Value ϖΞɻPass ͷதͰλάɺ֘ύε͕ϥ ΠςΟϯάύΠϓϥΠϯʢڥޫɺϥΠςΟϯάɺϐΫηϧϥΠςΟϯάʣͷதͰͲͷׂͰ͋Δ͔ Λ੍ޚͨ͠ΓɺଞͷύϥϝʔλʔΛఆ͢Δͷʹ༻͢Δɻ TagsSubShaderɺPassͷͲͪΒʹͰఆٛ͢Δ͜ͱ͕Ͱ͖Δ͕ɺ࣍ ͷλά Unity ʹΑΓೝࣝ͞ΕɺSubShaderͷTagsͰͳ͘ඞͣPassη Ϋγϣϯͷதʹఆٛ͢Δɻ • LightMode λά • RequireOptions λά -
PassͷTags LightModeλά LightMode λάϥΠςΟϯάύΠϓϥΠϯͰͷύεͷׂΛఆٛ͢Δɻ͜ΕΒͷλάखಈͰ༻͞ΕΔ͜ͱ كͰɺϥΠςΟϯάͱ૬ޓ࡞༻ͷ͋Δ΄ͱΜͲͷγΣʔμʔ Surface Shader Ͱهड़͞Εɺͦ͏͍ͬͨৄࡉ ͦͷதͰॲཧ͞ΕΔɻ LightMode
Ͱར༻Մೳͳ Always ৗʹϨϯμϦϯά͞ΕɺϥΠςΟϯάద༻͞Εͳ͍ɻ ForwardBase Forward Rendering Ͱ༻͞ΕɺڥޫɺϝΠϯͷσΟϨΫγϣφϧϥΠτɺ/SH ϥ Πτ͕ద༻͞ΕΔɻ ForwardAdd Forward Rendering Ͱ༻͞ΕɺϥΠτ͝ͱʹҰͭͷύεͰɺAdditive ϐΫηϧϥΠςΟ ϯά͕ద༻͞ΕΔɻ Deferred Deferred Shading Ͱ༻͞Εɺg-όοϑΝΛඳը͠·͢ɻ ShadowCaster ΦϒδΣΫτͷσϓεΛγϟυϚοϓਂςΫενϟʹඳը͠·͢ɻ -
PassͷTags RequireOptions λά ಛఆͷ֎෦͕݅ຬͨ͞Εͨ߹ͷΈϨϯμϦϯά͞ΕΔ͜ͱΛɺύεͰࢦఆ͢Δ͜ͱ͕Ͱ͖Δɻ͜Ε RequireOptions ʹΑΓ࣮ݱ͞ΕɺεϖʔεͰ۠ΒΕͨจࣈྻʹΑΔΦϓγϣϯɻ RequireOptions Ͱར༻Մೳͳ SoftVegetation ͠
ը࣭ઃఆ Ͱ Soft Vegetation ͕༗ޮͰ͋Δ߹ͷΈɺ͜ͷύεΛϨϯμϦϯά͢Δɻ -
Forward Renderingύε Forward Rendering ύε֤ΦϒδΣΫτΛɺΦϒδΣΫτʹ࡞༻͢ΔϥΠτʹͱ͍ͮͯϨϯμϦϯά͢Δɻϥ Πτͦͷͷɺઃఆ͓ΑͼڧʹΑͬͯɺForward Rendering ʹΑΓผʑʹѻΘΕΔɻ Forward RenderingͰѻ͏ύεʹ
➤ ʲForwardBaseʳ LightModeͰࢦఆͰ͖ΔBase Pass • ΦϒδΣΫτͷϐΫηϧΛͻͱͭͷࢦੑϥΠτͱΓͯ͢ͷٿ໘ௐ Ͱܭࢉ͞ΕͨϥΠτͰϨϯμϦϯά͢Δɻ͜ͷύεͰ͞Βʹଞͷϥ ΠτϚοϓɺڥޫϥΠςΟϯάɺ͓ΑͼࣗݾൃޫϥΠςΟϯάΛՃ ͢Δɻ ➤ ʲForwardAddʳLightModeͰࢦఆͰ͖ΔAdditional Passes • ΦϒδΣΫτʹ࡞༻͢Δɺ֤ʑͷՃ͞ΕͨϐΫηϧϥΠτͰϨϯμϦ ϯά͞ΕΔɻ͜ΕΒͷύεͷϥΠτӨΛ࣋ͯͳ͍ʢ݁Ռͱͯ͠ Forward Rendering ͕αϙʔτ͢ΔͷͻͱͭͷࢦੑϥΠτͱӨʣɻ ͕͋Δɻ -
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 ύεΛ༻ͯ͠ϨϯμϦϯά͞ΕΔ͜ͱʹͳΔɻ -
ܭࢉͷਫ਼ ➤ ϫʔϧυۭؒҐஔͱςΫενϟ࠲ඪͷ߹ɺfloatΛ༻͢Δɻ ➤ ଞͷͯ͢ʢϕΫτϧɺHDRͷ৭ͳͲʣͷ߹ɺhalfΛ༻͢ΔɻΑΓଟ͘ͷਫ਼͕ඞ ཁͳ߹ʹͷΈfloatʹ͢Δɻ ➤ ςΫενϟσʔλ্ͷඇৗʹ؆୯ͳૢ࡞ͰɺfixedΛ༻͢Δɻ ➤ ͋ΒΏΔݱͷσεΫτοϓ
GPU ɺৗʹͯ͢ͷࣄΛશͳුಈখਫ਼Ͱܭࢉ͠ ͍ͯͯɺfloat/half/fixed Ͱɺ·ͬͨ͘ಉ݁͡ՌͰऴΘΔɻ͜ͷࣄ͕ςετΛࠔʹͯ͠ ͓Γɺhalf/fixed ͷਫ਼Ͱ࣮ࡍेͳͷ͔Ͳ͏͔ɺPC্ͰΓʹ͍͘ɻ࡞ͨ͠ γΣʔμʔΛɺ࣮ࡍʹϞόΠϧσόΠεͰࢼ͢ඞཁ͕༗Δɻ ➤ ϞόΠϧ GPU ਫ਼ුಈখΛαϙʔτ͓ͯ͠Γɺେܭࢉ͕ΑΓૣ͘ɺΑΓল ΤωͰ͋Δɻ ➤ fixedͷਫ਼ɺҰൠతʹݹ͍ϞόΠϧ GPU ͰͷΈ͑Δɻݱͷଟ͘ͷ GPU(OpenGL ES 3 Metal Λ࣮ߦ͢Δ͜ͱ͕Ͱ͖Δͷ)ɺݻఆখͱਫ਼ුಈখΛɺ ෦తʹ·ͬͨ͘ಉ͡ͷͱͯ͠औΓѻ͏ɻΑͬͯ͏ػձগͳ͍ɻ -
Replaced Shaderͱ ➤ ΧϝϥʹөΔશମΛ୯ҰγΣʔμͰඳըͰ͖Δ ➤ ΧϝϥʹөΔશମͷɺಛఆͷγΣʔμͷ߹ͷΈඳըͨ͠ ͍߹λάΛࢦఆ͢Δ͜ͱͰՄೳʹͳΔɻ ➤ γΣʔμʔͷஔ͖͑ΛεΫϦϓτͰߦ͏ʹɺ Camera.RenderWithShader
Camera.SetReplacementShader ؔΛ༻͢Δɻ ➤ ྆ํͷؔҾͰreplacedShader ͱreplacementTag Λ༻ ͢Δɻ -
Replaced Shaderͷಈ࡞ ΧϝϥγʔϯΛ௨ৗͲ͓ΓʹϨϯμϦϯά͢ΔɻΦϒδΣΫτϚςϦΞϧΛ༻͢Δ ͕ɺ࣮ࡍʹ༻͞ΕΔ͜ͱʹͳΔγΣʔμʔ͕มߋ͞ΕΔɻ ➤ replacementTag ͕ۭͷ߹ɺγʔϯͷͯ͢ͷΦϒδΣΫτࢦఆ͞Εͨ replaced Shader ͰϨϯμϦϯά͞ΕΔɻ
➤ replacementTag ͕ۭͰͳ͍߹ɺϨϯμϦϯά͞ΕΔ֤ΦϒδΣΫτ • ΦϒδΣΫτͷγΣʔμʔSubShaderͷλάʹreplacementTagΛͭSubShader Λݕࡧ͢Δɻ • replacementTagΛ࣋ͨͳ͍߹ɺΦϒδΣΫτ ϨϯμϦϯά͞Εͳ͍ɻ • replacementTagΛݟ͚ͭͨ߹ɺͦͷSubShaderΛ༻ͯ͠ΦϒδΣΫτ͕ϨϯμϦ ϯά͞ΕΔɻ ͯ͢͠ͷγΣʔμʔ͕ɺྫ͑ɺ“RenderType” λά͕ “Opaque”ɺ“Transparent”ɺ “Background”ɺ“Overlay” ͷͰ͋ͬͨ߹ɺෆಁ໌ͳΦϒδΣΫτͷΈϨϯμϦϯά͢ Δ Replacement Shader ΛɺRenderType=“Solid”Ͱ͋ΔͻͱͭͷαϒγΣʔμʔΛ༻͠ ͯɺهड़͢Δ͜ͱ͕Ͱ͖Δɻͳ͓ɺͯ͢ͷ Unity ϏϧτΠϯγΣʔμʔ “RenderType” λάͷηοτΛ͍࣋ͬͯΔɻ -
ϏϧτΠϯγΣʔμʔͰͷ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ɻ -
༻ྫ 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λά͕ଘࡏ͠ͳ͍γΣʔμඳը͕ແࢹ͞Ε·͢ɻ -
ϚςϦΞϧΠϯεϖΫλʔͷΧελϚΠζ ΧελϜΤσΟλʔΛఆٛ͢Δʹɺ 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Λͬͯɺ͞Βʹ୯७ԽͰ͖Δɻ -
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); } } -
σϓεςΫενϟϔϧϓϚΫϩ ➤ 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 Λฦ͢ɻωΠςΟϒσϓεςΫενϟͷ͋ΔϓϥοτϑΥʔϜͰ͜ͷϚΫϩɺΧ ϝϥͷൣғʹҰக͢ΔΑ͏ʹɺΛઢܗԽ֦͠ு͢Δɻ -
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 } } } -
LODͱ ➤ Level of Detail ➤ ΧϝϥͱΦϒδΣΫτͷڑ͕Εͨ߹ʹϨϯμϦϯά͢Δ ϙϦΰϯͷΛݮΒ͢ࣄ͕Ͱ͖Δɻ ➤ Unity
ͰɺLOD Group ίϯϙʔωϯτΛ͏ͱΦϒδΣΫ τʹରͯ͠ LOD ϨϯμϦϯάΛηοτΞοϓͰ͖Δɻ -
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