Skip to content

Commit 5b2c382

Browse files
authored
Merge pull request #3291 from moisesjpelaez/background_world
World's background color strength fix
2 parents 2e26e33 + 2374c95 commit 5b2c382

6 files changed

Lines changed: 45 additions & 22 deletions

File tree

armory/Sources/iron/RenderPath.hx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,9 @@ class RenderPath {
295295
public function clearTarget(colorFlag: Null<Int> = null, depthFlag: Null<Float> = null) {
296296
if (colorFlag == -1) { // -1 == 0xffffffff
297297
if (Scene.active.world != null) {
298-
colorFlag = Scene.active.world.raw.background_color;
298+
var col = Scene.active.world.raw.background_color;
299+
var strength = Scene.active.world.probe != null ? Scene.active.world.probe.raw.strength : 1.0;
300+
colorFlag = Color.fromFloats(((col >> 16) & 0xff) / 255 * strength, ((col >> 8) & 0xff) / 255 * strength, (col & 0xff) / 255 * strength);
299301
}
300302
else if (Scene.active.camera != null) {
301303
var cc = Scene.active.camera.data.raw.clear_color;

armory/Sources/iron/object/Uniforms.hx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,11 @@ class Uniforms {
485485
v = helpVec;
486486
}
487487
case "_backgroundCol": {
488-
if (camera.data.raw.clear_color != null) helpVec.set(camera.data.raw.clear_color[0], camera.data.raw.clear_color[1], camera.data.raw.clear_color[2]);
488+
if (Scene.active.world != null) {
489+
var col = Scene.active.world.raw.background_color;
490+
helpVec.set(((col >> 16) & 0xff) / 255, ((col >> 8) & 0xff) / 255, (col & 0xff) / 255);
491+
}
492+
else if (camera.data.raw.clear_color != null) helpVec.set(camera.data.raw.clear_color[0], camera.data.raw.clear_color[1], camera.data.raw.clear_color[2]);
489493
v = helpVec;
490494
}
491495
case "_hosekSunDirection": {

armory/blender/arm/exporter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3645,7 +3645,7 @@ def post_export_world(world: bpy.types.World, out_world: Dict):
36453645
strength = world.arm_envtex_strength
36463646

36473647
mobile_mat = rpdat.arm_material_model in ('Mobile', 'Solid')
3648-
if mobile_mat:
3648+
if mobile_mat or '_EnvCol' in world.world_defs:
36493649
arm_radiance = False
36503650

36513651
out_probe = {'name': arm.utils.asset_name(world) if world.library else world.name}

armory/blender/arm/make_renderpath.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def add_world_defs():
154154
wrd.world_defs += '_Clusters'
155155
assets.add_khafile_def('arm_clusters')
156156

157-
if '_Rad' in wrd.world_defs and '_Brdf' not in wrd.world_defs:
157+
if ('_Rad' in wrd.world_defs or ('_EnvCol' in wrd.world_defs and rpdat.arm_material_model == 'Full')) and '_Brdf' not in wrd.world_defs:
158158
wrd.world_defs += '_Brdf'
159159
assets.add_khafile_def("arm_brdf")
160160

armory/blender/arm/make_world.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,29 @@
3131
shader_datas = []
3232

3333

34+
def add_world_def(world: bpy.types.World, define: str):
35+
if define not in world.world_defs:
36+
world.world_defs += define
37+
38+
39+
def add_global_def(define: str):
40+
wrd = bpy.data.worlds['Arm']
41+
if define not in wrd.world_defs:
42+
wrd.world_defs += define
43+
44+
45+
def add_irradiance_defs(world: bpy.types.World, rpdat):
46+
if rpdat.arm_irradiance and rpdat.arm_material_model != 'Solid':
47+
add_world_def(world, '_Irr')
48+
add_global_def('_Irr')
49+
assets.add_khafile_def("arm_irradiance")
50+
51+
52+
def mark_color_environment(world: bpy.types.World):
53+
add_world_def(world, '_EnvCol')
54+
add_global_def('_EnvCol')
55+
56+
3457
def build():
3558
"""Builds world shaders for all exported worlds."""
3659
global shader_datas
@@ -174,7 +197,7 @@ def build_node_tree(world: bpy.types.World, frag: Shader, vert: Shader, con: Sha
174197

175198
# film_transparent, do not render
176199
if bpy.context.scene is not None and bpy.context.scene.render.film_transparent:
177-
world.world_defs += '_EnvCol'
200+
mark_color_environment(world)
178201
frag.add_uniform('vec3 backgroundCol', link='_backgroundCol')
179202
frag.write('fragColor.rgb = backgroundCol;')
180203
return
@@ -195,14 +218,11 @@ def build_node_tree(world: bpy.types.World, frag: Shader, vert: Shader, con: Sha
195218

196219
# No world nodes/no output node, use background color
197220
if not is_parsed:
198-
solid_mat = rpdat.arm_material_model == 'Solid'
199-
if rpdat.arm_irradiance and not solid_mat:
200-
world.world_defs += '_Irr'
201-
assets.add_khafile_def("arm_irradiance")
221+
add_irradiance_defs(world, rpdat)
202222
col = world.color
203223
world.arm_envtex_color = [col[0], col[1], col[2], 1.0]
204224
world.arm_envtex_strength = 1.0
205-
world.world_defs += '_EnvCol'
225+
mark_color_environment(world)
206226

207227
# Clouds enabled
208228
if rpdat.arm_clouds and world.arm_use_clouds:
@@ -212,12 +232,13 @@ def build_node_tree(world: bpy.types.World, frag: Shader, vert: Shader, con: Sha
212232
wrd.world_defs += '_EnvClouds'
213233
frag_write_clouds(world, frag)
214234

215-
if '_EnvSky' in world.world_defs or '_EnvTex' in world.world_defs or '_EnvImg' in world.world_defs or '_EnvClouds' in world.world_defs:
235+
if '_EnvSky' in world.world_defs or '_EnvTex' in world.world_defs or '_EnvImg' in world.world_defs or '_EnvCol' in world.world_defs or '_EnvClouds' in world.world_defs:
216236
frag.add_uniform('float envmapStrength', link='_envmapStrength')
217237

218238
# Clear background color
219239
if '_EnvCol' in world.world_defs:
220-
frag.write('fragColor.rgb = backgroundCol;')
240+
frag.add_uniform('vec3 backgroundCol', link='_backgroundCol')
241+
frag.write('fragColor.rgb = backgroundCol * envmapStrength;')
221242

222243
elif '_EnvTex' in world.world_defs and '_EnvLDR' in world.world_defs:
223244
frag.write('fragColor.rgb = pow(fragColor.rgb, vec3(2.2));')
@@ -276,14 +297,11 @@ def parse_world_output(world: bpy.types.World, node_output: bpy.types.Node, frag
276297

277298

278299
def parse_surface(world: bpy.types.World, node_surface: bpy.types.Node, frag: Shader):
279-
wrd = bpy.data.worlds['Arm']
280300
rpdat = arm.utils.get_rp()
281-
solid_mat = rpdat.arm_material_model == 'Solid'
282301

283302
if node_surface.type in ('BACKGROUND', 'EMISSION'):
284303
# Append irradiance define
285-
if rpdat.arm_irradiance and not solid_mat:
286-
wrd.world_defs += '_Irr'
304+
add_irradiance_defs(world, rpdat)
287305

288306
# Extract environment strength
289307
# Todo: follow/parse strength input
@@ -294,11 +312,8 @@ def parse_surface(world: bpy.types.World, node_surface: bpy.types.Node, frag: Sh
294312
frag.write(f'fragColor.rgb = {out};')
295313

296314
if not node_surface.inputs[0].is_linked:
297-
solid_mat = rpdat.arm_material_model == 'Solid'
298-
if rpdat.arm_irradiance and not solid_mat:
299-
world.world_defs += '_Irr'
300315
world.arm_envtex_color = node_surface.inputs[0].default_value
301-
world.arm_envtex_strength = 1.0
316+
mark_color_environment(world)
302317

303318
else:
304319
log.warn(f'World node type {node_surface.type} must not be connected to the world output node!')

armory/blender/arm/write_probes.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,10 @@ def write_sky_irradiance(base_name):
439439

440440
def write_color_irradiance(base_name, col):
441441
"""Constant color irradiance"""
442-
# Adjust to Cycles
443-
irradiance_floats = [col[0] * 1.13, col[1] * 1.13, col[2] * 1.13]
442+
# Match shIrradiance()'s c4 factor so a constant color environment
443+
# decodes back to the same diffuse radiance at strength 1.
444+
sh_l00 = 1.0 / 0.886227
445+
irradiance_floats = [col[0] * sh_l00, col[1] * sh_l00, col[2] * sh_l00]
444446
for i in range(0, 24):
445447
irradiance_floats.append(0.0)
446448

0 commit comments

Comments
 (0)