Skip to content

Commit

Permalink
safe_mix
Browse files Browse the repository at this point in the history
  • Loading branch information
pragma37 committed Feb 26, 2023
1 parent dc54394 commit ff407cb
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 8 deletions.
4 changes: 3 additions & 1 deletion Malt/Shaders/Common/Math.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@

#define saturate(value) clamp((value), 0, 1)

#define map_range(value, from_min, from_max, to_min, to_max) (mix((to_min), (to_max), ((value) - (from_min)) / ((from_max) - (from_min))))
#define safe_mix(a, b, f) (f == 0 ? a : (f == 1 ? b : mix(a, b, f)))

#define map_range(value, from_min, from_max, to_min, to_max) (safe_mix((to_min), (to_max), ((value) - (from_min)) / ((from_max) - (from_min))))
#define map_range_clamped(value, from_min, from_max, to_min, to_max) clamp(map_range(value, from_min, from_max, to_min, to_max), min(to_min, to_max), max(to_max, to_min))

#define snap(value, range) (round((value) / (range)) * (range))
Expand Down
2 changes: 1 addition & 1 deletion Malt/Shaders/Node Utils 2/Float.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ float Float_max(float a, float b){ return max(a,b); }
/*META @meta: label=Smooth Minimum; @s: min=0.0; */

/*META @meta: label=Mix;*/
float Float_mix(float a, float b, float fac){ return mix(a,b,fac); }
float Float_mix(float a, float b, float fac){ return safe_mix(a,b,fac); }

/*META @meta: label=Sine;*/
float Float_sin(float a) { return sin(a); }
Expand Down
4 changes: 2 additions & 2 deletions Malt/Shaders/Node Utils 2/Vec2.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ vec2 Vec2_min(vec2 a, vec2 b){ return min(a,b); }
vec2 Vec2_max(vec2 a, vec2 b){ return max(a,b); }

/* META @meta: label=Mix 2D; @c: label=Factor; */
vec2 Vec2_mix(vec2 a, vec2 b, vec2 c){ return mix(a,b,c); }
vec2 Vec2_mix(vec2 a, vec2 b, vec2 c){ return safe_mix(a,b,c); }
/* META @meta: label=Mix; */
vec2 Vec2_mix_float(vec2 a, vec2 b, float fac){ return mix(a,b,fac); }
vec2 Vec2_mix_float(vec2 a, vec2 b, float fac){ return safe_mix(a,b,fac); }

/* META @meta: label=Normalize; */
vec2 Vec2_normalize(vec2 a){ return a != vec2(0) ? normalize(a) : vec2(0); }
Expand Down
4 changes: 2 additions & 2 deletions Malt/Shaders/Node Utils 2/Vec3.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ vec3 Vec3_min(vec3 a, vec3 b){ return min(a,b); }
vec3 Vec3_max(vec3 a, vec3 b){ return max(a,b); }

/*META @meta: label=Mix 3D; @a: subtype=Vector; @b: subtype=Vector; @c: label=Factor; subtype=Vector;*/
vec3 Vec3_mix(vec3 a, vec3 b, vec3 c){ return mix(a,b,c); }
vec3 Vec3_mix(vec3 a, vec3 b, vec3 c){ return safe_mix(a,b,c); }
/*META @meta: label=Mix; @a: subtype=Vector; @b: subtype=Vector;*/
vec3 Vec3_mix_float(vec3 a, vec3 b, float fac){ return mix(a,b,fac); }
vec3 Vec3_mix_float(vec3 a, vec3 b, float fac){ return safe_mix(a,b,fac); }

/*META @meta: label=Normalize; @a: subtype=Vector;*/
vec3 Vec3_normalize(vec3 a){ return a != 0 ? normalize(a) : vec3(0); }
Expand Down
4 changes: 2 additions & 2 deletions Malt/Shaders/Node Utils 2/Vec4.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ vec4 Vec4_min(vec4 a, vec4 b){ return min(a,b); }
vec4 Vec4_max(vec4 a, vec4 b){ return max(a,b); }

/*META @meta: label=Mix 4D; @a: subtype=Vector; @b: subtype=Vector; @c: label=Factor; subtype=Vector;*/
vec4 Vec4_mix(vec4 a, vec4 b, vec4 c){ return mix(a,b,c); }
vec4 Vec4_mix(vec4 a, vec4 b, vec4 c){ return safe_mix(a,b,c); }
/*META @meta: label=Mix; @a: subtype=Vector; @b: subtype=Vector;*/
vec4 Vec4_mix_float(vec4 a, vec4 b, float fac){ return mix(a,b,fac); }
vec4 Vec4_mix_float(vec4 a, vec4 b, float fac){ return safe_mix(a,b,fac); }

/*META @meta: label=Normalize; @a: subtype=Vector;*/
vec4 Vec4_normalize(vec4 a){ return a != vec4(0) ? normalize(a) : vec4(0); }
Expand Down

0 comments on commit ff407cb

Please sign in to comment.