Skip to content

Commit

Permalink
Parses texture sampler
Browse files Browse the repository at this point in the history
  • Loading branch information
AurL committed Oct 17, 2018
1 parent 402f627 commit 192e3cc
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ def create_cycles(gltf, material_idx):
else:
uvmap["gltf2_texcoord"] = 0 #TODO: set in precompute instead of here?

text = node_tree.nodes.new('ShaderNodeTexImage')
text.image = bpy.data.images[gltf.data.images[gltf.data.textures[pymaterial.emissive_texture.index].source].blender_image_name]
text.label = 'EMISSIVE'
text = BlenderTextureNode.create(gltf, pymaterial.emissive_texture.index, node_tree, 'EMISSIVE')
text.location = -1000,1000
add = node_tree.nodes.new('ShaderNodeAddShader')
add.location = 500,500
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ def create_cycles(gltf, material_idx):
else:
uvmap["gltf2_texcoord"] = 0 #TODO set in pre_compute instead of here

text = node_tree.nodes.new('ShaderNodeTexImage')
text.image = bpy.data.images[gltf.data.images[gltf.data.textures[pymaterial.normal_texture.index].source].blender_image_name]
text.label = 'NORMALMAP'
text = BlenderTextureNode.create(gltf, pymaterial.normal_texture.index, node_tree, 'NORMALMAP')
text.color_space = 'NONE'
text.location = -500, -500

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ def create_cycles(gltf, pypbr, mat_name, vertex_color):
BlenderTextureInfo.create(gltf, pypbr.base_color_texture.index)

# create UV Map / Mapping / Texture nodes / separate & math and combine
text_node = node_tree.nodes.new('ShaderNodeTexImage')
text_node.image = bpy.data.images[gltf.data.images[gltf.data.textures[pypbr.base_color_texture.index].source].blender_image_name]
text_node.label = 'BASE COLOR'
text_node = BlenderTextureNode.create(gltf, pypbr.base_color_texture.index, node_tree, 'BASE COLOR')
text_node.location = -1000,500

combine = node_tree.nodes.new('ShaderNodeCombineRGB')
Expand Down Expand Up @@ -199,8 +197,8 @@ def create_cycles(gltf, pypbr, mat_name, vertex_color):

# create UV Map / Mapping / Texture nodes / separate & math and combine
text_node = node_tree.nodes.new('ShaderNodeTexImage')
text_node.image = bpy.data.images[gltf.data.images[gltf.data.textures[pypbr.base_color_texture.index].source].blender_image_name]
text_node.label = 'BASE COLOR'
text_node = BlenderTextureNode.create(gltf, pypbr.base_color_texture.index, node_tree, 'BASE COLOR')

if vertex_color:
text_node.location = -2000,500
else:
Expand Down Expand Up @@ -259,10 +257,8 @@ def create_cycles(gltf, pypbr, mat_name, vertex_color):

elif pypbr.metallic_type == gltf.TEXTURE:
BlenderTextureInfo.create(gltf, pypbr.metallic_roughness_texture.index)
metallic_text = node_tree.nodes.new('ShaderNodeTexImage')
metallic_text.image = bpy.data.images[gltf.data.images[gltf.data.textures[pypbr.metallic_roughness_texture.index].source].blender_image_name]
metallic_text = BlenderTextureNode.create(gltf, pypbr.metallic_roughness_texture.index, node_tree, 'METALLIC ROUGHNESS')
metallic_text.color_space = 'NONE'
metallic_text.label = 'METALLIC ROUGHNESS'
metallic_text.location = -500,0

metallic_separate = node_tree.nodes.new('ShaderNodeSeparateRGB')
Expand All @@ -289,10 +285,8 @@ def create_cycles(gltf, pypbr, mat_name, vertex_color):
elif pypbr.metallic_type == gltf.TEXTURE_FACTOR:

BlenderTextureInfo.create(gltf, pypbr.metallic_roughness_texture.index)
metallic_text = node_tree.nodes.new('ShaderNodeTexImage')
metallic_text.image = bpy.data.images[gltf.data.images[gltf.data.textures[pypbr.metallic_roughness_texture.index].source].blender_image_name]
metallic_text = BlenderTextureNode.create(gltf, pypbr.metallic_roughness_texture.index, node_tree, 'METALLIC ROUGHNESS')
metallic_text.color_space = 'NONE'
metallic_text.label = 'METALLIC ROUGHNESS'
metallic_text.location = -1000,0

metallic_separate = node_tree.nodes.new('ShaderNodeSeparateRGB')
Expand Down
37 changes: 37 additions & 0 deletions addons/io_sketchfab_plugin/blender/imp/gltf2_blender_texture.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,40 @@ class BlenderTexture():
def create(gltf, pytexture_idx):
pytexture = gltf.data.textures[pytexture_idx]
BlenderImage.create(gltf, pytexture.source)

class BlenderTextureNode():

@staticmethod
def create(gltf, texture_index, node_tree, label=None):
text_node = node_tree.nodes.new('ShaderNodeTexImage')
gltf_texture = gltf.data.textures[texture_index]
text_node.image = bpy.data.images[gltf.data.images[gltf_texture.source].blender_image_name]

if gltf_texture.sampler is None:
return

sampler = gltf.data.samplers[gltf_texture.sampler]
# Only linear and closest for the moment

# NEAREST: 9728)
# LINEAR: 9729)
# NEAREST_MIPMAP_NEAREST: 9984
# LINEAR_MIPMAP_NEAREST: 9985
# NEAREST_MIPMAP_LINEAR: 9986
# LINEAR_MIPMAP_LINEAR: 9987

# WRAP_RPEAT: 10497
# WRAP_CLAMP_TO_EDGE: 33071
# WRAP_MIRRORED_REPEAT: 33648
if sampler.min_filter in [9728, 9984, 9986] and sampler.mag_filter in [9728, 9984, 9986]:
text_node.interpolation = 'Closest'
else:
text_node.interpolation = 'Linear'

if sampler.wrap_s == 33071 or sampler.wrap_t == 33071:
text_node.extension = 'Extend'

if label:
text_node.label = label

return text_node

0 comments on commit 192e3cc

Please sign in to comment.