Skip to content
This repository was archived by the owner on Mar 21, 2025. It is now read-only.

Commit ade3558

Browse files
committed
Fix material setters for 3.0 models
1 parent 2ca3ebc commit ade3558

7 files changed

Lines changed: 121 additions & 91 deletions

File tree

Editor/Automation/BaseMaterialSetter.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using System;
2323
using System.Collections.Generic;
2424
using System.Linq;
25+
using System.Runtime.CompilerServices;
2526
using UnityEditor;
2627
using UnityEngine;
2728

@@ -58,6 +59,11 @@ private void SetProperties(MaterialJsonData matJsonData, Material material)
5859
{
5960
foreach (var (key, value) in ApplyTextures(EntriesToDict(matJsonData.Textures)))
6061
{
62+
if (string.IsNullOrEmpty(key))
63+
{
64+
continue;
65+
}
66+
6167
if (value.IsNull)
6268
{
6369
material.SetTexture(key, null);
@@ -84,16 +90,31 @@ private void SetProperties(MaterialJsonData matJsonData, Material material)
8490

8591
foreach (var (key, value) in ApplyInts(EntriesToDict(matJsonData.Ints)))
8692
{
93+
if (string.IsNullOrEmpty(key))
94+
{
95+
continue;
96+
}
97+
8798
material.SetInt(key, value);
8899
}
89100

90101
foreach (var (key, value) in ApplyFloats(EntriesToDict(matJsonData.Floats)))
91102
{
103+
if (string.IsNullOrEmpty(key))
104+
{
105+
continue;
106+
}
107+
92108
material.SetFloat(key, value);
93109
}
94110

95111
foreach (var (key, value) in ApplyColors(EntriesToDict(matJsonData.Colors)))
96112
{
113+
if (string.IsNullOrEmpty(key))
114+
{
115+
continue;
116+
}
117+
97118
material.SetColor(key, value);
98119
}
99120
}
@@ -152,5 +173,21 @@ public static Dictionary<string, BaseMaterialSetter> AllShaderMap
152173
{
153174
yield break;
154175
}
176+
177+
protected static (string, T) MakeProperty<T>(string name, IReadOnlyDictionary<string, T> values, string key)
178+
{
179+
return values.TryGetValue(key, out T value) ? (name, value) : (null, default);
180+
}
181+
182+
protected static (string, T) MakeProperty<T>(string name, IReadOnlyDictionary<string, T> values)
183+
{
184+
return MakeProperty(name, values, name);
185+
}
186+
187+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
188+
protected static (string, T) MakeProperty<T>(string name, T value)
189+
{
190+
return (name, value);
191+
}
155192
}
156193
}

Editor/Automation/BuiltinMaterialSetters/BodyMaterialSetter.cs

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -34,77 +34,73 @@ public class BodyMaterialSetter : BaseMaterialSetter
3434

3535
protected override IEnumerable<(string, TextureJsonData)> ApplyTextures(IReadOnlyDictionary<string, TextureJsonData> textures)
3636
{
37-
yield return ("_MainTex", textures["_MainTex"]);
38-
yield return ("_LightMap", textures["_LightMap"]);
39-
yield return ("_RampMapWarm", textures["_DiffuseRampMultiTex"]);
40-
yield return ("_RampMapCool", textures["_DiffuseCoolRampMultiTex"]);
37+
yield return MakeProperty("_MainTex", textures);
38+
yield return MakeProperty("_LightMap", textures);
39+
yield return MakeProperty("_RampMapWarm", textures, "_DiffuseRampMultiTex");
40+
yield return MakeProperty("_RampMapCool", textures, "_DiffuseCoolRampMultiTex");
4141

42-
yield return ("_StockingsMap", textures["_StockRangeTex"]);
42+
yield return MakeProperty("_StockingsMap", textures, "_StockRangeTex");
4343
}
4444

