본문으로 바로가기
반응형





Frame debugger에서는 call이 하나로 보이나 왼쪽에 보면 Draw calls가 4라고 표시되고 있다. SRP Batcher의 개념에 대한건 URP 문서에서 다시 설명을~


Based on URP 7.1.8에서 제작하였으며, 이보다 낮은버젼에서는 동작을 보증하지 못합니다.


Legacy vertex and fragement 코드에 익숙한 분들은 어렵지 않게 사용하실수 있을거고 fragment shader 코드에 익숙치 않은 분들은 아래에

           

              //Lighting Calculate(Lambert)
              Light mainLight = GetMainLight(i.shadowCoord);
              float NdotL = saturate(dot(normalize(_MainLightPosition.xyz), i.normal));             
              float3 ambient = SampleSH(i.normal);              

              // half receiveshadow = MainLightRealtimeShadow(i.shadowCoord);
              // col.rgb *= NdotL * _MainLightColor.rgb * receiveshadow + ambient;

              col.rgb *= NdotL * _MainLightColor.rgb * mainLight.shadowAttenuation + ambient;

            


부분에 기존과 같이 라이팅 연산을 추가해서 사용하시면 됩니다. 지금은 기본 Lambert 라이팅으로 계산되고 있습니다.



Shader "URPDefaultCodeSample"
{
    Properties

     {

        [Header(UniversalRP Default Shader code)]
        [Space(20)]
        _TintColor("TintColor", color) = (1,1,1,1)
        _MainTex("Texture", 2D) = "white" {}

     // Toggle control opaque to TransparentCutout

        [Toggle]_AlphaTest("Alpha Test", float) = 0
        _Alpha("AlphaClip", Range(0,1)) = 0.5
         [Enum(UnityEngine.Rendering.CullMode)] _Cull("Cull Mode", Float) = 1
  

    }  

    SubShader
    {
    
        Name  "URPDefault"

        Tags {"RenderPipeline"="UniversalRenderPipeline" "RenderType"="Opaque" "Queue"="Geometry"}
     
       LOD 300
       Cull [_Cull]

        Pass
        {          
           HLSLPROGRAM


            #pragma prefer_hlslcc gles
            #pragma exclude_renderers d3d11_9x
            #pragma vertex vert
            #pragma fragment frag
          
            //include fog
            #pragma multi_compile_fog           

            // GPU Instancing
            #pragma multi_compile_instancing

            #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
            #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
            #pragma multi_compile _ _SHADOWS_SOFT
            #pragma shader_feature _ALPHATEST_ON
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
           
             CBUFFER_START(UnityPerMaterial)
             half4 _TintColor;
             sampler2D _MainTex;
             float4 _MainTex_ST;
             float   _Alpha;
             CBUFFER_END
            
             struct VertexInput
             {
                float4 vertex : POSITION;
                float2 uv     : TEXCOORD0;
                float3 normal : NORMAL;
              
                UNITY_VERTEX_INPUT_INSTANCE_ID                              
              };

            struct VertexOutput
              {
               float4 vertex      : SV_POSITION;
               float2 uv          : TEXCOORD0;
               float fogCoord     : TEXCOORD1;
               float3 normal      : NORMAL;
                            
               float4 shadowCoord : TEXCOORD2;
             
               //if shader need a view direction, use below code in shader 
               //float3 WorldSpaceViewDirection : TEXCOORD3;

               UNITY_VERTEX_INPUT_INSTANCE_ID
               UNITY_VERTEX_OUTPUT_STEREO
              };

          VertexOutput vert(VertexInput v)
            {
              VertexOutput o;
              UNITY_SETUP_INSTANCE_ID(v);
              UNITY_TRANSFER_INSTANCE_ID(v, o);
              UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);

              o.vertex = TransformObjectToHClip(v.vertex.xyz);
                        
              o.uv = v.uv.xy * _MainTex_ST.xy + _MainTex_ST.zw; ;
              o.normal = normalize(mul(v.normal, (float3x3)UNITY_MATRIX_I_M));
              
              o.fogCoord = ComputeFogFactor(o.vertex.z);
                 
              VertexPositionInputs vertexInput = GetVertexPositionInputs(v.vertex.xyz);
              o.shadowCoord = GetShadowCoord(vertexInput);
            
              //view direction
              //o.WorldSpaceViewDirection = _WorldSpaceCameraPos.xyz - mul(GetObjectToWorldMatrix(), float4(v.vertex.xyz, 1.0)).xyz;
 

              return o;
            }

            half4 frag(VertexOutput i) : SV_Target
            {
              UNITY_SETUP_INSTANCE_ID(i);
              UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
              
              float4 col = tex2D(_MainTex, i.uv) * _TintColor;
             

              //below texture sampling code does not use in material inspector             
              // float4 col = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
            

              Light mainLight = GetMainLight(i.shadowCoord);
             

              //Lighting Calculate(Lambert)              
              float NdotL = saturate(dot(normalize(_MainLightPosition.xyz), i.normal));            
              float3 ambient = SampleSH(i.normal);             

              // half receiveshadow = MainLightRealtimeShadow(i.shadowCoord);
              // col.rgb *= NdotL * _MainLightColor.rgb * receiveshadow + ambient;

              col.rgb *= NdotL * _MainLightColor.rgb * mainLight.shadowAttenuation + ambient;
             
              #if _ALPHATEST_ON
              clip(col.a - _Alpha);
              #endif

              //apply fog
              col.rgb = MixFog(col.rgb, i.fogCoord);
             

              return col;            
            }

            ENDHLSL  
        }

  Pass
    {
        Name "ShadowCaster"

        Tags{"LightMode" = "ShadowCaster"}

            Cull Back

            HLSLPROGRAM

            #pragma prefer_hlslcc gles
            #pragma exclude_renderers d3d11_9x
            #pragma target 2.0

            #pragma vertex ShadowPassVertex
            #pragma fragment ShadowPassFragment
           
            #pragma shader_feature _ALPHATEST_ON

           // GPU Instancing
            #pragma multi_compile_instancing
          
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/Shaders/ShadowCasterPass.hlsl"


             CBUFFER_START(UnityPerMaterial)
             half4 _TintColor;
             sampler2D _MainTex;
             float4 _MainTex_ST;
             float   _Alpha;
             CBUFFER_END

            struct VertexInput
            {         
            float4 vertex : POSITION;
            float4 normal : NORMAL;
           
            #if _ALPHATEST_ON
            float2 uv     : TEXCOORD0;
            #endif

            UNITY_VERTEX_INPUT_INSTANCE_ID 
            };
         
            struct VertexOutput
            {         
            float4 vertex : SV_POSITION;
            #if _ALPHATEST_ON
            float2 uv     : TEXCOORD0;
            #endif
            UNITY_VERTEX_INPUT_INSTANCE_ID         
            UNITY_VERTEX_OUTPUT_STEREO
 
            };

            VertexOutput ShadowPassVertex(VertexInput v)
            {
               VertexOutput o;
               UNITY_SETUP_INSTANCE_ID(v);
               UNITY_TRANSFER_INSTANCE_ID(v, o);
              UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);                            
          
              float3 positionWS = TransformObjectToWorld(v.vertex.xyz);
              float3 normalWS   = TransformObjectToWorldNormal(v.normal.xyz);
        
              float4 positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, _MainLightPosition.xyz));
             
              o.vertex = positionCS;
             #if _ALPHATEST_ON
              o.uv = v.uv * _MainTex_ST.xy + _MainTex_ST.zw; ;
             #endif

              return o;
            }


            half4 ShadowPassFragment(VertexOutput i) : SV_TARGET
            {  
                UNITY_SETUP_INSTANCE_ID(i);
                UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
              
                #if _ALPHATEST_ON
                float4 col = tex2D(_MainTex, i.uv);
                clip(col.a - _Alpha);
                #endif

                return 0;
            }

            ENDHLSL
        }
   
  Pass
        {
            Name "DepthOnly"
            Tags{"LightMode" = "DepthOnly"}

            ZWrite On
            ColorMask 0

            Cull Back

            HLSLPROGRAM
           
            #pragma prefer_hlslcc gles
            #pragma exclude_renderers d3d11_9x
            #pragma target 2.0
   
            // GPU Instancing
            #pragma multi_compile_instancing

            #pragma vertex vert
            #pragma fragment frag
               
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
               
            CBUFFER_START(UnityPerMaterial)
            CBUFFER_END
               
            struct VertexInput
            {
                float4 vertex : POSITION;                   
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

                struct VertexOutput
                {           
                float4 vertex : SV_POSITION;
                 
                UNITY_VERTEX_INPUT_INSTANCE_ID           
                UNITY_VERTEX_OUTPUT_STEREO                 
                };

            VertexOutput vert(VertexInput v)
            {
                VertexOutput o;
                UNITY_SETUP_INSTANCE_ID(v);
                UNITY_TRANSFER_INSTANCE_ID(v, o);
                UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);

                o.vertex = TransformObjectToHClip(v.vertex.xyz);

                return o;
            }

            half4 frag(VertexOutput IN) : SV_TARGET
            {       
                return 0;
            }
            ENDHLSL
        }
 
    }
}




code자체는 좀 더 다음을수 있으나 그건 틈틈히 하는걸로....
Lit 및 Simple Lit 기본 property인 _BaseMap 및 _Cutout을 사용하면 built-in code와 중복 선언이 되므로 피해야 함.


반응형

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

Shader Graph Custom Shader function  (0) 2020.02.15
Shader에서 Linear <-> sRGB 변환 함수  (0) 2020.02.14
LWRP Basic shader code  (0) 2019.09.08
Unity PBR Default  (0) 2019.07.10
Lightmap custom based Surface shader  (0) 2019.05.27