[Bf-extensions-cvs] [cd7ffdb8] master: glTF importer: fix emissive factor without emissive texture

Julien Duroure noreply at git.blender.org
Thu Jan 31 22:32:03 CET 2019


Commit: cd7ffdb8441f0a547fb0ab7169d361a3b441198d
Author: Julien Duroure
Date:   Thu Jan 31 22:31:45 2019 +0100
Branches: master
https://developer.blender.org/rBAcd7ffdb8441f0a547fb0ab7169d361a3b441198d

glTF importer: fix emissive factor without emissive texture

===================================================================

M	io_scene_gltf2/blender/imp/gltf2_blender_map_emissive.py
M	io_scene_gltf2/blender/imp/gltf2_blender_material.py

===================================================================

diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_map_emissive.py b/io_scene_gltf2/blender/imp/gltf2_blender_map_emissive.py
index 79cc93cd..1b56be2e 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_map_emissive.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_map_emissive.py
@@ -23,22 +23,23 @@ class BlenderEmissiveMap():
         raise RuntimeError("%s should not be instantiated" % cls)
 
     @staticmethod
-    def create(gltf, material_idx, vertex_color):
+    def create(gltf, material_idx, vertex_color, factor_only=False):
         """Create emissive map."""
         engine = bpy.context.scene.render.engine
         if engine in ['CYCLES', 'BLENDER_EEVEE']:
-            BlenderEmissiveMap.create_nodetree(gltf, material_idx, vertex_color)
+            BlenderEmissiveMap.create_nodetree(gltf, material_idx, vertex_color, factor_only)
 
-    def create_nodetree(gltf, material_idx, vertex_color):
+    def create_nodetree(gltf, material_idx, vertex_color, factor_only=False):
         """Create node tree."""
         pymaterial = gltf.data.materials[material_idx]
 
         material = bpy.data.materials[pymaterial.blender_material[vertex_color]]
         node_tree = material.node_tree
 
-        BlenderTextureInfo.create(gltf, pymaterial.emissive_texture.index)
+        if factor_only is False:
+            BlenderTextureInfo.create(gltf, pymaterial.emissive_texture.index)
 
-        # check if there is some emssive_factor on material
+        # check if there is some emissive_factor on material
         if pymaterial.emissive_factor is None:
             # Default in glTF specification is 0/0/0 --> No emission
             pymaterial.emissive_factor = [0.0, 0.0, 0.0]
@@ -50,46 +51,55 @@ class BlenderEmissiveMap():
         # add nodes
         emit = node_tree.nodes.new('ShaderNodeEmission')
         emit.location = 0, 1000
-        if pymaterial.emissive_factor != [1.0, 1.0, 1.0]:
-            mult_node = node_tree.nodes.new('ShaderNodeMixRGB')
-            mult_node.blend_type = 'MULTIPLY'
-            mult_node.inputs['Fac'].default_value = 1.0
-            mult_node.location = -500, 1000
-            mult_node.inputs['Color2'].default_value = [
-                                                        pymaterial.emissive_factor[0],
-                                                        pymaterial.emissive_factor[1],
-                                                        pymaterial.emissive_factor[2],
-                                                        1.0,
-                                                        ]
-        mapping = node_tree.nodes.new('ShaderNodeMapping')
-        mapping.location = -1500, 1000
-        uvmap = node_tree.nodes.new('ShaderNodeUVMap')
-        uvmap.location = -2000, 1000
-        if pymaterial.emissive_texture.tex_coord is not None:
-            uvmap["gltf2_texcoord"] = pymaterial.emissive_texture.tex_coord  # Set custom flag to retrieve TexCoord
-        else:
-            uvmap["gltf2_texcoord"] = 0  # TODO: set in precompute instead of here?
 
