diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_extract.py b/io_scene_gltf2/blender/exp/gltf2_blender_extract.py index 88932e5b179c531dd417280db73f026da0056e32..fb3c714bade384303bcf5b912dd2ede249887409 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 2d3466383f33425e15b5ed261fb290d3aadd5466..c083d8d6a33029f9fd7b2ada2b7c716d8f68150b 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 0000000000000000000000000000000000000000..b1ebdb4a12a1a2b78ca122071b65c7d781c7db29 --- /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 +