Fixed normal mapping

This commit is contained in:
raccoon
2025-05-19 03:49:54 +05:00
parent b5f3625e7d
commit 3cd92e1de4

View File

@@ -24,8 +24,9 @@ Shader "Custom/StandardSurf"
CGPROGRAM CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types // Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows alpha:fade #pragma surface surf Standard fullforwardshadows alpha:fade addshadow
#pragma target 3.0 #pragma target 3.5
#pragma shader_feature_local _NORMALMAP
UNITY_DECLARE_TEX2DARRAY(_DiffuseArray); UNITY_DECLARE_TEX2DARRAY(_DiffuseArray);
fixed4 _Color; fixed4 _Color;
@@ -40,6 +41,7 @@ Shader "Custom/StandardSurf"
struct Input struct Input
{ {
float2 uv_DiffuseArray; 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. // 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 // put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props) 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) void surf(Input IN, inout SurfaceOutputStandard o)
{ {
// Common UV for all texture arrays // Common UV for all texture arrays
@@ -66,17 +60,18 @@ Shader "Custom/StandardSurf"
// Albedo with alpha // Albedo with alpha
float4 diff = UNITY_SAMPLE_TEX2DARRAY(_DiffuseArray, uvw) * _Color; float4 diff = UNITY_SAMPLE_TEX2DARRAY(_DiffuseArray, uvw) * _Color;
o.Albedo = diff.rgb; o.Albedo = diff.rgb;
o.Alpha = diff.a; o.Alpha = _Color.a;
// Sample Metallic and smoothness (R = Metallic, A = Smoothness) plus slider variable // Sample Metallic and smoothness (R = Metallic, A = Smoothness) plus slider variable
float4 metalGloss = UNITY_SAMPLE_TEX2DARRAY(_MetallicArray, uvw); float4 metalGloss = UNITY_SAMPLE_TEX2DARRAY(_MetallicArray, uvw);
o.Metallic = metalGloss.r; o.Metallic = metalGloss.r;
o.Smoothness = metalGloss.a * _Glossiness; o.Smoothness = metalGloss.a * _Glossiness;
// Normal map // Normal map and it's scale
float4 normalTex = UNITY_SAMPLE_TEX2DARRAY(_NormalArray, uvw); float4 normalTex = UNITY_SAMPLE_TEX2DARRAY(_NormalArray, uvw);
float3 tangentNormal = UnpackAndScaleNormal(normalTex, _Bumpyness); float3 normalMap = UnpackNormal(normalTex);
o.Normal = tangentNormal; normalMap.xy *= _Bumpyness;
o.Normal = normalize(normalMap);
} }
ENDCG ENDCG