Skip to content

Commit

Permalink
v2.2.4
Browse files Browse the repository at this point in the history
- Added: Halftone / Stippling effect options
- Added: Matcap tint to diffuse
- Fixed: Black vertex lights should no longer affect light direction.
  • Loading branch information
Xiexe committed Jul 5, 2020
1 parent 32de4fa commit 60e4231
Show file tree
Hide file tree
Showing 19 changed files with 389 additions and 87 deletions.
2 changes: 1 addition & 1 deletion Editor/XSStyles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[InitializeOnLoad]
public class XSStyles : MonoBehaviour
{
public static string ver = "2.2.3";
public static string ver = "2.2.4";

//Help URLs
public static string mainURL = "https://docs.google.com/document/d/1xJ4PID_nwqVm_UCsO2c2gEdiEoWoCGeM_GDK_L8-aZE/edit#bookmark=id.xh0nk8x7ws1g";
Expand Down
33 changes: 30 additions & 3 deletions Editor/XSToonInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class XSToonInspector : ShaderGUI
MaterialProperty _MetallicGlossMap = null;
MaterialProperty _BakedCubemap = null;
MaterialProperty _Matcap = null;
MaterialProperty _MatcapTintToDiffuse = null;
MaterialProperty _MatcapTint = null;
MaterialProperty _ReflectivityMask = null;
MaterialProperty _Metallic = null;
Expand Down Expand Up @@ -79,9 +80,11 @@ public class XSToonInspector : ShaderGUI
MaterialProperty _SSDistortion = null;
MaterialProperty _SSPower = null;
MaterialProperty _SSScale = null;
//MaterialProperty _HalftoneDotSize = null;
//MaterialProperty _HalftoneDotAmount = null;
//MaterialProperty _HalftoneLineAmount = null;
MaterialProperty _HalftoneDotSize = null;
MaterialProperty _HalftoneDotAmount = null;
MaterialProperty _HalftoneLineAmount = null;
MaterialProperty _HalftoneLineIntensity = null;
MaterialProperty _HalftoneType = null;
MaterialProperty _UVSetAlbedo = null;
MaterialProperty _UVSetNormal = null;
MaterialProperty _UVSetDetNormal = null;
Expand Down Expand Up @@ -122,6 +125,7 @@ public class XSToonInspector : ShaderGUI
static bool showSpecular = false;
static bool showReflection = false;
static bool showRimlight = false;
static bool showHalftones = false;
static bool showSubsurface = false;
static bool showOutlines = false;
static bool showEmission = false;
Expand Down Expand Up @@ -177,6 +181,7 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro
DrawReflectionsSettings(materialEditor, material);
DrawEmissionSettings(materialEditor);
DrawRimlightSettings(materialEditor);
DrawHalfToneSettings(materialEditor);
DrawTransmissionSettings(materialEditor);
DrawAdvancedSettings(materialEditor);
DrawPatreonSettings(materialEditor);
Expand Down Expand Up @@ -369,6 +374,7 @@ private void DrawReflectionsSettings(MaterialEditor materialEditor, Material mat
XSStyles.SeparatorThin();
materialEditor.TexturePropertySingleLine(new GUIContent("Matcap", "Matcap Texture"), _Matcap, _MatcapTint);
materialEditor.ShaderProperty(_Glossiness, new GUIContent("Matcap Blur", "Matcap blur, blurs the Matcap, set to 1 for full clarity"), 2);
materialEditor.ShaderProperty(_MatcapTintToDiffuse, new GUIContent("Tint To Diffuse", "Tints matcap to diffuse color."), 2);
material.SetFloat("_Metallic", 0);
material.SetFloat("_ClearCoat", 0);
material.SetTexture("_MetallicGlossMap", null);
Expand Down Expand Up @@ -423,6 +429,27 @@ private void DrawRimlightSettings(MaterialEditor materialEditor)
}
}

