summaryrefslogtreecommitdiffstats
path: root/math.cginc
diff options
context:
space:
mode:
Diffstat (limited to 'math.cginc')
-rw-r--r--math.cginc26
1 files changed, 26 insertions, 0 deletions
diff --git a/math.cginc b/math.cginc
new file mode 100644
index 0000000..4b91209
--- /dev/null
+++ b/math.cginc
@@ -0,0 +1,26 @@
+#ifndef __MATH_INC
+#define __MATH_INC
+
+#define PI 3.14159265358979f
+#define RCP_PI (1.0f / PI)
+
+// Wrap a dot product. Assume it's already clamped.
+// At k=0, you get standard lambertian shading.
+// At k=0.5, you get half-lambertian shading.
+// At k=1.0, you get flat shading.
+// k must be on [0, 1].
+// Energy preserving, within some small bound.
+float wrapDotProduct(float XoY, float k) {
+ float lambertian = XoY;
+ float half_lambertian = pow(max(1e-4, (XoY + 0.5f) / (1.0f + 0.5f)), 2);
+ float flat = RCP_PI;
+
+ if (k < 0.5) {
+ return lerp(lambertian, half_lambertian, k * 2.0f);
+ } else {
+ return lerp(half_lambertian, flat, k * 2.0f - 1.0f);
+ }
+}
+
+#endif // __MATH_INC
+