2016-05-18 01:34:21 +02:00
|
|
|
import bpy
|
|
|
|
import math
|
2016-08-22 21:56:28 +02:00
|
|
|
import assets
|
|
|
|
import utils
|
|
|
|
import os
|
2016-05-18 01:34:21 +02:00
|
|
|
|
2016-06-21 14:43:50 +02:00
|
|
|
def is_pow(num):
|
2016-09-28 00:00:59 +02:00
|
|
|
return ((num & (num - 1)) == 0) and num != 0
|
2016-06-21 14:43:50 +02:00
|
|
|
|
2016-05-18 01:34:21 +02:00
|
|
|
def find_node_by_link(node_group, to_node, target_socket):
|
2016-09-28 00:00:59 +02:00
|
|
|
for link in node_group.links:
|
|
|
|
if link.to_node == to_node and link.to_socket == target_socket:
|
|
|
|
return link.from_node
|
2016-05-18 01:34:21 +02:00
|
|
|
|
|
|
|
def get_output_node(tree):
|
2016-09-28 00:00:59 +02:00
|
|
|
for n in tree.nodes:
|
|
|
|
if n.type == 'OUTPUT_MATERIAL':
|
|
|
|
return n
|
2016-05-18 01:34:21 +02:00
|
|
|
|
|
|
|
# Material output is used as starting point
|
|
|
|
def parse(self, material, c, defs):
|
2016-09-28 00:00:59 +02:00
|
|
|
tree = material.node_tree
|
|
|
|
output_node = get_output_node(tree)
|
|
|
|
|
|
|
|
# Traverse material tree
|
|
|
|
if output_node != None:
|
|
|
|
# Surface socket is linked
|
|
|
|
if output_node.inputs[0].is_linked:
|
|
|
|
surface_node = find_node_by_link(tree, output_node, output_node.inputs[0])
|
|
|
|
parse_from(self, material, c, defs, surface_node)
|
|
|
|
|
|
|
|
# Displace socket is linked
|
|
|
|
if output_node.inputs[2].is_linked:
|
|
|
|
displace_node = find_node_by_link(tree, output_node, output_node.inputs[2])
|
|
|
|
parse_material_displacement(self, material, c, defs, tree, displace_node, 1.0)
|
|
|
|
|
|
|
|
# No albedo color parsed, append white
|
|
|
|
if parse.const_color == None:
|
|
|
|
make_albedo_const([1.0, 1.0, 1.0, 1.0], c)
|
|
|
|
if parse.const_occlusion == None and '_OccTex' not in defs:
|
|
|
|
make_occlusion_const(1.0, c)
|
|
|
|
if parse.const_roughness == None and '_RoughTex' not in defs:
|
|
|
|
make_roughness_const(0.0, c)
|
|
|
|
if parse.const_metalness == None and '_MetTex' not in defs:
|
|
|
|
make_metalness_const(0.0, c)
|
|
|
|
# Enable texcoords
|
|
|
|
if '_Tex' not in defs:
|
|
|
|
for d in defs:
|
|
|
|
if d == '_BaseTex' or d == '_NorTex' or d == '_OccTex' or d == '_RoughTex' or d == '_MetTex' or d == '_HeightTex':
|
|
|
|
defs.append('_Tex')
|
|
|
|
break
|
2016-05-25 00:13:44 +02:00
|
|
|
|
|
|
|
def make_albedo_const(col, c):
|
2016-09-28 00:00:59 +02:00
|
|
|
const = {}
|
|
|
|
parse.const_color = const
|
|
|
|
c['bind_constants'].append(const)
|
|
|
|
const['name'] = 'baseCol'
|
|
|
|
const['vec4'] = [col[0], col[1], col[2], col[3]]
|
2016-05-25 00:13:44 +02:00
|
|
|
|
|
|
|
def make_roughness_const(f, c):
|
2016-09-28 00:00:59 +02:00
|
|
|
const = {}
|
|
|
|
parse.const_roughness = const
|
|
|
|
c['bind_constants'].append(const)
|
|
|
|
const['name'] = 'roughness'
|
|
|
|
const['float'] = f
|
2016-06-21 13:29:27 +02:00
|
|
|
|
|
|
|
def make_occlusion_const(f, c):
|
2016-09-28 00:00:59 +02:00
|
|
|
const = {}
|
|
|
|
parse.const_occlusion = const
|
|
|
|
c['bind_constants'].append(const)
|
|
|
|
const['name'] = 'occlusion'
|
|
|
|
const['float'] = f
|
2016-06-21 13:29:27 +02:00
|
|
|
|
2016-05-25 00:13:44 +02:00
|
|
|
def make_metalness_const(f, c):
|
2016-09-28 00:00:59 +02:00
|
|
|
const = {}
|
|
|
|
parse.const_metalness = const
|
|
|
|
c['bind_constants'].append(const)
|
|
|
|
const['name'] = 'metalness'
|
|
|
|
const['float'] = f
|
2016-05-18 01:34:21 +02:00
|
|
|
|
|
|
|
# Manualy set starting material point
|
|
|
|
def parse_from(self, material, c, defs, surface_node):
|
2016-09-28 00:00:59 +02:00
|
|
|
parse.const_color = None
|
|
|
|
parse.const_occlusion = None
|
|
|
|
parse.const_roughness = None
|
|
|
|
parse.const_metalness = None
|
|
|
|
|
|
|
|
tree = material.node_tree
|
|
|
|
parse_material_surface(self, material, c, defs, tree, surface_node, 1.0)
|
2016-05-18 01:34:21 +02:00
|
|
|
|
2016-10-15 12:17:33 +02:00
|
|
|
def make_texture(self, id, image_node, material, image_format='RGBA32'):
|
2016-09-28 00:00:59 +02:00
|
|
|
tex = {}
|
|
|
|
tex['name'] = id
|
|
|
|
image = image_node.image
|
|
|
|
if image != None:
|
|
|
|
if image.packed_file != None:
|
|
|
|
# Extract packed data
|
|
|
|
unpack_path = utils.get_fp() + '/build/compiled/Assets/unpacked'
|
|
|
|
if not os.path.exists(unpack_path):
|
|
|
|
os.makedirs(unpack_path)
|
|
|
|
unpack_filepath = unpack_path + '/' + image.name
|
|
|
|
# Write bytes if size is different or file does not exist yet
|
|
|
|
if os.path.isfile(unpack_filepath) == False or os.path.getsize(unpack_filepath) != image.packed_file.size:
|
|
|
|
with open(unpack_filepath, 'wb') as f:
|
|
|
|
f.write(image.packed_file.data)
|
|
|
|
# Add asset
|
|
|
|
assets.add(unpack_filepath)
|
|
|
|
else:
|
|
|
|
# Link image path to assets
|
|
|
|
assets.add(utils.safe_assetpath(image.filepath))
|
|
|
|
# Reference image name
|
|
|
|
tex['file'] = utils.extract_filename(image.filepath)
|
|
|
|
tex['file'] = utils.safe_filename(tex['file'])
|
2016-10-15 12:17:33 +02:00
|
|
|
if image_format != 'RGBA32':
|
|
|
|
tex['format'] = image_format
|
2016-09-28 00:00:59 +02:00
|
|
|
interpolation = image_node.interpolation
|
|
|
|
if bpy.data.worlds['Arm'].force_anisotropic_filtering:
|
|
|
|
interpolation = 'Smart'
|
|
|
|
if interpolation == 'Cubic': # Mipmap linear
|
|
|
|
tex['mipmap_filter'] = 'linear'
|
|
|
|
tex['generate_mipmaps'] = True
|
|
|
|
elif interpolation == 'Smart': # Mipmap anisotropic
|
|
|
|
tex['min_filter'] = 'anisotropic'
|
|
|
|
tex['mipmap_filter'] = 'linear'
|
|
|
|
tex['generate_mipmaps'] = True
|
|
|
|
if image_node.extension != 'REPEAT': # Extend or clip
|
|
|
|
tex['u_addressing'] = 'clamp'
|
|
|
|
tex['v_addressing'] = 'clamp'
|
|
|
|
else:
|
2016-09-29 22:49:22 +02:00
|
|
|
if bpy.data.worlds['Arm'].npot_texture_repeat == False and (is_pow(image.size[0]) == False or is_pow(image.size[1]) == False):
|
2016-09-28 00:00:59 +02:00
|
|
|
print('Armory Warning: ' + material.name + '/' + image.name + ' - non power of 2 texture can not use repeat mode')
|
|
|
|
tex['u_addressing'] = 'clamp'
|
|
|
|
tex['v_addressing'] = 'clamp'
|
|
|
|
|
|
|
|
if image.source == 'MOVIE': # Just append movie texture trait for now
|
|
|
|
movie_trait = {}
|
|
|
|
movie_trait['type'] = 'Script'
|
|
|
|
movie_trait['class_name'] = 'armory.trait.internal.MovieTexture'
|
|
|
|
movie_trait['parameters'] = [tex['file']]
|
|
|
|
for o in self.materialToGameObjectDict[material]:
|
|
|
|
o['traits'].append(movie_trait)
|
|
|
|
tex['source'] = 'movie'
|
|
|
|
tex['file'] = '' # MovieTexture will load the video
|
|
|
|
else:
|
|
|
|
tex['file'] = ''
|
|
|
|
return tex
|
2016-05-18 01:34:21 +02:00
|
|
|
|
2016-05-25 00:13:44 +02:00
|
|
|
def parse_value_node(node):
|
2016-09-28 00:00:59 +02:00
|
|
|
return node.outputs[0].default_value
|
2016-05-25 00:13:44 +02:00
|
|
|
|
|
|
|
def parse_float_input(tree, node, inp):
|
2016-09-28 00:00:59 +02:00
|
|
|
if inp.is_linked:
|
|
|
|
float_node = find_node_by_link(tree, node, inp)
|
|
|
|
if float_node.type == 'VALUE':
|
|
|
|
return parse_value_node(float_node)
|
|
|
|
else:
|
|
|
|
return inp.default_value
|
2016-05-25 00:13:44 +02:00
|
|
|
|
|
|
|
def parse_material_displacement(self, material, c, defs, tree, node, factor):
|
2016-09-28 00:00:59 +02:00
|
|
|
# Normal
|
|
|
|
if node.type == 'NORMAL_MAP':
|
|
|
|
normal_map_input = node.inputs[1]
|
|
|
|
parse_normal_map_socket(self, normal_map_input, material, c, defs, tree, node, factor)
|
2016-05-25 00:13:44 +02:00
|
|
|
|
|
|
|
def parse_material_surface(self, material, c, defs, tree, node, factor):
|
2016-09-28 00:00:59 +02:00
|
|
|
if node.type == 'GROUP' and node.node_tree.name.split('.', 1)[0] == 'Armory PBR':
|
|
|
|
parse_pbr_group(self, material, c, defs, tree, node, factor)
|
|
|
|
|
|
|
|
elif node.type == 'BSDF_TRANSPARENT':
|
|
|
|
parse_bsdf_transparent(self, material, c, defs, tree, node, factor)
|
|
|
|
|
|
|
|
elif node.type == 'BSDF_DIFFUSE':
|
|
|
|
parse_bsdf_diffuse(self, material, c, defs, tree, node, factor)
|
|
|
|
|
|
|
|
elif node.type == 'EMISSION':
|
|
|
|
parse_emission(self, material, c, defs, tree, node, factor)
|
|
|
|
|
|
|
|
elif node.type == 'BSDF_GLOSSY':
|
|
|
|
parse_bsdf_glossy(self, material, c, defs, tree, node, factor)
|
|
|
|
|
|
|
|
# elif node.type == 'BSDF_TRANSLUCENT':
|
|
|
|
# parse_bsdf_translucent(self, material, c, defs, tree, node, factor)
|
|
|
|
|
|
|
|
elif node.type == 'BSDF_GLASS':
|
|
|
|
parse_bsdf_glass(self, material, c, defs, tree, node, factor)
|
|
|
|
|
|
|
|
elif node.type == 'SUBSURFACE_SCATTERING':
|
|
|
|
parse_sss(self, material, c, defs, tree, node, factor)
|
|
|
|
|
|
|
|
elif node.type == 'BSDF_TOON':
|
|
|
|
parse_bsdf_toon(self, material, c, defs, tree, node, factor)
|
|
|
|
|
|
|
|
elif node.type == 'MIX_SHADER':
|
|
|
|
parse_mix_shader(self, material, c, defs, tree, node, factor)
|
|
|
|
|
|
|
|
else:
|
|
|
|
print('Armory Warning: Material node ' + node.type + ' in ' + material.name + ' is not yet supported! Please use "Armory PBR" node group for now.')
|
2016-09-08 14:08:31 +02:00
|
|
|
|
2016-05-25 00:13:44 +02:00
|
|
|
def parse_mix_shader(self, material, c, defs, tree, node, factor):
|
2016-09-28 00:00:59 +02:00
|
|
|
mixfactor = node.inputs[0].default_value * factor
|
|
|
|
if node.inputs[1].is_linked:
|
|
|
|
mixfactor0 = 1.0 - mixfactor
|
|
|
|
surface1_node = find_node_by_link(tree, node, node.inputs[1])
|
|
|
|
parse_material_surface(self, material, c, defs, tree, surface1_node, mixfactor0)
|
|
|
|
if node.inputs[2].is_linked:
|
|
|
|
surface2_node = find_node_by_link(tree, node, node.inputs[2])
|
|
|
|
parse_material_surface(self, material, c, defs, tree, surface2_node, mixfactor)
|
2016-05-18 01:34:21 +02:00
|
|
|
|
2016-05-25 00:13:44 +02:00
|
|
|
def parse_bsdf_transparent(self, material, c, defs, tree, node, factor):
|
2016-09-28 00:00:59 +02:00
|
|
|
defs.append('_AlphaTest')
|
|
|
|
|
2016-05-25 00:13:44 +02:00
|
|
|
def parse_sss(self, material, c, defs, tree, node, factor):
|
2016-09-28 00:00:59 +02:00
|
|
|
# Set stencil mask
|
|
|
|
# append '_SSS' to deferred_light
|
|
|
|
pass
|
|
|
|
|
2016-05-25 00:13:44 +02:00
|
|
|
def parse_bsdf_toon(self, material, c, defs, tree, node, factor):
|
2016-09-28 00:00:59 +02:00
|
|
|
# set pipe pass
|
|
|
|
defs.append('_Toon')
|
|
|
|
pass
|
|
|
|
|
2016-05-25 00:13:44 +02:00
|
|
|
def parse_bsdf_diffuse(self, material, c, defs, tree, node, factor):
|
2016-09-28 00:00:59 +02:00
|
|
|
# Color
|
|
|
|
base_color_input = node.inputs[0]
|
|
|
|
parse_base_color_socket(self, base_color_input, material, c, defs, tree, node, factor)
|
|
|
|
# Parse roughness but force 0.4 as minimum, set 0.0 metalness
|
|
|
|
add_metalness_const(0.0, c, factor)
|
|
|
|
roughness_input = node.inputs[1]
|
|
|
|
parse_roughness_socket(self, roughness_input, material, c, defs, tree, node, factor, minimum_val=0.4)
|
|
|
|
# Normal
|
|
|
|
normal_input = node.inputs[2]
|
|
|
|
if normal_input.is_linked:
|
|
|
|
normal_map_node = find_node_by_link(tree, node, normal_input)
|
|
|
|
if normal_map_node.type == 'NORMAL_MAP':
|
|
|
|
normal_map_input = normal_map_node.inputs[1]
|
|
|
|
parse_normal_map_socket(self, normal_map_input, material, c, defs, tree, node, factor)
|
2016-05-18 01:34:21 +02:00
|
|
|
|
2016-06-03 17:18:38 +02:00
|
|
|
def parse_emission(self, material, c, defs, tree, node, factor):
|
2016-09-28 00:00:59 +02:00
|
|
|
# Color
|
|
|
|
base_color_input = node.inputs[0]
|
|
|
|
parse_base_color_socket(self, base_color_input, material, c, defs, tree, node, factor)
|
|
|
|
# Multiply color by strength
|
|
|
|
strength_input = node.inputs[1]
|
|
|
|
strength = strength_input.default_value * 50.0 + 1.0
|
|
|
|
col = parse.const_color['vec4']
|
|
|
|
col[0] *= strength
|
|
|
|
col[1] *= strength
|
|
|
|
col[2] *= strength
|
|
|
|
parse.const_color['vec4'] = [col[0], col[1], col[2], col[3]]
|
2016-06-03 17:18:38 +02:00
|
|
|
|
2016-05-25 00:13:44 +02:00
|
|
|
def parse_bsdf_glossy(self, material, c, defs, tree, node, factor):
|
2016-09-28 00:00:59 +02:00
|
|
|
# Mix with current color
|
|
|
|
base_color_input = node.inputs[0]
|
|
|
|
parse_base_color_socket(self, base_color_input, material, c, defs, tree, node, factor)
|
|
|
|
# Parse sqrt roughness and set 1.0 metalness
|
|
|
|
add_metalness_const(1.0, c, factor)
|
|
|
|
roughness_input = node.inputs[1]
|
|
|
|
parse_roughness_socket(self, roughness_input, material, c, defs, tree, node, factor, sqrt_val=True)
|
2016-05-25 00:13:44 +02:00
|
|
|
|
2016-05-27 01:12:21 +02:00
|
|
|
def parse_bsdf_glass(self, material, c, defs, tree, node, factor):
|
2016-09-28 00:00:59 +02:00
|
|
|
# Mix with current color
|
|
|
|
base_color_input = node.inputs[0]
|
|
|
|
parse_base_color_socket(self, base_color_input, material, c, defs, tree, node, factor)
|
|
|
|
# Calculate alpha, TODO: take only glass color into account, separate getSocketColor method
|
|
|
|
col = parse.const_color['vec4']
|
|
|
|
sum = (col[0] + col[1] + col[2]) / 3
|
|
|
|
# Roughly guess color to match cycles
|
|
|
|
mincol = min(col[:3])
|
|
|
|
parse.const_color['vec4'] = [col[0] - mincol, col[1] - mincol, col[2] - mincol, 1.0 - (sum * 0.7)]
|
|
|
|
# Parse sqrt roughness and set 0.0 metalness
|
|
|
|
add_metalness_const(0.0, c, factor)
|
|
|
|
roughness_input = node.inputs[1]
|
|
|
|
parse_roughness_socket(self, roughness_input, material, c, defs, tree, node, factor, sqrt_val=True)
|
|
|
|
# Append translucent
|
|
|
|
defs.append('_Translucent')
|
2016-05-27 01:12:21 +02:00
|
|
|
|
2016-05-25 00:13:44 +02:00
|
|
|
def mix_float(f1, f2, factor=0.5):
|
2016-09-28 00:00:59 +02:00
|
|
|
return (f1 + f2) * factor
|
2016-05-25 00:13:44 +02:00
|
|
|
|
|
|
|
def mix_color_vec4(col1, col2, factor=0.5):
|
2016-09-28 00:00:59 +02:00
|
|
|
return [mix_float(col1[0], col2[0], factor), mix_float(col1[1], col2[1], factor), mix_float(col1[2], col2[2], factor), mix_float(col1[3], col2[3], factor)]
|
2016-05-18 01:34:21 +02:00
|
|
|
|
2016-05-19 22:22:41 +02:00
|
|
|
def parse_val_to_rgb(node, c, defs):
|
2016-09-28 00:00:59 +02:00
|
|
|
factor = node.inputs[0].default_value
|
|
|
|
if not node.inputs[0].is_linked: # Take ramp color
|
|
|
|
return node.color_ramp.evaluate(factor)
|
|
|
|
else: # Assume 2 colors interpolated by id for now
|
|
|
|
defs.append('_RampID')
|
|
|
|
# Link baseCol2 as color 2
|
|
|
|
const = {}
|
|
|
|
c['bind_constants'].append(const)
|
|
|
|
const['name'] = 'baseCol2'
|
|
|
|
res = node.color_ramp.elements[1].color
|
|
|
|
const['vec4'] = [res[0], res[1], res[2], res[3]]
|
|
|
|
# Return color 1
|
|
|
|
return node.color_ramp.elements[0].color
|
2016-05-19 22:22:41 +02:00
|
|
|
|
2016-09-05 17:03:20 +02:00
|
|
|
def add_base_color(c, col):
|
2016-09-28 00:00:59 +02:00
|
|
|
if parse.const_color == None:
|
|
|
|
make_albedo_const(col, c)
|
|
|
|
else:
|
|
|
|
const = parse.const_color
|
|
|
|
res = mix_color_vec4(col, const['vec4'])
|
|
|
|
const['vec4'] = [res[0], res[1], res[2], res[3]]
|
2016-05-25 00:13:44 +02:00
|
|
|
|
|
|
|
def parse_mix_rgb(self, material, c, defs, tree, node, factor):
|
2016-09-28 00:00:59 +02:00
|
|
|
# blend_type = MULTIPLY
|
|
|
|
# use_clamp = False
|
|
|
|
# Factor, col1, col2
|
|
|
|
parse_base_color_socket(self, node.inputs[1], material, c, defs, tree, node, factor)
|
|
|
|
# Assume color 2 as occlusion
|
|
|
|
parse_occlusion_socket(self, node.inputs[2], material, c, defs, tree, node, factor)
|
2016-05-25 00:13:44 +02:00
|
|
|
|
|
|
|
def add_albedo_tex(self, node, material, c, defs):
|
2016-09-28 00:00:59 +02:00
|
|
|
if '_BaseTex' not in defs:
|
|
|
|
defs.append('_BaseTex')
|
|
|
|
tex = make_texture(self, 'sbase', node, material)
|
|
|
|
c['bind_textures'].append(tex)
|
2016-05-19 22:22:41 +02:00
|
|
|
|
2016-05-25 00:13:44 +02:00
|
|
|
def add_metalness_tex(self, node, material, c, defs):
|
2016-09-28 00:00:59 +02:00
|
|
|
if '_MetTex' not in defs:
|
|
|
|
defs.append('_MetTex')
|
2016-10-15 12:17:33 +02:00
|
|
|
tex = make_texture(self, 'smetal', node, material, image_format='R8')
|
2016-09-28 00:00:59 +02:00
|
|
|
c['bind_textures'].append(tex)
|
|
|
|
if parse.const_metalness != None: # If texture is used, remove constant
|
|
|
|
c['bind_constants'].remove(parse.const_metalness)
|
2016-05-25 00:13:44 +02:00
|
|
|
|
|
|
|
def add_roughness_tex(self, node, material, c, defs):
|
2016-09-28 00:00:59 +02:00
|
|
|
if '_RoughTex' not in defs:
|
|
|
|
defs.append('_RoughTex')
|
2016-10-15 12:17:33 +02:00
|
|
|
tex = make_texture(self, 'srough', node, material, image_format='R8')
|
2016-09-28 00:00:59 +02:00
|
|
|
c['bind_textures'].append(tex)
|
|
|
|
if parse.const_roughness != None:
|
|
|
|
c['bind_constants'].remove(parse.const_roughness)
|
2016-05-25 00:13:44 +02:00
|
|
|
|
2016-08-07 23:12:14 +02:00
|
|
|
def add_roughness_strength(self, c, defs, f):
|
2016-09-28 00:00:59 +02:00
|
|
|
if '_RoughStr' not in defs:
|
|
|
|
defs.append('_RoughStr')
|
|
|
|
const = {}
|
|
|
|
c['bind_constants'].append(const)
|
|
|
|
const['name'] = 'roughnessStrength'
|
|
|
|
const['float'] = f
|
2016-08-04 22:38:56 +02:00
|
|
|
|
2016-05-25 00:13:44 +02:00
|
|
|
def add_occlusion_tex(self, node, material, c, defs):
|
2016-09-28 00:00:59 +02:00
|
|
|
if '_OccTex' not in defs:
|
|
|
|
defs.append('_OccTex')
|
2016-10-15 12:17:33 +02:00
|
|
|
tex = make_texture(self, 'socclusion', node, material, image_format='R8')
|
2016-09-28 00:00:59 +02:00
|
|
|
c['bind_textures'].append(tex)
|
2016-05-25 00:13:44 +02:00
|
|
|
|
2016-06-18 13:58:12 +02:00
|
|
|
def add_height_tex(self, node, material, c, defs):
|
2016-09-28 00:00:59 +02:00
|
|
|
if '_HeightTex' not in defs:
|
|
|
|
defs.append('_HeightTex')
|
2016-10-15 12:17:33 +02:00
|
|
|
tex = make_texture(self, 'sheight', node, material, image_format='R8')
|
2016-09-28 00:00:59 +02:00
|
|
|
c['bind_textures'].append(tex)
|
2016-06-18 13:58:12 +02:00
|
|
|
|
|
|
|
def add_height_strength(self, c, f):
|
2016-09-28 00:00:59 +02:00
|
|
|
const = {}
|
|
|
|
c['bind_constants'].append(const)
|
|
|
|
const['name'] = 'heightStrength'
|
|
|
|
const['float'] = f
|
2016-06-18 13:58:12 +02:00
|
|
|
|
2016-05-25 00:13:44 +02:00
|
|
|
def add_normal_tex(self, node, material, c, defs):
|
2016-09-28 00:00:59 +02:00
|
|
|
if '_NorTex' not in defs:
|
|
|
|
defs.append('_NorTex')
|
|
|
|
tex = make_texture(self, 'snormal', node, material)
|
|
|
|
c['bind_textures'].append(tex)
|
2016-05-25 00:13:44 +02:00
|
|
|
|
2016-08-07 23:12:14 +02:00
|
|
|
def add_normal_strength(self, c, defs, f):
|
2016-09-28 00:00:59 +02:00
|
|
|
if '_NorStr' not in defs:
|
|
|
|
defs.append('_NorStr')
|
|
|
|
const = {}
|
|
|
|
c['bind_constants'].append(const)
|
|
|
|
const['name'] = 'normalStrength'
|
|
|
|
const['float'] = f
|
2016-08-04 22:38:56 +02:00
|
|
|
|
2016-05-25 00:13:44 +02:00
|
|
|
def parse_base_color_socket(self, base_color_input, material, c, defs, tree, node, factor):
|
2016-09-28 00:00:59 +02:00
|
|
|
if base_color_input.is_linked:
|
|
|
|
color_node = find_node_by_link(tree, node, base_color_input)
|
|
|
|
if color_node.type == 'TEX_IMAGE':
|
|
|
|
add_albedo_tex(self, color_node, material, c, defs)
|
|
|
|
elif color_node.type == 'TEX_CHECKER':
|
|
|
|
pass
|
|
|
|
elif color_node.type == 'ATTRIBUTE': # Assume vcols for now
|
|
|
|
defs.append('_VCols')
|
|
|
|
elif color_node.type == 'VALTORGB':
|
|
|
|
col = parse_val_to_rgb(color_node, c, defs)
|
|
|
|
add_base_color(c, col)
|
|
|
|
elif color_node.type == 'MIX_RGB':
|
|
|
|
parse_mix_rgb(self, material, c, defs, tree, color_node, factor)
|
|
|
|
else: # Take node color
|
|
|
|
add_base_color(c, base_color_input.default_value)
|
2016-05-18 01:34:21 +02:00
|
|
|
|
2016-05-25 00:13:44 +02:00
|
|
|
def add_metalness_const(res, c, factor, minimum_val=0.0, sqrt_val=False):
|
2016-09-28 00:00:59 +02:00
|
|
|
if res < minimum_val:
|
|
|
|
res = minimum_val
|
|
|
|
if sqrt_val:
|
|
|
|
res = math.sqrt(res)
|
|
|
|
if parse.const_metalness == None:
|
|
|
|
make_metalness_const(res * factor, c)
|
|
|
|
else:
|
|
|
|
const = parse.const_metalness
|
|
|
|
const['float'] = mix_float(res, const['float'], factor=factor)
|
2016-05-25 00:13:44 +02:00
|
|
|
|
|
|
|
def parse_metalness_socket(self, metalness_input, material, c, defs, tree, node, factor, minimum_val=0.0, sqrt_val=False):
|
2016-09-28 00:00:59 +02:00
|
|
|
if metalness_input.is_linked:
|
|
|
|
metalness_node = find_node_by_link(tree, node, metalness_input)
|
|
|
|
add_metalness_tex(self, metalness_node, material, c, defs)
|
|
|
|
elif '_MetTex' not in defs:
|
|
|
|
res = metalness_input.default_value
|
|
|
|
add_metalness_const(res, c, factor, minimum_val, sqrt_val)
|
2016-05-25 00:13:44 +02:00
|
|
|
|
|
|
|
def add_roughness_const(res, c, factor, minimum_val=0.0, sqrt_val=False):
|
2016-09-28 00:00:59 +02:00
|
|
|
if res < minimum_val:
|
|
|
|
res = minimum_val
|
|
|
|
if sqrt_val:
|
|
|
|
res = math.sqrt(res)
|
|
|
|
if parse.const_roughness == None:
|
|
|
|
make_roughness_const(res * factor, c)
|
|
|
|
else:
|
|
|
|
const = parse.const_roughness
|
|
|
|
const['float'] = mix_float(res, const['float'], factor=factor)
|
|
|
|
|
2016-05-25 00:13:44 +02:00
|
|
|
def parse_roughness_socket(self, roughness_input, material, c, defs, tree, node, factor, minimum_val=0.0, sqrt_val=False):
|
2016-09-28 00:00:59 +02:00
|
|
|
if roughness_input.is_linked:
|
|
|
|
roughness_node = find_node_by_link(tree, node, roughness_input)
|
|
|
|
add_roughness_tex(self, roughness_node, material, c, defs)
|
|
|
|
elif '_RoughTex' not in defs:
|
|
|
|
res = parse_float_input(tree, node, roughness_input)
|
|
|
|
add_roughness_const(res, c, factor, minimum_val, sqrt_val)
|
2016-05-25 00:13:44 +02:00
|
|
|
|
|
|
|
def parse_normal_map_socket(self, normal_input, material, c, defs, tree, node, factor):
|
2016-09-28 00:00:59 +02:00
|
|
|
if normal_input.is_linked:
|
|
|
|
normal_node = find_node_by_link(tree, node, normal_input)
|
|
|
|
add_normal_tex(self, normal_node, material, c, defs)
|
2016-05-18 01:34:21 +02:00
|
|
|
|
2016-06-21 13:29:27 +02:00
|
|
|
def add_occlusion_const(res, c, factor):
|
2016-09-28 00:00:59 +02:00
|
|
|
if parse.const_occlusion == None:
|
|
|
|
make_occlusion_const(res * factor, c)
|
|
|
|
else:
|
|
|
|
const = parse.const_occlusion
|
|
|
|
const['float'] = mix_float(res, const['float'], factor=factor)
|
2016-06-21 13:29:27 +02:00
|
|
|
|
2016-05-25 00:13:44 +02:00
|
|
|
def parse_occlusion_socket(self, occlusion_input, material, c, defs, tree, node, factor):
|
2016-09-28 00:00:59 +02:00
|
|
|
if occlusion_input.is_linked:
|
|
|
|
occlusion_node = find_node_by_link(tree, node, occlusion_input)
|
|
|
|
add_occlusion_tex(self, occlusion_node, material, c, defs)
|
|
|
|
elif '_OccTex' not in defs:
|
|
|
|
res = occlusion_input.default_value[0] # Take only one channel
|
|
|
|
add_occlusion_const(res, c, factor)
|
2016-05-18 01:34:21 +02:00
|
|
|
|
2016-06-18 13:58:12 +02:00
|
|
|
def parse_height_socket(self, height_input, material, c, defs, tree, node, factor):
|
2016-09-28 00:00:59 +02:00
|
|
|
if height_input.is_linked:
|
|
|
|
height_node = find_node_by_link(tree, node, height_input)
|
|
|
|
add_height_tex(self, height_node, material, c, defs)
|
2016-06-18 13:58:12 +02:00
|
|
|
|
2016-05-25 00:13:44 +02:00
|
|
|
def parse_pbr_group(self, material, c, defs, tree, node, factor):
|
2016-09-28 00:00:59 +02:00
|
|
|
# Albedo Map
|
|
|
|
base_color_input = node.inputs[0]
|
|
|
|
parse_base_color_socket(self, base_color_input, material, c, defs, tree, node, factor)
|
|
|
|
# Occlusion Map
|
|
|
|
occlusion_input = node.inputs[1]
|
2016-10-09 16:06:18 +02:00
|
|
|
# occlusion_strength_input = node.inputs[2]
|
|
|
|
# occlusion_strength = occlusion_strength_input.default_value
|
2016-09-28 00:00:59 +02:00
|
|
|
parse_occlusion_socket(self, occlusion_input, material, c, defs, tree, node, factor)
|
|
|
|
# Roughness Map
|
2016-10-09 16:06:18 +02:00
|
|
|
roughness_input = node.inputs[3]
|
2016-09-28 00:00:59 +02:00
|
|
|
parse_roughness_socket(self, roughness_input, material, c, defs, tree, node, factor)
|
2016-10-09 16:06:18 +02:00
|
|
|
roughness_strength_input = node.inputs[4]
|
2016-09-28 00:00:59 +02:00
|
|
|
roughness_strength = roughness_strength_input.default_value
|
|
|
|
if roughness_strength != 1.0:
|
|
|
|
add_roughness_strength(self, c, defs, roughness_strength)
|
|
|
|
# Metalness Map
|
2016-10-09 16:06:18 +02:00
|
|
|
metalness_input = node.inputs[5]
|
2016-09-28 00:00:59 +02:00
|
|
|
parse_metalness_socket(self, metalness_input, material, c, defs, tree, node, factor)
|
|
|
|
# Normal Map
|
2016-10-09 16:06:18 +02:00
|
|
|
normal_map_input = node.inputs[6]
|
2016-09-28 00:00:59 +02:00
|
|
|
parse_normal_map_socket(self, normal_map_input, material, c, defs, tree, node, factor)
|
2016-10-09 16:06:18 +02:00
|
|
|
normal_strength_input = node.inputs[7]
|
2016-09-28 00:00:59 +02:00
|
|
|
normal_strength = normal_strength_input.default_value
|
|
|
|
if normal_strength != 1.0:
|
|
|
|
add_normal_strength(self, c, defs, normal_strength)
|
|
|
|
# Emission
|
2016-10-09 16:06:18 +02:00
|
|
|
emission_input = node.inputs[8]
|
|
|
|
emission_strength_input = node.inputs[9]
|
2016-09-28 00:00:59 +02:00
|
|
|
emission_strength = emission_strength_input.default_value
|
|
|
|
if emission_strength != 1.0: # Just multiply base color for now
|
|
|
|
if parse.const_color == None:
|
|
|
|
make_albedo_const([1.0, 1.0, 1.0, 1.0], c)
|
|
|
|
col = parse.const_color['vec4']
|
|
|
|
col[0] *= emission_strength
|
|
|
|
col[1] *= emission_strength
|
|
|
|
col[2] *= emission_strength
|
|
|
|
parse.const_color['vec4'] = [col[0], col[1], col[2], col[3]]
|
|
|
|
# Height Map
|
2016-10-09 16:06:18 +02:00
|
|
|
height_input = node.inputs[10]
|
2016-09-28 00:00:59 +02:00
|
|
|
parse_height_socket(self, height_input, material, c, defs, tree, node, factor)
|
|
|
|
# Height Strength
|
|
|
|
if height_input.is_linked:
|
2016-10-09 16:06:18 +02:00
|
|
|
height_strength_input = node.inputs[11]
|
2016-09-28 00:00:59 +02:00
|
|
|
add_height_strength(self, c, height_strength_input.default_value)
|
|
|
|
# Opacity
|
2016-10-09 16:06:18 +02:00
|
|
|
opacity_input = node.inputs[12]
|
2016-09-28 00:00:59 +02:00
|
|
|
opacity = opacity_input.default_value
|
2016-10-09 16:06:18 +02:00
|
|
|
opacity_strength_input = node.inputs[13]
|
2016-09-28 00:00:59 +02:00
|
|
|
opacity_strength = opacity_strength_input.default_value
|
|
|
|
opacity_val = opacity * opacity_strength
|
|
|
|
if opacity_val != 1.0:
|
|
|
|
if parse.const_color == None:
|
|
|
|
make_albedo_const([1.0, 1.0, 1.0, 1.0], c)
|
|
|
|
col = parse.const_color['vec4']
|
|
|
|
# sum = (col[0] + col[1] + col[2]) / 3
|
|
|
|
# mincol = min(col[:3])
|
|
|
|
# parse.const_color['vec4'] = [col[0] - mincol, col[1] - mincol, col[2] - mincol, 1.0 - (sum * 0.7)]
|
|
|
|
parse.const_color['vec4'] = [col[0], col[1], col[2], opacity_val]
|
|
|
|
# Append translucent
|
|
|
|
defs.append('_Translucent')
|