Skip to content

Commit

Permalink
factor out a reusable ModelFilter class
Browse files Browse the repository at this point in the history
  • Loading branch information
al2me6 committed Jul 25, 2024
1 parent ba9f3b2 commit 2f9f0a6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 30 deletions.
1 change: 1 addition & 0 deletions Source/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Shabby_FILES := \
Shabby.cs \
ShabLoader.cs \
MaterialDef.cs \
ModelFilter.cs \
MaterialReplacement.cs \
$e

Expand Down
37 changes: 7 additions & 30 deletions Source/MaterialReplacement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,49 +24,26 @@ You should have received a copy of the GNU General Public License
namespace Shabby
{

class MaterialReplacement
public class MaterialReplacement : ModelFilter
{
[Persistent(name = nameof(materialDef))] private string defName = null;
public MaterialDef materialDef = null;

public HashSet<string> targetMaterials;
public HashSet<string> targetTransforms;
public bool blanketApply;

public HashSet<string> ignoredMeshes;

readonly Dictionary<Material, Material> replacedMaterials = new Dictionary<Material, Material>();

public MaterialReplacement(ConfigNode node)
public MaterialReplacement(ConfigNode node) : base(node)
{
ConfigNode.LoadObjectFromConfig(this, node);

var defName = node.GetValue("materialDef");
if (string.IsNullOrEmpty(defName)) {
Debug.LogError("[Shabby] material replacement must reference a material definition");
} else if (!MaterialDefLibrary.items.TryGetValue(defName, out materialDef)) {
Debug.LogError($"[Shabby] failed to find material definition {defName}");
return;
}

targetMaterials = node.GetValuesList("targetMaterial").ToHashSet();
targetTransforms = node.GetValuesList("targetTransform").ToHashSet();

if (targetMaterials.Count > 0 && targetTransforms.Count > 0) {
Debug.LogError($"[Shabby] material replacement {defName} may not specify both materials and transforms");
targetTransforms.Clear();
if (!MaterialDefLibrary.items.TryGetValue(defName, out materialDef)) {
Debug.LogError($"[Shabby] failed to find material definition {defName}");
}

blanketApply = targetMaterials.Count == 0 && targetTransforms.Count == 0;

ignoredMeshes = node.GetValuesList("ignoreMesh").ToHashSet();
}

public bool MatchMaterial(Renderer renderer) => targetMaterials.Contains(renderer.sharedMaterial.name);

public bool MatchTransform(Transform transform) => targetTransforms.Contains(transform.name);

public void ApplyToSharedMaterialIfNotIgnored(Renderer renderer)
{
if (ignoredMeshes.Contains(renderer.transform.name)) return;
if (MatchIgnored(renderer)) return;
var sharedMat = renderer.sharedMaterial;
if (!replacedMaterials.TryGetValue(sharedMat, out var replacementMat)) {
replacementMat = materialDef.Instantiate(sharedMat);
Expand Down
54 changes: 54 additions & 0 deletions Source/ModelFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
This file is part of Shabby.
Shabby 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.
Shabby 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 Shabby. If not, see
<http://www.gnu.org/licenses/>.
*/

using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace Shabby
{
public class ModelFilter
{
public HashSet<string> targetMaterials;
public HashSet<string> targetTransforms;
public bool blanketApply;

public HashSet<string> ignoredMeshes;

public ModelFilter(ConfigNode node)
{
targetMaterials = node.GetValuesList("targetMaterial").ToHashSet();
targetTransforms = node.GetValuesList("targetTransform").ToHashSet();

if (targetMaterials.Count > 0 && targetTransforms.Count > 0) {
Debug.LogError("[Shabby] model filter may not specify both materials and transforms");
targetTransforms.Clear();
}

blanketApply = targetMaterials.Count == 0 && targetTransforms.Count == 0;

ignoredMeshes = node.GetValuesList("ignoreMesh").ToHashSet();
}

public bool MatchMaterial(Renderer renderer) => targetMaterials.Contains(renderer.sharedMaterial.name);
public bool MatchTransform(Transform transform) => targetTransforms.Contains(transform.name);

public bool MatchIgnored(Renderer renderer) => ignoredMeshes.Contains(renderer.transform.name);
}

}
1 change: 1 addition & 0 deletions Source/Shabby.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ModelFilter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Shabby.cs" />
<Compile Include="MaterialDef.cs" />
Expand Down

0 comments on commit 2f9f0a6

Please sign in to comment.