[Bf-extensions-cvs] [5bf477b1] master: glTF importer: Fix vertex color import

Julien Duroure noreply at git.blender.org
Thu Feb 7 19:05:43 CET 2019


Commit: 5bf477b14fe6f2093c3cb1752bffe55fac9cd563
Author: Julien Duroure
Date:   Thu Feb 7 19:04:52 2019 +0100
Branches: master
https://developer.blender.org/rBA5bf477b14fe6f2093c3cb1752bffe55fac9cd563

glTF importer: Fix vertex color import

glTF stores vertexcolor data in linear, blender in sRGB

===================================================================

M	io_scene_gltf2/blender/exp/gltf2_blender_extract.py
M	io_scene_gltf2/blender/imp/gltf2_blender_mesh.py
A	io_scene_gltf2/io/com/gltf2_io_color_management.py

===================================================================

diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_extract.py b/io_scene_gltf2/blender/exp/gltf2_blender_extract.py
index 88932e5b..fb3c714b 100755
--- a/io_scene_gltf2/blender/exp/gltf2_blender_extract.py
+++ b/io_scene_gltf2/blender/exp/gltf2_blender_extract.py
@@ -21,6 +21,7 @@ from mathutils.geometry import tessellate_polygon
 
 from . import gltf2_blender_export_keys
 from ...io.com.gltf2_io_debug import print_console
+from ...io.com.gltf2_io_color_management import color_srgb_to_scene_linear
 from io_scene_gltf2.blender.exp import gltf2_blender_gather_skins
 
 #
@@ -113,19 +114,6 @@ def decompose_transition(matrix, context, export_settings):
 
     return translation, rotation, scale
 
-
-def color_srgb_to_scene_linear(c):
-    """
-    Convert from sRGB to scene linear color space.
-
-    Source: Cycles addon implementation, node_color.h.
-    """
-    if c < 0.04045:
-        return 0.0 if c < 0.0 else c * (1.0 / 12.92)
-    else:
-        return pow((c + 0.055) * (1.0 / 1.055), 2.4)
-
-
 def extract_primitive_floor(a, indices, use_tangents):
     """Shift indices, that the first one starts with 0. It is assumed, that the indices are packed."""
     attributes = {
diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_mesh.py b/io_scene_gltf2/blender/imp/gltf2_blender_mesh.py
index 2d346638..c083d8d6 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_mesh.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_mesh.py
@@ -17,6 +17,7 @@ import bmesh
 
 from .gltf2_blender_primitive import BlenderPrimitive
 from ...io.imp.gltf2_io_binary import BinaryData
+from ...io.com.gltf2_io_color_management import color_linear_to_srgb
 from ..com.gltf2_blender_conversion import loc_gltf_to_blender
 
 
@@ -165,11 +166,20 @@ class BlenderMesh():
                         vert_idx = mesh.loops[loop_idx].vertex_index
                         if vert_idx in range(offset, offset + prim.vertices_length):
                             cpt_idx = vert_idx - offset
+                            # Need to convert from linear (glTF to sRGB (blender))
                             # check dimension, and add alpha if needed
                             if len(color_data[cpt_idx]) == 3:
-                                vertex_color_data = color_data[cpt_idx] + (1.0,)
+                                srgb_color = [
+                                    color_linear_to_srgb(color_data[cpt_idx][0]),
+                                    color_linear_to_srgb(color_data[cpt_idx][1]),
+                                    color_linear_to_srgb(color_data[cpt_idx][2]),
+                                    1.0]
                             else:
-                                vertex_color_data = color_data[cpt_idx]
-                            vertex_color.data[loop_idx].color = vertex_color_data
+                                srgb_color = [
+                                    color_linear_to_srgb(color_data[cpt_idx][0]),
+                                    color_linear_to_srgb(color_data[cpt_idx][1]),
+                                    color_linear_to_srgb(color_data[cpt_idx][2]),
+                                    color_data[cpt_idx][3]]
+                            vertex_color.data[loop_idx].color = srgb_color
             offset = offset + prim.vertices_length
 
diff --git a/io_scene_gltf2/io/com/gltf2_io_color_management.py b/io_scene_gltf2/io/com/gltf2_io_color_management.py
new file mode 100644
index 00000000..b1ebdb4a
--- /dev/null
+++ b/io_scene_gltf2/io/com/gltf2_io_color_management.py
@@ -0,0 +1,37 @@
+# Copyright 2019 The glTF-Blender-IO authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+def color_srgb_to_scene_linear(c):
+    """
+    Convert from sRGB to scene linear color space.
+
+    Source: Cycles addon implementation, node_color.h.
+    """
+    if c < 0.04045:
+        return 0.0 if c < 0.0 else c * (1.0 / 12.92)
+    else:
+        return pow((c + 0.055) * (1.0 / 1.055), 2.4)
+
+def color_linear_to_srgb(c):
+    """
+    Convert from linear to sRGB color space.
+
+    Source: Cycles addon implementation, node_color.h.
+    """
+    if c < 0.0031308:
+        return 0.0 if c < 0.0 else c * 12.92
+    else:
+        return 1.055 * pow(c, 1.0 / 2.4) - 0.055
+



More information about the Bf-extensions-cvs mailing list