4545
protected override IEnumerable<(string, float)> ApplyFloats(IReadOnlyDictionary<string, float> floats)
4646
{
47-
// yield return ("_Cull", floats["_CullMode"]);
47+
// yield return MakeProperty("_Cull", floats, "_CullMode");
4848

4949
// TODO Float 某些值不准,比如 _SrcBlend 和 _DstBlend
50-
// yield return ("_SrcBlendColor", floats["_SrcBlend"]);
51-
// yield return ("_DstBlendColor", floats["_DstBlend"]);
50+
// yield return MakeProperty("_SrcBlendColor", floats, "_SrcBlend");
51+
// yield return MakeProperty("_DstBlendColor", floats, "_DstBlend");
5252

53-
yield return ("_AlphaTest", floats["_EnableAlphaCutoff"]);
54-
yield return ("_AlphaTestThreshold", floats["_AlphaCutoff"]);
53+
yield return MakeProperty("_AlphaTest", floats, "_EnableAlphaCutoff");
54+
yield return MakeProperty("_AlphaTestThreshold", floats, "_AlphaCutoff");
5555

56-
yield return ("_EmissionThreshold", floats["_EmissionThreshold"]);
57-
yield return ("_EmissionIntensity", floats["_EmissionIntensity"]);
56+
yield return MakeProperty("_EmissionThreshold", floats);
57+
yield return MakeProperty("_EmissionIntensity", floats);
5858

59-
yield return ("_RimShadowCt", floats["_RimShadowCt"]);
60-
yield return ("_RimShadowIntensity", floats["_RimShadowIntensity"]);
59+
yield return MakeProperty("_RimShadowCt", floats);
60+
yield return MakeProperty("_RimShadowIntensity", floats);
6161

6262
for (int i = 0; i <= 7; i++)
6363
{
64-
yield return ($"_SpecularIntensity{i}", floats[$"_SpecularIntensity{i}"]);
65-
yield return ($"_SpecularShininess{i}", floats[$"_SpecularShininess{i}"]);
66-
yield return ($"_SpecularRoughness{i}", floats[$"_SpecularRoughness{i}"]);
64+
yield return MakeProperty($"_SpecularIntensity{i}", floats);
65+
yield return MakeProperty($"_SpecularShininess{i}", floats);
66+
yield return MakeProperty($"_SpecularRoughness{i}", floats);
6767

68-
yield return ($"_RimShadowWidth{i}", floats[$"_RimShadowWidth{i}"]);
69-
yield return ($"_RimShadowFeather{i}", floats[$"_RimShadowFeather{i}"]);
68+
yield return MakeProperty($"_RimShadowWidth{i}", floats);
69+
yield return MakeProperty($"_RimShadowFeather{i}", floats);
7070

71-
yield return ($"_mmBloomIntensity{i}", floats[$"_mBloomIntensity{i}"]);
71+
yield return MakeProperty($"_mmBloomIntensity{i}", floats, $"_mBloomIntensity{i}");
7272
}
7373

7474
// Stockings
75-
yield return ("_StockingsDarkWidth", floats["_StockDarkWidth"]);
76-
yield return ("_StockingsPower", floats["_Stockpower"]);
77-
yield return ("_StockingsLightedWidth", floats["_Stockpower1"]);
78-
yield return ("_StockingsLightedIntensity", floats["_StockSP"]);
79-
yield return ("_StockingsRoughness", floats["_StockRoughness"]);
75+
yield return MakeProperty("_StockingsDarkWidth", floats, "_StockDarkWidth");
76+
yield return MakeProperty("_StockingsPower", floats, "_Stockpower");
77+
yield return MakeProperty("_StockingsLightedWidth", floats, "_Stockpower1");
78+
yield return MakeProperty("_StockingsLightedIntensity", floats, "_StockSP");
79+
yield return MakeProperty("_StockingsRoughness", floats, "_StockRoughness");
8080
}
8181

