From 71b217c624b09d68735f34c1b9f31b3f5b37e2e1 Mon Sep 17 00:00:00 2001 From: Ghostkeeper Date: Sat, 3 Apr 2021 17:29:33 +0200 Subject: [PATCH] Fix colour calculations if spectrum is entirely one value If the range of the colour spectrum is 0, i.e. there is only one value for the current colour spectrum, then this would previously give a division by 0 in the shader, causing the final colour to become black. This is unexpected and makes the layer view hard to read. Instead, we'll now use the middle of the range then. This was likely a problem for a long time but only really became visible due to the colour spectrum now showing only the range of values for the visible structures. Previously it was a problem e.g. for layer thickness if all layers had the same thickness (i.e. initial layer height == layer height). --- plugins/SimulationView/layers3d.shader | 30 +++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/plugins/SimulationView/layers3d.shader b/plugins/SimulationView/layers3d.shader index 6a2ff63b8e..1b4c3be4cf 100644 --- a/plugins/SimulationView/layers3d.shader +++ b/plugins/SimulationView/layers3d.shader @@ -44,7 +44,15 @@ vertex41core = vec4 feedrateGradientColor(float abs_value, float min_value, float max_value) { - float value = (abs_value - min_value)/(max_value - min_value); + float value; + if(abs(max_value - min_value) < 0.0001) //Max and min are equal (barring floating point rounding errors). + { + value = 0.5; //Pick a colour in exactly the middle of the range. + } + else + { + value = (abs_value - min_value) / (max_value - min_value); + } float red = value; float green = 1-abs(1-4*value); if (value > 0.375) @@ -57,7 +65,15 @@ vertex41core = vec4 layerThicknessGradientColor(float abs_value, float min_value, float max_value) { - float value = (abs_value - min_value)/(max_value - min_value); + float value; + if(abs(max_value - min_value) < 0.0001) //Max and min are equal (barring floating point rounding errors). + { + value = 0.5; //Pick a colour in exactly the middle of the range. + } + else + { + value = (abs_value - min_value) / (max_value - min_value); + } float red = min(max(4*value-2, 0), 1); float green = min(1.5*value, 0.75); if (value > 0.75) @@ -70,7 +86,15 @@ vertex41core = vec4 lineWidthGradientColor(float abs_value, float min_value, float max_value) { - float value = (abs_value - min_value) / (max_value - min_value); + float value; + if(abs(max_value - min_value) < 0.0001) //Max and min are equal (barring floating point rounding errors). + { + value = 0.5; //Pick a colour in exactly the middle of the range. + } + else + { + value = (abs_value - min_value) / (max_value - min_value); + } float red = value; float green = 1 - abs(1 - 4 * value); if(value > 0.375)