ShaderMaths.cginc 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // The following comment prevents Unity from auto upgrading the shader. Please keep it to keep backward compatibility.
  2. // UNITY_SHADER_NO_UPGRADE
  3. #ifndef _VLB_SHADER_MATHS_INCLUDED_
  4. #define _VLB_SHADER_MATHS_INCLUDED_
  5. inline float dot2(float2 v) { return dot(v,v); }
  6. inline float lerpClamped(float a, float b, float t) { return lerp(a, b, saturate(t)); }
  7. inline float invLerp(float a, float b, float t) { return (t - a) / (b - a); }
  8. inline float invLerpClamped(float a, float b, float t) { return saturate(invLerp(a, b, t)); }
  9. inline float fromABtoCD_Clamped(float valueAB, float A, float B, float C, float D) { return lerpClamped(C, D, invLerpClamped(A, B, valueAB)); }
  10. // Returns 1.0 if a >= b, 0.0 otherwise
  11. inline float isEqualOrGreater(float a, float b) { return step(b, a); }
  12. // Get signed distance of pos from the plane (normal ; d).
  13. // Normal should be normalized.
  14. // If we want to disable this feature, we could set normal and d to 0 (no discard in this case).
  15. inline float DistanceToPlane(float3 pos, float3 normal, float d) { return dot(normal, pos) + d; }
  16. // https://www.iquilezles.org/www/articles/functions/functions.htm
  17. // A nice choice to remap the 0..1 interval into 0..1, such that the corners are mapped to 0 and the center to 1.
  18. // In other words, parabola(0) = parabola(1) = 0, and parabola(1/2) = 1.
  19. inline float parabola( float x, float k )
  20. {
  21. return pow( 4.0*x*(1.0-x), k );
  22. }
  23. // https://en.wikipedia.org/wiki/Smoothstep
  24. inline float smootherstep(float edge0, float edge1, float x)
  25. {
  26. // Scale, and clamp x to 0..1 range
  27. x = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
  28. // Evaluate polynomial
  29. return x*x*x*(x*(x * 6 - 15) + 10);
  30. }
  31. #endif