diff options
author | Charlie Jolly <charlie> | 2021-12-14 00:20:07 +0300 |
---|---|---|
committer | Charlie Jolly <mistajolly@gmail.com> | 2021-12-14 21:27:01 +0300 |
commit | 5b61737a8f41688699fd1d711a25b7cea86d1530 (patch) | |
tree | fcea899c2663e666a6b2e9f15b31dfe2064c7a42 /source/blender/gpu | |
parent | 44232a2ce6d8762f10366f6bb660a37ce3e1acbd (diff) |
Nodes: Add vector support to Map Range node
This replaces lost functionality from the old GN Attribute Map Range node.
This also adds vector support to the shader version of the node.
Notes:
This breaks forward compatibility as this node now uses data storage.
Reviewed By: HooglyBoogly, brecht
Differential Revision: https://developer.blender.org/D12760
Diffstat (limited to 'source/blender/gpu')
-rw-r--r-- | source/blender/gpu/shaders/material/gpu_shader_material_map_range.glsl | 147 |
1 files changed, 143 insertions, 4 deletions
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 7853aae31a1..1def3abec26 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 @@ -4,13 +4,128 @@ float smootherstep(float edge0, float edge1, float x) return x * x * x * (x * (x * 6.0 - 15.0) + 10.0); } +vec3 smootherstep(vec3 edge0, vec3 edge1, vec3 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 vector_map_range_linear(float value, + float fromMin, + float fromMax, + float toMin, + float toMax, + float steps, + vec3 v_value, + vec3 v_from_min, + vec3 v_from_max, + vec3 v_to_min, + vec3 v_to_max, + vec3 v_steps, + float use_clamp, + out float result, + out vec3 v_result) +{ + vec3 factor = safe_divide((v_value - v_from_min), (v_from_max - v_from_min)); + v_result = v_to_min + factor * (v_to_max - v_to_min); + if (use_clamp > 0.0) { + v_result.x = (v_to_min.x > v_to_max.x) ? clamp(v_result.x, v_to_max.x, v_to_min.x) : + clamp(v_result.x, v_to_min.x, v_to_max.x); + v_result.y = (v_to_min.y > v_to_max.y) ? clamp(v_result.y, v_to_max.y, v_to_min.y) : + clamp(v_result.y, v_to_min.y, v_to_max.y); + v_result.z = (v_to_min.z > v_to_max.z) ? clamp(v_result.z, v_to_max.z, v_to_min.z) : + clamp(v_result.z, v_to_min.z, v_to_max.z); + } +} + +void vector_map_range_stepped(float value, + float fromMin, + float fromMax, + float toMin, + float toMax, + float steps, + vec3 v_value, + vec3 v_from_min, + vec3 v_from_max, + vec3 v_to_min, + vec3 v_to_max, + vec3 v_steps, + float use_clamp, + out float result, + out vec3 v_result) +{ + vec3 factor = safe_divide((v_value - v_from_min), (v_from_max - v_from_min)); + factor = safe_divide(floor(factor * (v_steps + 1.0)), v_steps); + v_result = v_to_min + factor * (v_to_max - v_to_min); + if (use_clamp > 0.0) { + v_result.x = (v_to_min.x > v_to_max.x) ? clamp(v_result.x, v_to_max.x, v_to_min.x) : + clamp(v_result.x, v_to_min.x, v_to_max.x); + v_result.y = (v_to_min.y > v_to_max.y) ? clamp(v_result.y, v_to_max.y, v_to_min.y) : + clamp(v_result.y, v_to_min.y, v_to_max.y); + v_result.z = (v_to_min.z > v_to_max.z) ? clamp(v_result.z, v_to_max.z, v_to_min.z) : + clamp(v_result.z, v_to_min.z, v_to_max.z); + } +} + +void vector_map_range_smoothstep(float value, + float fromMin, + float fromMax, + float toMin, + float toMax, + float steps, + vec3 v_value, + vec3 v_from_min, + vec3 v_from_max, + vec3 v_to_min, + vec3 v_to_max, + vec3 v_steps, + float use_clamp, + out float result, + out vec3 v_result) +{ + vec3 factor = safe_divide((v_value - v_from_min), (v_from_max - v_from_min)); + factor = clamp(factor, 0.0, 1.0); + factor = (3.0 - 2.0 * factor) * (factor * factor); + v_result = v_to_min + factor * (v_to_max - v_to_min); +} + +void vector_map_range_smootherstep(float value, + float fromMin, + float fromMax, + float toMin, + float toMax, + float steps, + vec3 v_value, + vec3 v_from_min, + vec3 v_from_max, + vec3 v_to_min, + vec3 v_to_max, + vec3 v_steps, + float use_clamp, + out float result, + out vec3 v_result) +{ + vec3 factor = safe_divide((v_value - v_from_min), (v_from_max - v_from_min)); + factor = clamp(factor, 0.0, 1.0); + factor = factor * factor * factor * (factor * (factor * 6.0 - 15.0) + 10.0); + v_result = v_to_min + factor * (v_to_max - v_to_min); +} + void map_range_linear(float value, float fromMin, float fromMax, float toMin, float toMax, float steps, - out float result) + vec3 v_value, + vec3 v_from_min, + vec3 v_from_max, + vec3 v_to_min, + vec3 v_to_max, + vec3 v_steps, + float use_clamp, + out float result, + out vec3 v_result) { if (fromMax != fromMin) { result = toMin + ((value - fromMin) / (fromMax - fromMin)) * (toMax - toMin); @@ -26,7 +141,15 @@ void map_range_stepped(float value, float toMin, float toMax, float steps, - out float result) + vec3 v_value, + vec3 v_from_min, + vec3 v_from_max, + vec3 v_to_min, + vec3 v_to_max, + vec3 v_steps, + float use_clamp, + out float result, + out vec3 v_result) { if (fromMax != fromMin) { float factor = (value - fromMin) / (fromMax - fromMin); @@ -44,7 +167,15 @@ void map_range_smoothstep(float value, float toMin, float toMax, float steps, - out float result) + vec3 v_value, + vec3 v_from_min, + vec3 v_from_max, + vec3 v_to_min, + vec3 v_to_max, + vec3 v_steps, + float use_clamp, + out float result, + out vec3 v_result) { if (fromMax != fromMin) { float factor = (fromMin > fromMax) ? 1.0 - smoothstep(fromMax, fromMin, value) : @@ -62,7 +193,15 @@ void map_range_smootherstep(float value, float toMin, float toMax, float steps, - out float result) + vec3 v_value, + vec3 v_from_min, + vec3 v_from_max, + vec3 v_to_min, + vec3 v_to_max, + vec3 v_steps, + float use_clamp, + out float result, + out vec3 v_result) { if (fromMax != fromMin) { float factor = (fromMin > fromMax) ? 1.0 - smootherstep(fromMax, fromMin, value) : |