• [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 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; } } }
| Global | Linear | Exp | Exp2 • ϑΥάϞʔυΛఆٛ͢ΔɻσϑΥϧτ Global ͰɺϑΥά͕ϨϯμϦ ϯάઃఆͰ༗ޮԽ͞Ε͍ͯΔ͔ʹΑΓ Off ͔ Exp2 ʹม͞ΕΔɻ ➤ Color ColorValue • ϑΥάͷ৭Λઃఆ͢Δɻ ➤ Density FloatValue • Exp ʢࢦʣϞʔυͷϑΥάͷີΛઃఆ͢Δɻ ➤ Range FloatValue, FloatValue • Linear ʢϦχΞʣϞʔυͷϑΥάͷ Near ͓Αͼ Far ͷൣғΛઃఆ͢ Δɻ
(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 } } } }
★ 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 ʹΑ੍ͬͯޚ͞ΕΔɻ ಁ໌ʹ௨ৗɺೋछྨ͋Δɻͻͱͭैདྷܕ ͷΞϧϑΝ ϒϨϯσΟϯάʢΦϒδΣΫτͷϑΣʔυΞτʹΘΕΔʣɻ ͏ͻͱͭɺΑΓཧతʹઆಘྗͷ͋ΔʮϓϨϚϧνϓϥ Πυ ϒϨϯσΟϯάʯʢಁ໌ͷαʔϑΣʔε͕దͳڸ໘ࣹΛอͯΔΑ͏ʹ͢Δʣɻ ※
★ 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 ίΞςοηϨʔγϣ ϯͷαʔϑΣʔεγΣʔμʔΛࢀরɻ
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 } } }
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 } } }
= "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 } } }
("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 } } }
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 } } }