Просмотр исходного кода

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).
Ghostkeeper 3 лет назад
Родитель
Сommit
71b217c624
1 измененных файлов с 27 добавлено и 3 удалено
  1. 27 3
      plugins/SimulationView/layers3d.shader

+ 27 - 3
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)