-
Notifications
You must be signed in to change notification settings - Fork 84
/
shaders.py
1404 lines (1113 loc) · 47.7 KB
/
shaders.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (C) 2021 Victor Soupday
# This file is part of CC/iC Blender Tools <https://github.com/soupday/cc_blender_tools>
#
# CC/iC Blender Tools is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# CC/iC Blender Tools is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with CC/iC Blender Tools. If not, see <https://www.gnu.org/licenses/>.
import bpy
import math
import os
from mathutils import Vector, Color
from . import imageutils, jsonutils, meshutils, materials, wrinkle, nodeutils, params, utils, vars
def get_prop_value(mat_cache, prop_name):
parameters = mat_cache.parameters
try:
return eval("parameters." + prop_name, None, locals())
except:
return None
def eval_texture_rules(tex_type):
prefs = vars.prefs()
if tex_type in params.TEXTURE_RULES:
tex_rule = params.TEXTURE_RULES[tex_type]
try:
return eval(tex_rule, None, locals())
except:
return False
else:
return True
def exec_var_param(var_def, mat_cache, mat_json):
try:
parameters = mat_cache.parameters
prop_name = var_def[0]
default_value = var_def[1]
func = var_def[2]
args = var_def[3:]
exec_expression = str(default_value)
if mat_json:
if func == "" or func == "=":
# expression is json var value
json_value = jsonutils.get_material_json_var(mat_json, args[0])
if json_value is not None:
exec_expression = str(json_value)
elif func != "DEF" and not args:
exec_expression = func + f"({default_value})"
elif func != "DEF" and args:
# construct eval function code
func_expression = func + "("
first = True
missing_args = False
for arg in args:
if not first:
func_expression += ", "
first = False
arg_value = jsonutils.get_material_json_var(mat_json, arg)
if arg_value is None:
missing_args = True
func_expression += str(arg_value)
func_expression += ")"
if not missing_args:
exec_expression = func_expression
exec_code = "parameters." + prop_name + " = " + exec_expression
exec(exec_code, None, locals())
utils.log_info("Applying: " + exec_code)
except:
utils.log_error("exec_var_param(): error in expression: " + exec_code)
utils.log_error(str(var_def))
def eval_input_param(input_def, mat_cache):
try:
parameters = mat_cache.parameters
input_socket = input_def[0]
func = input_def[1]
args = input_def[2:]
if func == "" or func == "=":
# expression is mat_cache parameter
exec_expression = "parameters." + args[0]
else:
# construct eval function code
exec_expression = func + "("
first = True
for arg in args:
if not first:
exec_expression += ", "
first = False
exec_expression += "parameters." + arg
exec_expression += ")"
return eval(exec_expression, None, locals())
except:
utils.log_error("eval_input_param(): error in expression: " + exec_expression)
return None
def eval_tiling_param(texture_def, mat_cache, start_index = 4):
try:
parameters = mat_cache.parameters
func = texture_def[start_index]
args = texture_def[start_index + 1:]
if func == "" or func == "=":
# expression is mat_cache parameter
exec_expression = "parameters." + args[0]
else:
# construct eval function code
exec_expression = func + "("
first = True
for arg in args:
if not first:
exec_expression += ", "
first = False
exec_expression += "parameters." + arg
exec_expression += ")"
return eval(exec_expression, None, locals())
except:
utils.log_error("eval_tiling_param(): error in expression: " + exec_expression)
return None
def eval_parameters_func(parameters, func, args, default = None):
try:
# construct eval function code
exec_expression = func + "("
first = True
for arg in args:
if not first:
exec_expression += ", "
first = False
exec_expression += "parameters." + arg
exec_expression += ")"
return eval(exec_expression, None, locals())
except:
utils.log_error("eval_parameters_func(): error in expression: " + exec_expression)
return default
def eval_prop(prop_name, mat_cache):
try:
parameters = mat_cache.parameters
exec_expression = "parameters." + prop_name
return eval(exec_expression, None, locals())
except:
utils.log_error("eval_prop(): error in expression: " + exec_expression)
return None
def exec_prop(prop_name, mat_cache, value):
try:
parameters = mat_cache.parameters
exec_expression = "parameters." + prop_name + " = " + str(value)
exec(exec_expression, None, locals())
except:
utils.log_error("exec_prop(): error in expression: " + exec_expression)
return None
def fetch_prop_defaults(obj, mat_cache, mat_json):
vars.block_property_update = True
shader = params.get_shader_name(mat_cache)
matrix_group = params.get_shader_def(shader)
if matrix_group and "vars" in matrix_group.keys():
for var_def in matrix_group["vars"]:
exec_var_param(var_def, mat_cache, mat_json)
if shader == "rl_hair_shader":
check_legacy_hair(obj, mat_cache, mat_json)
#if mat_cache.get_base_name() in vars.GAME_BASE_SKIN_NAMES:
# mat_cache.parameters.default_roughness_power = 0.75
vars.block_property_update = False
def check_legacy_hair(obj, mat_cache, mat_json):
root_map_path = None
id_map_path = None
flow_map_path = None
try:
root_map_path = mat_json["Custom Shader"]["Image"]["Hair Root Map"]["Texture Path"]
except:
pass
try:
id_map_path = mat_json["Custom Shader"]["Image"]["Hair ID Map"]["Texture Path"]
except:
pass
try:
flow_map_path = mat_json["Custom Shader"]["Image"]["Hair Flow Map"]["Texture Path"]
except:
pass
if not meshutils.has_vertex_color_data(obj):
mat_cache.parameters.hair_vertex_color_strength = 0.0
# if hair does not have a root map or id map or flow map, then it is (probably) legacy and needs adjusting
if not root_map_path and not id_map_path and not flow_map_path:
mat_cache.parameters.hair_enable_color = 0.0
mat_cache.parameters.hair_vertex_color_strength = 0.0
mat_cache.parameters.hair_specular_blend = 1.0
mat_cache.parameters.hair_anisotropic_roughness = 0.05
mat_cache.parameters.hair_anisotropic_strength = 0.15
mat_cache.parameters.hair_anisotropic_strength2 = 0.15
return
def apply_prop_matrix(bsdf_node, group_node, mat_cache, shader_name):
matrix_group = params.get_shader_def(shader_name)
if group_node and matrix_group and "inputs" in matrix_group.keys():
for input_def in matrix_group["inputs"]:
if input_def[0] in group_node.inputs:
prop_value = eval_input_param(input_def, mat_cache)
if prop_value is not None:
nodeutils.set_node_input_value(group_node, input_def[0], prop_value)
if bsdf_node and matrix_group and "bsdf" in matrix_group.keys():
bsdf_nodes = nodeutils.get_custom_bsdf_nodes(bsdf_node)
for input_def in matrix_group["bsdf"]:
for n in bsdf_nodes:
if input_def[0] in n.inputs:
prop_value = eval_input_param(input_def, mat_cache)
if prop_value is not None:
nodeutils.set_node_input_value(n, input_def[0], prop_value)
def apply_basic_prop_matrix(node: bpy.types.Node, mat_cache, shader_name):
matrix_group = params.get_shader_def(shader_name)
if matrix_group and "inputs" in matrix_group.keys():
for input in matrix_group["inputs"]:
if input[0] in node.inputs:
prop_value = eval_input_param(input, mat_cache)
if prop_value is not None:
nodeutils.set_node_input_value(node, input[0], prop_value)
# Prop matrix eval, parameter conversion functions
#
def func_iris_brightness(v):
prefs = vars.prefs()
if prefs.render_target == "CYCLES" and prefs.refractive_eyes == "SSR":
if utils.B410():
v = v * prefs.cycles_ssr_iris_brightness_b410
else:
v = v * prefs.cycles_ssr_iris_brightness_b341
elif prefs.render_target == "EEVEE" and prefs.refractive_eyes == "SSR":
if utils.B420():
v = v * prefs.eevee_ssr_iris_brightness_b420
else:
v = v * prefs.eevee_ssr_iris_brightness_b341
return v
def func_sss_skin(s):
prefs = vars.prefs()
if prefs.render_target == "CYCLES":
if utils.B400():
s = s * prefs.cycles_sss_skin_b410
else:
s = s * prefs.cycles_sss_skin_b341
else:
if utils.B420():
s = s * prefs.eevee_sss_skin_b420
else:
s = s * prefs.eevee_sss_skin_b341
return s
def func_sss_hair(s):
prefs = vars.prefs()
if prefs.render_target == "CYCLES":
if utils.B400():
s = s * prefs.cycles_sss_hair_b410
else:
s = s * prefs.cycles_sss_hair_b341
else:
if utils.B420():
s = s * prefs.eevee_sss_hair_b420
else:
s = s * prefs.eevee_sss_hair_b341
return s
def func_sss_teeth(s):
prefs = vars.prefs()
if prefs.render_target == "CYCLES":
if utils.B400():
s = s * prefs.cycles_sss_teeth_b410
else:
s = s * prefs.cycles_sss_teeth_b341
else:
if utils.B420():
s = s * prefs.eevee_sss_teeth_b420
else:
s = s * prefs.eevee_sss_teeth_b341
return s
def func_sss_tongue(s):
prefs = vars.prefs()
if prefs.render_target == "CYCLES":
if utils.B400():
s = s * prefs.cycles_sss_tongue_b410
else:
s = s * prefs.cycles_sss_tongue_b341
else:
if utils.B420():
s = s * prefs.eevee_sss_tongue_b420
else:
s = s * prefs.eevee_sss_tongue_b341
return s
def func_sss_eyes(s):
prefs = vars.prefs()
if prefs.render_target == "CYCLES":
if utils.B400():
s = s * prefs.cycles_sss_eyes_b410
else:
s = s * prefs.cycles_sss_eyes_b341
else:
if utils.B420():
s = s * prefs.eevee_sss_eyes_b420
else:
s = s * prefs.eevee_sss_eyes_b341
return s
def func_sss_default(s):
prefs = vars.prefs()
if prefs.render_target == "CYCLES":
if utils.B400():
s = s * prefs.cycles_sss_default_b410
else:
s = s * prefs.cycles_sss_default_b341
else:
if utils.B420():
s = s * prefs.eevee_sss_default_b420
else:
s = s * prefs.eevee_sss_default_b341
return s
def func_sss_falloff_saturated(f, s):
falloff = Color((f[0], f[1], f[2]))
falloff.s *= s
return [falloff.r, falloff.g, falloff.b, 1.0]
def func_sss_radius_eyes_cycles(r):
prefs = vars.prefs()
r = r * vars.EYES_SSS_RADIUS_SCALE
return r
def func_sss_radius_eyes_eevee(r, f):
prefs = vars.prefs()
r = r * vars.EYES_SSS_RADIUS_SCALE
return [f[0] * r, f[1] * r, f[2] * r]
def func_sss_radius_hair_cycles(r):
prefs = vars.prefs()
r = r * vars.HAIR_SSS_RADIUS_SCALE
return r
def func_sss_radius_hair_eevee(r, f, s):
prefs = vars.prefs()
r = r * vars.HAIR_SSS_RADIUS_SCALE
falloff = Color((f[0], f[1], f[2]))
falloff.s *= s
return [falloff.r * r, falloff.g * r, falloff.b * r]
def func_sss_radius_teeth_eevee(r, f):
prefs = vars.prefs()
r = r * vars.TEETH_SSS_RADIUS_SCALE
return [f[0] * r, f[1] * r, f[2] * r]
def func_sss_radius_tongue_eevee(r, f):
prefs = vars.prefs()
r = r * vars.TONGUE_SSS_RADIUS_SCALE
return [f[0] * r, f[1] * r, f[2] * r]
def func_sss_radius_default_eevee(r, f):
prefs = vars.prefs()
r = r * vars.DEFAULT_SSS_RADIUS_SCALE
return [f[0] * r, f[1] * r, f[2] * r]
def func_sss_radius_skin_cycles(r):
prefs = vars.prefs()
r = r * vars.SKIN_SSS_RADIUS_SCALE
#if utils.B400():
# r *= 2/3
return r
def func_sss_radius_skin_eevee(r, f, s):
prefs = vars.prefs()
r = r * vars.SKIN_SSS_RADIUS_SCALE
falloff = Color((f[0], f[1], f[2]))
falloff.s *= s
return [falloff.r * r, falloff.g * r, falloff.b * r]
def func_roughness_power(p):
prefs = vars.prefs()
#if prefs.build_skin_shader_dual_spec:
# return p * 1.0
#else:
# return p
if prefs.render_target == "CYCLES":
if utils.B410():
return p * prefs.cycles_roughness_power_b410
else:
return p * prefs.cycles_roughness_power_b341
else:
if utils.B420():
return p * prefs.eevee_roughness_power_b420
else:
return p * prefs.eevee_roughness_power_b341
def func_a(a, b, c):
return a
def func_b(a, b, c):
return b
def func_b(a, b, c):
return c
def func_mul(a, b):
return a * b
def func_tiling(scale):
return 1.0 / scale
def func_emission_scale(v):
return v * vars.EMISSION_SCALE
def func_color_bytes(jc: list):
return [ jc[0] / 255.0, jc[1] / 255.0, jc[2] / 255.0, 1.0 ]
def func_color_vector(jc: list):
if type(jc) == list:
for i in range(0, len(jc)):
jc[i] /= 255.0
return jc
def func_export_byte3(c):
return [c[0] * 255.0, c[1] * 255.0, c[2] * 255.0]
def func_occlusion_range(r, m):
return utils.lerp(m, 1.0, r)
def func_occlusion_strength(s):
return pow(s, 1.0 / 3.0)
def func_occlusion_color(c):
return utils.lerp_color(c, (0,0,0,1), 0.75)
def func_one_minus(v):
return 1.0 - v
def func_sqrt(v):
return math.sqrt(v)
def func_pow_2(v):
return math.pow(v, 2.0)
def func_sclera_brightness(b):
prefs = vars.prefs()
if prefs.render_target == "CYCLES":
b *= 1.0
return b
def func_iris_scale(i):
return i * 1.00
def func_parallax_iris_scale(i, s):
return (func_iris_scale(i) * s)
def func_parallax_iris_tiling(i, s):
return 1.0 / (func_parallax_iris_scale(i, s))
def func_get_iris_scale(iris_uv_radius):
return 0.16 / iris_uv_radius
def func_half(s):
return s * 0.5
def func_third(s):
return s * 0.3333
def func_two_third(s):
return s * 0.6666
def func_divide_1000(v):
return v / 1000.0
def func_divide_100(v):
return v / 100.0
def func_divide_200(v):
return v / 200.0
def func_divide_2(v):
return v / 2.0
def func_mul_1000(v):
return v * 1000.0
def func_mul_100(v):
return v * 100.0
def func_mul_2(v):
return v * 2.0
def func_limbus_dark_radius(limbus_dark_scale):
#return 1 / limbus_dark_scale
#t = utils.inverse_lerp(0.0, 10.0, limbus_dark_scale)
#return utils.lerp(0.155, 0.08, t) + 0.025
limbus_dark_scale = max(limbus_dark_scale, 0.01)
ds = pow(0.01, 0.2) / limbus_dark_scale
dm = pow(0.5, 0.2) / limbus_dark_scale
de = dm + (dm - ds)
return de
def func_limbus_dark_width(limbus_dark_scale):
#return 1 / limbus_dark_scale
#t = utils.inverse_lerp(0.0, 10.0, limbus_dark_scale)
#return utils.lerp(0.155, 0.08, t) + 0.025
limbus_dark_scale = max(limbus_dark_scale, 0.01)
ds = pow(0.01, 0.2) / limbus_dark_scale
dm = pow(0.5, 0.2) / limbus_dark_scale
de = dm + (dm - ds)
return ds / de
def func_export_limbus_dark_scale(ldr):
#return 1 / limbus_dark_radius
#t = utils.inverse_lerp(0.155, 0.08, limbus_dark_radius - 0.025)
#return utils.clamp(utils.lerp(0.0, 10.0, t), 0, 10)
M = pow(0.5, 0.2)
S = pow(0.01, 0.2)
lds = (2 * M - S) / ldr
return lds
def func_brightness(b):
"""Shader brightness adjust"""
if b <= 1.0:
return b
B = (b - 1)*4 + 1
return B
def func_export_brightness(B):
"""Shader brightness adjust"""
if B <= 1.0:
return B
b = (B - 1)/4 + 1
return b
def func_saturation(s):
"""Shader saturation adjust"""
if s <= 1.0:
return s
S = (s - 1)*3 + 1
return S
def func_export_saturation(S):
"""Shader saturation adjust"""
if S <= 1.0:
return S
s = (S - 1)/3 + 1
return s
def func_brightness_mod(b):
"""Brightness adjust to be used directly in modify color BCHS"""
B = (b - 1)*5 + 1
return B
def func_export_brightness_mod(B):
"""Brightness adjust to be used directly in modify color BCHS"""
b = (B - 1)/5 + 1
return b
def func_saturation_mod(s):
"""Saturation adjust to be used directly in modify color BCHS"""
S = (s - 1)*3 + 1
return S
def func_export_saturation_mod(S):
"""Saturation adjust to be used directly in modify color BCHS"""
s = (S - 1)/3 + 1
return s
def func_get_eye_depth(depth):
return (depth / 3.0)
def func_export_eye_depth(depth):
return (depth) * 3.0
def func_set_eye_depth(depth):
return depth * 1.5
def func_set_parallax_iris_depth(depth):
return depth * 1.5
def func_index_1(values: list):
return values[0] / 255.0
def func_index_2(values: list):
return values[1] / 255.0
def func_index_3(values: list):
return values[2] / 255.0
def func_export_combine_xyz(x, y, z):
return [x * 255.0, y * 255.0, z * 255.0]
def func_normal_strength(s):
prefs = vars.prefs()
if prefs.render_target == "CYCLES":
if utils.B400():
s = s * prefs.cycles_normal_b410
else:
s = s * prefs.cycles_normal_b341
else:
if utils.B420():
s = s * prefs.eevee_normal_b420
else:
s = s * prefs.eevee_normal_b341
return s
def func_skin_normal_strength(s):
prefs = vars.prefs()
if prefs.render_target == "CYCLES":
if utils.B400():
s = s * prefs.cycles_normal_skin_b410
else:
s = s * prefs.cycles_normal_skin_b341
else:
if utils.B420():
s = s * prefs.eevee_normal_skin_b420
else:
s = s * prefs.eevee_normal_skin_b341
return s
def func_micro_normal_strength(s):
prefs = vars.prefs()
if prefs.render_target == "CYCLES":
if utils.B400():
s = s * prefs.cycles_micro_normal_b410
else:
s = s * prefs.cycles_micro_normal_b341
else:
if utils.B420():
s = s * prefs.eevee_micro_normal_b420
else:
s = s * prefs.eevee_micro_normal_b341
return s
#
# End Prop matrix eval, parameter conversion functions
def set_image_node_tiling(nodes, links, node, mat_cache, texture_def, shader, tex_json):
prefs = vars.prefs()
tex_type = texture_def[2]
tiling_mode = "NONE"
if len(texture_def) > 3:
tiling_mode = texture_def[3]
tiling = (1, 1, 1)
offset = (0, 0, 0)
# fetch any tiling and offset from the json data (if available)
if tex_json:
if "Tiling" in tex_json.keys():
tiling = tex_json["Tiling"]
if len(tiling) == 2:
tiling.append(1)
if tiling != [1,1,1]:
tiling_mode = "OFFSET"
if "Offset" in tex_json.keys():
offset = tex_json["Offset"]
if len(offset) == 2:
offset.append(0)
if offset != [0,0,0]:
tiling_mode = "OFFSET"
# evaluate any tiling parameter from the texture def
if len(texture_def) > 5:
tiling_value = eval_tiling_param(texture_def, mat_cache)
if tiling_value is not None:
tiling = (tiling_value, tiling_value, 1)
node_name = "tiling_" + shader + "_" + tex_type + "_mapping"
node_label = tex_type + " Mapping"
location = node.location
location = (location[0] - 900, location[1] - 100)
if tiling_mode == "EYE_PARALLAX":
if prefs.refractive_eyes == "SSR" or mat_cache.is_eye():
tiling_mode = "CENTERED"
if tiling_mode == "CENTERED":
node_group = nodeutils.get_node_group("tiling_pivot_mapping")
tiling_node = nodeutils.make_node_group_node(nodes, node_group, node_label, node_name)
tiling_node.location = location
nodeutils.set_node_input_value(tiling_node, "Tiling", tiling)
nodeutils.set_node_input_value(tiling_node, "Pivot", (0.5, 0.5, 0))
nodeutils.link_nodes(links, tiling_node, "Vector", node, "Vector")
elif tiling_mode == "OFFSET":
node_group = nodeutils.get_node_group("tiling_offset_mapping")
tiling_node = nodeutils.make_node_group_node(nodes, node_group, node_label, node_name)
tiling_node.location = location
nodeutils.set_node_input_value(tiling_node, "Tiling", tiling)
nodeutils.set_node_input_value(tiling_node, "Offset", offset)
nodeutils.link_nodes(links, tiling_node, "Vector", node, "Vector")
elif tiling_mode == "EYE_PARALLAX":
node_group = nodeutils.get_node_group("tiling_cornea_parallax_mapping")
mapping_node = nodeutils.make_node_group_node(nodes, node_group, node_label, node_name)
mapping_node.location = location
nodeutils.link_nodes(links, mapping_node, "Vector", node, "Vector")
shader_name = params.get_shader_name(mat_cache)
shader_def = params.get_shader_def(shader_name)
if "mapping" in shader_def.keys():
mapping_defs = shader_def["mapping"]
for mapping_def in mapping_defs:
if len(mapping_def) > 1:
socket_name = mapping_def[1]
nodeutils.set_node_input_value(mapping_node, socket_name, eval_tiling_param(mapping_def, mat_cache, 2))
def init_character_property_defaults(chr_cache, chr_json, only:list=None):
prefs = vars.prefs()
processed = []
utils.log_info("")
utils.log_info("Initializing Material Property Defaults:")
utils.log_info("----------------------------------------")
if chr_json:
utils.log_info("(Using Json Data)")
else:
utils.log_info("(No Json Data)")
# Advanced properties
for obj in chr_cache.get_cache_objects():
obj_cache = chr_cache.get_object_cache(obj)
if obj_cache and not obj_cache.disabled and obj_cache.is_mesh() and obj not in processed:
processed.append(obj)
obj_json = jsonutils.get_object_json(chr_json, obj)
utils.log_info("Object: " + obj.name + " (" + obj_cache.object_type + ")")
utils.log_indent()
for mat in obj.data.materials:
if only and mat not in only: continue
if mat and mat not in processed:
processed.append(mat)
mat_cache = chr_cache.get_material_cache(mat)
if mat_cache and not mat_cache.user_added:
mat_json = jsonutils.get_material_json(obj_json, mat)
utils.log_info("Material: " + mat.name + " (" + mat_cache.material_type + ")")
utils.log_indent()
if mat_cache.is_eye():
cornea_mat, cornea_mat_cache = materials.get_cornea_mat(obj, mat, mat_cache)
if cornea_mat:
mat_json = jsonutils.get_material_json(obj_json, cornea_mat)
fetch_prop_defaults(obj, mat_cache, mat_json)
if chr_json is None and chr_cache.is_actor_core():
try:
mat_cache.parameters.default_ao_strength = 0.4
mat_cache.parameters.default_ao_power = 1.0
mat_cache.parameters.default_specular_scale = 0.4
except:
pass
if mat_cache.source_name.startswith("Ga_Skin_"):
try:
if prefs.render_target == "EEVEE":
mat_cache.parameters.default_roughness_power = 0.5
else:
mat_cache.parameters.default_roughness_power = 0.75
except:
pass
utils.log_recess()
utils.log_recess()
def set_shader_input_props(shader_def, mat_cache, socket, value):
"""Look up and set the properties for the shader inputs.
"""
for texture_def in shader_def["inputs"]:
if texture_def[0] == socket:
props = texture_def[2:]
for prop in props:
vars.block_property_update = True
exec_prop(prop, mat_cache, value)
vars.block_property_update = False
def apply_texture_matrix(nodes, links, shader_node,
mat, mat_cache, shader_name, mat_json,
obj, processed_images,
offset = Vector((0,0)), sub_shader = False, textures = None):
if textures is None:
textures = {}
shader_def = params.get_shader_def(shader_name)
location = shader_node.location
x = location[0] - 600 + offset.x
y = location[1] + 300 + offset.y
c = 0
image_nodes = []
if shader_def and "textures" in shader_def.keys():
for shader_input in shader_node.inputs:
for texture_def in shader_def["textures"]:
socket_name = texture_def[0]
if socket_name == shader_input.name:
alpha_socket_name = texture_def[1]
tex_type = texture_def[2]
sample_map = len(texture_def) > 3 and texture_def[3] == "SAMPLE"
# check texture rules, if we should connect this texture at all
if not eval_texture_rules(tex_type):
continue
# there is no need to sample vertex colors for hair if there is Json Data present
if mat_json and sample_map and tex_type == "HAIRVERTEXCOLOR":
continue
json_id = imageutils.get_image_type_json_id(tex_type)
tex_json = jsonutils.get_texture_info(mat_json, json_id)
tex_path = None
suffix = None
image_id = "(" + tex_type + ")"
image_node = nodeutils.get_node_by_id(nodes, image_id)
# for user added materials, don't mess with the users textures...
image = None
if image_node and image_node.image and mat_cache.user_added:
image = image_node.image
elif tex_type == "HAIRVERTEXCOLOR" or tex_type == "WEIGHTMAP" or tex_type == "COLORID" or tex_type == "RGBMASK":
image = imageutils.find_material_image(mat, tex_type, processed_images, tex_json)
else:
image = imageutils.find_material_image(mat, tex_type, processed_images, tex_json, mat_json)
if image_node and image_node.image and image:
if image != image_node.image:
utils.log_info("Replacing image node image with: " + image.name)
image_node.image = image
try:
if image and image.filepath:
tex_path = image.filepath
else:
tex_path = tex_json["Texture Path"]
suffix = os.path.splitext(os.path.basename(tex_path))[0].split("_")[-1]
except:
tex_path = ""
suffix = ""
if sample_map:
# SAMPLE is a special case where the texture is sampled into a color value property:
# e.g Vertex Color sampled into hair_vertex_color
if image == None or len(obj.data.vertex_colors) == 0:
# if there is no sample map, set it's corresponding strength properties to zero:
# e.g. Vertex Color uses Vertex Color Strength with props: hair_vertex_color_strength
strength_socket_name = socket_name + " Strength"
nodeutils.set_node_input_value(shader_node, strength_socket_name, 0.0)
set_shader_input_props(shader_def, mat_cache, strength_socket_name, 0.0)
else:
vars.block_property_update = True
sample_prop = texture_def[4]
sample_color = [image.pixels[0], image.pixels[1], image.pixels[2], 1.0]
exec_prop(sample_prop, mat_cache, sample_color)
nodeutils.set_node_input_value(shader_node, socket_name, sample_color)
utils.log_detail(f"Sample Map Removing Image: {image}")
bpy.data.images.remove(image)
vars.block_property_update = False
elif image:
if not image_node:
image_node = nodeutils.make_image_node(nodes, image, image_id)
image_node.location = (x, y)
y += 100
x -= 300
c += 1
if c == 3:
c = 0
x += 900
y -= 700
set_image_node_tiling(nodes, links, image_node, mat_cache, texture_def,
shader_name, tex_json)
# ensure bump maps are connected to the correct socket
if socket_name == "Normal Map" and suffix and suffix.lower() == "bump":
socket_name = "Bump Map"
if socket_name:
if tex_type == "ALPHA" and "_diffuse" in image.name.lower():
nodeutils.link_nodes(links, image_node, "Alpha", shader_node, socket_name)
else:
nodeutils.link_nodes(links, image_node, "Color", shader_node, socket_name)
if alpha_socket_name:
nodeutils.link_nodes(links, image_node, "Alpha", shader_node, alpha_socket_name)
if image_node and image_node.image:
image_nodes.append(image_node)
textures[tex_type] = { "node": image_node, "image": image_node.image }
# main shader post processing
if not sub_shader:
# remove any extra image nodes:
if not mat_cache.user_added:
for n in nodes:
if n.type == "TEX_IMAGE" and n not in image_nodes:
utils.log_info("Removing unused image node: " + n.name)
nodes.remove(n)
# finally disconnect bump map if normal map is also present (this is only supposed to be one, but it is possible to bug CC3 and get both):
if nodeutils.has_connected_input(shader_node, "Bump Map") and nodeutils.has_connected_input(shader_node, "Normal Map"):
bump_node, bump_socket = nodeutils.get_node_and_socket_connected_to_input(shader_node, "Bump Map")
nodeutils.unlink_node_output(links, shader_node, "Bump Map")
def connect_tearline_shader(obj_cache, obj, mat, mat_json, processed_images):
props = vars.props()
prefs = vars.prefs()
mat_cache = props.get_material_cache(mat)
nodes = mat.node_tree.nodes
links = mat.node_tree.links
shader_label = "Tearline Shader"
shader_name = "rl_tearline_shader"
shader_group = "rl_tearline_shader"
mix_shader_group = ""
if prefs.render_target == "CYCLES":
shader_group = "rl_tearline_cycles_shader"
mix_shader_group = "rl_tearline_cycles_mix_shader"
bsdf, group = nodeutils.reset_shader(mat_cache, nodes, links, shader_label, shader_name, shader_group, mix_shader_group)
apply_prop_matrix(bsdf, group, mat_cache, shader_name)
nodeutils.clean_unused_image_nodes(nodes)
materials.set_material_alpha(mat, "BLEND", shadows=False)
def connect_eye_occlusion_shader(obj_cache, obj, mat, mat_json, processed_images):
props = vars.props()
prefs = vars.prefs()
mat_cache = props.get_material_cache(mat)