Notice
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags
more
Archives
Today
Total
관리 메뉴

자라고싶다

[HLSL] Diffuse Wrap + Shadow Ramp 본문

Unity/UnityShader

[HLSL] Diffuse Wrap + Shadow Ramp

멍산 2023. 10. 17. 00:35

RampTexture 256*16

RampTexture의 Wrap Mode 는 Clamp 로 설정.

Shader "Example/DiffuseWrap"
{

    Properties
    {
         _TintColor("TintColor", Color) = (1,1,1,1)
         _RampTexture("RampTexture", 2D) = "white"{}
         _RampThreshold("RampThreshold", Range(0,1)) = 0.5

    }


        SubShader
    {

        Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }

        Pass
        {

            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"

            struct Attributes
            {
                float4 positionOS   : POSITION;
                float3 normal       : NORMAL;
            };

            struct Varyings
            {
                float4 positionHCS  : SV_POSITION;
                float3 normal       : NORMAL;
                float3 normalWS     : TEXCOORD1;
                float3 positionWS   : TEXCOORD2;
                float4 shadowcoord  : TEXCOORD3;
            };


            half4 _TintColor;
            half _RampThreshold;

            TEXTURE2D(_RampTexture);
            SAMPLER(sampler_RampTexture);


            Varyings vert(Attributes IN)
            {
                Varyings OUT;

                OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
                OUT.normal = IN.normal;
                OUT.normalWS = TransformObjectToWorldNormal(IN.normal);
                OUT.positionWS = TransformObjectToWorld(IN.positionOS);
                OUT.shadowcoord = TransformWorldToShadowCoord(OUT.positionWS);

                return OUT;
            }


            half4 frag(Varyings IN) : SV_Target
            {
                half4 color = _TintColor;

                Light light = GetMainLight(IN.shadowcoord);

                float3 L = light.direction;
                float3 N = IN.normalWS;
                float NDL = (dot(N, L) + _RampThreshold) / (1 + _RampThreshold); //Wrap Diffuse Term

                float3 Ramp = SAMPLE_TEXTURE2D(_RampTexture, sampler_RampTexture, float2(NDL,0.5));

                half3 diffuse = color.rgb * Ramp;

                return half4(diffuse.rgb,1);
            }
            ENDHLSL
        }
    }
}

 

참고자료

https://gamedevforever.com/150

'Unity > UnityShader' 카테고리의 다른 글

[CustomEditor]프로퍼티 #1  (0) 2024.04.23
[HLSL]Tessellation + Displacement  (1) 2023.11.23
[HLSL]2 Pass Outline  (0) 2023.10.16
[HLSL]Triplanar  (0) 2023.10.16
[HLSL]DepthTexture  (0) 2023.10.16