def get_normal_map(self, material, shader): # Normal Map [Normal] -> [Normal] Principled BSDF normal_map = get_from_node(material.node_tree, 'NORMAL_MAP', to_node=shader, from_socket_name='Normal', to_socket_name='Normal') if normal_map: # Image Texture [Color] -> [Color] Normal Map return get_from_node(material.node_tree, 'TEX_IMAGE', to_node=normal_map, from_socket_name='Color', to_socket_name='Color')
def get_diffuse(self, material, shader): for i in ('Color', 'Alpha'): # Image Texture [Color/Alpha] -> [Socket] Principled BSDF return get_from_node(material.node_tree, 'TEX_IMAGE', to_node=shader, from_socket_name=i, to_socket_name='Base Color')
def get_normal_strength(self, material, shader): # Normal Map [Normal] -> [Normal] Principled BSDF normal_map = get_from_node(material.node_tree, 'NORMAL_MAP', to_node=shader, from_socket_name='Normal', to_socket_name='Normal') if normal_map: return normal_map.inputs['Strength'].default_value else: return 1
def get_roughness_map(self, material, shader): # Math [Value] -> [Roughness] Principled BSDF math_node = get_from_node(material.node_tree, 'MATH', to_node=shader, from_socket_name='Value', to_socket_name='Roughness') if math_node: # Image Texture [Color] -> [Input] Math return get_from_node(material.node_tree, 'TEX_IMAGE', to_node=math_node, from_socket_name='Color', to_socket_name='Value') else: # Image Texture [Color] -> [Roughness] Principled BSDF return get_from_node(material.node_tree, 'TEX_IMAGE', to_node=shader, from_socket_name='Color', to_socket_name='Roughness')
def get_roughness(self, material, shader): # Math [Value] -> [Roughness] Principled BSDF math_node = get_from_node(material.node_tree, 'MATH', to_node=shader, from_socket_name='Value', to_socket_name='Roughness') if math_node: for input_ in math_node.inputs: if input_.name == 'Value' and not input_.is_linked: return input_.default_value else: return shader.inputs['Roughness'].default_value
def make_textures(self, material): results = [] shader = None if material.node_tree is not None: output = get_root_node(material.node_tree, 'OUTPUT_MATERIAL') if output: shader = get_from_node(material.node_tree, 'BSDF_PRINCIPLED', to_node=output, from_socket_name='BSDF', to_socket_name='Surface') if shader: image_textures = self.get_images(material, shader) last_texid = 0 for i, (type_, image_texture) in enumerate(reversed(image_textures)): if image_texture is not None: last_texid = len(image_textures) - i - 1 break for i, (type_, image_texture) in enumerate(image_textures): if image_texture is None: if self._empty_textures: # fill empty slot result = self.make_empty_texture(type_) results.append((type_, ) + result) else: break elif image_texture: result = self.make_texture(type_, image_texture) results.append((type_, ) + result) if i >= last_texid: break return results
def make_material(self, material): gltf_material = { 'name': material.name, 'alphaMode': 'OPAQUE', 'alphaCutoff': material.alpha_threshold, 'doubleSided': not material.use_backface_culling, 'pbrMetallicRoughness': { 'extras': {}, }, } gltf_material['alphaMode'] = { 'OPAQUE': 'OPAQUE', 'BLEND': 'BLEND', 'CLIP': 'MASK' }.get(material.blend_method, 'OPAQUE') shader = None if material.node_tree is not None: output = get_root_node(material.node_tree, 'OUTPUT_MATERIAL') shader = None if output: shader = get_from_node(material.node_tree, 'BSDF_PRINCIPLED', to_node=output, from_socket_name='BSDF', to_socket_name='Surface') if not shader: return gltf_material if self._render_type == 'rp': # RenderPipeline # emission = tuple(shader.inputs['Emission'].default_value)[:3] emission = (0, 0, 0) # alpha = shader.inputs['Alpha'].default_value alpha = 1 clearcoat = shader.inputs['Clearcoat'].default_value normal_strength = self.get_normal_strength(material, shader) if sum(emission) > 0: # emission gltf_material['pbrMetallicRoughness'].update({ 'baseColorFactor': emission + (1, ), 'metallicFactor': 0, 'roughnessFactor': 1, }) gltf_material['pbrMetallicRoughness']['extras']['ior'] = 1.51 if alpha < 1: emit = [ SHADING_MODEL_TRANSPARENT_EMISSIVE, normal_strength, alpha ] else: emit = [SHADING_MODEL_EMISSIVE, normal_strength, 0] gltf_material['emissiveFactor'] = tuple(emit) else: # not emission gltf_material['pbrMetallicRoughness'].update({ 'baseColorFactor': (1, 1, 1, 1), 'metallicFactor': self.get_metallic(material, shader), 'roughnessFactor': self.get_roughness(material, shader), }) gltf_material['pbrMetallicRoughness']['extras'][ 'ior'] = shader.inputs['IOR'].default_value if alpha < 1: emit = [ SHADING_MODEL_TRANSPARENT_GLASS, normal_strength, alpha ] elif clearcoat: emit = [SHADING_MODEL_CLEARCOAT, normal_strength, 0] else: emit = [SHADING_MODEL_DEFAULT, normal_strength, 0] gltf_material['emissiveFactor'] = tuple(emit) else: # not RenderPipeline emission = (0, 0, 0) alpha = 1 gltf_material['pbrMetallicRoughness'].update({ 'baseColorFactor': (1, 1, 1, alpha), 'metallicFactor': self.get_metallic(material, shader), 'roughnessFactor': self.get_roughness(material, shader), }) gltf_material['emissiveFactor'] = tuple(emission) # if alpha < 1: # gltf_material['alphaMode'] = 'BLEND' # gltf_material['alphaCutoff'] = alpha # else: # gltf_material['alphaMode'] = 'OPAQUE' # gltf_material['alphaCutoff'] = 0 return gltf_material