private void DrawHalfToneSettings(MaterialEditor materialEditor)
{
showHalftones = XSStyles.ShurikenFoldout("Halftones", showHalftones);
if (showHalftones)
{
materialEditor.ShaderProperty(_HalftoneType, new GUIContent("Halftone Style", "Controls where halftone and stippling effects are drawn."));

if(_HalftoneType.floatValue == 1 || _HalftoneType.floatValue == 2)
{
materialEditor.ShaderProperty(_HalftoneDotSize, new GUIContent("Stippling Scale", "How large should the stippling pattern be?"));
materialEditor.ShaderProperty(_HalftoneDotAmount, new GUIContent("Stippling Density", "How dense is the stippling effect?"));
}

if(_HalftoneType.floatValue == 0 || _HalftoneType.floatValue == 2)
{
materialEditor.ShaderProperty(_HalftoneLineAmount, new GUIContent("Halftone Line Count", "How many lines should the halftone shadows have?"));
materialEditor.ShaderProperty(_HalftoneLineIntensity, new GUIContent("Halftone Line Intensity", "How dark should the halftone lines be?"));
}
}
}

private void DrawTransmissionSettings(MaterialEditor materialEditor)
{
showSubsurface = XSStyles.ShurikenFoldout("Transmission", showSubsurface);
Expand Down
4 changes: 3 additions & 1 deletion Main/CGIncludes/XSDefines.cginc
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ half4 _Color, _ShadowRim,
_OutlineColor, _SSColor, _OcclusionColor,
_EmissionColor, _MatcapTint, _RimColor;

half _MatcapTintToDiffuse;
half _Cutoff;
half _FadeDitherDistance;
half _EmissionToDiffuse, _ScaleWithLightSensitivity;
Expand All @@ -166,6 +167,7 @@ half _ShadowRimRange, _ShadowRimThreshold, _ShadowRimSharpness, _ShadowSharpness
half _SSDistortion, _SSPower, _SSScale;
half _OutlineWidth;

int _HalftoneType;
int _FadeDither;
int _SpecMode, _SpecularStyle, _ReflectionMode, _ReflectionBlendMode, _ClearCoat;
int _TilingMode, _VertexColorAlbedo, _ScaleWithLight;
Expand All @@ -175,7 +177,7 @@ int _UVSetAlbedo, _UVSetNormal, _UVSetDetNormal,
_UVSetThickness, _UVSetOcclusion, _UVSetReflectivity,
_UVSetEmission;

// half _HalftoneDotSize, _HalftoneDotAmount, _HalftoneLineAmount;
half _HalftoneDotSize, _HalftoneDotAmount, _HalftoneLineAmount, _HalftoneLineIntensity;

//Defines for helper functions
#define grayscaleVec float3(0.2125, 0.7154, 0.0721)
90 changes: 46 additions & 44 deletions Main/CGIncludes/XSHelperFunctions.cginc
Original file line number Diff line number Diff line change
Expand Up @@ -243,48 +243,50 @@ void calcAlpha(inout XSLighting i)
}

// //Halftone functions, finish implementing later.. Not correct right now.
// half2 rotateUV(half2 uv, half rotation)
// {
// half mid = 0.5;
// return half2(
// cos(rotation) * (uv.x - mid) + sin(rotation) * (uv.y - mid) + mid,
// cos(rotation) * (uv.y - mid) - sin(rotation) * (uv.x - mid) + mid
// );
// }

// half DotHalftone(XSLighting i, half scalar) //Scalar can be anything from attenuation to a dot product
// {
// bool inMirror = IsInMirror();
// half2 uv = i.screenUV;
// #if UNITY_SINGLE_PASS_STEREO
// uv *= 2;
// #endif

// half2 nearest = 2 * frac(100 * uv) - 1;
// half dist = length(nearest);
// half dotSize = 10 * scalar;
// half dotMask = step(dotSize, dist);

