glTF: implement occlusion strength (importer & exporter)

This commit is contained in:
Julien Duroure 2020-12-01 20:52:55 +01:00
parent 008bcf44ef
commit d5c0d4b77c
3 changed files with 32 additions and 2 deletions

View File

@ -15,7 +15,7 @@
bl_info = {
'name': 'glTF 2.0 format',
'author': 'Julien Duroure, Scurest, Norbert Nopper, Urs Hanselmann, Moritz Becher, Benjamin Schmithüsen, Jim Eckerlein, and many external contributors',
"version": (1, 5, 5),
"version": (1, 5, 6),
'blender': (2, 91, 0),
'location': 'File > Import-Export',
'description': 'Import-Export as glTF 2.0',

View File

@ -123,6 +123,19 @@ def __gather_normal_scale(primary_socket, export_settings):
# MaterialOcclusionTextureInfo only
def __gather_occlusion_strength(primary_socket, export_settings):
# Look for a MixRGB node that mixes with pure white in front of
# primary_socket. The mix factor gives the occlusion strength.
node = gltf2_blender_get.previous_node(primary_socket)
if node and node.type == 'MIX_RGB' and node.blend_type == 'MIX':
fac = gltf2_blender_get.get_const_from_socket(node.inputs['Fac'], kind='VALUE')
col1 = gltf2_blender_get.get_const_from_socket(node.inputs['Color1'], kind='RGB')
col2 = gltf2_blender_get.get_const_from_socket(node.inputs['Color2'], kind='RGB')
if fac is not None:
if col1 == [1, 1, 1] and col2 is None:
return fac
if col1 is None and col2 == [1, 1, 1]:
return 1.0 - fac # reversed for reversed inputs
return None

View File

@ -433,13 +433,30 @@ def normal(mh: MaterialHelper, location, normal_socket):
)
# [Texture] => [Separate R] =>
# [Texture] => [Separate R] => [Mix Strength] =>
def occlusion(mh: MaterialHelper, location, occlusion_socket):
x, y = location
if mh.pymat.occlusion_texture is None:
return
strength = mh.pymat.occlusion_texture.strength
if strength is None: strength = 1.0
if strength != 1.0:
# Mix with white
node = mh.node_tree.nodes.new('ShaderNodeMixRGB')
node.label = 'Occlusion Strength'
node.location = x - 140, y
node.blend_type = 'MIX'
# Outputs
mh.node_tree.links.new(occlusion_socket, node.outputs[0])
# Inputs
node.inputs['Fac'].default_value = strength
node.inputs['Color1'].default_value = [1, 1, 1, 1]
occlusion_socket = node.inputs['Color2']
x -= 200
# Separate RGB
node = mh.node_tree.nodes.new('ShaderNodeSeparateRGB')
node.location = x - 150, y - 75