[Bf-extensions-cvs] [9f1b91ca] master: glTF importer: import data from texture samplers + exporter fix

Julien Duroure noreply at git.blender.org
Sun Nov 17 21:22:29 CET 2019


Commit: 9f1b91ca13163419509c3e65ad89a1492e611305
Author: Julien Duroure
Date:   Sun Nov 17 21:21:47 2019 +0100
Branches: master
https://developer.blender.org/rBA9f1b91ca13163419509c3e65ad89a1492e611305

glTF importer: import data from texture samplers + exporter fix

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

M	io_scene_gltf2/__init__.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_sampler.py
M	io_scene_gltf2/blender/imp/gltf2_blender_material_utils.py
M	io_scene_gltf2/io/com/gltf2_io_constants.py

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

diff --git a/io_scene_gltf2/__init__.py b/io_scene_gltf2/__init__.py
index e01723a2..0d8a517d 100755
--- a/io_scene_gltf2/__init__.py
+++ b/io_scene_gltf2/__init__.py
@@ -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": (1, 1, 10),
+    "version": (1, 1, 11),
     'blender': (2, 81, 6),
     'location': 'File > Import-Export',
     'description': 'Import-Export as glTF 2.0',
diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_gather_sampler.py b/io_scene_gltf2/blender/exp/gltf2_blender_gather_sampler.py
index 840c98f4..78fbc282 100755
--- a/io_scene_gltf2/blender/exp/gltf2_blender_gather_sampler.py
+++ b/io_scene_gltf2/blender/exp/gltf2_blender_gather_sampler.py
@@ -64,13 +64,13 @@ def __gather_name(blender_shader_node, export_settings):
 
 
 def __gather_wrap_s(blender_shader_node, export_settings):
-    if blender_shader_node.extension == 'CLIP':
+    if blender_shader_node.extension == 'EXTEND':
         return 33071
     return None
 
 
 def __gather_wrap_t(blender_shader_node, export_settings):
-    if blender_shader_node.extension == 'CLIP':
+    if blender_shader_node.extension == 'EXTEND':
         return 33071
     return None
 
@@ -79,7 +79,7 @@ def __gather_wrap_t(blender_shader_node, export_settings):
 def gather_sampler_from_texture_slot(blender_texture: bpy.types.TextureSlot, export_settings):
     magFilter = 9729
     wrap = 10497
-    if blender_texture.texture.extension == 'CLIP':
+    if blender_texture.texture.extension == 'EXTEND':
         wrap = 33071
 
     minFilter = 9986
diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_material_utils.py b/io_scene_gltf2/blender/imp/gltf2_blender_material_utils.py
index 16a40d1a..c9d81a4f 100644
--- a/io_scene_gltf2/blender/imp/gltf2_blender_material_utils.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_material_utils.py
@@ -15,6 +15,9 @@
 import bpy
 from .gltf2_blender_image import BlenderImage
 from ..com.gltf2_blender_conversion import texture_transform_gltf_to_blender
+from io_scene_gltf2.io.com.gltf2_io import Sampler
+from io_scene_gltf2.io.com.gltf2_io_debug import print_console
+from io_scene_gltf2.io.com.gltf2_io_constants import TextureFilter, TextureWrap
 
 def make_texture_block(gltf, node_tree, tex_info, location, label, name=None, colorspace=None):
     """Creates a block of Shader Nodes for the given TextureInfo.
@@ -45,7 +48,12 @@ def make_texture_block(gltf, node_tree, tex_info, location, label, name=None, co
         if tex_img.image:
             tex_img.image.colorspace_settings.is_data = True
 
-    # TODO do sampler
+    if pytexture.sampler is not None:
+        pysampler = gltf.data.samplers[pytexture.sampler]
+    else:
+        pysampler = Sampler.from_dict({})
+    set_filtering(tex_img, pysampler)
+    set_wrap_mode(tex_img, pysampler)
 
     # Mapping (transforms UVs for KHR_texture_transform)
 
@@ -84,3 +92,58 @@ def make_texture_block(gltf, node_tree, tex_info, location, label, name=None, co
 
     return tex_img
 
+def set_filtering(tex_img, pysampler):
+    """Set the filtering/interpolation on an Image Texture from the glTf sampler."""
+    minf = pysampler.min_filter
+    magf = pysampler.mag_filter
+
+    # Ignore mipmapping
+    if minf in [TextureFilter.NearestMipmapNearest, TextureFilter.NearestMipmapLinear]:
+        minf = TextureFilter.Nearest
+    elif minf in [TextureFilter.LinearMipmapNearest, TextureFilter.LinearMipmapLinear]:
+        minf = TextureFilter.Linear
+
+    # If both are nearest or the only specified one was nearest, use nearest.
+    if (minf, magf) in [
+        (TextureFilter.Nearest, TextureFilter.Nearest),
+        (TextureFilter.Nearest, None),
+        (None, TextureFilter.Nearest),
+    ]:
+        tex_img.interpolation = 'Closest'
+    else:
+        tex_img.interpolation = 'Linear'
+
+def set_wrap_mode(tex_img, pysampler):
+    """Set the extension on an Image Texture node from the pysampler."""
+    wrap_s = pysampler.wrap_s
+    wrap_t = pysampler.wrap_t
+
+    if wrap_s is None:
+        wrap_s = TextureWrap.Repeat
+    if wrap_t is None:
+        wrap_t = TextureWrap.Repeat
+
+    # The extension property on the Image Texture node can only handle the case
+    # where both directions are the same and are either REPEAT or CLAMP_TO_EDGE.
+    if (wrap_s, wrap_t) == (TextureWrap.Repeat, TextureWrap.Repeat):
+        extension = TextureWrap.Repeat
+    elif (wrap_s, wrap_t) == (TextureWrap.ClampToEdge, TextureWrap.ClampToEdge):
+        extension = TextureWrap.ClampToEdge
+    else:
+        print_console('WARNING',
+            'texture wrap mode unsupported: (%s, %s)' % (wrap_name(wrap_s), wrap_name(wrap_t)),
+        )
+        # Default to repeat
+        extension = TextureWrap.Repeat
+
+    if extension == TextureWrap.Repeat:
+        tex_img.extension = 'REPEAT'
+    elif extension == TextureWrap.ClampToEdge:
+        tex_img.extension = 'EXTEND'
+
+def wrap_name(wrap):
+    if wrap == TextureWrap.ClampToEdge: return 'CLAMP_TO_EDGE'
+    if wrap == TextureWrap.MirroredRepeat: return 'MIRRORED_REPEAT'
+    if wrap == TextureWrap.Repeat: return 'REPEAT'
+    return 'UNKNOWN (%s)' % wrap
+
diff --git a/io_scene_gltf2/io/com/gltf2_io_constants.py b/io_scene_gltf2/io/com/gltf2_io_constants.py
index c97908cd..9757a2a7 100755
--- a/io_scene_gltf2/io/com/gltf2_io_constants.py
+++ b/io_scene_gltf2/io/com/gltf2_io_constants.py
@@ -103,6 +103,21 @@ class DataType:
         }[num_elems]
 
 
+class TextureFilter(IntEnum):
+    Nearest = 9728
+    Linear = 9729
+    NearestMipmapNearest = 9984
+    LinearMipmapNearest = 9985
+    NearestMipmapLinear = 9986
+    LinearMipmapLinear = 9987
+
+
+class TextureWrap(IntEnum):
+    ClampToEdge = 33071
+    MirroredRepeat = 33648
+    Repeat = 10497
+
+
 #################
 # LEGACY DEFINES



More information about the Bf-extensions-cvs mailing list