8282
protected override IEnumerable<(string, Color)> ApplyColors(IReadOnlyDictionary<string, Color> colors)
8383
{
84-
yield return ("_Color", colors["_Color"]);
85-
yield return ("_BackColor", colors["_BackColor"]);
86-
yield return ("_EmissionColor", colors["_EmissionTintColor"]);
87-
yield return ("_RimShadowOffset", colors["_RimShadowOffset"]);
84+
yield return MakeProperty("_Color", colors);
85+
yield return MakeProperty("_BackColor", colors);
86+
yield return MakeProperty("_EmissionColor", colors, "_EmissionTintColor");
87+
yield return MakeProperty("_RimShadowOffset", colors);
8888

8989
for (int i = 0; i <= 7; i++)
9090
{
91-
yield return ($"_SpecularColor{i}", colors[$"_SpecularColor{i}"]);
92-
yield return ($"_RimColor{i}", colors[$"_RimColor{i}"]);
93-
yield return ($"_RimShadowColor{i}", colors[$"_RimShadowColor{i}"]);
94-
yield return ($"_OutlineColor{i}", colors[$"_OutlineColor{i}"]);
95-
96-
if (colors.TryGetValue($"_mBloomColor{i}", out Color bloomColor))
97-
{
98-
yield return ($"_BloomColor{i}", bloomColor);
99-
}
91+
yield return MakeProperty($"_SpecularColor{i}", colors);
92+
yield return MakeProperty($"_RimColor{i}", colors);
93+
yield return MakeProperty($"_RimShadowColor{i}", colors);
94+
yield return MakeProperty($"_OutlineColor{i}", colors);
95+
yield return MakeProperty($"_BloomColor{i}", colors, $"_mBloomColor{i}");
10096
}
10197

10298
// Texture Scale Offset
103-
yield return ("_Maps_ST", colors["_MainMaps_ST"]);
99+
yield return MakeProperty("_Maps_ST", colors, "_MainMaps_ST");
104100

105101
// Stockings
106-
yield return ("_StockingsColor", colors["_Stockcolor"]);
107-
yield return ("_StockingsColorDark", colors["_StockDarkcolor"]);
102+
yield return MakeProperty("_StockingsColor", colors, "_Stockcolor");
103+
yield return MakeProperty("_StockingsColorDark", colors, "_StockDarkcolor");
108104
}
109105
}
110106
}

Editor/Automation/BuiltinMaterialSetters/EyeShadowMaterialSetter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class EyeShadowMaterialSetter : BaseMaterialSetter
3333

3434
protected override IEnumerable<(string, Color)> ApplyColors(IReadOnlyDictionary<string, Color> colors)
3535
{
36-
yield return ("_Color", colors["_Color"]);
36+
yield return MakeProperty("_Color", colors);
3737
}
3838
}
3939
}

Editor/Automation/BuiltinMaterialSetters/FaceMaskMaterialSetter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class FaceMaskMaterialSetter : BaseMaterialSetter
3333

3434
protected override IEnumerable<(string, Color)> ApplyColors(IReadOnlyDictionary<string, Color> colors)
3535
{
36-
yield return ("_Color", colors["_Color"]);
36+
yield return MakeProperty("_Color", colors);
3737
}
3838
}
3939
}

Editor/Automation/BuiltinMaterialSetters/FaceMaterialSetter.cs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,43 +33,40 @@ public class FaceMaterialSetter : BaseMaterialSetter
3333

3434
protected override IEnumerable<(string, TextureJsonData)> ApplyTextures(IReadOnlyDictionary<string, TextureJsonData> textures)
3535
{
36-
yield return ("_MainTex", textures["_MainTex"]);
37-
yield return ("_FaceMap", textures["_FaceMap"]);
38-
yield return ("_ExpressionMap", textures["_FaceExpression"]);
36+
yield return MakeProperty("_MainTex", textures);
37+
yield return MakeProperty("_FaceMap", textures);
38+
yield return MakeProperty("_ExpressionMap", textures, "_FaceExpression");
3939
}
4040

