PBR urp 简单写法

Shader "JZ/PBRGai"
{
    Properties
    {    
         [Header(_________________________All Tex___________________________)]
         [HDR]_AllColor("AllColor",Color)=(1,1,1,0)
         [Toggle]_OpenAlbedo("OpenAlbedo",float)=1
         [Toggle]_OpenEmission("OpenEmission",float)=0
         _Culloff("Culloff",Range(0,1))=0.5


         [Header(_________________________Main Tex___________________________)]
         [HDR]_MainColor("MainColor",Color)=(1,1,1,1)
         _MainTeture("MainTex",2D)="white" {}
         _MainU("MainU",float)=0
          _MainV("MainV",float)=0
        

          [Header(_________________________ASM Module___________________________)]
         [Toggle]_DistortASS("DistortASM",float)=0
         _SecTex("ARM",2D)="white" {}
         _SecTexU("SecU",float)=0
         _SecTexV("SecV",float)=0
         _AOScale("AOScale",float)=1
         _MetllicScale("MetllicScale",Range(0,1))=0
         _RoughScale("smoothScale",Range(0,1))=0

         [Header(_________________________Cube Module___________________________)]
        _CubeMap("CubeMap",Cube)="black"{}
        _CubeScale("CubeScale",float)=1
         
        [Header(_________________________Distort Module___________________________)]
        [Enum(uv1,0,uv2,1)]_DistortTexUV("DistortTexUV",float)=0
         _DistortTex("DistortTex",2D)="black" {}
         _DistortU("DistortU",float)=0
         _DistortV("DistortV",float)=0
         _DistortScale("DistortScale",Range(0,1))=0

    
      


         [Header(_________________________Normal Module___________________________)]
         [Toggle]_DistortNormal("DistortNormal",float)=0
         _NormalMap("NormalTex",2D)="bump" {}
         _NormalV("NormalV",float)=0
         _NormalU("NormalU",float)=0
         _NormalIntensity("NormalScale",float)=1


       

        [Header(_________________________Vertex Module___________________________)]
        [Toggle]_DistortOffset("DistortOffset",float)=0
        _OffsetTex("OffsetTex",2D)="white" {}
        _OffsetTexU("OffsetU",float)=0
        _OffsetTexV("OffsetV",float)=0

        _OffsetMaskTex("OffsetMaskTex",2D)="white"{}
        _OffsetMaskU("OffsetMaskU",float)=0
        _OffsetMaskV("OffsetMaskV",float)=0

        _VertexOffsetScale("VertexOffsetScale",float)=0

        [Header(_________________________Opacity Module___________________________)]


        [Toggle]_OffsetOpacityFrequency("OffsetOpacityFrequency",float)=0
        _OpacityMaskTex("OpacityMaskTex",2D)="white"{}
        _OpacityMaskU("OpacityMaskU",float)=0
        _OpacityMaskU("OpacityMaskU",float)=0

        [Enum(OpacityMask,0,OffsetMask,1)]_MaskModule("MaskModule",float)=0
        _AlphaTex("AlphaTex",2D)="white"{}
        _AlphaTexU("AlphaTexU",float)=0
        _AlphaTexV("AlphaTexV",float)=0
        _MaskAddBias("MaskAddBias",float)=0

        [Header(_________________________Dither Module___________________________)]
        [Toggle]_OpenDither("OpenDither",float)=0
        _DitherBias("DitherBias",Range(0,1))=0
       




        
    
        
    }
 
    SubShader
    {
        Tags { "Queue"="Transparent" "RenderType" = "TransparentCutout" "IgnoreProjector" = "True" "RenderPipeline" = "UniversalPipeline" }
        LOD 100
 
        Pass
        {
            Name "Forward"
            Tags { "LightMode"="UniversalForward" }
            HLSLPROGRAM
            #pragma multi_compile _ _SCREEN_SPACE_OCCLUSION
            #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
            #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
            #pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS _ADDITIONAL_OFF
            #pragma multi_compile _ _ADDITIONAL_LIGHT_SHADOWS
            #pragma multi_compile _ _SHADOWS_SOFT
            #pragma multi_compile _ _MIXED_LIGHTING_SUBTRACTIVE    
            #pragma multi_compile _ LIGHTMAP_SHADOW_MIXING
            #pragma multi_compile _ SHADOWS_SHADOWMASK
            #pragma multi_compile _ DIRLIGHTMAP_COMBINED
            #pragma multi_compile _ LIGHTMAP_ON
            #pragma vertex vert
            #pragma fragment frag
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
            #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
            #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl"
            #include "Packages/com.unity.shadergraph/ShaderGraphLibrary/Functions.hlsl"
 
            struct Attributes
            {
                float4 positionOS       : POSITION;
                float2 texcoord0        : TEXCOORD0;
                float2 texcoord1        : TEXCOORD1;
                float4 tangent          : TANGENT;
                float4 normal           : NORMAL;
                

            };
 
            struct Varyings
            {
                float4 positionCS       : SV_POSITION;
                float4 lightmapUVOrVertexSH : TEXCOORD0;
                #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
                float4 shadowCoord : TEXCOORD1;
                #endif
                float4 normal      : TEXCOORD2;
                float4 tangent     : TEXCOORD3;
                float4 bitangent   : TEXCOORD4;
                float4 uv0         : TEXCOORD5;
                float4 uv1         : TEXCOORD6;
                float4 uv2         : TEXCOORD7;
                float4 uv3         : TEXCOORD8;
                float4 screenPos   : TEXCOORD9;

            };
            
            inline half remap(half x, half t1, half t2, half s1, half s2)
            {
                return (x - t1) / (t2 - t1) * (s2 - s1) + s1;
            }
            inline float Dither4x4Bayer( int x, int y )
            {
            const float dither[ 16 ] = {
                 1,  9,  3, 11,
                13,  5, 15,  7,
                 4, 12,  2, 10,
                16,  8, 14,  6 };
            int r = y * 4 + x;
            return dither[r] / 16; // same # of instructions as pre-dividing due to compiler magic
            }
 
            CBUFFER_START(UnityPerMaterial)
            half4 _NormalMap_ST;
            half4 _MainColor;
            half4 _MainTeture_ST;
            half4 _Emission;
            half  _NormalIntensity;
            half  _Metallic;
            half  _Gloss;

            half  _MainU;
            half  _MainV;

            half _NormalV;
            half _NormalU;
            half _DistortNormal;
            half _DistortASS;
            half4 _SecTex_ST;
            half _SecTexU;
            half _SecTexV;
            half _RoughScale;
            half _MetllicScale;
            half _AOScale;
            half _CubeScale;
            //Color
            half4 _AllColor;
            half _OpenAlbedo;
            half _OpenEmission;


            //Tex Offset
            half _DistortOffset;
            half4 _OffsetTex_ST;
            half _OffsetTexU;
            half _OffsetTexV;

            half4 _OffsetMaskTex_ST;
            half _OffsetMaskU;
            half _OffsetMaskV;

            half _OffsetOpacityFrequency;
            half4 _OpacityMaskTex_ST;
            half _OpacityMaskU;
            half _OpacityMaskV;

            half4  _AlphaTex_ST;
            half _AlphaTexU;
            half _AlphaTexV;

            half _MaskModule;
            half _VertexOffsetScale;
            half _MaskAddBias;
            half _OpenDither;
            half _DitherBias;
            half _Culloff;


             half    _DistortTexUV;
            half4   _DistortTex_ST;
            half    _DistortU;
            half    _DistortV;
            half    _DistortScale;

           



            CBUFFER_END
            TEXTURE2D (_NormalMap);SAMPLER(sampler_NormalMap);
            TEXTURE2D (_MainTeture);SAMPLER(sampler_MainTeture);
            TEXTURE2D (_SecTex);SAMPLER(sampler_SecTex);
            TEXTURE2D (_DistortTex);SAMPLER(sampler_DistortTex);

            TEXTURE2D (_OffsetTex);SAMPLER(sampler_OffsetTex);
            TEXTURE2D (_OffsetMaskTex);SAMPLER(sampler_OffsetMaskTex);
            TEXTURE2D (_OpacityMaskTex);SAMPLER(sampler_OpacityMaskTex);
            TEXTURE2D (_AlphaTex);SAMPLER(sampler_AlphaTex);
            samplerCUBE  _CubeMap;
            Varyings vert(Attributes v)
            {
                Varyings o = (Varyings)0;

                o.uv0.xy=((_DistortTexUV)?(TRANSFORM_TEX(v.texcoord0,_DistortTex)):(TRANSFORM_TEX(v.texcoord1,_DistortTex)));
                o.uv0.xy+=half2(_DistortU,_DistortV)*_Time.y;
                half3 distort_tex =saturate(SAMPLE_TEXTURE2D_LOD( _DistortTex, sampler_DistortTex, o.uv0.xy,0).rgb);
                distort_tex=dot(distort_tex,float3( 0.299, 0.587, 0.114 ));//灰度值
                half2 distort_uv=distort_tex.rr;
                
                
                o.uv0.zw =TRANSFORM_TEX(v.texcoord0,_MainTeture);
                o.uv1.xy=TRANSFORM_TEX(v.texcoord0,_NormalMap);
                o.uv1.zw=TRANSFORM_TEX(v.texcoord0,_SecTex);


               o.uv2.xy=TRANSFORM_TEX(v.texcoord0,_OffsetTex);
               o.uv2.xy=((_DistortOffset==0)?o.uv2.xy:lerp(o.uv2.xy,distort_uv,_DistortScale));           
               o.uv2.xy+=half2(_OffsetTexU,_OffsetTexV)*_Time.y;

              
               o.uv2.zw=TRANSFORM_TEX(v.texcoord0,_OffsetMaskTex);
               o.uv2.zw+=half2(_OffsetMaskU,_OffsetMaskV)*_Time.y;

             
               o.uv3.xy=TRANSFORM_TEX(v.texcoord0,_OpacityMaskTex);
               o.uv3.xy=((_OffsetOpacityFrequency==1)?(o.uv3.xy+half2(_OffsetMaskU,_OffsetMaskV)*_Time.y):(o.uv3.xy+half2(_OpacityMaskU,_OpacityMaskV)*_Time.y));

            
               o.uv3.zw=TRANSFORM_TEX(v.texcoord0,_AlphaTex);
               o.uv3.zw+=half2(_AlphaTexU,_AlphaTexV)*_Time.y;
                half3 vertex_data=saturate(SAMPLE_TEXTURE2D_LOD(_OffsetTex,sampler_OffsetTex,o.uv2.xy,0).rgb);
                half3 vertex_mask=saturate(SAMPLE_TEXTURE2D_LOD(_OffsetMaskTex,sampler_OffsetMaskTex,o.uv2.zw,0).rgb);
                vertex_data*=vertex_mask;
             

                 v.positionOS.xyz+=(vertex_data*v.normal*_VertexOffsetScale);
                o.positionCS = TransformObjectToHClip(v.positionOS.xyz);
                half3 positionWS=TransformObjectToWorld(v.positionOS.xyz);
                VertexNormalInputs normalInput = GetVertexNormalInputs(v.normal, v.tangent);
                o.normal=half4(normalInput.normalWS,positionWS.x);
                o.tangent=half4(normalInput.tangentWS,positionWS.y);
                o.bitangent=half4(normalInput.bitangentWS,positionWS.z);

                #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
                VertexPositionInputs vertexInput = (VertexPositionInputs)0;
                vertexInput.positionWS = positionWS;
                vertexInput.positionCS = o.positionCS;
                o.shadowCoord = GetShadowCoord( vertexInput );
                #endif
                o.screenPos=ComputeScreenPos(o.positionCS);
                return o;
            }
 
            half4 frag(Varyings input) : SV_Target
            {   



              //AllColor
                half4 final_color=_AllColor*_AllColor;

              //DistortU
                half3 distort_tex =saturate(SAMPLE_TEXTURE2D( _DistortTex, sampler_DistortTex, input.uv0.xy).rgb);//扰动贴图
                distort_tex=dot(distort_tex,float3( 0.299, 0.587, 0.114 ));//灰度
                half2 distort_uv=distort_tex.rr;

                 //MainUV
                input.uv0.zw+=half2(_MainU,_MainV)*_Time.y;
                half4 main_tex =(SAMPLE_TEXTURE2D( _MainTeture, sampler_MainTeture, input.uv0.zw))*_MainColor;//主贴图

             

               half4 c;
                half3 normalWS=normalize(input.normal.xyz);
                half3 tangentWS=normalize(input.tangent.xyz);
                half3 bitangentWS=normalize(input.bitangent.xyz);
                half3 world_pos=half3(input.normal.w,input.tangent.w,input.bitangent.w);
                half3 world_view=normalize( _WorldSpaceCameraPos.xyz  - world_pos);
                half3x3 TBN=half3x3(tangentWS,bitangentWS,normalWS);

                half4 ShadowCoords = half4(0, 0, 0, 0);
                #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
                    ShadowCoords = input.shadowCoord;
                #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
                    ShadowCoords = TransformWorldToShadowCoord( world_pos );
                #endif

              //  half3 Albedo=SAMPLE_TEXTURE2D(_MainTeture,sampler_MainTeture,input.uv1.xy);
             
                input.uv1.xy=((_DistortNormal==0)?(input.uv1.xy):(lerp(input.uv1.xy,distort_uv,_DistortScale)));    
                half3 normal_data=UnpackNormalScale(SAMPLE_TEXTURE2D(_NormalMap,sampler_NormalMap,input.uv1.xy),_NormalIntensity);
              

                half Specular = 0.5;
                
               
                half Alpha = 1;
                

                normal_data=normalize(mul(normal_data,TBN));
                
                //CubeMap
                half3 reflect_dir=reflect(-world_view,normal_data);
                half3 cube_color=texCUBE(_CubeMap,reflect_dir).xyz*_CubeScale;

                
                //SecTex
                input.uv1.zw+=half2(_SecTexU,_SecTexV)*_Time.y;
                input.uv1.zw=((_DistortASS==1)?(lerp(input.uv1.zw,distort_uv,_DistortScale)):input.uv1.zw  );
                half4 SecTex=(SAMPLE_TEXTURE2D( _SecTex, sampler_SecTex, input.uv1.zw));
               
           
                half  Smoothness= saturate((SecTex.g)*_RoughScale);
                half Metallic = saturate( SecTex.b*_MetllicScale);
                half3 albedo=((_OpenAlbedo)?(cube_color+main_tex)*final_color.xyz:half3(0,0,0));
                half3 emission=((_OpenEmission)?(cube_color+main_tex)*final_color.xyz:half3(0,0,0));
                half Occlusion = SecTex.r*_AOScale;
                half3 SH = SampleSH(normal_data.xyz);
                
                InputData inputData;
                inputData.positionWS = world_pos;
                inputData.viewDirectionWS = world_view;
                inputData.shadowCoord = ShadowCoords;
                inputData.normalWS=normal_data;
                inputData.bakedGI = SAMPLE_GI( IN.lightmapUVOrVertexSH.xy, SH, inputData.normalWS );
                inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
                inputData.shadowMask = SAMPLE_SHADOWMASK(IN.lightmapUVOrVertexSH.xy);
                half4 color = UniversalFragmentPBR(
                    inputData, 
                    albedo, 
                    Metallic, 
                    Specular, 
                    Smoothness, 
                    Occlusion, 
                    emission, 
                    Alpha);
                half vertex_data=dot(saturate(SAMPLE_TEXTURE2D(_OffsetTex,sampler_OffsetTex,input.uv2.xy)),float3( 0.299, 0.587, 0.114)).r;
                half vertex_mask=dot(saturate(SAMPLE_TEXTURE2D(_OffsetMaskTex,sampler_OffsetMaskTex,input.uv2.zw)),float3( 0.299, 0.587, 0.114 )).r;
                vertex_data*=vertex_mask;

                half alpha_mask=dot(SAMPLE_TEXTURE2D(_OpacityMaskTex,sampler_OffsetTex,input.uv3.xy),float3( 0.299, 0.587, 0.114 )).r;
                half alpha_tex=dot(SAMPLE_TEXTURE2D(_AlphaTex,sampler_OffsetTex,input.uv3.zw),float3( 0.299, 0.587, 0.114 )).r;
                alpha_mask=((_MaskModule==0)?alpha_mask:vertex_data);
                alpha_tex=saturate(alpha_mask*alpha_tex+_MaskAddBias);
                alpha_tex=remap(alpha_tex,0,1,0,2);
                
                half2 screenPos=input.screenPos.xy / input.screenPos.w * _ScreenParams.xy;
                half dither = Dither4x4Bayer( fmod(screenPos.x, 4), fmod(screenPos.y, 4) );
                dither=step(dither,alpha_tex+_DitherBias);
                alpha_tex=((_OpenDither==0)?alpha_tex:dither);
               
               
                clip(alpha_tex-_Culloff);
                
                //return color;
                return float4(color.xyz,1);
            }
            ENDHLSL
        }
        Pass
        {
            Name "ShadowCaster"
            Tags { "LightMode"="ShadowCaster" }

            ZWrite On
            ZTest LEqual
            AlphaToMask Off

            HLSLPROGRAM
            
            #pragma vertex vert
            #pragma fragment frag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl"
            #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"

            struct VertexInput
            {
                float4 vertex : POSITION;
                float3 ase_normal : NORMAL;
            };

            struct VertexOutput
            {
                float4 clipPos : SV_POSITION;
            };

            CBUFFER_START(UnityPerMaterial)
            float4 _NormalMap_ST;
            float4 _MainColor;
            float4 _MainTeture_ST;
            float4 _Emission;
            float4 _RoughnessMap_ST;
            float _NormalIntensity;
            float _Metallic;
            float _Gloss;
            CBUFFER_END
            
            float3 _LightDirection;

            VertexOutput vert( VertexInput v )
            {
                VertexOutput o;

                float3 positionWS = TransformObjectToWorld( v.vertex.xyz );
                float3 normalWS = TransformObjectToWorldDir(v.ase_normal);

                float4 clipPos = TransformWorldToHClip( ApplyShadowBias( positionWS, normalWS, _LightDirection ) );

                o.clipPos = clipPos;
                return o;
            }

            half4 frag(VertexOutput IN) : SV_TARGET
            {
                float4 ShadowCoords = float4( 0, 0, 0, 0 );

                #if defined(ASE_NEEDS_FRAG_SHADOWCOORDS)
                    #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
                        ShadowCoords = IN.shadowCoord;
                    #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
                        ShadowCoords = TransformWorldToShadowCoord( WorldPosition );
                    #endif
                #endif

                float Alpha = 1;
                float AlphaClipThreshold = 0.5;
                float AlphaClipThresholdShadow = 0.5;

                return 0;
            }
            ENDHLSL
        }
        
    }
    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值