armory/blender/arm/make_world.py

278 lines
10 KiB
Python
Raw Normal View History

2016-02-08 14:58:55 +01:00
import bpy
2017-12-13 00:10:30 +01:00
import os
2016-02-08 14:58:55 +01:00
from bpy.types import NodeTree, Node, NodeSocket
from bpy.props import *
2017-03-15 12:30:14 +01:00
import arm.write_probes as write_probes
import arm.assets as assets
import arm.utils
2017-11-20 14:32:36 +01:00
import arm.node_utils as node_utils
2017-03-15 12:30:14 +01:00
import arm.log as log
2017-12-05 23:06:24 +01:00
import arm.make_state as state
2020-06-22 22:37:21 +02:00
from arm.material import make_shader
from arm.material.shader import ShaderContext, Shader
2016-02-08 14:58:55 +01:00
2019-05-24 15:21:22 +02:00
callback = None
2017-12-13 00:10:30 +01:00
def build():
worlds = []
for scene in bpy.data.scenes:
2020-06-22 22:37:21 +02:00
if scene.arm_export and scene.world is not None and scene.world not in worlds:
2017-12-13 00:10:30 +01:00
worlds.append(scene.world)
2020-06-22 22:37:21 +02:00
create_world_shaders(scene.world)
def create_world_shaders(world: bpy.types.World):
"""Creates fragment and vertex shaders for the given world."""
world_name = arm.utils.safestr(world.name)
shader_data = {'name': world_name + '_data', 'contexts': []}
shader_props = {
'name': 'world_' + world_name,
'depth_write': False,
'compare_mode': 'less',
'cull_mode': 'clockwise',
'color_attachments': ['_HDR'],
'vertex_elements': [{'name': 'pos', 'data': 'float3'}, {'name': 'nor', 'data': 'float3'}]
}
# ShaderContext expects a material, but using a world also works
shader_context = ShaderContext(world, shader_data, shader_props)
vert = shader_context.make_vert()
frag = shader_context.make_frag()
vert.add_out('vec3 normal')
vert.add_uniform('mat4 SMVP')
frag.add_include('compiled.inc')
frag.add_in('vec3 normal')
frag.add_out('vec4 fragColor')
vert.write('''normal = nor;
vec4 position = SMVP * vec4(pos, 1.0);
gl_Position = vec4(position);''')
build_node_tree(world, frag, vert)
frag.write('fragColor = vec4(0.0);')
# TODO: Rework shader export so that it doesn't depend on materials
# to prevent workaround code like this
rel_path = os.path.join(arm.utils.build_dir(), 'compiled', 'Shaders')
full_path = os.path.join(arm.utils.get_fp(), rel_path)
if not os.path.exists(full_path):
os.makedirs(full_path)
# Output: world_[world_name].[frag/vert].glsl
make_shader.write_shader(rel_path, shader_context.vert, 'vert', world_name, 'world')
make_shader.write_shader(rel_path, shader_context.frag, 'frag', world_name, 'world')
2016-02-08 14:58:55 +01:00
def build_node_tree(world: bpy.types.World, frag: Shader, vert: Shader):
"""Generates the shader code for the given world."""
world_name = arm.utils.safestr(world.name)
world.world_defs = ''
rpdat = arm.utils.get_rp()
2020-06-18 15:38:14 +02:00
if callback is not None:
2019-05-24 15:21:22 +02:00
callback()
2020-06-18 15:38:14 +02:00
# Traverse world node tree
2020-06-18 15:38:14 +02:00
is_parsed = False
if world.node_tree is not None:
2017-11-20 14:32:36 +01:00
output_node = node_utils.get_node_by_type(world.node_tree, 'OUTPUT_WORLD')
2020-06-18 15:38:14 +02:00
if output_node is not None:
2017-12-13 00:10:30 +01:00
parse_world_output(world, output_node)
2020-06-18 15:38:14 +02:00
is_parsed = True
if not is_parsed:
2017-11-13 10:19:07 +01:00
solid_mat = rpdat.arm_material_model == 'Solid'
2018-03-15 23:24:48 +01:00
if rpdat.arm_irradiance and not solid_mat:
world.world_defs += '_Irr'
2018-12-18 23:48:38 +01:00
c = world.color
2017-11-16 10:43:34 +01:00
world.arm_envtex_color = [c[0], c[1], c[2], 1.0]
2017-12-13 00:10:30 +01:00
world.arm_envtex_strength = 1.0
2020-06-18 15:38:14 +02:00
# Clear to color if no texture or sky is provided
if '_EnvSky' not in world.world_defs and '_EnvTex' not in world.world_defs:
if '_EnvImg' not in world.world_defs:
world.world_defs += '_EnvCol'
# Irradiance json file name
world.arm_envtex_name = world_name
world.arm_envtex_irr_name = world_name
write_probes.write_color_irradiance(world_name, world.arm_envtex_color)
2017-11-10 15:46:05 +01:00
# film_transparent
2020-06-18 15:38:14 +02:00
if bpy.context.scene is not None and hasattr(bpy.context.scene.render, 'film_transparent') and bpy.context.scene.render.film_transparent:
world.world_defs += '_EnvTransp'
world.world_defs += '_EnvCol'
2017-11-10 15:46:05 +01:00
# Clouds enabled
2017-08-21 20:16:06 +02:00
if rpdat.arm_clouds:
world.world_defs += '_EnvClouds'
if '_EnvSky' in world.world_defs or '_EnvTex' in world.world_defs or '_EnvImg' in world.world_defs or '_EnvClouds' in world.world_defs:
world.world_defs += '_EnvStr'
2016-11-08 15:14:56 +01:00
2017-12-13 00:10:30 +01:00
def parse_world_output(world, node):
if node.inputs[0].is_linked:
2017-11-20 14:32:36 +01:00
surface_node = node_utils.find_node_by_link(world.node_tree, node, node.inputs[0])
2017-12-13 00:10:30 +01:00
parse_surface(world, surface_node)
2020-06-18 15:38:14 +02:00
2017-12-13 00:10:30 +01:00
def parse_surface(world, node):
2017-01-28 20:00:04 +01:00
wrd = bpy.data.worlds['Arm']
rpdat = arm.utils.get_rp()
2017-11-13 10:19:07 +01:00
solid_mat = rpdat.arm_material_model == 'Solid'
2020-06-18 15:38:14 +02:00
# Extract environment strength
if node.type == 'BACKGROUND':
2020-06-18 15:38:14 +02:00
2017-01-28 20:00:04 +01:00
# Append irradiance define
2018-03-15 23:24:48 +01:00
if rpdat.arm_irradiance and not solid_mat:
2017-09-06 13:28:59 +02:00
wrd.world_defs += '_Irr'
2017-01-28 20:00:04 +01:00
2017-12-13 00:10:30 +01:00
world.arm_envtex_color = node.inputs[0].default_value
world.arm_envtex_strength = node.inputs[1].default_value
# Strength
if node.inputs[0].is_linked:
2017-11-20 14:32:36 +01:00
color_node = node_utils.find_node_by_link(world.node_tree, node, node.inputs[0])
2017-12-13 00:10:30 +01:00
parse_color(world, color_node)
2020-06-18 15:38:14 +02:00
def parse_color(world, node):
2016-11-05 20:57:04 +01:00
wrd = bpy.data.worlds['Arm']
rpdat = arm.utils.get_rp()
2017-10-01 19:42:47 +02:00
mobile_mat = rpdat.arm_material_model == 'Mobile' or rpdat.arm_material_model == 'Solid'
2016-11-05 20:57:04 +01:00
# Env map included
2020-06-18 15:38:14 +02:00
if node.type == 'TEX_ENVIRONMENT' and node.image is not None:
2017-01-04 00:13:52 +01:00
image = node.image
filepath = image.filepath
2020-06-18 15:38:14 +02:00
if image.packed_file is 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
# Reference image name
2017-12-13 00:10:30 +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':
2017-12-13 00:10:30 +01:00
tex_file = base[0] + '.hdr'
target_format = 'HDR'
else:
2017-12-13 00:10:30 +01:00
tex_file = base[0] + '.jpg'
target_format = 'JPEG'
2020-06-18 15:38:14 +02:00
if image.packed_file is not 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)
2017-12-13 00:10:30 +01:00
unpack_filepath = unpack_path + '/' + tex_file
filepath = unpack_filepath
if do_convert:
if not os.path.isfile(unpack_filepath):
2018-03-26 16:28:27 +02:00
arm.utils.unpack_image(image, unpack_filepath, file_format=target_format)
2020-06-18 15:38:14 +02:00
elif not os.path.isfile(unpack_filepath) or os.path.getsize(unpack_filepath) != image.packed_file.size:
with open(unpack_filepath, 'wb') as f:
f.write(image.packed_file.data)
2020-06-18 15:38:14 +02:00
assets.add(unpack_filepath)
else:
if do_convert:
2018-03-26 16:28:27 +02:00
unpack_path = arm.utils.get_fp_build() + '/compiled/Assets/unpacked'
if not os.path.exists(unpack_path):
os.makedirs(unpack_path)
converted_path = unpack_path + '/' + tex_file
filepath = converted_path
# TODO: delete cache when file changes
if not os.path.isfile(converted_path):
2018-03-26 16:28:27 +02:00
arm.utils.convert_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-12-13 00:10:30 +01:00
world.arm_envtex_name = tex_file
world.arm_envtex_irr_name = tex_file.rsplit('.', 1)[0]
disable_hdr = target_format == 'JPEG'
2020-06-18 15:38:14 +02:00
2017-08-21 12:17:55 +02:00
mip_count = world.arm_envtex_num_mips
2018-03-15 23:24:48 +01:00
mip_count = write_probes.write_probes(filepath, disable_hdr, mip_count, arm_radiance=rpdat.arm_radiance)
2020-06-18 15:38:14 +02:00
2017-08-21 12:17:55 +02:00
world.arm_envtex_num_mips = mip_count
2020-06-18 15:38:14 +02:00
# Append envtex define
world.world_defs += '_EnvTex'
# Append LDR define
if disable_hdr:
world.world_defs += '_EnvLDR'
# Append radiance define
2018-03-15 23:24:48 +01:00
if rpdat.arm_irradiance and rpdat.arm_radiance and not mobile_mat:
2017-09-06 13:28:59 +02:00
wrd.world_defs += '_Rad'
2016-11-03 19:07:16 +01:00
# Static image background
2020-06-18 15:38:14 +02:00
elif node.type == 'TEX_IMAGE':
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-12-13 00:10:30 +01:00
tex_file = arm.utils.extract_filename(image.filepath)
world.arm_envtex_name = tex_file
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
2017-12-13 00:10:30 +01:00
world.arm_envtex_strength *= 0.1
2020-06-18 15:38:14 +02:00
world.world_defs += '_EnvSky'
2017-11-27 14:29:21 +01:00
assets.add_khafile_def('arm_hosek')
2020-06-18 15:38:14 +02:00
2017-12-13 00:10:30 +01:00
world.arm_envtex_sun_direction = [node.sun_direction[0], node.sun_direction[1], node.sun_direction[2]]
2017-08-21 12:17:55 +02:00
world.arm_envtex_turbidity = node.turbidity
world.arm_envtex_ground_albedo = node.ground_albedo
2020-06-18 15:38:14 +02:00
# 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
2018-12-19 13:33:17 +01:00
if rpdat.arm_radiance and rpdat.arm_irradiance and not mobile_mat:
2017-09-06 13:28:59 +02:00
wrd.world_defs += '_Rad'
2017-10-25 19:48:45 +02:00
hosek_path = 'armory/Assets/hosek/'
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
2018-08-16 23:07:50 +02:00
assets.add(sdk_path + '/' + hosek_path + 'hosek_radiance.hdr')
for i in range(0, 8):
2018-08-16 23:07:50 +02:00
assets.add(sdk_path + '/' + hosek_path + 'hosek_radiance_' + str(i) + '.hdr')
2020-06-18 15:38:14 +02:00
2017-08-21 12:17:55 +02:00
world.arm_envtex_name = 'hosek'
world.arm_envtex_num_mips = 8