def frag_write_clouds(world: bpy.types.World, frag: Shader): """References: GPU PRO 7 - Real-time Volumetric Cloudscapes https://www.guerrilla-games.com/read/the-real-time-volumetric-cloudscapes-of-horizon-zero-dawn https://github.com/sebh/TileableVolumeNoise """ frag.add_uniform('sampler3D scloudsBase', link='$clouds_base.raw') frag.add_uniform('sampler3D scloudsDetail', link='$clouds_detail.raw') frag.add_uniform('sampler2D scloudsMap', link='$clouds_map.png') frag.add_uniform('float time', link='_time') frag.add_const('float', 'cloudsLower', str(round(world.arm_clouds_lower * 100) / 100)) frag.add_const('float', 'cloudsUpper', str(round(world.arm_clouds_upper * 100) / 100)) frag.add_const( 'vec2', 'cloudsWind', 'vec2(' + str(round(world.arm_clouds_wind[0] * 100) / 100) + ',' + str(round(world.arm_clouds_wind[1] * 100) / 100) + ')') frag.add_const('float', 'cloudsPrecipitation', str(round(world.arm_clouds_precipitation * 100) / 100)) frag.add_const('float', 'cloudsSecondary', str(round(world.arm_clouds_secondary * 100) / 100)) frag.add_const('float', 'cloudsSteps', str(round(world.arm_clouds_steps * 100) / 100)) frag.add_function( '''float remap(float old_val, float old_min, float old_max, float new_min, float new_max) { \treturn new_min + (((old_val - old_min) / (old_max - old_min)) * (new_max - new_min)); }''') frag.add_function( '''float getDensityHeightGradientForPoint(float height, float cloud_type) { \tconst vec4 stratusGrad = vec4(0.02f, 0.05f, 0.09f, 0.11f); \tconst vec4 stratocumulusGrad = vec4(0.02f, 0.2f, 0.48f, 0.625f); \tconst vec4 cumulusGrad = vec4(0.01f, 0.0625f, 0.78f, 1.0f); \tfloat stratus = 1.0f - clamp(cloud_type * 2.0f, 0, 1); \tfloat stratocumulus = 1.0f - abs(cloud_type - 0.5f) * 2.0f; \tfloat cumulus = clamp(cloud_type - 0.5f, 0, 1) * 2.0f; \tvec4 cloudGradient = stratusGrad * stratus + stratocumulusGrad * stratocumulus + cumulusGrad * cumulus; \treturn smoothstep(cloudGradient.x, cloudGradient.y, height) - smoothstep(cloudGradient.z, cloudGradient.w, height); }''') frag.add_function('''float sampleCloudDensity(vec3 p) { \tfloat cloud_base = textureLod(scloudsBase, p, 0).r * 40; // Base noise \tvec3 weather_data = textureLod(scloudsMap, p.xy, 0).rgb; // Weather map \tcloud_base *= getDensityHeightGradientForPoint(p.z, weather_data.b); // Cloud type \tcloud_base = remap(cloud_base, weather_data.r, 1.0, 0.0, 1.0); // Coverage \tcloud_base *= weather_data.r; \tfloat cloud_detail = textureLod(scloudsDetail, p, 0).r * 2; // Detail noise \tfloat cloud_detail_mod = mix(cloud_detail, 1.0 - cloud_detail, clamp(p.z * 10.0, 0, 1)); \tcloud_base = remap(cloud_base, cloud_detail_mod * 0.2, 1.0, 0.0, 1.0); \treturn cloud_base; }''') func_cloud_radiance = 'float cloudRadiance(vec3 p, vec3 dir) {\n' if '_EnvSky' in world.world_defs: # Nishita sky if 'vec3 sunDir' in frag.uniforms: func_cloud_radiance += '\tvec3 sun_dir = sunDir;\n' # Hosek else: func_cloud_radiance += '\tvec3 sun_dir = hosekSunDirection;\n' else: func_cloud_radiance += '\tvec3 sun_dir = vec3(0, 0, -1);\n' func_cloud_radiance += '''\tconst int steps = 8; \tfloat step_size = 0.5 / float(steps); \tfloat d = 0.0; \tp += sun_dir * step_size; \tfor(int i = 0; i < steps; ++i) { \t\td += sampleCloudDensity(p + sun_dir * float(i) * step_size); \t} \treturn 1.0 - d; }''' frag.add_function(func_cloud_radiance) func_trace_clouds = '''vec3 traceClouds(vec3 sky, vec3 dir) { \tconst float step_size = 0.5 / float(cloudsSteps); \tfloat T = 1.0; \tfloat C = 0.0; \tvec2 uv = dir.xy / dir.z * 0.4 * cloudsLower + cloudsWind * time * 0.02; \tfor (int i = 0; i < cloudsSteps; ++i) { \t\tfloat h = float(i) / float(cloudsSteps); \t\tvec3 p = vec3(uv * 0.04, h); \t\tfloat d = sampleCloudDensity(p); \t\tif (d > 0) { \t\t\t// float radiance = cloudRadiance(p, dir); \t\t\tC += T * exp(h) * d * step_size * 0.6 * cloudsPrecipitation; \t\t\tT *= exp(-d * step_size); \t\t\tif (T < 0.01) break; \t\t} \t\tuv += (dir.xy / dir.z) * step_size * cloudsUpper; \t} ''' if world.arm_darken_clouds: func_trace_clouds += '\t// Darken clouds when the sun is low\n' # Nishita sky if 'vec3 sunDir' in frag.uniforms: func_trace_clouds += '\tC *= smoothstep(-0.02, 0.25, sunDir.z);\n' # Hosek else: func_trace_clouds += '\tC *= smoothstep(0.04, 0.32, hosekSunDirection.z);\n' func_trace_clouds += '\treturn vec3(C) + sky * T;\n}' frag.add_function(func_trace_clouds)
def parse_color(world: bpy.types.World, node: bpy.types.Node, frag: Shader): wrd = bpy.data.worlds['Arm'] rpdat = arm.utils.get_rp() mobile_mat = rpdat.arm_material_model == 'Mobile' or rpdat.arm_material_model == 'Solid' # Env map included if node.type == 'TEX_ENVIRONMENT' and node.image is not None: world.world_defs += '_EnvTex' frag.add_include('std/math.glsl') frag.add_uniform('sampler2D envmap', link='_envmap') image = node.image filepath = image.filepath if image.packed_file is None and not os.path.isfile( arm.utils.asset_path(filepath)): log.warn(world.name + ' - unable to open ' + image.filepath) return # Reference image name 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 is not None: # Extract packed data 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): arm.utils.unpack_image(image, unpack_filepath, file_format=target_format) 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) assets.add(unpack_filepath) else: if do_convert: 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): arm.utils.convert_image(image, converted_path, file_format=target_format) assets.add(converted_path) else: # Link image path to assets assets.add(arm.utils.asset_path(image.filepath)) # Generate prefiltered envmaps world.arm_envtex_name = tex_file world.arm_envtex_irr_name = tex_file.rsplit('.', 1)[0] disable_hdr = target_format == 'JPEG' mip_count = world.arm_envtex_num_mips mip_count = write_probes.write_probes(filepath, disable_hdr, mip_count, arm_radiance=rpdat.arm_radiance) world.arm_envtex_num_mips = mip_count # Append LDR define if disable_hdr: world.world_defs += '_EnvLDR' # Append radiance define if rpdat.arm_irradiance and rpdat.arm_radiance and not mobile_mat: wrd.world_defs += '_Rad' # Static image background elif node.type == 'TEX_IMAGE': world.world_defs += '_EnvImg' # Background texture frag.add_uniform('sampler2D envmap', link='_envmap') frag.add_uniform('vec2 screenSize', link='_screenSize') image = node.image filepath = image.filepath if image.packed_file is not None: # Extract packed data filepath = arm.utils.build_dir() + '/compiled/Assets/unpacked' unpack_path = arm.utils.get_fp() + filepath 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 assets.add(arm.utils.asset_path(image.filepath)) # Reference image name 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' # Generate prefiltered envmaps world.arm_envtex_name = tex_file world.arm_envtex_irr_name = tex_file.rsplit('.', 1)[0] disable_hdr = target_format == 'JPEG' mip_count = world.arm_envtex_num_mips mip_count = write_probes.write_probes(filepath, disable_hdr, mip_count, arm_radiance=rpdat.arm_radiance) world.arm_envtex_num_mips = mip_count # Append sky define elif node.type == 'TEX_SKY': # Match to cycles world.arm_envtex_strength *= 0.1 world.world_defs += '_EnvSky' assets.add_khafile_def('arm_hosek') frag.add_uniform('vec3 A', link="_hosekA") frag.add_uniform('vec3 B', link="_hosekB") frag.add_uniform('vec3 C', link="_hosekC") frag.add_uniform('vec3 D', link="_hosekD") frag.add_uniform('vec3 E', link="_hosekE") frag.add_uniform('vec3 F', link="_hosekF") frag.add_uniform('vec3 G', link="_hosekG") frag.add_uniform('vec3 H', link="_hosekH") frag.add_uniform('vec3 I', link="_hosekI") frag.add_uniform('vec3 Z', link="_hosekZ") frag.add_uniform('vec3 hosekSunDirection', link="_hosekSunDirection") frag.add_function( '''vec3 hosekWilkie(float cos_theta, float gamma, float cos_gamma) { \tvec3 chi = (1 + cos_gamma * cos_gamma) / pow(1 + H * H - 2 * cos_gamma * H, vec3(1.5)); \treturn (1 + A * exp(B / (cos_theta + 0.01))) * (C + D * exp(E * gamma) + F * (cos_gamma * cos_gamma) + G * chi + I * sqrt(cos_theta)); }''') world.arm_envtex_sun_direction = [ node.sun_direction[0], node.sun_direction[1], node.sun_direction[2] ] world.arm_envtex_turbidity = node.turbidity world.arm_envtex_ground_albedo = node.ground_albedo # Irradiance json file name wname = arm.utils.safestr(world.name) world.arm_envtex_irr_name = wname write_probes.write_sky_irradiance(wname) # Radiance if rpdat.arm_radiance and rpdat.arm_irradiance and not mobile_mat: wrd.world_defs += '_Rad' hosek_path = 'armory/Assets/hosek/' sdk_path = arm.utils.get_sdk_path() # Use fake maps for now assets.add(sdk_path + '/' + hosek_path + 'hosek_radiance.hdr') for i in range(0, 8): assets.add(sdk_path + '/' + hosek_path + 'hosek_radiance_' + str(i) + '.hdr') world.arm_envtex_name = 'hosek' world.arm_envtex_num_mips = 8