Skip to content

Commit 79419f5

Browse files
committed
hair specular shift + jitter
1 parent 0199da2 commit 79419f5

File tree

8 files changed

+38
-12
lines changed

8 files changed

+38
-12
lines changed

Resources/Luka_Backlace/Editor/Interface.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ public class Interface : ShaderGUI
264264
private MaterialProperty prop_SheenColor = null;
265265
private MaterialProperty prop_SheenIntensity = null;
266266
private MaterialProperty prop_SheenRoughness = null;
267+
private MaterialProperty prop_SpecularJitter = null;
267268
// rim lighting properties
268269
private MaterialProperty prop_ToggleRimlight = null;
269270
private MaterialProperty prop_RimColor = null;
@@ -1620,6 +1621,7 @@ private void DrawUI(MaterialEditor materialEditor, MaterialProperty[] properties
16201621
prop_TangentMap = FindProperty("_TangentMap", properties);
16211622
prop_Anisotropy = FindProperty("_Anisotropy", properties);
16221623
prop_SpecularTint = FindProperty("_SpecularTint", properties); // hair uses it, so display twice
1624+
prop_SpecularJitter = FindProperty("_SpecularJitter", properties);
16231625
int specularMode = (int)prop_SpecularMode.floatValue;
16241626
switch (specularMode)
16251627
{
@@ -1641,6 +1643,7 @@ private void DrawUI(MaterialEditor materialEditor, MaterialProperty[] properties
16411643
materialEditor.ShaderProperty(prop_SecondarySpecularShift, languages.speak("prop_SecondarySpecularShift"));
16421644
materialEditor.ShaderProperty(prop_SecondarySpecularColor, languages.speak("prop_SecondarySpecularColor"));
16431645
materialEditor.ShaderProperty(prop_SpecularExponent, languages.speak("prop_SpecularExponent"));
1646+
materialEditor.ShaderProperty(prop_SpecularJitter, languages.speak("prop_SpecularJitter"));
16441647
break;
16451648
case 4: // cloth
16461649
materialEditor.ShaderProperty(prop_SheenColor, languages.speak("prop_SheenColor"));

Resources/Luka_Backlace/Includes/Backlace_Properties.cginc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ float3 _VertexManipulationScale;
167167
float _SecondarySpecularShift;
168168
float4 _SecondarySpecularColor;
169169
float _SpecularExponent;
170+
float _SpecularJitter;
170171
#elif defined(_SPECULARMODE_CLOTH) // CLOTH SPECULAR
171172
float4 _SheenColor;
172173
float _SheenIntensity;

Resources/Luka_Backlace/Includes/Backlace_Shading.cginc

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -563,19 +563,33 @@ void AddAlpha(inout BacklaceSurfaceData Surface)
563563
// kajiya-kay hair specular
564564
float3 HairDirectSpecular(float3 tangentDir, float3 lightDir, inout BacklaceSurfaceData Surface)
565565
{
566-
float2 flow = UNITY_SAMPLE_TEX2D(_HairFlowMap, Uvs[_HairFlowMap_UV]).rg * 2 - 1;
566+
// sample
567+
float3 flowSample = UNITY_SAMPLE_TEX2D(_HairFlowMap, Uvs[_HairFlowMap_UV]).rgb;
568+
float2 flow = flowSample.rg * 2 - 1;
569+
float shiftMask = saturate(flowSample.b);
567570
float3 hairTangent = normalize(flow.x * Surface.TangentDir + flow.y * Surface.BitangentDir);
568-
float3 shiftedTangent1 = normalize(hairTangent + Surface.NormalDir * _PrimarySpecularShift);
571+
// generate jitter
572+
float jitter = 0.0;
573+
if (_SpecularJitter > 0.001) // don't branch? let compiler handle it
574+
{
575+
float noise = frac(sin(dot(Uvs[_HairFlowMap_UV] * 25.0, float2(12.9898, 78.233))) * 43758.5453);
576+
jitter = (noise * 2.0 - 1.0) * _SpecularJitter;
577+
}
578+
// primary shift/spec
579+
float primaryShift = (_PrimarySpecularShift + jitter) * shiftMask;
580+
float3 shiftedTangent1 = normalize(hairTangent + Surface.NormalDir * primaryShift);
569581
float dotT1L = dot(shiftedTangent1, lightDir);
570582
float dotT1V = dot(shiftedTangent1, Surface.ViewDir);
571-
float sinT1L = sqrt(1.0 - dotT1L * dotT1L);
572-
float sinT1V = sqrt(1.0 - dotT1V * dotT1V);
583+
float sinT1L = sqrt(saturate(1.0 - dotT1L * dotT1L));
584+
float sinT1V = sqrt(saturate(1.0 - dotT1V * dotT1V));
573585
float primarySpec = pow(saturate(dotT1L * dotT1V + sinT1L * sinT1V), _SpecularExponent);
574-
float3 shiftedTangent2 = normalize(hairTangent + Surface.NormalDir * _SecondarySpecularShift);
586+
// secondary shift/spec
587+
float secondaryShift = (_SecondarySpecularShift + jitter) * shiftMask;
588+
float3 shiftedTangent2 = normalize(hairTangent + Surface.NormalDir * secondaryShift);
575589
float dotT2L = dot(shiftedTangent2, lightDir);
576590
float dotT2V = dot(shiftedTangent2, Surface.ViewDir);
577-
float sinT2L = sqrt(1.0 - dotT2L * dotT2L);
578-
float sinT2V = sqrt(1.0 - dotT2V * dotT2V);
591+
float sinT2L = sqrt(saturate(1.0 - dotT2L * dotT2L));
592+
float sinT2V = sqrt(saturate(1.0 - dotT2V * dotT2V));
579593
float secondarySpec = pow(saturate(dotT2L * dotT2V + sinT2L * sinT2V), _SpecularExponent);
580594
float3 secondaryColor = Surface.Albedo.rgb * _SecondarySpecularColor.rgb;
581595
return (primarySpec * Surface.SpecularColor) + (secondarySpec * secondaryColor);

Resources/Luka_Backlace/Languages/English.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@
10381038
},
10391039
{
10401040
"key": "prop_HairFlowMap",
1041-
"value": "Hair Flow/Tangent Map (RG)"
1041+
"value": "Hair Flow (RG) Shift (B)"
10421042
},
10431043
{
10441044
"key": "prop_PrimarySpecularShift",
@@ -2755,6 +2755,10 @@
27552755
{
27562756
"key": "legacy_mode",
27572757
"value": "Legacy Hardware Support\n\nIf you experience compilation errors or need to use this shader on older hardware that doesn't support Shader Model 5.0, you can enable Legacy Mode.\n\nHow to Enable:\n1. Navigate to Resources/Luka_Backlace/Includes/\n2. Open these three files in a text editor:\n • Backlace_Forward.cginc\n • Backlace_Shadow.cginc\n • Backlace_Outline.cginc\n3. In each file, find line 6: // #define BACKLACE_LEGACY_MODE\n4. Remove the // to uncomment it (so it reads: #define BACKLACE_LEGACY_MODE)\n\nWhat This Does:\n• Compiles the shader for Shader Model 3.0 (compatible with older GPUs)\n• Automatically disables AudioLink and LTCGI integration to stay within hardware limits\n• All other features remain functional\n\nNote: You must edit all three files for the shader to compile correctly."
2758+
},
2759+
{
2760+
"key": "prop_SpecularJitter",
2761+
"value": "Specular Jitter"
27582762
}
27592763
]
27602764
}

Shaders/Backlace.shader

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ Shader "luka/backlace/default"
215215
_SecondarySpecularShift ("Secondary Specular Shift", Range(-1, 1)) = 0.1
216216
[HDR] _SecondarySpecularColor ("Secondary Specular Color", Color) = (1, 1, 1, 1)
217217
_SpecularExponent ("Specular Exponent", Range(1, 256)) = 64
218+
_SpecularJitter ("Specular Jitter", Range(0, 1)) = 0.02
218219
// cloth specular
219220
_SheenColor ("Sheen Color", Color) = (1, 1, 1, 1)
220221
_SheenIntensity ("Sheen Intensity", Float) = 0.5
@@ -783,7 +784,7 @@ Shader "luka/backlace/default"
783784

784785
// Rendering Settings
785786
// Tags { "RenderType" = "TransparentCutout" "Queue" = "AlphaTest" } or Transparent
786-
Tags { "RenderType" = "Transparent" "Queue" = "Transparent" "VRCFallback"="Toon" "Backlace"="1.8.0" }
787+
Tags { "RenderType" = "Transparent" "Queue" = "Transparent" "VRCFallback"="Toon" "Backlace"="1.8.5" }
787788
Blend [_SrcBlend] [_DstBlend]
788789
ZWrite [_ZWrite]
789790
Cull [_Cull]

Shaders/Backlace_All.shader

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ Shader "luka/backlace/all"
215215
_SecondarySpecularShift ("Secondary Specular Shift", Range(-1, 1)) = 0.1
216216
[HDR] _SecondarySpecularColor ("Secondary Specular Color", Color) = (1, 1, 1, 1)
217217
_SpecularExponent ("Specular Exponent", Range(1, 256)) = 64
218+
_SpecularJitter ("Specular Jitter", Range(0, 1.0)) = 0.02
218219
// cloth specular
219220
_SheenColor ("Sheen Color", Color) = (1, 1, 1, 1)
220221
_SheenIntensity ("Sheen Intensity", Float) = 0.5
@@ -783,7 +784,7 @@ Shader "luka/backlace/all"
783784

784785
// Rendering Settings
785786
// Tags { "RenderType" = "TransparentCutout" "Queue" = "AlphaTest" } or Transparent
786-
Tags { "RenderType" = "Transparent" "Queue" = "Transparent" "VRCFallback"="Toon" "Backlace"="1.8.0" }
787+
Tags { "RenderType" = "Transparent" "Queue" = "Transparent" "VRCFallback"="Toon" "Backlace"="1.8.5" }
787788
Blend [_SrcBlend] [_DstBlend]
788789
ZWrite [_ZWrite]
789790
Cull [_Cull]

Shaders/Backlace_Grabpass.shader

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ Shader "luka/backlace/grabpass"
215215
_SecondarySpecularShift ("Secondary Specular Shift", Range(-1, 1)) = 0.1
216216
[HDR] _SecondarySpecularColor ("Secondary Specular Color", Color) = (1, 1, 1, 1)
217217
_SpecularExponent ("Specular Exponent", Range(1, 256)) = 64
218+
_SpecularJitter ("Specular Jitter", Range(0, 1.0)) = 0.02
218219
// cloth specular
219220
_SheenColor ("Sheen Color", Color) = (1, 1, 1, 1)
220221
_SheenIntensity ("Sheen Intensity", Float) = 0.5
@@ -783,7 +784,7 @@ Shader "luka/backlace/grabpass"
783784

784785
// Rendering Settings
785786
// Tags { "RenderType" = "TransparentCutout" "Queue" = "AlphaTest" } or Transparent
786-
Tags { "RenderType" = "Transparent" "Queue" = "Transparent" "VRCFallback"="Toon" "Backlace"="1.8.0" }
787+
Tags { "RenderType" = "Transparent" "Queue" = "Transparent" "VRCFallback"="Toon" "Backlace"="1.8.5" }
787788
Blend [_SrcBlend] [_DstBlend]
788789
ZWrite [_ZWrite]
789790
Cull [_Cull]

Shaders/Backlace_Outline.shader

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ Shader "luka/backlace/outline"
215215
_SecondarySpecularShift ("Secondary Specular Shift", Range(-1, 1)) = 0.1
216216
[HDR] _SecondarySpecularColor ("Secondary Specular Color", Color) = (1, 1, 1, 1)
217217
_SpecularExponent ("Specular Exponent", Range(1, 256)) = 64
218+
_SpecularJitter ("Specular Jitter", Range(0, 1.0)) = 0.02
218219
// cloth specular
219220
_SheenColor ("Sheen Color", Color) = (1, 1, 1, 1)
220221
_SheenIntensity ("Sheen Intensity", Float) = 0.5
@@ -783,7 +784,7 @@ Shader "luka/backlace/outline"
783784

784785
// Rendering Settings
785786
// Tags { "RenderType" = "TransparentCutout" "Queue" = "AlphaTest" } or Transparent
786-
Tags { "RenderType" = "Transparent" "Queue" = "Transparent" "VRCFallback"="Toon" "Backlace"="1.8.0" }
787+
Tags { "RenderType" = "Transparent" "Queue" = "Transparent" "VRCFallback"="Toon" "Backlace"="1.8.5" }
787788
Blend [_SrcBlend] [_DstBlend]
788789
ZWrite [_ZWrite]
789790
Cull [_Cull]

0 commit comments

Comments
 (0)