Skip to content

Commit 830a9a0

Browse files
authored
Handle UI display color space in MaterialX Viewer (#2394)
This PR fixes discrepancies between the rendered preview material in MaterialXView and the NanoGUI color picker. In both the `Color3` and `Color4` cases, it completes the following conversions: 1. Uses `Color3::linearToSrgb()` to convert the stored value in "lin_rec709" color space to sRGB space for use in the color picker. 2. After user selects a color in the GUI and presses the "Pick" button, converts the selected RGB values to linear space using the `Color3::srgbToLinear()` conversion function. The converted linear color value is what is ultimately written to the material. In the `Color4` case, the alpha channel is left untouched, both during reading and writing.
1 parent ec0955e commit 830a9a0

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

source/MaterialXView/Editor.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,11 @@ void PropertyEditor::addItemToForm(const mx::UIPropertyItem& item, const std::st
327327
}
328328
else
329329
{
330+
// Transform stored linear space color3 value to sRGBA for nanogui color picker
330331
mx::Color3 v = value->asA<mx::Color3>();
331-
ng::Color c(v[0], v[1], v[2], 1.0);
332+
mx::Color3 displayCol = v.linearToSrgb();
333+
334+
ng::Color c(displayCol[0], displayCol[1], displayCol[2], 1.0);
332335

333336
new ng::Label(twoColumns, label);
334337
auto colorVar = new EditorColorPicker(twoColumns, c);
@@ -339,7 +342,10 @@ void PropertyEditor::addItemToForm(const mx::UIPropertyItem& item, const std::st
339342
mx::MaterialPtr material = viewer->getSelectedMaterial();
340343
if (material)
341344
{
342-
mx::Vector3 v(c.r(), c.g(), c.b());
345+
// Transform sRGBA color picker value to linear space for writing to material
346+
mx::Color3 linearCol = mx::Color3(c.r(), c.g(), c.b()).srgbToLinear();
347+
mx::Vector3 v(linearCol[0], linearCol[1], linearCol[2]);
348+
343349
material->modifyUniform(path, mx::Value::createValue(v));
344350
}
345351
});
@@ -354,7 +360,10 @@ void PropertyEditor::addItemToForm(const mx::UIPropertyItem& item, const std::st
354360

355361
new ng::Label(twoColumns, label);
356362
mx::Color4 v = value->asA<mx::Color4>();
357-
ng::Color c(v[0], v[1], v[2], v[3]);
363+
mx::Color3 displayCol = mx::Color3(v[0], v[1], v[2]).linearToSrgb();
364+
365+
ng::Color c(displayCol[0], displayCol[1], displayCol[2], v[3]);
366+
358367
auto colorVar = new EditorColorPicker(twoColumns, c);
359368
colorVar->set_fixed_size({ 100, 20 });
360369
colorVar->set_font_size(15);
@@ -363,7 +372,10 @@ void PropertyEditor::addItemToForm(const mx::UIPropertyItem& item, const std::st
363372
mx::MaterialPtr material = viewer->getSelectedMaterial();
364373
if (material)
365374
{
366-
mx::Vector4 v(c.r(), c.g(), c.b(), c.w());
375+
// Transform sRGBA color picker value to linear space for writing to material
376+
mx::Color3 linearCol = mx::Color3(c.r(), c.g(), c.b()).srgbToLinear();
377+
mx::Vector3 v(linearCol[0], linearCol[1], linearCol[2]);
378+
367379
material->modifyUniform(path, mx::Value::createValue(v));
368380
}
369381
});

0 commit comments

Comments
 (0)