Home Game Development unity – How do I add URP lighting response to the next handwritten shader?

unity – How do I add URP lighting response to the next handwritten shader?

0
unity – How do I add URP lighting response to the next handwritten shader?

[ad_1]

I am on Unity. I am not very skilled with shaders nor shader graphs, however I managed to get this one to work on URP. At present I am altering the sunshine depth or brightness “manually” with the _LightIntensity variable. However this shader utilized to a fabric in a sprite renderer isn’t being affected by URP 2D lights, like world, form mild, until they’re volumetric. After I place a type of 2D URP lights on prime of the sprite within the scene, lights wont make brighter components of the sprite.
Any means I can add to the code to make it affected by 2D lights?

Shader "Customized/URPTilemapLitHSV" {
    Properties {
        _MainTex ("Map", 2D) = "white" {}
        _Tileset("Tileset", 2D) = "white" {}
        _Size("Map & Tileset Measurement", Vector) = (16, 16, 20, 13)
        _LightIntensity("Mild Depth", Vary(0, 5)) = 1
        _HueShift("Hue Shift", Vary(-180, 180)) = 0
        _Saturation("Saturation", Vary(0, 2)) = 1
        _Brightness("Brightness", Vary(0, 2)) = 1
    }
    SubShader {
        Tags {
            "RenderPipeline" = "UniversalPipeline"
            "Queue" = "Clear"
        }
        LOD 200
        Go {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma goal 3.0
            #embrace "Packages/com.unity.render-pipelines.common/ShaderLibrary/Core.hlsl"
            sampler2D _MainTex;
            sampler2D _Tileset;
            sampler2D _LightTex;
            float4 _Size;
            float _HueShift;
            float _Saturation;
            float _Brightness;
            struct appdata {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };
            struct v2f {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };
            v2f vert(appdata v) {
                v2f o;
                o.uv = v.uv;
                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                return o;
            }
            float3 RGBtoHSV(float3 colour) {
                float3 hsv;
                float minVal = min(min(colour.r, colour.g), colour.b);
                float maxVal = max(max(colour.r, colour.g), colour.b);
                hsv.z = maxVal;
                float delta = maxVal - minVal;
                if (maxVal > 0.0) {
                    hsv.y = delta / maxVal;
                    if (colour.r >= maxVal)
                        hsv.x = (colour.g - colour.b) / delta;
                    else if (colour.g >= maxVal)
                        hsv.x = 2.0 + (colour.b - colour.r) / delta;
                    else
                        hsv.x = 4.0 + (colour.r - colour.g) / delta;
                    hsv.x *= 60.0;
                    if (hsv.x < 0.0)
                        hsv.x += 360.0;
                }
                else {
                    hsv.y = 0.0;
                    hsv.x = 0.0;
                }
                return hsv;
            }
            float3 HSVtoRGB(float3 hsv) {
                float3 rgb;
                if (hsv.y == 0.0) {
                    rgb = float3(hsv.z, hsv.z, hsv.z);
                }
                else {
                    float hue = hsv.x / 60.0;
                    int i = ground(hue);
                    float f = hue - i;
                    float p = hsv.z * (1.0 - hsv.y);
                    float q = hsv.z * (1.0 - hsv.y * f);
                    float t = hsv.z * (1.0 - hsv.y * (1.0 - f));
                    if (i == 0)
                        rgb = float3(hsv.z, t, p);
                    else if (i == 1)
                        rgb = float3(q, hsv.z, p);
                    else if (i == 2)
                        rgb = float3(p, hsv.z, t);
                    else if (i == 3)
                        rgb = float3(p, q, hsv.z);
                    else if (i == 4)
                        rgb = float3(t, p, hsv.z);
                    else
                        rgb = float3(hsv.z, p, q);
                }
                return rgb;
            }
            float4 frag(v2f IN) : SV_Target {
                float2 mapLocation = IN.uv * _Size.xy;
                float2 inTileUV = frac(mapLocation);
                mapLocation = (mapLocation - inTileUV) / _Size.xy;
                inTileUV = inTileUV * 1275.0f / 1280.0f + 1.0f / 1280.0f;
                float4 grad = float4(ddx(IN.uv), ddy(IN.uv));
                float4 tileIndex = tex2D(_MainTex, mapLocation);
                tileIndex = (tileIndex * 255.0f + inTileUV.xyxy) / _Size.zwzw;
                float4 over = tex2Dgrad(_Tileset, tileIndex.zw, grad.xy, grad.zw);
                float3 colorRGB = over.rgb;
                // Apply hue shift
                float3 colorHSV = RGBtoHSV(colorRGB);
                colorHSV.x += _HueShift;
                colorHSV.x = frac(colorHSV.x / 360.0) * 360.0;
                colorRGB = HSVtoRGB(colorHSV);
                // Apply saturation and brightness
                colorRGB = saturate(colorRGB * _Saturation) * _Brightness;
                float4 finalColor = float4(colorRGB, over.a);
                // Multiply by mild depth
               finalColor.rgb *= _LightIntensity;
                if (finalColor.a < 0.5)
                    discard;
                return finalColor;
            }
            ENDHLSL
        }
    }
    FallBack "Diffuse"
}

I appeared into the URP sprite lit shader and it appears a really complicated code can not seem to get any concepts from it. It appears to distinguish the kind of mild that’s utilized be URP world, form and so on…

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here