-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b47b0a
commit ce78cd5
Showing
13 changed files
with
199 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using cc.dingemans.bigibas123.MaterialDedup.Editor.Model; | ||
using cc.dingemans.bigibas123.MaterialDedup.Runtime; | ||
using UnityEngine; | ||
|
||
namespace cc.dingemans.bigibas123.MaterialDedup.Editor | ||
{ | ||
public static class MatSlotExtensions | ||
{ | ||
public static IEnumerable<MaterialReference> AsMaterialRefs(this MaterialDeduplicatorBehavior dup) | ||
{ | ||
//TODO add materials referenced in animations as well | ||
return dup.Renderers.AsMaterialRefs(); | ||
} | ||
|
||
public static IEnumerable<MaterialReference> AsMaterialRefs(this IEnumerable<Renderer> renders) => | ||
renders.SelectMany(renderer => renderer.AsMaterialRefs()); | ||
|
||
public static IEnumerable<MaterialReference> AsMaterialRefs(this Renderer rend) => | ||
rend.sharedMaterials.Select((_, b) => new MaterialReference(rend, b)); | ||
|
||
public static List<DeduplicatedMaterial> AsDedupList(this IEnumerable<MaterialReference> avatarMaterials) | ||
{ | ||
var resolvedMaterials = new List<DeduplicatedMaterial>(); | ||
foreach (var avatarMat in avatarMaterials) | ||
{ | ||
if (avatarMat.Material == null) continue; | ||
|
||
var found = false; | ||
foreach (var resolved in resolvedMaterials) | ||
{ | ||
if ( | ||
avatarMat.HasPropertiesSameAs(resolved) | ||
&& IsAnimatedTheSame(resolved,avatarMat) | ||
) | ||
{ | ||
found = true; | ||
resolved.AddRefForReplacement(avatarMat); | ||
break; | ||
} | ||
} | ||
|
||
if (found) continue; | ||
var newMaterial = new DeduplicatedMaterial(avatarMat.Material); | ||
newMaterial.AddRefForReplacement(avatarMat); | ||
resolvedMaterials.Add(newMaterial); | ||
} | ||
|
||
return resolvedMaterials; | ||
} | ||
|
||
private static bool IsAnimatedTheSame(DeduplicatedMaterial dedupped, MaterialReference mat) | ||
{ | ||
return true; //TODO | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,47 @@ | ||
using cc.dingemans.bigibas123.MaterialDedup.Runtime; | ||
using System.Linq; | ||
using cc.dingemans.bigibas123.MaterialDedup.Runtime; | ||
using UnityEditor; | ||
using UnityEditor.UIElements; | ||
using UnityEngine; | ||
using UnityEngine.UIElements; | ||
|
||
namespace cc.dingemans.bigibas123.MaterialDedup.Editor | ||
{ | ||
[CustomEditor(typeof(MaterialDeduplicatorBehavior))] | ||
public class MaterialDeduplicatorBehaviorEditor : UnityEditor.Editor | ||
{ | ||
|
||
public override VisualElement CreateInspectorGUI() | ||
{ | ||
VisualElement inspector = new VisualElement(); | ||
foreach (var renderer in ((MaterialDeduplicatorBehavior)target).Renderers) | ||
{ | ||
var rendererFold = new Foldout | ||
{ | ||
text = renderer.name, | ||
}; | ||
var container = rendererFold.contentContainer; | ||
|
||
var list = renderer.AsMaterialRefs().ToList(); | ||
var listView = new ListView(list, | ||
makeItem: () => new GroupBox { focusable = false}, | ||
bindItem: (elem, i) => | ||
{ | ||
var matRef = list[i]; | ||
var gb = elem as GroupBox; | ||
var of = new ObjectField(){focusable = false, objectType = typeof(Material)}; | ||
of.label = matRef.Slot.ToString(); | ||
of.value = matRef.Material; | ||
of.SetEnabled(false); | ||
gb.contentContainer.Add(of); | ||
} | ||
); | ||
container.Add(listView); | ||
|
||
|
||
inspector.Add(rendererFold); | ||
} | ||
|
||
return inspector; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Linq; | ||
using UnityEngine; | ||
|
||
namespace cc.dingemans.bigibas123.MaterialDedup.Editor.Model | ||
{ | ||
public abstract class MaterialContainer | ||
{ | ||
public abstract Material Material { get; } | ||
public abstract string Name { get; } | ||
|
||
public bool HasPropertiesSameAs(MaterialContainer other) | ||
{ | ||
return HasPropertiesSameAs(this, other); | ||
} | ||
|
||
public static bool HasPropertiesSameAs(MaterialContainer matc1, MaterialContainer matc2) | ||
{ | ||
var mat1 = matc1.Material; | ||
var mat2 = matc2.Material; | ||
if (mat1.shader != mat2.shader) return false; | ||
|
||
foreach (MaterialPropertyType propType in (MaterialPropertyType[])Enum.GetValues( | ||
typeof(MaterialPropertyType))) | ||
{ | ||
var mat1Props = mat1.GetPropertyNames(propType); | ||
var mat2Props = mat2.GetPropertyNames(propType); | ||
if (mat1Props.Length != mat2Props.Length) return false; | ||
if (mat1Props.Any(mat1Prop => !mat2Props.Contains(mat1Prop))) return false; | ||
|
||
foreach (var propName in mat1Props) | ||
{ | ||
switch (propType) | ||
{ | ||
case MaterialPropertyType.Float | ||
when Math.Abs(mat1.GetFloat(propName) - mat2.GetFloat(propName)) > Double.Epsilon: | ||
return false; | ||
case MaterialPropertyType.Int when mat1.GetInteger(propName) != mat2.GetInteger(propName): | ||
return false; | ||
case MaterialPropertyType.Vector when mat1.GetVector(propName) != mat2.GetVector(propName): | ||
return false; | ||
case MaterialPropertyType.Matrix when mat1.GetMatrix(propName) != mat2.GetMatrix(propName): | ||
return false; | ||
case MaterialPropertyType.Texture when mat1.GetTexture(propName) != mat2.GetTexture(propName): | ||
return false; | ||
case MaterialPropertyType.ConstantBuffer | ||
when mat1.GetConstantBuffer(propName) != mat2.GetConstantBuffer(propName): | ||
return false; | ||
case MaterialPropertyType.ComputeBuffer: | ||
Debug.LogError( | ||
$"{MaterialDedup.TAG} Checking compute buffer equality is not supported right now!"); | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
9 changes: 4 additions & 5 deletions
9
Editor/MaterialReference.cs → Editor/Model/MaterialReference.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.