diff options
author | Julien Duroure <julien.duroure@gmail.com> | 2019-02-07 21:04:52 +0300 |
---|---|---|
committer | Julien Duroure <julien.duroure@gmail.com> | 2019-02-07 21:04:52 +0300 |
commit | 5bf477b14fe6f2093c3cb1752bffe55fac9cd563 (patch) | |
tree | e145707827c5adb8ff989d296fdec80cfb07037e /io_scene_gltf2 | |
parent | 9c7fb7e2ca052ca18fe24b69c040832882c00535 (diff) |
glTF importer: Fix vertex color import
glTF stores vertexcolor data in linear, blender in sRGB
Diffstat (limited to 'io_scene_gltf2')
-rwxr-xr-x | io_scene_gltf2/blender/exp/gltf2_blender_extract.py | 14 | ||||
-rwxr-xr-x | io_scene_gltf2/blender/imp/gltf2_blender_mesh.py | 16 | ||||
-rw-r--r-- | io_scene_gltf2/io/com/gltf2_io_color_management.py | 37 |
3 files changed, 51 insertions, 16 deletions
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 + |