Algorithm

4 minutes read

In this section I’ll try to explain the algorithm behind the Tark to Blend add-on.

Objective

For each imported material, the script must be capable to :

  1. Detect the content of the node tree ;
  2. Detect if the material is opaque, transparent, emissive or a puddle ;
  3. Find the texture(s) required to complete the material setup ;
  4. Load the textures, the nodes and link them to form a functional material.

Structure

The add-on contains several Python files :

Important functions

Finding if the material is transparent

Unfortunately, there is no reliable way to guess if a material is transparent or not, as the textures are the same between the transparent and opaque shaders except for the alpha channel.

My method is to search for keywords contained in the TRANSPARENT_NAMES list in tarkdata.py, based on manual observations. These keywords must appear in the diffuse texture name or in the material name.

Algorithm to detect transparent materials
TRANSPARENT_NAMES = ["decal", "Decal", "_hair_", "Hair_", "bruno",
                "setka", "chain_fence", "plastic_bucket",
                "drop_wood", "Cactus_D_Mask","facecover_gasmask_GP5_glass",
                "gasmask_gp7_glass.png", "smoke_thin_2-2_random",
                "City_road_marking", "Reserve_paint_edge","Red_Stripes",
                "Reserve_Wall_Crack", "curtains5", "Web_atlas"
                "Lighthouse_plaster_damaged_bottom_trim_no_border",
                "Lighthouse_Concrete_Damaged_opacity_light",
                "City_glass5_transparent", "Chemical_plant", "grille",
                "City_glass_broken_transparent", "Concrete_trims",
                "Electronica_4_glass", "city_palm_plant_d_DRAFT_TO_CHANGE",
                "item_barter_valuable_rolex_Glass", "lab_glass",
                "Glass_stains", "paper_garbage2_footsteps", "Floor_metal_grate_rust_01",
                "City_broken_glass_atlas", "Flowers_D", "lab_ChemProtectGlass",
                "bulletholes_concrete", "Reserve_factroy_glass",
                "City_glass_broken_transparent", "City_Asphalt_Trim",
                "Vendors_snow_footprint"]

You will notice that “glass” is not a keyword, even though it would be reasonable to think it should be. Some “glass” materials aere actually not transparent, which means it cannot be used this way.

Another way to find if a texture is transparent is to detect if it’s an “atlas”. An atlas is a texture containing different objects, for example several types of plants. In fact, many vegetal objects have an “atlas” texture.

example of atlas texture
pine05_Atlas_Billboards.png

But many objects that have an “atlas” texture are not transparent, for example posters displayed on the walls are regrouped in such a texture. To exclude them, I created a list containing every non-transparent “atlas” texture :

    ATLAS_EXCEPTIONS = ["Atlas2.png", "atlas_1_D.png", "atlas_2_D.png", "atlas_cafe_DIF.png",
                "barns_atlas_1_D.png", "barns_atlas_2_D.png", "bipod_all_atlas_v8_bt10_LOD0_diff.png",
                "bipod_all_atlas_v8_bt10_LOD0_diff_0.png", "bipod_all_atlas_v8_bt10_LOD0_diff_1.png",
                "bipod_all_atlas_v8_bt10_LOD1_diff.png", "bipod_all_atlas_v8_bt10_LOD1_diff_0.png",
                "City_puddle_atlas_D.png", "details_atlas_bridge_albedo.png", "fck_atlas_d.png",
                "fck_atlas_d_0.png", "Icebreaker_decal_atlas_02_d.png","Icebreaker_parts_atlas_01_D.png",
                "Icebreaker_parts_atlas_01_D_0.png", "Lab_cable_atlas_dif.png",
                "Lighthouse_Plywood_atlas_D.png", "muzzle_ar10_odin_works_atlas_7_muzzle_brake_762x51_LOD0_diff.png",
                "muzzle_ar10_odin_works_atlas_7_muzzle_brake_762x51_LOD1_diff.png",
                "Old_Electrical_Atlas_Part_1_D.png", "Old_Electrical_Atlas_Part_2_D.png",
                "Quests_photo_atlas_2_D.png", "Reserve_Hospital_and_HQ_posters_atlas.png", 
                "Reserve_sport_posters_atlas.png", "sanatorium_ATLAS_1.png",
                "shopping_mall_LED_panel_atlas_albedo.png", "Strahovaya_posters_atlas_1.png",
                "Strahovaya_posters_atlas_2.png", "stump05_Atlas.png", "summerhouse_atlas_d.png",
                "Woodbox_atlas_d.png", "Xmass_posters_atlas_01.png", "Xmass_posters_atlas_02.png",
                "Xmass_posters_atlas_03.png"]

This way, in the algorithm, we can assume : “an atlas texture is transparent, except if it’s in the ATLAS_EXCEPTIONS list”.

Finding the glossy texture

This algorithm is stupid because of the absolutely chaotic file naming scheme. I made a list of possible naming conventions :

import numpy as np
DGN_TYPE_LIST = np.array([["_D.png", "_G.png", "_N.png"],
                      ["_d.png", "_g.png", "_n.png"],
                      ["_diff.png", "_gloss.png", "_nrm.png"],
                      ["_DIF.png", "_GLOS.png", "_NRM.png"],
                      ["_dif.png", "_glos.png", "_nrm.png"],
                      ["_albedo.png", "_gloss.png", "_normal.png"],
                      ["[Albedo].png", "[Gloss].png", "[Normal].png"]])

For example, an ammo box could have textures named :

But sometimes you can have : ammo_box_DIF.png, ammo_box_gloss.png, ammo_box_n.png !

This algorithm starts with the assumption that the material follows such a naming convention. But very often, the glossy texture does exist but does not follow this convention, which means you have to search all possibilities before dismissing it as a non-glossy material. This makes this algorithm slow, but I wanted it to be exhaustive.

glossy algorithm

Another problem is materials that can share textures. For example, the Skier scene contains containers of different colors that have different diffuse textures, but they all share glossy and normal textures.

Building the material

The last step is building the material. I will not detail here the functions that determine if the material is emissive or a puddle, it’s basically searching for keywords in the texture name, just like for the transparency.

This algorithm has to take into account different situations depending on which textures exist for the material.

material algorithm