From 5b61737a8f41688699fd1d711a25b7cea86d1530 Mon Sep 17 00:00:00 2001 From: Charlie Jolly Date: Mon, 13 Dec 2021 21:20:07 +0000 Subject: 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 --- .../material/gpu_shader_material_map_range.glsl | 147 ++++++++++++++++++++- 1 file changed, 143 insertions(+), 4 deletions(-) (limited to 'source/blender/gpu') 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) : -- cgit v1.2.3