diff options
author | Charlie Jolly <charlie> | 2019-12-07 15:35:07 +0300 |
---|---|---|
committer | Charlie Jolly <mistajolly@gmail.com> | 2019-12-07 15:52:42 +0300 |
commit | 958d0d4236b1cfa600a7f36f6bdc51fdd0d98a97 (patch) | |
tree | 567a1e3209e6302dd784c054b858a8b5fb92bc9f /source/blender/gpu/shaders/material | |
parent | 0406eb110332a863811d7fb6228f9a7ca514e022 (diff) |
Shader Nodes: Add Interpolation modes to Map Range node
Modes: Linear interpolation (default), stepped linear, smoothstep and smootherstep.
This also includes an additional option for the **Clamp node** to switch between **Min Max** (default) and **Range** mode.
This was needed to allow clamping when **To Max** is less than **To Min**.
Reviewed By: JacquesLucke, brecht
Differential Revision: https://developer.blender.org/D5827
Diffstat (limited to 'source/blender/gpu/shaders/material')
-rw-r--r-- | source/blender/gpu/shaders/material/gpu_shader_material_clamp.glsl | 5 | ||||
-rw-r--r-- | source/blender/gpu/shaders/material/gpu_shader_material_map_range.glsl | 69 |
2 files changed, 72 insertions, 2 deletions
diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_clamp.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_clamp.glsl index b8842064b6f..b196aed690f 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_clamp.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_clamp.glsl @@ -2,3 +2,8 @@ void clamp_value(float value, float min, float max, out float result) { result = clamp(value, min, max); } + +void clamp_range(float value, float min, float max, out float result) +{ + result = (max > min) ? clamp(value, min, max) : clamp(value, max, min); +} diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_map_range.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_map_range.glsl index a185774f4b3..7853aae31a1 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_map_range.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_map_range.glsl @@ -1,5 +1,16 @@ -void map_range( - float value, float fromMin, float fromMax, float toMin, float toMax, out float result) +float smootherstep(float edge0, float edge1, float x) +{ + x = clamp(safe_divide((x - edge0), (edge1 - edge0)), 0.0, 1.0); + return x * x * x * (x * (x * 6.0 - 15.0) + 10.0); +} + +void map_range_linear(float value, + float fromMin, + float fromMax, + float toMin, + float toMax, + float steps, + out float result) { if (fromMax != fromMin) { result = toMin + ((value - fromMin) / (fromMax - fromMin)) * (toMax - toMin); @@ -8,3 +19,57 @@ void map_range( result = 0.0; } } + +void map_range_stepped(float value, + float fromMin, + float fromMax, + float toMin, + float toMax, + float steps, + out float result) +{ + if (fromMax != fromMin) { + float factor = (value - fromMin) / (fromMax - fromMin); + factor = (steps > 0.0) ? floor(factor * (steps + 1.0)) / steps : 0.0; + result = toMin + factor * (toMax - toMin); + } + else { + result = 0.0; + } +} + +void map_range_smoothstep(float value, + float fromMin, + float fromMax, + float toMin, + float toMax, + float steps, + out float result) +{ + if (fromMax != fromMin) { + float factor = (fromMin > fromMax) ? 1.0 - smoothstep(fromMax, fromMin, value) : + smoothstep(fromMin, fromMax, value); + result = toMin + factor * (toMax - toMin); + } + else { + result = 0.0; + } +} + +void map_range_smootherstep(float value, + float fromMin, + float fromMax, + float toMin, + float toMax, + float steps, + out float result) +{ + if (fromMax != fromMin) { + float factor = (fromMin > fromMax) ? 1.0 - smootherstep(fromMax, fromMin, value) : + smootherstep(fromMin, fromMax, value); + result = toMin + factor * (toMax - toMin); + } + else { + result = 0.0; + } +} |