// return dotMask;
// }

// half LineHalftone(XSLighting i, half scalar)
// {
// // #if defined(DIRECTIONAL)
// // scalar = saturate(scalar + ((1-i.attenuation) * 0.2));
// // #endif
// bool inMirror = IsInMirror();
// half2 uv = i.screenUV;
// uv = rotateUV(uv, -0.785398);
// #if UNITY_SINGLE_PASS_STEREO
// _HalftoneLineAmount = _HalftoneLineAmount * 2;

// #endif
// uv.x = sin(uv.x * _HalftoneLineAmount);

// half2 steppedUV = smoothstep(0,0.2,uv.x);
// half lineMask = steppedUV * 0.2 * scalar;

// return saturate(lineMask);
// }
float2 SphereUV( float3 coords /*viewDir?*/)
{
float3 nc = normalize(coords);
float lat = acos(nc.y);
float lon = atan2(nc.z, nc.x);
float2 coord = 1.0 - (float2(lon, lat) * float2(1.0/UNITY_PI, 1.0/UNITY_PI));
return (coord + float4(0, 1-unity_StereoEyeIndex,1,1.0).xy) * float4(0, 1-unity_StereoEyeIndex,1,1.0).zw;
}

half2 rotateUV(half2 uv, half rotation)
{
half mid = 0.5;
return half2(
cos(rotation) * (uv.x - mid) + sin(rotation) * (uv.y - mid) + mid,
cos(rotation) * (uv.y - mid) - sin(rotation) * (uv.x - mid) + mid
);
}

half DotHalftone(XSLighting i, half scalar) //Scalar can be anything from attenuation to a dot product
{
bool inMirror = IsInMirror();
half2 uv = SphereUV(calcViewDir(i.worldPos));
uv.xy *= _HalftoneDotAmount;
half2 nearest = 2 * frac(100 * uv) - 1;
half dist = length(nearest);
half dotSize = 100 * _HalftoneDotSize * scalar;
half dotMask = step(dotSize, dist);

return lerp(1, 1-dotMask, smoothstep(0, 0.4, 1/distance(i.worldPos, _WorldSpaceCameraPos)));;
}

half LineHalftone(XSLighting i, half scalar)
{
// #if defined(DIRECTIONAL)
// scalar = saturate(scalar + ((1-i.attenuation) * 0.2));
// #endif
bool inMirror = IsInMirror();
half2 uv = SphereUV(calcViewDir(i.worldPos));
uv = rotateUV(uv, -0.785398);
uv.x = sin(uv.x * _HalftoneLineAmount * scalar);

half2 steppedUV = smoothstep(0,0.2,uv.x);
half lineMask = lerp(1, steppedUV, smoothstep(0, 0.4, 1/distance(i.worldPos, _WorldSpaceCameraPos)));

return saturate(lineMask);
}
//
20 changes: 19 additions & 1 deletion Main/CGIncludes/XSLighting.cginc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ half4 BRDF_XSLighting(XSLighting i)
half4 occlusion = lerp(_OcclusionColor, 1, i.occlusion.r);
half4 outlineColor = calcOutlineColor(i, d, indirectDiffuse, lightCol);

half lineHalftone = 0;
half stippling = 0;
bool usingLineHalftone = 0;
if(_HalftoneType == 0 || _HalftoneType == 2)
{
lineHalftone = lerp(1, LineHalftone(i, 1), 1-saturate(dot(shadowRim * ramp, grayscaleVec)));
usingLineHalftone = 1;
}

if(_HalftoneType == 1 || _HalftoneType == 2)
{
stippling = DotHalftone(i, saturate(dot(directSpecular + rimLight + directSpecular, grayscaleVec))) * saturate(dot(shadowRim * ramp, grayscaleVec));
directSpecular *= stippling;
indirectSpecular *= lerp(0.5, 1, stippling); // Don't want these to go completely black, looks weird
rimLight *= stippling;
}