4141
protected override IEnumerable<(string, float)> ApplyFloats(IReadOnlyDictionary<string, float> floats)
4242
{
43-
if (floats.TryGetValue("_UseUVChannel2", out float useUV2))
44-
{
45-
yield return ("_FaceMapUV2", useUV2);
46-
}
43+
yield return MakeProperty("_FaceMapUV2", floats, "_UseUVChannel2");
4744

48-
yield return ("_EmissionThreshold", floats["_EmissionThreshold"]);
49-
yield return ("_EmissionIntensity", floats["_EmissionIntensity"]);
45+
yield return MakeProperty("_EmissionThreshold", floats);
46+
yield return MakeProperty("_EmissionIntensity", floats);
5047

51-
yield return ("_NoseLinePower", floats["_NoseLinePower"]);
48+
yield return MakeProperty("_NoseLinePower", floats);
5249

53-
yield return ("_mmBloomIntensity0", floats["_mBloomIntensity0"]);
50+
yield return MakeProperty("_mmBloomIntensity0", floats, "_mBloomIntensity0");
5451
}
5552

5653
protected override IEnumerable<(string, Color)> ApplyColors(IReadOnlyDictionary<string, Color> colors)
5754
{
58-
yield return ("_Color", colors["_Color"]);
59-
yield return ("_ShadowColor", colors["_ShadowColor"]);
60-
yield return ("_EyeShadowColor", colors["_EyeShadowColor"]);
61-
yield return ("_EmissionColor", Color.white); // 眼睛高亮
62-
yield return ("_OutlineColor0", colors["_OutlineColor"]);
63-
yield return ("_NoseLineColor", colors["_NoseLineColor"]);
55+
yield return MakeProperty("_Color", colors);
56+
yield return MakeProperty("_ShadowColor", colors);
57+
yield return MakeProperty("_EyeShadowColor", colors);
58+
yield return MakeProperty("_EmissionColor", Color.white); // 眼睛高亮
59+
yield return MakeProperty("_OutlineColor0", colors, "_OutlineColor");
60+
yield return MakeProperty("_NoseLineColor", colors);
6461

6562
// Texture Scale Offset
66-
yield return ("_Maps_ST", colors["_MainMaps_ST"]);
63+
yield return MakeProperty("_Maps_ST", colors, "_MainMaps_ST");
6764

6865
// Expression
69-
yield return ("_ExCheekColor", colors["_ExCheekColor"]);
70-
yield return ("_ExShyColor", colors["_ExShyColor"]);
71-
yield return ("_ExShadowColor", colors["_ExShadowColor"]);
72-
yield return ("_ExEyeColor", colors["_ExEyeColor"]);
66+
yield return MakeProperty("_ExCheekColor", colors);
67+
yield return MakeProperty("_ExShyColor", colors);
68+
yield return MakeProperty("_ExShadowColor", colors);
69+
yield return MakeProperty("_ExEyeColor", colors);
7370
}
7471
}
7572
}

Editor/Automation/BuiltinMaterialSetters/HairMaterialSetter.cs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,47 +33,47 @@ public class HairMaterialSetter : BaseMaterialSetter
3333

3434
protected override IEnumerable<(string, TextureJsonData)> ApplyTextures(IReadOnlyDictionary<string, TextureJsonData> textures)
3535
{
36-
yield return ("_MainTex", textures["_MainTex"]);
37-
yield return ("_LightMap", textures["_LightMap"]);
38-
yield return ("_RampMapWarm", textures["_DiffuseRampMultiTex"]);
39-
yield return ("_RampMapCool", textures["_DiffuseCoolRampMultiTex"]);
36+
yield return MakeProperty("_MainTex", textures);
37+
yield return MakeProperty("_LightMap", textures);
38+
yield return MakeProperty("_RampMapWarm", textures, "_DiffuseRampMultiTex");
39+
yield return MakeProperty("_RampMapCool", textures, "_DiffuseCoolRampMultiTex");
4040
}
4141

