Skip to content
Snippets Groups Projects
Commit 5bf477b1 authored by Julien Duroure's avatar Julien Duroure
Browse files

glTF importer: Fix vertex color import

glTF stores vertexcolor data in linear, blender in sRGB
parent 9c7fb7e2
No related branches found
No related tags found
No related merge requests found
...@@ -21,6 +21,7 @@ from mathutils.geometry import tessellate_polygon ...@@ -21,6 +21,7 @@ from mathutils.geometry import tessellate_polygon
from . import gltf2_blender_export_keys from . import gltf2_blender_export_keys
from ...io.com.gltf2_io_debug import print_console 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 from io_scene_gltf2.blender.exp import gltf2_blender_gather_skins
# #
...@@ -113,19 +114,6 @@ def decompose_transition(matrix, context, export_settings): ...@@ -113,19 +114,6 @@ def decompose_transition(matrix, context, export_settings):
return translation, rotation, scale 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): 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.""" """Shift indices, that the first one starts with 0. It is assumed, that the indices are packed."""
attributes = { attributes = {
......
...@@ -17,6 +17,7 @@ import bmesh ...@@ -17,6 +17,7 @@ import bmesh
from .gltf2_blender_primitive import BlenderPrimitive from .gltf2_blender_primitive import BlenderPrimitive
from ...io.imp.gltf2_io_binary import BinaryData 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 from ..com.gltf2_blender_conversion import loc_gltf_to_blender
...@@ -165,11 +166,20 @@ class BlenderMesh(): ...@@ -165,11 +166,20 @@ class BlenderMesh():
vert_idx = mesh.loops[loop_idx].vertex_index vert_idx = mesh.loops[loop_idx].vertex_index
if vert_idx in range(offset, offset + prim.vertices_length): if vert_idx in range(offset, offset + prim.vertices_length):
cpt_idx = vert_idx - offset cpt_idx = vert_idx - offset
# Need to convert from linear (glTF to sRGB (blender))
# check dimension, and add alpha if needed # check dimension, and add alpha if needed
if len(color_data[cpt_idx]) == 3: 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: else:
vertex_color_data = color_data[cpt_idx] srgb_color = [
vertex_color.data[loop_idx].color = vertex_color_data 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 offset = offset + prim.vertices_length
# 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment