Last active
December 22, 2019 04:40
-
-
Save Kittoes0124/abbe60262ee7ede28ea5f963b98e16ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class ShaderHelpers | |
{ | |
private static readonly Vector3 m_normal_xxx = new Vector3(1f, 1f, 1f); | |
private static readonly Vector3 m_normal_xyy = new Vector3(1f, -1f, -1f); | |
private static readonly Vector3 m_normal_yxy = new Vector3(-1f, 1f, -1f); | |
private static readonly Vector3 m_normal_yyx = new Vector3(-1f, -1f, 1f); | |
public static float Clamp(float value, float x, float y) { | |
if (x > y) { | |
var z = x; | |
x = y; | |
y = z; | |
} | |
if (value < x) { | |
return x; | |
} | |
else if (value > y) { | |
return y; | |
} | |
else { | |
return value; | |
} | |
} | |
/// <remarks> | |
/// http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm | |
/// </remarks> | |
public static Vector3 GetNormal(in Vector3 position, Func<Vector3, float> signedDistanceFunction, float epsilon = 0.0001f) { | |
var xxx = m_normal_xxx; | |
var xyy = m_normal_xyy; | |
var yxy = m_normal_yxy; | |
var yyx = m_normal_yyx; | |
return Vector3.Normalize( | |
(xxx * signedDistanceFunction(position + (xxx * epsilon))) | |
+ (xyy * signedDistanceFunction(position + (xyy * epsilon))) | |
+ (yxy * signedDistanceFunction(position + (yxy * epsilon))) | |
+ (yyx * signedDistanceFunction(position + (yyx * epsilon))) | |
); | |
} | |
/// <remarks> | |
/// https://thebookofshaders.com/glossary/?search=mix | |
/// </remarks> | |
public static float Mix(float x, float y, float i) => ((x * (1.0f - i)) + (y * i)); | |
/// <remarks> | |
/// https://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm | |
/// </remarks> | |
public static float SmoothIntersection(float x, float y, float i) { | |
var h = Clamp((0.5f - ((0.5f * (y - x)) / i)), 0.0f, 1.0f); | |
return (Mix(x, y, h) + ((i * h) * (1.0f - h))); | |
} | |
/// <remarks> | |
/// https://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm | |
/// </remarks> | |
public static float SmoothSubtraction(float x, float y, float i) { | |
var h = Clamp((0.5f - ((0.5f * (y + x)) / i)), 0.0f, 1.0f); | |
return (Mix(-x, y, h) + ((i * h) * (1.0f - h))); | |
} | |
/// <remarks> | |
/// https://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm | |
/// </remarks> | |
public static float SmoothUnion(float x, float y, float i) { | |
var h = Clamp((0.5f + ((0.5f * (y - x)) / i)), 0.0f, 1.0f); | |
return (Mix(x, y, h) - ((i * h) * (1.0f - h))); | |
} | |
} | |
static void MakeCheckerboard(int size = 20, bool prettify = true, ConsoleColor primaryColor = ConsoleColor.Black, ConsoleColor secondaryColor = ConsoleColor.White) { | |
var originalConsoleBackgroundColor = Console.BackgroundColor; | |
var originalConsoleForegroundColor = Console.ForegroundColor; | |
Console.BackgroundColor = secondaryColor; | |
Console.ForegroundColor = primaryColor; | |
for (var x = 0; (x < size); ++x) | |
for (var y = 0; (y < (size >> Convert.ToInt32(prettify))); ++y) { | |
Console.SetCursorPosition(x, y); | |
Console.Write((!IsOdd(x) ^ IsOdd(y)) ? '█' : ' '); | |
} | |
Console.BackgroundColor = originalConsoleBackgroundColor; | |
Console.ForegroundColor = originalConsoleForegroundColor; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment