본문으로 바로가기

Material Property Drawer Example

category Technical Report/Unity Shader 2019. 2. 7. 14:09
반응형










// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'


Shader "Test/Material Property Drawer Example" {

  Properties {

  [Header(Material Property Drawer Example)]
  [Space(20)]
  _TintColor("TintColor", Color) = (1,1,1,1)
  _MainTex ("Texture", 2D) = "white" {}
  [NoScaleOffset]_SecondTex ("Additional Texture", 2D) = "white" {}

  [Space(30)]
  //Toggle shader feature. it can use only multiply properties.
  [Toggle] _Invert ("Invert Color", Float) = 0

  //Toggle Keyword Enum
  [Toggle(ENABLE_Gray)] _Gray("GrayScale", Float) = 0

  [Space(30)]
  // All following properties are valid for both SrcFactor & DstFactor in the Blend command.
  //Source refers to the calculated color, Destination is the color already on the screen.
  //The blend factors are ignored if BlendOp is using logical operations.
  [Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend ("Src Blend", Float) = 1
  [Enum(UnityEngine.Rendering.BlendMode)] _DstBlend ("Dst Blend", Float) = 1

 
  [Enum(Off, 0, On, 1)] _ZWrite ("ZWrite", Float) = 0
  [Enum(UnityEngine.Rendering.CompareFunction)] _ZTest ("ZTest", Float) = 0
  [Enum(UnityEngine.Rendering.CullMode)] _Cull ("Cull Mode", Float) = 1
 
  // This is used with "#pragma multi_compile" in shaders, to enable or disable parts of shader code.
  // Each name will enable "property name" + underscore + "enum name", uppercased, shader keyword.
  [Space(30)]
  [KeywordEnum(None, Add, Multiply)] _Overlay ("Overlay mode", Float) = 0
 
  // _Shininess ("Shininess", Range (0.01, 1)) = 0.08 }
  // PowerSlider show non-linear curve slide
  [Space(30)]
  [PowerSlider(3.0)] _Shininess ("Shininess", Range (0.01, 1)) = 0.08 }
 
SubShader
{

Tags { "Queue"="Transparent" "RenderType"="Transparent" }

      Blend [_SrcBlend] [_DstBlend]
      ZWrite [_ZWrite]
      ZTest [_ZTest]
      Cull [_Cull]

 Pass
 {
     CGPROGRAM

     
     #pragma shader_feature _INVERT_ON      
     #pragma shader_feature ENABLE_Gray[각주:1]
     #pragma multi_compile _OVERLAY_NONE _OVERLAY_ADD _OVERLAY_MULTIPLY
 
     #pragma vertex vert
     #pragma fragment frag

     //make fogwork
     #pragma multi_compile_fog     [각주:2]
 
     #include "UnityCG.cginc"

       sampler2D _MainTex;
       float4 _MainTex_ST;
       sampler2D _SecondTex;
       float4 _SecondTex_ST;
       
       fixed4 _TintColor;
       float _Shininess;

 
      struct appdata
        {
          float4 vertex : POSITION;
          float2 uv : TEXCOORD0;     
        };

       struct v2f
       {
          float4 uv : TEXCOORD0;
          float4 vertex : SV_POSITION;
           UNITY_FOG_COORDS(1)    
       };
 
       v2f vert (appdata v)
       {
           v2f o;
           o.vertex = UnityObjectToClipPos(v.vertex);
           //  mul( UNITY_MATRIX_VP, _Object2World);
           //  mul( UNITY_MATRIX_MVP, v.vertex ) ;

           o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex);
           o.uv.zw = TRANSFORM_TEX(v.uv, _SecondTex);
           UNITY_TRANSFER_FOG(o,o.vertex);
           return o;
    
       }
    
       fixed4 frag (v2f i) : SV_Target
       {
          fixed4 col = tex2D(_MainTex, i.uv.xy) * _TintColor;
      
                 #if _INVERT_ON
        
                    col = 1 - col;
   
                 #endif [각주:3]

   
                 #if ENABLE_Gray
   
                    col = (col.r + col.g + col.b)/3;
   
                 #endif

   

           fixed4 secCol = tex2D(_SecondTex, i.uv.zw);
   
                  #if _OVERLAY_ADD
   
                    col += secCol;

       
                  #elif _OVERLAY_MULTIPLY
   
                    col *= secCol;
   
                  #endif

                 
                
                 UNITY_APPLY_FOG(i.fogCoord, col);
                 
                
                col *= _Shininess;
            
             return col;
           }
        ENDCG
       }
      }
   }






  1. 위와 아래 shader_feature의 차이는 코드에서 보이든 ON으로 구분하느냐 해당 키워드를 토글해서 활성화 하느냐의 차이이다. [본문으로]
  2. fog는 굳이 안 넣어도 상관이 없다면 fog 관련 구문을 다 빼주면 된다. [본문으로]
  3. 이렇게 if 분기로 구분할수도 있지만 multiply로 해당 구문을 곱해서 사용할수도 있다. 이렇게 사용하면 당연 셰이더에서는 계산을 하게 된다. 분기를 늘리지 게산을 0으로 만들지 차이. [본문으로]
반응형

'Technical Report > Unity Shader' 카테고리의 다른 글

Fragment Lambert Lit AlphaBlend  (0) 2019.02.09
Fragment Lambert Lit  (0) 2019.02.09
Unity Compute Shader  (0) 2018.11.17
Fake FixedLight Environment  (0) 2018.08.14
360 Equirectangular Projection in Unity  (0) 2018.08.07