File size: 8,613 Bytes
39149b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# v10 real diversity: rebuild the model masters M_V10Mesh (opaque) and M_V10Foliage (masked, two sided foliage) in place
# (the 258 material instances keep their textures) with a per-look cover layer: CovColor / CovAmt / CovRough put snow,
# moss, dust or sand on upward faces (world normal, value-noise patches), Tint / RMul give seasons and wet looks.
import unreal as u, os, traceback
MEL = u.MaterialEditingLibrary; EAL = u.EditorAssetLibrary
V10 = os.path.dirname(os.path.dirname(os.path.abspath(__file__))); LOG = os.path.join(V10, 'out', 'v10_masters4_log.txt'); open(LOG, 'w').close()
def log(m):
    with open(LOG, 'a', encoding='utf-8') as f: f.write(str(m) + '\n')
F3, F1 = u.CustomMaterialOutputType.CMOT_FLOAT3, u.CustomMaterialOutputType.CMOT_FLOAT1
COV = r'''
float2 q = WP.xy / 70.0; float2 qi = floor(q); float2 qf = frac(q); qf = qf * qf * (3 - 2 * qf);
float a = frac(sin(dot(qi, float2(127.1, 311.7))) * 43758.5453), b = frac(sin(dot(qi + float2(1, 0), float2(127.1, 311.7))) * 43758.5453);
float c = frac(sin(dot(qi + float2(0, 1), float2(127.1, 311.7))) * 43758.5453), d = frac(sin(dot(qi + float2(1, 1), float2(127.1, 311.7))) * 43758.5453);
float nz = lerp(lerp(a, b, qf.x), lerp(c, d, qf.x), qf.y);
float2 r = WP.xy / 17.0; float2 ri = floor(r); float2 rf = frac(r); rf = rf * rf * (3 - 2 * rf);
float a2 = frac(sin(dot(ri, float2(127.1, 311.7))) * 43758.5453), b2 = frac(sin(dot(ri + float2(1, 0), float2(127.1, 311.7))) * 43758.5453);
float c2 = frac(sin(dot(ri + float2(0, 1), float2(127.1, 311.7))) * 43758.5453), d2 = frac(sin(dot(ri + float2(1, 1), float2(127.1, 311.7))) * 43758.5453);
float nz2 = lerp(lerp(a2, b2, rf.x), lerp(c2, d2, rf.x), rf.y);
float m = saturate(CovAmt * 1.6) * smoothstep(0.25 + 0.5 * (1 - CovAmt), 0.9, normalize(NW).z * 0.85 + nz * 0.3 + (nz2 - 0.5) * 0.3);
'''
def build(name, foliage):
    m = u.load_asset('/Game/Xfer/V10/Materials/' + name)
    defs = {}
    for n in ('BC', 'NM', 'RT', 'OP'):
        try: defs[n] = MEL.get_material_default_texture_parameter_value(m, n)
        except Exception: pass
    MEL.delete_all_material_expressions(m)
    for f in ('used_with_instanced_static_meshes', 'used_with_nanite'): m.set_editor_property(f, True)
    if foliage:
        m.set_editor_property('blend_mode', u.BlendMode.BLEND_MASKED); m.set_editor_property('two_sided', True)
        m.set_editor_property('shading_model', u.MaterialShadingModel.MSM_TWO_SIDED_FOLIAGE); m.set_editor_property('opacity_mask_clip_value', .8)
        m.set_editor_property('tangent_space_normal', False)
    uv = MEL.create_material_expression(m, u.MaterialExpressionTextureCoordinate, -1700, 0)
    wp = MEL.create_material_expression(m, u.MaterialExpressionWorldPosition, -1700, 120)
    nw = MEL.create_material_expression(m, u.MaterialExpressionVertexNormalWS, -1700, 220)
    T = {}
    names = [('BC', u.MaterialSamplerType.SAMPLERTYPE_COLOR), ('NM', u.MaterialSamplerType.SAMPLERTYPE_NORMAL), ('RT', u.MaterialSamplerType.SAMPLERTYPE_LINEAR_COLOR)] + ([('OP', u.MaterialSamplerType.SAMPLERTYPE_LINEAR_COLOR)] if foliage else [])
    for k, (nm, st) in enumerate(names):
        e = MEL.create_material_expression(m, u.MaterialExpressionTextureObjectParameter, -1500, -200 + 150 * k); e.set_editor_property('parameter_name', nm); e.set_editor_property('sampler_type', st)
        if defs.get(nm): e.set_editor_property('texture', defs[nm])
        T[nm] = e
    P = {}
    sc = [('RMul', 1.0), ('MetalAmt', 0.0), ('AOAmt', 1.0), ('SSSAmt', .55), ('CovAmt', 0.0), ('CovRough', .8), ('CovDetail', .8), ('Bright', 1.12 if foliage else 1.0), ('SeasonAmt', 0.0)] + ([('UpBend', .5), ('AOScale', .5)] if foliage else [])
    for k, (nm, dv) in enumerate(sc):
        e = MEL.create_material_expression(m, u.MaterialExpressionScalarParameter, -1500, 500 + 80 * k); e.set_editor_property('parameter_name', nm); e.set_editor_property('default_value', dv); P[nm] = e
    for k, (nm, dv) in enumerate([('Tint', u.LinearColor(1, 1, 1, 1)), ('RCh', u.LinearColor(0, 1, 0, 0)), ('CovColor', u.LinearColor(.9, .92, .95, 1)), ('SeasonCol', u.LinearColor(1, .45, .12, 1))]):
        e = MEL.create_material_expression(m, u.MaterialExpressionVectorParameter, -1500, 1300 + 90 * k); e.set_editor_property('parameter_name', nm); e.set_editor_property('default_value', dv); P[nm] = e
    def node(code, ins, out, y, x=-700):
        c = MEL.create_material_expression(m, u.MaterialExpressionCustom, x, y); c.set_editor_property('code', code); c.set_editor_property('output_type', out)
        L = []
        for n in ['UV'] + ins:
            ci = u.CustomInput(); ci.set_editor_property('input_name', n); L.append(ci)
        c.set_editor_property('inputs', L); MEL.connect_material_expressions(uv, '', c, 'UV')
        for n in ins:
            src = T.get(n) or P.get(n) or {'WP': wp, 'NW': nw}.get(n)
            MEL.connect_material_expressions(src, '', c, n)
        return c
    samp = 'Texture2DSampleBias(BC, BCSampler, UV, -1)' if foliage else 'Texture2DSample(BC, BCSampler, UV)'
    SEA = 'float lum = dot(col, float3(0.3, 0.59, 0.11)); col = lerp(col, lum * SeasonCol.rgb * 2.2, saturate(SeasonAmt));'
    MEL.connect_material_property(node(COV + 'float3 col = %s.rgb * Tint.rgb * Bright; ' % samp + SEA + ' float lc = dot(col, float3(0.3, 0.59, 0.11)); float3 cv = CovColor.rgb * lerp(1.0, saturate(0.45 + 2.2 * lc), CovDetail); return lerp(col, cv, m);', ['BC', 'Tint', 'Bright', 'CovColor', 'CovAmt', 'CovDetail', 'WP', 'NW', 'SeasonCol', 'SeasonAmt'], F3, -300), '', u.MaterialProperty.MP_BASE_COLOR)
    if foliage:
        tn = node('float2 t = Texture2DSample(NM, NMSampler, UV).xy * 2 - 1; return float3(t, sqrt(saturate(1 - dot(t, t))));', ['NM'], F3, -100, -1000)
        tr = MEL.create_material_expression(m, u.MaterialExpressionTransform, -800, -100)
        tr.set_editor_property('transform_source_type', u.MaterialVectorCoordTransformSource.TRANSFORMSOURCE_TANGENT); tr.set_editor_property('transform_type', u.MaterialVectorCoordTransform.TRANSFORM_WORLD)
        MEL.connect_material_expressions(tn, '', tr, '')
        bend = MEL.create_material_expression(m, u.MaterialExpressionCustom, -500, -100); bend.set_editor_property('code', 'return normalize(lerp(normalize(Nw), float3(0, 0, 1), saturate(UpBend)));'); bend.set_editor_property('output_type', F3)
        L = []
        for n in ('Nw', 'UpBend'):
            ci = u.CustomInput(); ci.set_editor_property('input_name', n); L.append(ci)
        bend.set_editor_property('inputs', L); MEL.connect_material_expressions(tr, '', bend, 'Nw'); MEL.connect_material_expressions(P['UpBend'], '', bend, 'UpBend')
        MEL.connect_material_property(bend, '', u.MaterialProperty.MP_NORMAL)
        MEL.connect_material_property(node('return lerp(1, Texture2DSample(RT, RTSampler, UV).r, AOAmt * AOScale);', ['RT', 'AOAmt', 'AOScale'], F1, 250), '', u.MaterialProperty.MP_AMBIENT_OCCLUSION)
        MEL.connect_material_property(node('return Texture2DSampleBias(OP, OPSampler, UV, -1).r;', ['OP'], F1, 400), '', u.MaterialProperty.MP_OPACITY_MASK)
        MEL.connect_material_property(node(COV + 'float3 col = Texture2DSample(BC, BCSampler, UV).rgb * Tint.rgb; ' + SEA + ' float lc = dot(col, float3(0.3, 0.59, 0.11)); float3 cv = CovColor.rgb * lerp(1.0, saturate(0.45 + 2.2 * lc), CovDetail); return lerp(col, cv, m) * SSSAmt;', ['BC', 'Tint', 'SSSAmt', 'CovColor', 'CovAmt', 'CovDetail', 'WP', 'NW', 'SeasonCol', 'SeasonAmt'], F3, 550), '', u.MaterialProperty.MP_SUBSURFACE_COLOR)
    else:
        MEL.connect_material_property(node('float2 t = Texture2DSample(NM, NMSampler, UV).xy * 2 - 1; return float3(t, sqrt(saturate(1 - dot(t, t))));', ['NM'], F3, -100), '', u.MaterialProperty.MP_NORMAL)
        MEL.connect_material_property(node('return Texture2DSample(RT, RTSampler, UV).b * MetalAmt;', ['RT', 'MetalAmt'], F1, 250), '', u.MaterialProperty.MP_METALLIC)
        MEL.connect_material_property(node('return lerp(1, Texture2DSample(RT, RTSampler, UV).r, AOAmt);', ['RT', 'AOAmt'], F1, 400), '', u.MaterialProperty.MP_AMBIENT_OCCLUSION)
    MEL.connect_material_property(node(COV + 'return lerp(saturate(dot(Texture2DSample(RT, RTSampler, UV).rgb, RCh.rgb) * RMul), CovRough, m);', ['RT', 'RCh', 'RMul', 'CovRough', 'CovAmt', 'WP', 'NW'], F1, 100), '', u.MaterialProperty.MP_ROUGHNESS)
    MEL.recompile_material(m); EAL.save_loaded_asset(m, False); log('%s rebuilt (cover layer, foliage=%s)' % (name, foliage))
for nm, fol in (('M_V10Mesh', False), ('M_V10Foliage', True)):
    try: build(nm, fol)
    except Exception: log('FAIL %s %s' % (nm, traceback.format_exc()))
log('V10_MASTERS4_DONE')