4242
protected override IEnumerable<(string, float)> ApplyFloats(IReadOnlyDictionary<string, float> floats)
4343
{
44-
// yield return ("_Cull", floats["_CullMode"]);
44+
// yield return MakeProperty("_Cull", floats, "_CullMode");
4545

46-
yield return ("_AlphaTest", floats["_EnableAlphaCutoff"]);
47-
yield return ("_AlphaTestThreshold", floats["_AlphaCutoff"]);
46+
yield return MakeProperty("_AlphaTest", floats, "_EnableAlphaCutoff");
47+
yield return MakeProperty("_AlphaTestThreshold", floats, "_AlphaCutoff");
4848

49-
yield return ("_EmissionThreshold", floats["_EmissionThreshold"]);
50-
yield return ("_EmissionIntensity", floats["_EmissionIntensity"]);
49+
yield return MakeProperty("_EmissionThreshold", floats);
50+
yield return MakeProperty("_EmissionIntensity", floats);
5151

52-
yield return ("_RimShadowCt", floats["_RimShadowCt"]);
53-
yield return ("_RimShadowIntensity", floats["_RimShadowIntensity"]);
54-
yield return ("_RimShadowWidth0", floats["_RimShadowWidth0"]);
55-
yield return ("_RimShadowFeather0", floats["_RimShadowFeather0"]);
52+
yield return MakeProperty("_RimShadowCt", floats);
53+
yield return MakeProperty("_RimShadowIntensity", floats);
54+
yield return MakeProperty("_RimShadowWidth0", floats);
55+
yield return MakeProperty("_RimShadowFeather0", floats);
5656

57-
yield return ("_SpecularIntensity0", floats["_SpecularIntensity0"]);
58-
yield return ("_SpecularShininess0", floats["_SpecularShininess0"]);
59-
yield return ("_SpecularRoughness0", floats["_SpecularRoughness0"]);
57+
yield return MakeProperty("_SpecularIntensity0", floats);
58+
yield return MakeProperty("_SpecularShininess0", floats);
59+
yield return MakeProperty("_SpecularRoughness0", floats);
6060

61-
yield return ("_mmBloomIntensity0", floats["_mBloomIntensity0"]);
61+
yield return MakeProperty("_mmBloomIntensity0", floats, "_mBloomIntensity0");
6262
}
6363

6464
protected override IEnumerable<(string, Color)> ApplyColors(IReadOnlyDictionary<string, Color> colors)
6565
{
66-
yield return ("_Color", colors["_Color"]);
67-
yield return ("_BackColor", colors["_BackColor"]);
68-
yield return ("_SpecularColor0", colors["_SpecularColor0"]);
69-
yield return ("_RimColor0", colors["_RimColor0"]);
70-
yield return ("_OutlineColor0", colors["_OutlineColor0"]);
66+
yield return MakeProperty("_Color", colors);
67+
yield return MakeProperty("_BackColor", colors);
68+
yield return MakeProperty("_SpecularColor0", colors);
69+
yield return MakeProperty("_RimColor0", colors);
70+
yield return MakeProperty("_OutlineColor0", colors);
7171

72-
yield return ("_RimShadowOffset", colors["_RimShadowOffset"]);
73-
yield return ("_RimShadowColor0", colors["_RimShadowColor0"]);
72+
yield return MakeProperty("_RimShadowOffset", colors);
73+
yield return MakeProperty("_RimShadowColor0", colors);
7474

7575
// Texture Scale Offset
76-
yield return ("_Maps_ST", colors["_MainMaps_ST"]);
76+
yield return MakeProperty("_Maps_ST", colors, "_MainMaps_ST");
7777
}
7878
}
7979
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.stalomeow.star-rail-npr-shader",
3-
"version": "2.10.1",
3+
"version": "2.10.2",
44
"displayName": "StarRail NPR Shader",
55
"description": "Fan-made shaders for Unity URP attempting to replicate the shading of Honkai: Star Rail.",
66
"unity": "2022.3",

0 commit comments

Comments
 (0)