3131shader_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+
3457def 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
278299def 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!' )
0 commit comments