half4 col;
col = diffuse * shadowRim;
calcReflectionBlending(i, col, indirectSpecular.xyzz);
Expand All @@ -59,6 +76,7 @@ half4 BRDF_XSLighting(XSLighting i)
calcClearcoat(col, i, d, untouchedNormal, indirectDiffuse, lightCol, viewDir, lightDir, ramp);
col += calcEmission(i, lightAvg);

float4 finalColor = lerp(col, outlineColor, i.isOutline);
float4 finalColor = lerp(col, outlineColor, i.isOutline) * lerp(1, lineHalftone, _HalftoneLineIntensity * usingLineHalftone);
//finalColor = lerp(finalColor, stippling, 0.9999);
return finalColor;
}
12 changes: 7 additions & 5 deletions Main/CGIncludes/XSLightingFunctions.cginc
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ half3 getVertexLightsDir(XSLighting i, half4 vertexLightAtten)
half3 dirZ = toLightZ - i.worldPos;
half3 dirW = toLightW - i.worldPos;

dirX *= length(toLightX) * vertexLightAtten.x;
dirY *= length(toLightY) * vertexLightAtten.y;
dirZ *= length(toLightZ) * vertexLightAtten.z;
dirW *= length(toLightW) * vertexLightAtten.w;
dirX *= length(toLightX) * vertexLightAtten.x * unity_LightColor[0];
dirY *= length(toLightY) * vertexLightAtten.y * unity_LightColor[1];
dirZ *= length(toLightZ) * vertexLightAtten.z * unity_LightColor[2];
dirW *= length(toLightW) * vertexLightAtten.w * unity_LightColor[3];

half3 dir = (dirX + dirY + dirZ + dirW) / 4;
return dir;
Expand Down Expand Up @@ -263,6 +263,8 @@ half3 calcIndirectSpecular(XSLighting i, DotProducts d, half4 metallicSmoothness
{
spec *= (indirectLight + (_LightColor0 * i.attenuation) * 0.5);
}

spec *= lerp(1, i.diffuseColor, _MatcapTintToDiffuse);
}
spec = lerp(spec, spec * ramp, metallicSmoothness.w); // should only not see shadows on a perfect mirror.
return spec;
Expand Down Expand Up @@ -296,7 +298,7 @@ half4 calcRamp(XSLighting i, DotProducts d)

half3 calcIndirectDiffuse(XSLighting i)
{// We don't care about anything other than the color from probes for toon lighting.
half3 indirectDiffuse = ShadeSH9(float4(0,1,0,1));//half3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w);
half3 indirectDiffuse = ShadeSH9(float4(0,0.5,0,1));//half3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w);
return indirectDiffuse;
}

Expand Down
9 changes: 6 additions & 3 deletions Main/Shaders/XSToon2.0 Cutout.shader
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
_BakedCubemap("Baked Cubemap", CUBE) = "black" {}
_Matcap("Matcap", 2D) = "black" {}
[HDR]_MatcapTint("Matcap Tint", Color) = (1,1,1,1)
_MatcapTintToDiffuse("Matcap Tint To Diffuse", Range(0,1)) = 0
_ReflectivityMask("Reflection Mask" , 2D) = "white" {}
_Metallic("Metallic", Range(0,1)) = 0
_Glossiness("Smoothness", Range(0,1)) = 0
Expand Down Expand Up @@ -79,9 +80,11 @@
_SSPower("Subsurface Power", Range(0,3)) = 1
_SSScale("Subsurface Scale", Range(0,3)) = 1

