From 3cd92e1de4c5f12a85262ba2c2d43c427d22b1f5 Mon Sep 17 00:00:00 2001 From: raccoon Date: Mon, 19 May 2025 03:49:54 +0500 Subject: [PATCH] Fixed normal mapping --- .../shaders/minecraft/StandardSurf.shader | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/Assets/dbl-world/shaders/minecraft/StandardSurf.shader b/Assets/dbl-world/shaders/minecraft/StandardSurf.shader index 9cfc6fd..89ccc70 100644 --- a/Assets/dbl-world/shaders/minecraft/StandardSurf.shader +++ b/Assets/dbl-world/shaders/minecraft/StandardSurf.shader @@ -24,8 +24,9 @@ Shader "Custom/StandardSurf" CGPROGRAM // Physically based Standard lighting model, and enable shadows on all light types - #pragma surface surf Standard fullforwardshadows alpha:fade - #pragma target 3.0 + #pragma surface surf Standard fullforwardshadows alpha:fade addshadow + #pragma target 3.5 + #pragma shader_feature_local _NORMALMAP UNITY_DECLARE_TEX2DARRAY(_DiffuseArray); fixed4 _Color; @@ -40,6 +41,7 @@ Shader "Custom/StandardSurf" struct Input { float2 uv_DiffuseArray; + INTERNAL_DATA // For proper normal handling }; // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader. @@ -49,14 +51,6 @@ Shader "Custom/StandardSurf" // put more per-instance properties here UNITY_INSTANCING_BUFFER_END(Props) - float3 UnpackAndScaleNormal(float4 c, float scale) - { - float3 n; - n.xy = (c.xy * 2 - 1) * scale; - n.z = sqrt(saturate(1.0 - dot(n.xy, n.xy))); - return n; - } - void surf(Input IN, inout SurfaceOutputStandard o) { // Common UV for all texture arrays @@ -66,17 +60,18 @@ Shader "Custom/StandardSurf" // Albedo with alpha float4 diff = UNITY_SAMPLE_TEX2DARRAY(_DiffuseArray, uvw) * _Color; o.Albedo = diff.rgb; - o.Alpha = diff.a; + o.Alpha = _Color.a; // Sample Metallic and smoothness (R = Metallic, A = Smoothness) plus slider variable float4 metalGloss = UNITY_SAMPLE_TEX2DARRAY(_MetallicArray, uvw); o.Metallic = metalGloss.r; o.Smoothness = metalGloss.a * _Glossiness; - // Normal map + // Normal map and it's scale float4 normalTex = UNITY_SAMPLE_TEX2DARRAY(_NormalArray, uvw); - float3 tangentNormal = UnpackAndScaleNormal(normalTex, _Bumpyness); - o.Normal = tangentNormal; + float3 normalMap = UnpackNormal(normalTex); + normalMap.xy *= _Bumpyness; + o.Normal = normalize(normalMap); } ENDCG