Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve normal strength factor and blending #1

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,50 @@ COMPUTE_EYEDEPTH(o.eyeDepth);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


// https://forum.unity.com/threads/how-to-control-normal-map-strength-by-a-variable.366174/#post-2373555
// Optimized version that will only lerp the xy parts of the normal vector
// Copied unity `UnpackNormal` macro to optimize math further, instead of
// normal.xy = UnpackNormal(packed).xy * strength;
// normal.z = sqrt(1 - saturate(dot(normal.xy, normal.xy)));
// This ensures a uniform vector result and does it only once
// Note: lerp is not called anymore as we lerp toward (0,0,1)
inline fixed3 UnpackNormalScaled(fixed4 packed, fixed strength) {
fixed3 normal;
#if defined(UNITY_NO_DXT5nm)
normal.xy = packed.xy;
#elif defined(UNITY_ASTC_NORMALMAP_ENCODING)
normal.xy = packed.wy;
#else
packed.x *= packed.w;
normal.xy = packed.xy;
#endif
normal.xy = (normal.xy * 2 - 1) * strength;
normal.z = sqrt(1 - saturate(dot(normal.xy, normal.xy)));
return normal;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


inline void BlendNormal(inout fixed3 n1, fixed3 n2) {
// https://blog.selfshadow.com/publications/blending-in-detail/
#if defined(BLEND_NORMAL_RNM)
n2.xy *= -1; n1.z += 1;
n1 = normalize(n1 * dot(n1, n2) - n2 * n1.z);
#elif defined(BLEND_NORMAL_PDN)
n1 = normalize(fixed3(n1.xy / n1.z + n2.xy / n2.z, 1));
#elif defined(BLEND_NORMAL_WHITEOUT)
n1 = normalize(fixed3(n1.xy + n2.xy, n1.z * n2.z));
#else // defined(BLEND_NORMAL_UDM)
n1 = normalize(fixed3(n1.xy + n2.xy, n1.z));
#endif
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


float Noise(float2 uv, float gain, float amplitude, float frequency, float scale, float lacunarity, float octaves) {
float result;
float frequencyL = frequency;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ Properties{
[HideInInspector][HDR]_SpecColor("Specular Color", Color) = (0.086,0.086,0.086,1)
[HideInInspector]_SoftFactor("Soft Factor", Range(0.0001, 1)) = 0.5
[HideInInspector]_NormalMap1("Normal Map 1", 2D) = "bump" {}
[HideInInspector]_NormalMap1Strength("Normal Map 1 Strength", Range(0.001, 10)) = 1
[HideInInspector]_NormalMap1Strength("Normal Map 1 Strength", Range(-3, 3)) = 1
[HideInInspector][NoScaleOffset]_NormalMap2("Normal Map 2", 2D) = "bump" {}
[HideInInspector]_NormalMap2Tiling("Normal Map 2 Tiling", float) = 0.7
[HideInInspector]_NormalMap2Strength("Normal Map 2 Strength", Range(0.001, 10)) = 1
[HideInInspector]_NormalMap2Strength("Normal Map 2 Strength", Range(-3, 3)) = 1
[HideInInspector]_NormalMap2Flow("Normal Map 2 Flow", float) = 0.5
[HideInInspector]_MicrowaveScale("Micro Waves Scale", Range(0.5, 10)) = 1
[HideInInspector]_MicrowaveStrength("Micro Waves Strength", Range(0.001, 1.5)) = 0.5
[HideInInspector]_MicrowaveStrength("Micro Waves Strength", Range(-1.5, 1.5)) = 0.5
[HideInInspector]_ParallaxAmount("Parallax Amount", float) = 0.1
[HideInInspector]_ParallaxFlow("Parallax Flow", float) = 40
[HideInInspector]_ParallaxNormal2Offset("Parallax Normal Map 2 Offset", float) = 1
Expand Down Expand Up @@ -137,11 +137,11 @@ tex *= tex2D(_AlbedoTex2, uvd * _Albedo2Tiling);
tex *= _AlbedoIntensity;
float3 albedo = ((tex - 0.5) * _AlbedoContrast + 0.5).rgb;

float3 normal = UnpackNormal(tex2D(_NormalMap1, uvn)) * _NormalMap1Strength;
float3 normal = UnpackNormalScaled(tex2D(_NormalMap1, uvn), _NormalMap1Strength);
#ifdef EFFECT_NORMALMAP2
normal += UnpackNormal(tex2D(_NormalMap2, uvnd * _NormalMap2Tiling)) * _NormalMap2Strength;
BlendNormal(normal, UnpackNormalScaled(tex2D(_NormalMap2, uvnd * _NormalMap2Tiling), _NormalMap2Strength));
#ifdef EFFECT_MICROWAVE
normal += UnpackNormal(tex2D(_NormalMap2, (uv + uvnd) * 2 * _MicrowaveScale)) * _MicrowaveStrength;
BlendNormal(normal, UnpackNormalScaled(tex2D(_NormalMap2, (uv + uvnd) * 2 * _MicrowaveScale), _MicrowaveStrength));
#endif
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ Properties{
[HideInInspector]_Metallic("Metallic", Range(-1,2)) = 0.0
[HideInInspector]_SoftFactor("Soft Factor", Range(0.0001, 1)) = 0.5
[HideInInspector]_NormalMap1("Normal Map 1", 2D) = "bump" {}
[HideInInspector]_NormalMap1Strength("Normal Map 1 Strength", Range(0.001, 10)) = 1
[HideInInspector]_NormalMap1Strength("Normal Map 1 Strength", Range(-3, 3)) = 1
[HideInInspector][NoScaleOffset]_NormalMap2("Normal Map 2", 2D) = "bump" {}
[HideInInspector]_NormalMap2Tiling("Normal Map 2 Tiling", float) = 1.2
[HideInInspector]_NormalMap2Strength("Normal Map 2 Strength", Range(0.001, 10)) = 1
[HideInInspector]_NormalMap2Strength("Normal Map 2 Strength", Range(-3, 3)) = 1
[HideInInspector]_NormalMap2Flow("Normal Map 2 Flow", float) = 0.5
[HideInInspector]_MicrowaveScale("Micro Waves Scale", Range(0.5, 10)) = 1
[HideInInspector]_MicrowaveStrength("Micro Waves Strength", Range(0.001, 1.5)) = 0.5
[HideInInspector]_MicrowaveStrength("Micro Waves Strength", Range(-1.5, 1.5)) = 0.5
[HideInInspector]_ParallaxAmount("Parallax Amount", float) = 0.1
[HideInInspector]_ParallaxFlow("Parallax Flow", float) = 40
[HideInInspector]_ParallaxNormal2Offset("Parallax Normal Map 2 Offset", float) = 1
Expand Down Expand Up @@ -130,14 +130,11 @@ tex *= tex2D(_AlbedoTex2, uvd * _Albedo2Tiling);
tex *= _AlbedoIntensity;
float3 albedo = ((tex - 0.5) * _AlbedoContrast + 0.5).rgb;

float3 normal = UnpackNormal(tex2D(_NormalMap1, uvn)) * _NormalMap1Strength;
float3 normal = UnpackNormalScaled(tex2D(_NormalMap1, uvn), _NormalMap1Strength);
#ifdef EFFECT_NORMALMAP2
normal += UnpackNormal(tex2D(_NormalMap2, uvnd * _NormalMap2Tiling)) * _NormalMap2Strength;
BlendNormal(normal, UnpackNormalScaled(tex2D(_NormalMap2, uvnd * _NormalMap2Tiling), _NormalMap2Strength));
#ifdef EFFECT_MICROWAVE
normal -= UnpackNormal(tex2D(_NormalMap2, (uv + uvnd) * 2 * _MicrowaveScale)) * _MicrowaveStrength;
normal = normalize(normal / 3);
#else
normal = normalize(normal / 2);
BlendNormal(normal, UnpackNormalScaled(tex2D(_NormalMap2, (uv + uvnd) * 2 * _MicrowaveScale), _MicrowaveStrength));
#endif
#endif

Expand Down