summaryrefslogtreecommitdiffstats
path: root/math.cginc
diff options
context:
space:
mode:
authoryum <yum.food.vr@gmail.com>2025-08-06 16:42:42 -0700
committeryum <yum.food.vr@gmail.com>2025-08-06 16:42:42 -0700
commit99d161288bfe2d10c331c97e6b7571f9c884e912 (patch)
tree6ef130c4801de52f697c8d6996d9c4b0fb5f3964 /math.cginc
initial commit
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
+