_HalftoneDotSize("Halftone Dot Size", Float) = 1.7
_HalftoneDotAmount("Halftone Dot Amount", Float) = 50
_HalftoneLineAmount("Halftone Line Amount", Float) = 150
[Enum(Shadows, 0, Highlights, 1, Shadows And Highlights, 2, Off, 3)] _HalftoneType("Halftones Type", Int) = 3
_HalftoneDotSize("Halftone Dot Size", Float) = 0.5
_HalftoneDotAmount("Halftone Dot Amount", Float) = 5
_HalftoneLineAmount("Halftone Line Amount", Float) = 2000
_HalftoneLineIntensity("Halftone Line Intensity", Range(0,1)) = 1

[Enum(UV1,0,UV2,1)] _UVSetAlbedo("Albedo UVs", Int) = 0
[Enum(UV1,0,UV2,1)] _UVSetNormal("Normal Map UVs", Int) = 0
Expand Down
9 changes: 6 additions & 3 deletions Main/Shaders/XSToon2.0 CutoutA2C Outlined.shader
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
_BakedCubemap("Baked Cubemap", CUBE) = "black" {}
_Matcap("Matcap", 2D) = "black" {}
[HDR]_MatcapTint("Matcap Tint", Color) = (1,1,1,1)
_MatcapTintToDiffuse("Matcap Tint To Diffuse", Range(0,1)) = 0
_ReflectivityMask("Reflection Mask" , 2D) = "white" {}
_Metallic("Metallic", Range(0,1)) = 0
_Glossiness("Smoothness", Range(0,1)) = 0
Expand Down Expand Up @@ -79,9 +80,11 @@
_SSPower("Subsurface Power", Range(0,3)) = 1
_SSScale("Subsurface Scale", Range(0,3)) = 1

_HalftoneDotSize("Halftone Dot Size", Float) = 1.7
_HalftoneDotAmount("Halftone Dot Amount", Float) = 50
_HalftoneLineAmount("Halftone Line Amount", Float) = 150
[Enum(Shadows, 0, Highlights, 1, Shadows And Highlights, 2, Off, 3)] _HalftoneType("Halftones Type", Int) = 3
_HalftoneDotSize("Halftone Dot Size", Float) = 0.5
_HalftoneDotAmount("Halftone Dot Amount", Float) = 5
_HalftoneLineAmount("Halftone Line Amount", Float) = 2000
_HalftoneLineIntensity("Halftone Line Intensity", Range(0,1)) = 1

[Enum(UV1,0,UV2,1)] _UVSetAlbedo("Albedo UVs", Int) = 0
[Enum(UV1,0,UV2,1)] _UVSetNormal("Normal Map UVs", Int) = 0
Expand Down
9 changes: 6 additions & 3 deletions Main/Shaders/XSToon2.0 CutoutA2C.shader
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
_BakedCubemap("Baked Cubemap", CUBE) = "black" {}
_Matcap("Matcap", 2D) = "black" {}
[HDR]_MatcapTint("Matcap Tint", Color) = (1,1,1,1)
_MatcapTintToDiffuse("Matcap Tint To Diffuse", Range(0,1)) = 0
_ReflectivityMask("Reflection Mask" , 2D) = "white" {}
_Metallic("Metallic", Range(0,1)) = 0
_Glossiness("Smoothness", Range(0,1)) = 0
Expand Down Expand Up @@ -79,9 +80,11 @@
_SSPower("Subsurface Power", Range(0,3)) = 1
_SSScale("Subsurface Scale", Range(0,3)) = 1

_HalftoneDotSize("Halftone Dot Size", Float) = 1.7
_HalftoneDotAmount("Halftone Dot Amount", Float) = 50
_HalftoneLineAmount("Halftone Line Amount", Float) = 150
[Enum(Shadows, 0, Highlights, 1, Shadows And Highlights, 2, Off, 3)] _HalftoneType("Halftones Type", Int) = 3
_HalftoneDotSize("Halftone Dot Size", Float) = 0.5
_HalftoneDotAmount("Halftone Dot Amount", Float) = 5
_HalftoneLineAmount("Halftone Line Amount", Float) = 2000
_HalftoneLineIntensity("Halftone Line Intensity", Range(0,1)) = 1