-        text = node_tree.nodes.new('ShaderNodeTexImage')
-        if gltf.data.images[
-            gltf.data.textures[pymaterial.emissive_texture.index].source
-        ].blender_image_name is not None:
-            text.image = bpy.data.images[gltf.data.images[
+        if factor_only is False:
+            if pymaterial.emissive_factor != [1.0, 1.0, 1.0]:
+                mult_node = node_tree.nodes.new('ShaderNodeMixRGB')
+                mult_node.blend_type = 'MULTIPLY'
+                mult_node.inputs['Fac'].default_value = 1.0
+                mult_node.location = -500, 1000
+                mult_node.inputs['Color2'].default_value = [
+                                                            pymaterial.emissive_factor[0],
+                                                            pymaterial.emissive_factor[1],
+                                                            pymaterial.emissive_factor[2],
+                                                            1.0,
+                                                            ]
+            mapping = node_tree.nodes.new('ShaderNodeMapping')
+            mapping.location = -1500, 1000
+            uvmap = node_tree.nodes.new('ShaderNodeUVMap')
+            uvmap.location = -2000, 1000
+            if pymaterial.emissive_texture.tex_coord is not None:
+                uvmap["gltf2_texcoord"] = pymaterial.emissive_texture.tex_coord  # Set custom flag to retrieve TexCoord
+            else:
+                uvmap["gltf2_texcoord"] = 0  # TODO: set in precompute instead of here?
+
+            text = node_tree.nodes.new('ShaderNodeTexImage')
+            if gltf.data.images[
                 gltf.data.textures[pymaterial.emissive_texture.index].source
-            ].blender_image_name]
-        text.label = 'EMISSIVE'
-        text.location = -1000, 1000
-        add = node_tree.nodes.new('ShaderNodeAddShader')
-        add.location = 500, 500
+            ].blender_image_name is not None:
+                text.image = bpy.data.images[gltf.data.images[
+                    gltf.data.textures[pymaterial.emissive_texture.index].source
+                ].blender_image_name]
+            text.label = 'EMISSIVE'
+            text.location = -1000, 1000
+
+            # create links
+            node_tree.links.new(mapping.inputs[0], uvmap.outputs[0])
+            node_tree.links.new(text.inputs[0], mapping.outputs[0])
+            if pymaterial.emissive_factor != [1.0, 1.0, 1.0]:
+                node_tree.links.new(mult_node.inputs[1], text.outputs[0])
+                node_tree.links.new(emit.inputs[0], mult_node.outputs[0])
+            else:
+                node_tree.links.new(emit.inputs[0], text.outputs[0])
 
-        # create links
-        node_tree.links.new(mapping.inputs[0], uvmap.outputs[0])
-        node_tree.links.new(text.inputs[0], mapping.outputs[0])
-        if pymaterial.emissive_factor != [1.0, 1.0, 1.0]:
-            node_tree.links.new(mult_node.inputs[1], text.outputs[0])
-            node_tree.links.new(emit.inputs[0], mult_node.outputs[0])
         else:
-            node_tree.links.new(emit.inputs[0], text.outputs[0])
+            emissive_color = pymaterial.emissive_factor
+            emissive_color.append(1.0) # add alpha
+            emit.inputs[0].default_value = emissive_color
+
+
+        add = node_tree.nodes.new('ShaderNodeAddShader')
+        add.location = 500, 500
 
         # following  links will modify PBR node tree
         node_tree.links.new(add.inputs[0], emit.outputs[0])
diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_material.py b/io_scene_gltf2/blender/imp/gltf2_blender_material.py
index fcd7c7d2..c3a28077 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_material.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_material.py
@@ -82,6 +82,10 @@ class BlenderMaterial():
             # add emission map if needed
             if pymaterial.emissive_texture is not None:
                 BlenderEmissiveMap.create(gltf, material_idx, vertex_color)
+            elif pymaterial.emissive_factor is not None:
+            # add emissive factor only if there is not emissive texture
+                BlenderEmissiveMap.create(gltf, material_idx, vertex_color, factor_only=True)
+
 
             # add normal map if needed
             if pymaterial.normal_texture is not None:



More information about the Bf-extensions-cvs mailing list