Skip to content

Commit

Permalink
Apply sheen feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
donmccurdy committed Mar 7, 2022
1 parent 59a5051 commit 45a2fdf
Showing 1 changed file with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,22 @@ def __gather_clearcoat_extension(blender_material, export_settings):

return Extension('KHR_materials_clearcoat', clearcoat_extension, False)

# See: https://github.com/sobotka/blender/blob/4dd1068a5789097b50eab159adc995906fa5ea84/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl#L2-L6
def __tint_from_color(color):
# Luminance approximation.
luminance = color[0] * 0.3 + color[1] * 0.6 + color[2] * 0.1

if luminance > 0:
# Normalize to isolate hue and saturation.
return [
color[0] / luminance,
color[1] / luminance,
color[2] / luminance
]

return [1, 1, 1]

def __gather_sheen_extension(blender_material, export_settings):
sheen_enabled = False
sheen_extension = {}

sheen_socket = gltf2_blender_get.get_socket(blender_material, 'Sheen')
Expand All @@ -333,21 +347,22 @@ def __gather_sheen_extension(blender_material, export_settings):
if (isinstance(sheen_socket, bpy.types.NodeSocket)
and isinstance(sheen_tint_socket, bpy.types.NodeSocket)
and isinstance(base_color_socket, bpy.types.NodeSocket)):
sheen = gltf2_blender_get.get_factor_from_socket(sheen_socket, 'VALUE') or 0
tint = gltf2_blender_get.get_factor_from_socket(sheen_tint_socket, 'VALUE') or 0
base_color = gltf2_blender_get.get_factor_from_socket(base_color_socket, 'RGB') or [1, 1, 1]
sheen = gltf2_blender_get.get_factor_from_socket(sheen_socket, kind='VALUE') or 0
tint = gltf2_blender_get.get_factor_from_socket(sheen_tint_socket, kind='VALUE') or 0
base_color = gltf2_blender_get.get_factor_from_socket(base_color_socket, kind='RGB') or [1, 1, 1]
base_color_tint = __tint_from_color(base_color)
sheen_extension['sheenColorFactor'] = [
sheen * ((1 - tint) + (tint * base_color[0])),
sheen * ((1 - tint) + (tint * base_color[1])),
sheen * ((1 - tint) + (tint * base_color[2]))
max(0, min(1, sheen * ((1 - tint) + (tint * base_color_tint[0])))),
max(0, min(1, sheen * ((1 - tint) + (tint * base_color_tint[1])))),
max(0, min(1, sheen * ((1 - tint) + (tint * base_color_tint[2])))),
]

if not any(sheen_extension['sheenColorFactor']):
return None

roughness_socket = gltf2_blender_get.get_socket(blender_material, 'Roughness')
if isinstance(roughness_socket, bpy.types.NodeSocket) and not roughness_socket.is_linked:
sheen_extension['sheenRoughnessFactor'] = gltf2_blender_get.get_factor_from_socket(roughness_socket, 'VALUE')
sheen_extension['sheenRoughnessFactor'] = gltf2_blender_get.get_factor_from_socket(roughness_socket, kind='VALUE')

return Extension('KHR_materials_sheen', sheen_extension, False)

Expand Down

0 comments on commit 45a2fdf

Please sign in to comment.