armory/blender/arm/make_world.py

370 lines
13 KiB
Python
Raw Normal View History

2016-02-08 14:58:55 +01:00
import bpy
from bpy.types import NodeTree, Node, NodeSocket
from bpy.props import *
import os
import json
2017-03-15 12:30:14 +01:00
import arm.write_probes as write_probes
import arm.assets as assets
import arm.utils
import arm.nodes as nodes
import arm.log as log
2016-02-08 14:58:55 +01:00
2016-10-19 13:28:06 +02:00
def build_node_trees(active_worlds):
s = bpy.data.filepath.split(os.path.sep)
s.pop()
fp = os.path.sep.join(s)
os.chdir(fp)
# Make sure Assets dir exists
2017-05-23 01:03:44 +02:00
if not os.path.exists(arm.utils.build_dir() + '/compiled/Assets/materials'):
os.makedirs(arm.utils.build_dir() + '/compiled/Assets/materials')
# Export world nodes
world_outputs = []
for world in active_worlds:
2016-10-19 13:28:06 +02:00
output = build_node_tree(world)
world_outputs.append(output)
return world_outputs
2016-02-08 14:58:55 +01:00
2016-10-19 13:28:06 +02:00
def build_node_tree(world):
output = {}
dat = {}
output['material_datas'] = [dat]
2017-05-13 17:17:43 +02:00
wname = arm.utils.safestr(world.name)
dat['name'] = wname + '_material'
context = {}
dat['contexts'] = [context]
2016-10-17 00:02:51 +02:00
context['name'] = 'world'
context['bind_constants'] = []
context['bind_textures'] = []
2017-08-30 09:19:10 +02:00
wrd = bpy.data.worlds['Arm']
wrd.world_defs = ''
# Traverse world node tree
2017-08-30 09:19:10 +02:00
parsed = False
if world.node_tree != None:
output_node = nodes.get_node_by_type(world.node_tree, 'OUTPUT_WORLD')
if output_node != None:
parse_world_output(world, output_node, context)
parsed = True
if parsed == False:
2017-09-06 13:28:59 +02:00
if wrd.arm_irradiance and wrd.arm_rplist[wrd.arm_rplist_index].arm_material_model != 'Restricted':
2017-08-30 09:19:10 +02:00
wrd.world_defs += '_Irr'
envmap_strength_const = {}
envmap_strength_const['name'] = 'envmapStrength'
envmap_strength_const['float'] = 1.0
context['bind_constants'].append(envmap_strength_const)
world.arm_envtex_color = [0.051, 0.051, 0.051, 1.0]
world.arm_envtex_strength = envmap_strength_const['float']
# Clear to color if no texture or sky is provided
if '_EnvSky' not in wrd.world_defs and '_EnvTex' not in wrd.world_defs:
2016-11-03 19:07:16 +01:00
if '_EnvImg' not in wrd.world_defs:
wrd.world_defs += '_EnvCol'
# Irradiance json file name
2017-08-21 12:17:55 +02:00
world.arm_envtex_name = wname
world.arm_envtex_irr_name = wname
write_probes.write_color_irradiance(wname, world.arm_envtex_color)
# Clouds enabled
2017-08-21 20:16:06 +02:00
rpdat = arm.utils.get_rp()
if rpdat.arm_clouds:
wrd.world_defs += '_EnvClouds'
# Percentage closer soft shadows
2017-08-21 20:16:06 +02:00
if rpdat.arm_pcss_state == 'On':
wrd.world_defs += '_PCSS'
2017-03-15 12:30:14 +01:00
sdk_path = arm.utils.get_sdk_path()
assets.add(sdk_path + 'armory/Assets/noise64.png')
2016-09-29 22:49:22 +02:00
assets.add_embedded_data('noise64.png')
2017-01-17 14:48:47 +01:00
# Screen-space ray-traced shadows
2017-08-21 20:16:06 +02:00
if rpdat.arm_ssrs:
2017-01-17 14:48:47 +01:00
wrd.world_defs += '_SSRS'
2017-08-21 12:17:55 +02:00
if wrd.arm_two_sided_area_lamp:
2017-05-13 00:05:50 +02:00
wrd.world_defs += '_TwoSidedAreaLamp'
2017-08-21 15:36:21 +02:00
# Store contexts
2017-08-21 20:16:06 +02:00
if rpdat.rp_hdr == False:
2017-08-21 15:36:21 +02:00
wrd.world_defs += '_LDR'
2016-10-09 16:06:18 +02:00
# Alternative models
2017-08-21 20:16:06 +02:00
if rpdat.arm_material_model == 'Cycles':
2017-05-06 00:22:15 +02:00
wrd.world_defs += '_Cycles'
2017-02-25 17:13:22 +01:00
# TODO: Lamp texture test..
2017-08-21 12:17:55 +02:00
if wrd.arm_lamp_texture != '':
2017-08-30 09:19:10 +02:00
wrd.world_defs += '_LampColTex'
2017-02-25 17:13:22 +01:00
2017-08-21 12:17:55 +02:00
if wrd.arm_lamp_ies_texture != '':
2017-08-30 09:19:10 +02:00
wrd.world_defs += '_LampIES'
2017-08-17 14:37:04 +02:00
assets.add_embedded_data('iestexture.png')
2017-02-22 15:50:19 +01:00
voxelgi = False
2017-08-22 10:04:13 +02:00
voxelao = False
2017-08-21 20:16:06 +02:00
if rpdat.rp_shadowmap == 'None':
2017-08-19 12:10:06 +02:00
wrd.world_defs += '_NoShadows'
assets.add_khafile_def('arm_no_shadows')
2017-08-21 20:16:06 +02:00
if rpdat.rp_voxelgi:
2017-08-19 12:10:06 +02:00
voxelgi = True
2017-08-22 10:04:13 +02:00
elif rpdat.rp_voxelao:
voxelao = True
2017-08-21 20:16:06 +02:00
if rpdat.rp_dfrs:
2017-08-19 12:10:06 +02:00
wrd.world_defs += '_DFRS'
assets.add_khafile_def('arm_sdf')
2017-08-21 20:16:06 +02:00
if rpdat.rp_dfao:
2017-08-19 12:10:06 +02:00
wrd.world_defs += '_DFAO'
assets.add_khafile_def('arm_sdf')
2017-08-21 20:16:06 +02:00
if rpdat.rp_dfgi:
2017-08-19 12:10:06 +02:00
wrd.world_defs += '_DFGI'
assets.add_khafile_def('arm_sdf')
wrd.world_defs += '_Rad' # Always do radiance for gi
wrd.world_defs += '_Irr'
2017-02-22 15:50:19 +01:00
if voxelgi:
assets.add_khafile_def('arm_voxelgi')
2017-08-21 20:30:39 +02:00
if rpdat.arm_voxelgi_revoxelize:
2017-05-17 23:02:36 +02:00
assets.add_khafile_def('arm_voxelgi_revox')
2017-08-21 20:30:39 +02:00
if rpdat.arm_voxelgi_camera:
2017-08-03 14:01:04 +02:00
wrd.world_defs += '_VoxelGICam'
2017-08-21 20:30:39 +02:00
if rpdat.arm_voxelgi_shadows:
2017-08-13 20:28:06 +02:00
wrd.world_defs += '_VoxelGIDirect'
wrd.world_defs += '_VoxelGIShadow'
2017-08-21 20:30:39 +02:00
if rpdat.arm_voxelgi_refraction:
2017-08-13 20:28:06 +02:00
wrd.world_defs += '_VoxelGIDirect'
wrd.world_defs += '_VoxelGIRefract'
2017-02-22 15:50:19 +01:00
wrd.world_defs += '_VoxelGI'
wrd.world_defs += '_Rad' # Always do radiance for voxels
wrd.world_defs += '_Irr'
2017-08-22 10:04:13 +02:00
elif voxelao:
assets.add_khafile_def('arm_voxelgi')
if rpdat.arm_voxelgi_revoxelize:
assets.add_khafile_def('arm_voxelgi_revox')
wrd.world_defs += '_VoxelAO'
wrd.world_defs += '_Rad'
wrd.world_defs += '_Irr'
2017-04-19 23:11:02 +02:00
if arm.utils.get_gapi().startswith('direct3d'): # Flip Y axis in drawQuad command
wrd.world_defs += '_InvY'
2016-11-08 15:14:56 +01:00
# Area lamps
for lamp in bpy.data.lamps:
if lamp.type == 'AREA':
wrd.world_defs += '_PolyLight'
break
# Data will be written after render path has been processed to gather all defines
return output
def write_output(output):
# Add datas to khafile
2016-10-17 00:02:51 +02:00
dir_name = 'world'
# Append world defs
wrd = bpy.data.worlds['Arm']
2017-08-21 15:36:21 +02:00
data_name = 'world' + wrd.world_defs
# Reference correct shader context
dat = output['material_datas'][0]
dat['shader'] = data_name + '/' + data_name
2016-10-17 00:02:51 +02:00
assets.add_shader2(dir_name, data_name)
# Write material json
2017-05-23 01:03:44 +02:00
path = arm.utils.build_dir() + '/compiled/Assets/materials/'
asset_path = path + dat['name'] + '.arm'
2017-03-15 12:30:14 +01:00
arm.utils.write_arm(asset_path, output)
assets.add(asset_path)
2016-06-30 13:22:05 +02:00
def parse_world_output(world, node, context):
if node.inputs[0].is_linked:
2016-10-19 13:28:06 +02:00
surface_node = nodes.find_node_by_link(world.node_tree, node, node.inputs[0])
parse_surface(world, surface_node, context)
def parse_surface(world, node, context):
2017-01-28 20:00:04 +01:00
wrd = bpy.data.worlds['Arm']
2017-09-06 13:28:59 +02:00
restricted = wrd.arm_rplist[wrd.arm_rplist_index].arm_material_model == 'Restricted'
2017-01-28 20:00:04 +01:00
# Extract environment strength
if node.type == 'BACKGROUND':
2017-01-28 20:00:04 +01:00
# Append irradiance define
2017-09-06 13:28:59 +02:00
if wrd.arm_irradiance and not restricted:
wrd.world_defs += '_Irr'
2017-01-28 20:00:04 +01:00
# Strength
envmap_strength_const = {}
envmap_strength_const['name'] = 'envmapStrength'
envmap_strength_const['float'] = node.inputs[1].default_value
# Always append for now, even though envmapStrength is not always needed
context['bind_constants'].append(envmap_strength_const)
if node.inputs[0].is_linked:
2016-10-19 13:28:06 +02:00
color_node = nodes.find_node_by_link(world.node_tree, node, node.inputs[0])
parse_color(world, color_node, context, envmap_strength_const)
# Cache results
2017-08-21 12:17:55 +02:00
world.arm_envtex_color = node.inputs[0].default_value
world.arm_envtex_strength = envmap_strength_const['float']
def parse_color(world, node, context, envmap_strength_const):
2016-11-05 20:57:04 +01:00
wrd = bpy.data.worlds['Arm']
2017-09-06 13:28:59 +02:00
restricted = wrd.arm_rplist[wrd.arm_rplist_index].arm_material_model == 'Restricted'
2016-11-05 20:57:04 +01:00
# Env map included
2016-11-24 23:24:55 +01:00
if node.type == 'TEX_ENVIRONMENT' and node.image != None:
2017-01-04 00:13:52 +01:00
image = node.image
filepath = image.filepath
2017-05-13 17:17:43 +02:00
if image.packed_file == None and not os.path.isfile(arm.utils.asset_path(filepath)):
2017-01-04 00:13:52 +01:00
log.warn(world.name + ' - unable to open ' + image.filepath)
return
tex = {}
context['bind_textures'].append(tex)
tex['name'] = 'envmap'
2016-11-09 15:36:32 +01:00
tex['u_addressing'] = 'clamp'
tex['v_addressing'] = 'clamp'
# Reference image name
2017-03-15 12:30:14 +01:00
tex['file'] = arm.utils.extract_filename(image.filepath)
base = tex['file'].rsplit('.', 1)
ext = base[1].lower()
if ext == 'hdr':
target_format = 'HDR'
else:
target_format = 'JPEG'
do_convert = ext != 'hdr' and ext != 'jpg'
if do_convert:
if ext == 'exr':
tex['file'] = base[0] + '.hdr'
target_format = 'HDR'
else:
tex['file'] = base[0] + '.jpg'
target_format = 'JPEG'
if image.packed_file != None:
# Extract packed data
2017-05-23 01:03:44 +02:00
unpack_path = arm.utils.get_fp_build() + '/compiled/Assets/unpacked'
if not os.path.exists(unpack_path):
os.makedirs(unpack_path)
unpack_filepath = unpack_path + '/' + tex['file']
filepath = unpack_filepath
if do_convert:
if not os.path.isfile(unpack_filepath):
2017-03-15 12:30:14 +01:00
arm.utils.write_image(image, unpack_filepath, file_format=target_format)
elif 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)
assets.add(unpack_filepath)
else:
if do_convert:
2017-05-23 01:03:44 +02:00
converted_path = arm.utils.get_fp_build() + '/compiled/Assets/unpacked/' + tex['file']
filepath = converted_path
# TODO: delete cache when file changes
if not os.path.isfile(converted_path):
2017-03-15 12:30:14 +01:00
arm.utils.write_image(image, converted_path, file_format=target_format)
assets.add(converted_path)
else:
# Link image path to assets
2017-05-13 17:17:43 +02:00
assets.add(arm.utils.asset_path(image.filepath))
# Generate prefiltered envmaps
2017-08-21 12:17:55 +02:00
world.arm_envtex_name = tex['file']
world.arm_envtex_irr_name = tex['file'].rsplit('.', 1)[0]
disable_hdr = target_format == 'JPEG'
2017-08-21 12:17:55 +02:00
mip_count = world.arm_envtex_num_mips
mip_count = write_probes.write_probes(filepath, disable_hdr, mip_count, arm_radiance=wrd.arm_radiance)
2017-08-21 12:17:55 +02:00
world.arm_envtex_num_mips = mip_count
# Append envtex define
2017-09-06 13:28:59 +02:00
wrd.world_defs += '_EnvTex'
# Append LDR define
if disable_hdr:
2017-09-06 13:28:59 +02:00
wrd.world_defs += '_EnvLDR'
# Append radiance define
2017-09-06 13:28:59 +02:00
if wrd.arm_irradiance and wrd.arm_radiance and not restricted:
wrd.world_defs += '_Rad'
2016-11-03 19:07:16 +01:00
# Static image background
elif node.type == 'TEX_IMAGE':
2017-09-06 13:28:59 +02:00
wrd.world_defs += '_EnvImg'
tex = {}
context['bind_textures'].append(tex)
tex['name'] = 'envmap'
2016-11-03 19:07:16 +01:00
# No repeat for now
tex['u_addressing'] = 'clamp'
tex['v_addressing'] = 'clamp'
2016-11-03 19:07:16 +01:00
image = node.image
filepath = image.filepath
if image.packed_file != None:
# Extract packed data
2017-05-23 01:03:44 +02:00
filepath = arm.utils.build_dir() + '/compiled/Assets/unpacked'
2017-03-15 12:30:14 +01:00
unpack_path = arm.utils.get_fp() + filepath
2016-11-03 19:07:16 +01:00
if not os.path.exists(unpack_path):
os.makedirs(unpack_path)
unpack_filepath = unpack_path + '/' + image.name
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)
assets.add(unpack_filepath)
else:
# Link image path to assets
2017-05-13 17:17:43 +02:00
assets.add(arm.utils.asset_path(image.filepath))
2016-11-03 19:07:16 +01:00
# Reference image name
2017-03-15 12:30:14 +01:00
tex['file'] = arm.utils.extract_filename(image.filepath)
2016-11-03 19:07:16 +01:00
# Append sky define
elif node.type == 'TEX_SKY':
2016-12-21 00:51:04 +01:00
# Match to cycles
envmap_strength_const['float'] *= 0.1
2016-10-12 17:52:27 +02:00
2017-09-06 13:28:59 +02:00
wrd.world_defs += '_EnvSky'
# Append sky properties to material
const = {}
const['name'] = 'sunDirection'
sun_direction = [node.sun_direction[0], node.sun_direction[1], node.sun_direction[2]]
sun_direction[1] *= -1 # Fix Y orientation
const['vec3'] = list(sun_direction)
context['bind_constants'].append(const)
2017-08-21 12:17:55 +02:00
world.arm_envtex_sun_direction = sun_direction
world.arm_envtex_turbidity = node.turbidity
world.arm_envtex_ground_albedo = node.ground_albedo
# Irradiance json file name
2017-05-13 17:17:43 +02:00
wname = arm.utils.safestr(world.name)
2017-08-21 12:17:55 +02:00
world.arm_envtex_irr_name = wname
2017-05-13 17:17:43 +02:00
write_probes.write_sky_irradiance(wname)
# Radiance
2017-09-06 13:28:59 +02:00
if wrd.arm_radiance_sky and wrd.arm_radiance and wrd.arm_irradiance and not restricted:
wrd.world_defs += '_Rad'
2017-08-21 12:17:55 +02:00
if wrd.arm_radiance_sky_type == 'Hosek':
2016-11-05 20:57:04 +01:00
hosek_path = 'armory/Assets/hosek/'
else:
hosek_path = 'armory/Assets/hosek_fake/'
2017-03-15 12:30:14 +01:00
sdk_path = arm.utils.get_sdk_path()
2016-10-12 17:52:27 +02:00
# Use fake maps for now
2016-11-05 20:57:04 +01:00
assets.add(sdk_path + hosek_path + 'hosek_radiance.hdr')
for i in range(0, 8):
2016-11-05 20:57:04 +01:00
assets.add(sdk_path + hosek_path + 'hosek_radiance_' + str(i) + '.hdr')
2017-08-21 12:17:55 +02:00
world.arm_envtex_name = 'hosek'
world.arm_envtex_num_mips = 8