[Enum(UV1,0,UV2,1)] _UVSetAlbedo("Albedo UVs", Int) = 0
[Enum(UV1,0,UV2,1)] _UVSetNormal("Normal Map UVs", Int) = 0
Expand Down
9 changes: 6 additions & 3 deletions Main/Shaders/XSToon2.0 CutoutA2CMasked.shader
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
_BakedCubemap("Baked Cubemap", CUBE) = "black" {}
_Matcap("Matcap", 2D) = "black" {}
[HDR]_MatcapTint("Matcap Tint", Color) = (1,1,1,1)
_MatcapTintToDiffuse("Matcap Tint To Diffuse", Range(0,1)) = 0
_ReflectivityMask("Reflection Mask" , 2D) = "white" {}
_Metallic("Metallic", Range(0,1)) = 0
_Glossiness("Smoothness", Range(0,1)) = 0
Expand Down Expand Up @@ -80,9 +81,11 @@
_SSPower("Subsurface Power", Range(0,3)) = 1
_SSScale("Subsurface Scale", Range(0,3)) = 1

_HalftoneDotSize("Halftone Dot Size", Float) = 1.7
_HalftoneDotAmount("Halftone Dot Amount", Float) = 50
_HalftoneLineAmount("Halftone Line Amount", Float) = 150
[Enum(Shadows, 0, Highlights, 1, Shadows And Highlights, 2, Off, 3)] _HalftoneType("Halftones Type", Int) = 3
_HalftoneDotSize("Halftone Dot Size", Float) = 0.5
_HalftoneDotAmount("Halftone Dot Amount", Float) = 5
_HalftoneLineAmount("Halftone Line Amount", Float) = 2000
_HalftoneLineIntensity("Halftone Line Intensity", Range(0,1)) = 1

[Enum(UV1,0,UV2,1)] _UVSetAlbedo("Albedo UVs", Int) = 0
[Enum(UV1,0,UV2,1)] _UVSetNormal("Normal Map UVs", Int) = 0
Expand Down
9 changes: 6 additions & 3 deletions Main/Shaders/XSToon2.0 Dithered Outlined.shader
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
_BakedCubemap("Baked Cubemap", CUBE) = "black" {}
_Matcap("Matcap", 2D) = "black" {}
[HDR]_MatcapTint("Matcap Tint", Color) = (1,1,1,1)
_MatcapTintToDiffuse("Matcap Tint To Diffuse", Range(0,1)) = 0
_ReflectivityMask("Reflection Mask" , 2D) = "white" {}
_Metallic("Metallic", Range(0,1)) = 0
_Glossiness("Smoothness", Range(0,1)) = 0
Expand Down Expand Up @@ -83,9 +84,11 @@
_SSPower("Subsurface Power", Range(0,3)) = 1
_SSScale("Subsurface Scale", Range(0,3)) = 1

_HalftoneDotSize("Halftone Dot Size", Float) = 1.7
_HalftoneDotAmount("Halftone Dot Amount", Float) = 50
_HalftoneLineAmount("Halftone Line Amount", Float) = 150
[Enum(Shadows, 0, Highlights, 1, Shadows And Highlights, 2, Off, 3)] _HalftoneType("Halftones Type", Int) = 3
_HalftoneDotSize("Halftone Dot Size", Float) = 0.5
_HalftoneDotAmount("Halftone Dot Amount", Float) = 5
_HalftoneLineAmount("Halftone Line Amount", Float) = 2000
_HalftoneLineIntensity("Halftone Line Intensity", Range(0,1)) = 1

[Enum(UV1,0,UV2,1)] _UVSetAlbedo("Albedo UVs", Int) = 0
[Enum(UV1,0,UV2,1)] _UVSetNormal("Normal Map UVs", Int) = 0
Expand Down
Loading

0 comments on commit 60e4231

Please sign in to comment.