glTF exporter: add alpha export support

This commit is contained in:
Julien Duroure 2019-08-03 07:18:25 +02:00
parent 2c08beb969
commit 1e20236039
4 changed files with 23 additions and 5 deletions

View File

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

View File

@ -91,6 +91,11 @@ def __gather_extras(sockets_or_slots, export_settings):
def __gather_mime_type(sockets_or_slots, export_settings):
# force png if Alpha contained so we can export alpha
for socket in sockets_or_slots:
if socket.name == "Alpha":
return "image/png"
if export_settings["gltf_image_format"] == "NAME":
extension = __get_extension_from_slot(sockets_or_slots, export_settings)
extension = extension.lower()
@ -175,13 +180,16 @@ def __get_image_data(sockets_or_slots, export_settings) -> gltf2_blender_image.E
target_channel = None
# Change target channel for metallic and roughness.
# some sockets need channel rewriting (gltf pbr defines fixed channels for some attributes)
if socket.name == 'Metallic':
target_channel = 2
elif socket.name == 'Roughness':
target_channel = 1
elif socket.name == 'Occlusion' and len(sockets_or_slots) > 1 and sockets_or_slots[1] is not None:
target_channel = 0
elif socket.name == 'Alpha' and len(sockets_or_slots) > 1 and sockets_or_slots[1] is not None:
composed_image.set_alpha(True)
target_channel = 3
if target_channel is not None:
if composed_image is None:
@ -189,8 +197,8 @@ def __get_image_data(sockets_or_slots, export_settings) -> gltf2_blender_image.E
composed_image[target_channel] = image[source_channel]
else:
# If we're not assigning target channels, just return the first valid image.
return image
# copy full image...eventually following sockets might overwrite things
composed_image = image
return composed_image

View File

@ -94,7 +94,14 @@ def __gather_base_color_texture(blender_material, export_settings):
base_color_socket = gltf2_blender_get.get_socket_or_texture_slot_old(blender_material, "BaseColor")
if base_color_socket is None:
base_color_socket = gltf2_blender_get.get_socket_or_texture_slot(blender_material, "Background")
return gltf2_blender_gather_texture_info.gather_texture_info((base_color_socket,), export_settings)
alpha_socket = gltf2_blender_get.get_socket_or_texture_slot(blender_material, "Alpha")
if alpha_socket is not None and alpha_socket.is_linked:
inputs = (base_color_socket, alpha_socket, )
else:
inputs = (base_color_socket,)
return gltf2_blender_gather_texture_info.gather_texture_info(inputs, export_settings)
def __get_tex_from_socket(blender_shader_socket: bpy.types.NodeSocket):

View File

@ -41,6 +41,9 @@ class ExportImage:
self._blender_image = blender_image
self._has_alpha = has_alpha
def set_alpha(self, alpha: bool):
self._has_alpha = alpha
@classmethod
def from_blender_image(cls, blender_image: bpy.types.Image):
img = np.array(blender_image.pixels)