Skip to content

Commit

Permalink
Merge pull request #83 from KisaragiEffective/refactor/split-interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
KisaragiEffective authored Sep 12, 2024
2 parents b2b9e01 + bc3dddd commit 87c06d1
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 43 deletions.
14 changes: 0 additions & 14 deletions Editor/Transform/Environment/Common/IPostExpansionTransformer.cs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using ResoniteImportHelper.Marker;
using ResoniteImportHelper.UnityEditorUtility;
using UnityEngine;

namespace ResoniteImportHelper.Transform.Environment.Common
{
/// <summary>
/// <see cref="Material.shader"/> を変更しない<see cref="Material"/>の書き換えを定義する。
/// </summary>
internal interface ISameShaderMaterialTransformPass
{
[NotPublicAPI]
public Material RewriteInline(Material material);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions Editor/Transform/Environment/Common/IShaderFamily.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using ResoniteImportHelper.Marker;
using UnityEngine;

namespace ResoniteImportHelper.Transform.Environment.Common
{
/// <summary>
/// <see cref="Shader"/> の集合を定義する。
/// </summary>
internal interface IShaderFamily
{
[NotPublicAPI]
public bool Contains(Shader shader);

[NotPublicAPI]
internal sealed bool Contains(Material material) => this.Contains(material.shader);
}
}
3 changes: 3 additions & 0 deletions Editor/Transform/Environment/Common/IShaderFamily.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "ResoniteImportHelper.Transform.Environment.Common",
"rootNamespace": "global::ResoniteImportHelper.Transform.Environment.Common",
"references": [
"GUID:da758e96819041b2b57ad6379d1c0c58"
"GUID:da758e96819041b2b57ad6379d1c0c58",
"GUID:041f840041904fa4965ea30baa9487a6"
],
"includePlatforms": [
"Editor"
Expand Down
42 changes: 17 additions & 25 deletions Editor/Transform/Environment/LilToon/LilToonHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace ResoniteImportHelper.Transform.Environment.LilToon
{
internal sealed class LilToonHandler: IPostExpansionTransformer
internal sealed class LilToonHandler: ISameShaderMaterialTransformPass
{
private static ResourceAllocator currentAllocator;

Expand All @@ -30,8 +30,7 @@ internal LilToonHandler(ResourceAllocator allocator)
#warning lilToon 2.0.0 is under develop and this Transformer may not able to work or be compiled correctly.
#warning This is not supported yet. Please downgrade lilToon to 1.x series.
#endif
[NotPublicAPI]
public void PerformInlineTransform(GameObject modifiableRoot)
internal void PerformInlineTransform(GameObject modifiableRoot)
{
#if !RIH_HAS_LILTOON
Debug.LogWarning("This project does not have supported version of lilToon, skipping this IPostExpansionTransformer");
Expand All @@ -42,31 +41,11 @@ public void PerformInlineTransform(GameObject modifiableRoot)
o.TryGetComponent(out SkinnedMeshRenderer smr) ? smr : null
).Where(o => o != null))
{
var sharedMaterials = new List<Material>(renderer.sharedMaterials.Length);

foreach (var material in renderer.sharedMaterials)
{
if (UsesLilToonShader(material))
{
var variant = currentAllocator.Save(MaterialUtility.CreateVariant(material));
PerformBakeTexture(variant);
sharedMaterials.Add(variant);
}
else
{
sharedMaterials.Add(material);
}
}

renderer.sharedMaterials = sharedMaterials.ToArray();
renderer.sharedMaterials = renderer.sharedMaterials.Select(RewriteInline).ToArray();
}
}

private static bool UsesLilToonShader(Material m)
{
// FIXME: this is fuzzy
return m.shader.name.StartsWith("Hidden/lil");
}
private static bool UsesLilToonShader(Material m) => LilToonShaderFamily.Instance.Contains(m.shader);

private static void PerformBakeTexture(Material m)
{
Expand Down Expand Up @@ -228,5 +207,18 @@ [CanBeNull] HarmonyLib.Harmony
#endif
}
#endregion

[NotPublicAPI]
public Material RewriteInline(Material material)
{
Debug.Log($"try rewrite: {material} ({AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(material))})");

if (!UsesLilToonShader(material)) return material;

var variant = currentAllocator.Save(MaterialUtility.CreateVariant(material));
PerformBakeTexture(variant);
return variant;

}
}
}
20 changes: 20 additions & 0 deletions Editor/Transform/Environment/LilToon/LilToonShaderFamily.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using ResoniteImportHelper.Marker;
using ResoniteImportHelper.Transform.Environment.Common;
using UnityEngine;

namespace ResoniteImportHelper.Transform.Environment.LilToon
{
internal sealed class LilToonShaderFamily: IShaderFamily
{
internal static readonly LilToonShaderFamily Instance = new();

private LilToonShaderFamily() {}

[NotPublicAPI]
public bool Contains(Shader shader)
{
// FIXME: this is fuzzy
return shader.name.StartsWith("Hidden/lil");
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Editor/UnityEditorUtility/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
[assembly: InternalsVisibleTo("ResoniteImportHelper.Serialization")]
[assembly: InternalsVisibleTo("ResoniteImportHelper.Lint")]
[assembly: InternalsVisibleTo("ResoniteImportHelper.Transform.Environment.LilToon")]
[assembly: InternalsVisibleTo("ResoniteImportHelper.Transform.Environment.Common")]

0 comments on commit 87c06d1

Please sign in to comment.