Skip to content

Commit f0b31f4

Browse files
authored
feat: assign material props when changing a material to use Toon3Das2D from Toon (#656)
* add MaterialPropertyValue class * add ToonMaterialUtility * update ToonEditorConstants * use string constants with slashes
1 parent 938e0b2 commit f0b31f4

File tree

6 files changed

+207
-1
lines changed

6 files changed

+207
-1
lines changed

com.unity.toonshader/Editor/Scripts/GUI/UnityToon3Das2DGUI.cs

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using System.Collections.Generic;
22
using Unity.Rendering.Toon;
33
using UnityEngine;
4+
using UnityEngine.Rendering;
45

56
namespace UnityEditor.Rendering.Toon {
67
internal class UnityToon3Das2DGUI : UnityEditor.ShaderGUI {
7-
private Material m_lastMaterial;
88

99
public override void OnGUI(MaterialEditor mEditor, MaterialProperty[] props) {
1010

@@ -274,6 +274,87 @@ MaterialPropertyInfo MaterialNameToPropertyInfo(MaterialName m, MaterialProperty
274274

275275
//----------------------------------------------------------------------------------------------------------------------
276276

277+
//key: Toon3Das2D prop. value: Toon3D prop
278+
static readonly Dictionary<string, string> TOON_3D_AS_2D_TO_3D_MAP = new Dictionary<string, string> {
279+
280+
// Shading thresholds
281+
{ ToonConstants.SHADER_PROP_BASE_TO_1ST_SHADE_START, "_BaseColor_Step" },
282+
{ ToonConstants.SHADER_PROP_BASE_TO_1ST_SHADE_FEATHER, "_BaseShade_Feather" },
283+
{ ToonConstants.SHADER_PROP_1ST_TO_2ND_SHADE_START, "_1st_ShadeColor_Step" },
284+
{ ToonConstants.SHADER_PROP_1ST_TO_2ND_SHADE_FEATHER, "_1st_ShadeColor_Feather" },
285+
286+
// Highlight
287+
{ ToonConstants.SHADER_PROP_HIGHLIGHT_COLOR, "_HighColor" },
288+
{ ToonConstants.SHADER_PROP_HIGHLIGHT_TEX, "_HighColor_Tex" },
289+
290+
// Outline
291+
{ ToonConstants.SHADER_PROP_OUTLINE_MODE, "_OUTLINE" },
292+
{ ToonConstants.SHADER_PROP_OUTLINE_WIDTH, "_Outline_Width" },
293+
{ ToonConstants.SHADER_PROP_OUTLINE_WIDTH_MAP, "_Outline_Sampler" },
294+
{ ToonConstants.SHADER_PROP_OUTLINE_COLOR, "_Outline_Color" },
295+
{ ToonConstants.SHADER_PROP_OUTLINE_BASE_COLOR_BLEND, "_Is_BlendBaseColor" },
296+
{ ToonConstants.SHADER_PROP_OUTLINE_LIGHT_COLOR_BLEND, "_Is_LightColor_Outline" },
297+
{ ToonConstants.SHADER_PROP_OUTLINE_OFFSET_Z, "_Offset_Z" },
298+
{ ToonConstants.SHADER_PROP_OUTLINE_NEAR, "_Nearest_Distance" },
299+
{ ToonConstants.SHADER_PROP_OUTLINE_FAR, "_Farthest_Distance" },
300+
301+
// Outline normal options (closest equivalents)
302+
{ ToonConstants.SHADER_PROP_OUTLINE_USE_NORMAL_MAP, "_Is_BakedNormal" },
303+
{ ToonConstants.SHADER_PROP_OUTLINE_NORMAL_MAP, "_BakedNormal" },
304+
};
305+
306+
307+
308+
//----------------------------------------------------------------------------------------------------------------------
309+
public override void AssignNewShaderToMaterial(
310+
Material mat,
311+
Shader oldShader,
312+
Shader newShader)
313+
{
314+
315+
string oldShaderPath = AssetDatabase.GetAssetPath(oldShader);
316+
if (!oldShaderPath.StartsWith(ToonEditorConstants.PACKAGE_PATH)) {
317+
base.AssignNewShaderToMaterial(mat, oldShader, newShader);
318+
return;
319+
}
320+
321+
//Upgrade from Toon 3D
322+
bool upgradeFromToon3D = oldShader.name.EndsWith("Toon") || oldShader.name.EndsWith("Toon(Tessellation)");
323+
if (!upgradeFromToon3D) {
324+
base.AssignNewShaderToMaterial(mat, oldShader, newShader);
325+
return;
326+
}
327+
Dictionary<string, MaterialPropertyValue> captured = ToonMaterialUtility.CaptureMaterialValues(mat);
328+
329+
base.AssignNewShaderToMaterial(mat, oldShader, newShader);
330+
331+
//Assign captured values
332+
foreach (KeyValuePair<string, string> kv in TOON_3D_AS_2D_TO_3D_MAP) {
333+
string targetName = kv.Key;
334+
string srcName = kv.Value;
335+
336+
// Ensure target exists in new material and we captured the source
337+
if (!mat.HasProperty(targetName) || !captured.TryGetValue(srcName, out MaterialPropertyValue srcVal)) {
338+
continue;
339+
}
340+
341+
// Ensure types match between new target and captured source
342+
int targetIndex = newShader.FindPropertyIndex(targetName);
343+
if (targetIndex < 0) {
344+
continue;
345+
}
346+
ShaderPropertyType targetType = newShader.GetPropertyType(targetIndex);
347+
if (targetType != srcVal.type) {
348+
continue;
349+
}
350+
351+
srcVal.ApplyToMaterial(mat, targetName);
352+
}
353+
354+
}
355+
356+
357+
277358
private readonly Dictionary<string, MaterialPropertyUIElement> m_materialPropertyUIElements = new Dictionary<string, MaterialPropertyUIElement>();
278359

279360
private ToonMaterialState m_materialState;
@@ -534,6 +615,9 @@ static readonly GUIContent LIGHTING_FOLDOUT
534615
bool m_normalMapFoldout = false;
535616
bool m_outlineFoldout = false;
536617
bool m_lightingFoldout = false;
618+
619+
private Material m_lastMaterial;
620+
537621

538622
struct ToonMaterialState {
539623
internal bool useOutline;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11

2+
using System.IO;
3+
using Unity.Rendering.Toon;
4+
25
namespace UnityEditor.Rendering.Toon {
36

47
internal static class ToonEditorConstants {
58

69
internal const int CUR_MATERIAL_VERSION = (int) ToonMaterialVersion.Initial;
10+
11+
internal static readonly string PACKAGE_PATH = Path.Combine("Packages", ToonConstants.PACKAGE_NAME).Replace('\\','/');
12+
713
}
814

915
} //end namespace
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using UnityEngine;
2+
using UnityEngine.Rendering;
3+
4+
namespace Unity.Rendering.Toon {
5+
6+
[System.Serializable]
7+
internal class MaterialPropertyValue {
8+
internal static MaterialPropertyValue FromMaterial(Material mat, int propIndex) {
9+
MaterialPropertyValue value = new MaterialPropertyValue();
10+
Shader shader = mat.shader;
11+
string name = shader.GetPropertyName(propIndex);
12+
ShaderPropertyType type = shader.GetPropertyType(propIndex);
13+
value.type = type;
14+
15+
switch (type) {
16+
case ShaderPropertyType.Color:
17+
value.color = mat.GetColor(name);
18+
break;
19+
case ShaderPropertyType.Vector:
20+
value.vector = mat.GetVector(name);
21+
break;
22+
case ShaderPropertyType.Float:
23+
case ShaderPropertyType.Range:
24+
value.floatValue = mat.GetFloat(name);
25+
break;
26+
case ShaderPropertyType.Texture:
27+
value.texture = mat.GetTexture(name);
28+
value.texOffset = mat.GetTextureOffset(name);
29+
value.texScale = mat.GetTextureScale(name);
30+
break;
31+
}
32+
33+
return value;
34+
}
35+
36+
internal void ApplyToMaterial(Material mat, string targetName) {
37+
switch (type) {
38+
case ShaderPropertyType.Color:
39+
mat.SetColor(targetName, color);
40+
break;
41+
case ShaderPropertyType.Vector:
42+
mat.SetVector(targetName, vector);
43+
break;
44+
case ShaderPropertyType.Float:
45+
case ShaderPropertyType.Range:
46+
mat.SetFloat(targetName, floatValue);
47+
break;
48+
case ShaderPropertyType.Texture:
49+
mat.SetTexture(targetName, texture);
50+
mat.SetTextureOffset(targetName, texOffset);
51+
mat.SetTextureScale(targetName, texScale);
52+
break;
53+
}
54+
}
55+
56+
//----------------------------------------------------------------------------------------------------------------------
57+
58+
internal ShaderPropertyType type;
59+
internal Color color;
60+
internal Vector4 vector;
61+
internal float floatValue;
62+
internal Texture texture;
63+
internal Vector2 texOffset;
64+
internal Vector2 texScale;
65+
66+
67+
}
68+
69+
} //end namespace

com.unity.toonshader/Runtime/Scripts/MaterialPropertyValue.cs.meta

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Collections.Generic;
2+
using UnityEngine;
3+
4+
namespace Unity.Rendering.Toon {
5+
6+
internal static class ToonMaterialUtility {
7+
internal static Dictionary<string, MaterialPropertyValue> CaptureMaterialValues(Material mat) {
8+
9+
Dictionary<string, MaterialPropertyValue> store = new Dictionary<string, MaterialPropertyValue>();
10+
Shader shader = mat.shader;
11+
int count = shader.GetPropertyCount();
12+
for (int i = 0; i < count; i++) {
13+
string name = shader.GetPropertyName(i);
14+
15+
if (!mat.HasProperty(name))
16+
continue;
17+
MaterialPropertyValue value = MaterialPropertyValue.FromMaterial(mat, i);
18+
store[name] = value;
19+
}
20+
return store;
21+
}
22+
23+
}
24+
25+
}

com.unity.toonshader/Runtime/Scripts/Utilities/ToonMaterialUtility.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)