Skip to content

Commit

Permalink
Merge pull request #24 from didimoinc/release/4.4.10
Browse files Browse the repository at this point in the history
Release 4.4.10
  • Loading branch information
Hugo-Pereira authored Sep 12, 2022
2 parents 2d46b0c + 2c3406b commit 8565234
Show file tree
Hide file tree
Showing 417 changed files with 160,392 additions and 1,038,603 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using UnityEngine;
using Didimo.AssetFitter.Editor.Graph;
using UnityEditor;

namespace Didimo.Core.Editor
{
Expand All @@ -10,17 +11,26 @@ public class AssetFitterDidimoManagerTab : DidimoManagerTab

public override void Draw(DidimoManager manager)
{
scrollPosition = GUILayout.BeginScrollView(scrollPosition);
scrollPosition = GUILayout.BeginScrollView(scrollPosition, false, false, GUIStyle.none, GUI.skin.verticalScrollbar);
Didimo.AssetFitter.Editor.Graph.Controller assetFitterController = ControllerLoader.LoadDefault();

if (!assetFitterControllerEditor)
{
assetFitterControllerEditor = (ControllerEditor)ControllerEditor.CreateEditor(assetFitterController);
}
GUIStyle style = new GUIStyle(GUI.skin.box);
style.padding = new RectOffset(0, 100, 0, 0);
style.margin = new RectOffset(0, 50, 0, 0);

GUILayout.BeginVertical(GUILayout.Width(600));
GUILayout.BeginHorizontal(style, GUILayout.MaxWidth(200));
GUILayout.BeginVertical();
GUILayout.Label(" ", GUILayout.MaxWidth(20));
GUILayout.EndVertical();
GUILayout.BeginVertical();
assetFitterControllerEditor.DrawEditor();

GUILayout.EndVertical();
GUILayout.EndHorizontal();

GUILayout.EndScrollView();
}
Expand Down
3 changes: 3 additions & 0 deletions com.didimo.sdk.assetfitter/Editor/Scripts/Graph/GraphView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ void OnViewTransformChanged(UnityEditor.Experimental.GraphView.GraphView graphVi
GraphViewChange OnGraphViewChanged(GraphViewChange change)
{
if (change.edgesToCreate != null)
{
change.edgesToCreate.ForEach(CreateEdge);
}

if (change.elementsToRemove != null)
{
Expand All @@ -58,6 +60,7 @@ GraphViewChange OnGraphViewChanged(GraphViewChange change)
else Debug.Log("Unknown type " + element.GetType());
}
}

EditorUtility.SetDirty(graphData);
return change;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static bool OpenGraph(int instanceID, int line)
else window = CreateInstance<GraphWindow>();
window.graphData = graphData;
window.titleContent = new GUIContent(graphData.name);
window.CreateGraphView();
window.BuildView();
window.Show();
return true;
}
Expand Down
39 changes: 23 additions & 16 deletions com.didimo.sdk.assetfitter/Editor/Scripts/Nodes/NodeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public NodeView(GraphNode node)
{
this.node = node;

title = node.title;

// VisualElement titleContainer = this.Q("title");
titleContainer.style.backgroundColor = bgcolor;
Expand All @@ -50,19 +49,19 @@ public NodeView(GraphNode node)

style.width = node.width;

UpdateNodeTitle();
CreateMonoScriptField();
CreatePorts();

if (!node.expanded) ToggleCollapse();
RegisterCallback<GeometryChangedEvent>(OnGeometryChangedEvent);

// EditorApplication.delayCall += () => initialize();
}

// void initialize()
// {
// RefreshExpandedState();
// }
void UpdateNodeTitle()
{
title = node.title;
if (!string.IsNullOrEmpty(node.id)) title += " (" + node.id + ")";
}

void OnGeometryChangedEvent(GeometryChangedEvent e)
{
Expand All @@ -78,10 +77,12 @@ void CreatePorts()
foreach (var info in node.GetType().GetFields())
{
DirectionAttribute direction = info.GetCustomAttribute<DirectionAttribute>();
ExposeAttribute expose = info.GetCustomAttribute<ExposeAttribute>();
if (direction != null)
{
var expose = info.GetCustomAttribute<ExposeAttribute>();
Port port = CreatePort(expose == null ? direction.name : "", direction, info.FieldType);
// Port port = CreatePort(expose == null ? direction.name : "", direction, info.FieldType);
bool showName = expose != null ? expose.showLabel : true;
Port port = CreatePort(showName ? direction.name : "", direction, info.FieldType);

if (expose != null)
{
Expand All @@ -94,7 +95,7 @@ void CreatePorts()
}
else if (info.GetCustomAttribute<ExposeAttribute>() != null)
{
TryGetContainer("properties").Add(GetExposedControl(info, info.Name));
TryGetContainer("properties").Add(GetExposedControl(info));
}
}

Expand All @@ -107,7 +108,9 @@ void CreatePorts()

Port CreatePort(string name, DirectionAttribute direction, Type type)
{
var port = InstantiatePort(Orientation.Horizontal, direction is InputAttribute ? Direction.Input : Direction.Output, direction.single ? Capacity.Single : Capacity.Multi, type);
Port port = InstantiatePort(Orientation.Horizontal, direction is InputAttribute ? Direction.Input : Direction.Output, direction.single ? Capacity.Single : Capacity.Multi, type);
// if (String.IsNullOrEmpty(name)) port.Remove(port.Q("type"));
// else
port.portName = name;
if (TypeColors.GetColorByType(type, out Color color, 2f))
port.portColor = color;
Expand All @@ -123,7 +126,7 @@ public class PortReference

void OnExposedValueChanged(FieldInfo fieldInfo) { }

VisualElement GetExposedControl(FieldInfo fieldInfo, string label = "") =>
VisualElement GetExposedControl(FieldInfo fieldInfo) =>
FieldView.GetInstance(node, fieldInfo).element;

VisualElement TryGetContainer(string name) => this.Q(name) != null ? this.Q(name) : AddContainer(name);
Expand All @@ -138,13 +141,12 @@ VisualElement AddContainer(string name)

void CreateMonoScriptField()
{
var guids = AssetDatabase.FindAssets("t:MonoScript " + node.GetType().Name);
string[] guids = AssetDatabase.FindAssets("t:MonoScript " + node.GetType().Name);
for (int i = 0; i < guids.Length; i++)
{
var scriptAsset = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guids[i]), typeof(MonoScript)) as MonoScript;
MonoScript scriptAsset = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guids[i]), typeof(MonoScript)) as MonoScript;
if (scriptAsset.name == node.GetType().Name)
{
// var contents = this.Q("contents");
var container = new VisualElement() { style = { backgroundColor = bgcolor } };
container.Add(new ObjectField() { objectType = typeof(MonoScript), value = scriptAsset });
this.Q("contents").Insert(0, container);
Expand All @@ -168,14 +170,19 @@ void ToggleIDField()
else RemoveIDField();
}


void CreateIDField()
{
if (idFieldContainer != null) return;
TextField idField;
idFieldContainer = new VisualElement() { style = { backgroundColor = bgcolor } };
idFieldContainer.Add(idField = new TextField());
this.Q("contents").Insert(0, idFieldContainer);
idField.RegisterValueChangedCallback(evt => node.id = idField.text);
void Changed(string text)
{
node.id = idField.text;
}
idField.RegisterValueChangedCallback(e => Changed(idField.text)); //evt => node.id = idField.text
idField.SetValueWithoutNotify(node.id);
}

Expand Down

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

179 changes: 179 additions & 0 deletions com.didimo.sdk.assetfitter/Editor/Scripts/Tools/MenuOverrides.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
using System.Linq;

using UnityEngine;
using static UnityEngine.ColorUtility;
using static UnityEngine.Object;
using static Didimo.AssetFitter.Editor.Graph.AssetTools;
using static Didimo.AssetFitter.Editor.Graph.MeshTools;
using System.IO;
using UnityEditor;

public static class MenuOverrides
{
[MenuItem("CONTEXT/MeshRenderer/Convert to Skinned Mesh")]
static void ConvertToSkinnedMesh(MenuCommand command)
{
MeshRenderer renderer = command.context as MeshRenderer;
MeshFilter filter = renderer.GetComponent<MeshFilter>();

SkinnedMeshRenderer skin = renderer.gameObject.AddComponent<SkinnedMeshRenderer>();
skin.sharedMaterials = renderer.sharedMaterials;
skin.sharedMesh = filter.sharedMesh;

EditorApplication.delayCall += () => EditorApplication.delayCall += () => { DestroyImmediate(filter); DestroyImmediate(renderer); };
}

[MenuItem("CONTEXT/SkinnedMeshRenderer/Convert to Mesh Renderer & Filter")]
static void ConvertToMeshRenderer(MenuCommand command)
{
SkinnedMeshRenderer renderer = command.context as SkinnedMeshRenderer;
renderer.gameObject.AddComponent<MeshRenderer>().sharedMaterials = renderer.sharedMaterials;
renderer.gameObject.AddComponent<MeshFilter>().sharedMesh = renderer.sharedMesh;
EditorApplication.delayCall += () => UnityEngine.Object.DestroyImmediate(renderer);
}

[MenuItem("CONTEXT/SkinnedMeshRenderer/Convert to Mesh Renderer & Filter (Baked)")]
static void ConvertToMeshRendererBaked(MenuCommand command)
{
SkinnedMeshRenderer renderer = command.context as SkinnedMeshRenderer;
Mesh mesh = UnityEngine.Object.Instantiate(renderer.sharedMesh);
mesh.name = renderer.sharedMesh.name + " (Baked)";
renderer.BakeMesh(mesh);
renderer.gameObject.AddComponent<MeshRenderer>().sharedMaterials = renderer.sharedMaterials;
renderer.gameObject.AddComponent<MeshFilter>().sharedMesh = mesh;
EditorApplication.delayCall += () => UnityEngine.Object.DestroyImmediate(renderer);
}

[MenuItem("CONTEXT/MeshRenderer/Set Submesh Materials")]
static void SetMRSubMeshMaterials(MenuCommand command)
{
MeshRenderer renderer = command.context as MeshRenderer;
renderer.sharedMaterials = GetSubMeshUVColors(renderer.GetComponent<MeshFilter>().sharedMesh.subMeshCount, GetDefaultShader());
}

[MenuItem("CONTEXT/SkinnedMeshRenderer/Set Submesh Materials")]
static void SetSMSubMeshMaterials(MenuCommand command)
{
SkinnedMeshRenderer renderer = command.context as SkinnedMeshRenderer;
renderer.sharedMaterials = GetSubMeshUVColors(renderer.sharedMesh.subMeshCount, GetDefaultShader());
}

[MenuItem("CONTEXT/SkinnedMeshRenderer/Remove Root Bone Reference and Re-Assign", true)]
static bool RemoveRootBone_Validation(MenuCommand command)
{
SkinnedMeshRenderer renderer = command.context as SkinnedMeshRenderer;
return renderer && renderer.rootBone.name != "GrpMRealtimeRigExport" && renderer.GetComponentInParent<Didimo.DidimoComponents>();
}

[MenuItem("CONTEXT/SkinnedMeshRenderer/Remove Root Bone Reference and Re-Assign")]
static void RemoveRootBone(MenuCommand command)
{
SkinnedMeshRenderer renderer = command.context as SkinnedMeshRenderer;
Mesh mesh = renderer.sharedMesh;
Transform[] bones = renderer.bones;
Transform rootBone = bones.FirstOrDefault(b => b.name == "GrpMRealtimeRigExport");

int rootBoneIndex = System.Array.IndexOf(renderer.bones, renderer.rootBone);
renderer.bones = bones.Where(b => renderer.rootBone != b && !b.GetComponent<SkinnedMeshRenderer>()).ToArray();
renderer.rootBone = bones.FirstOrDefault(b => b.name == "GrpMRealtimeRigExport");
Mesh nmesh = CloneAsset(mesh);

BoneWeights weights = new BoneWeights(nmesh.boneWeights);
BoneWeight[] nWeights = new BoneWeight[weights.weights.Count];

for (int i = 0; i < weights.weights.Count; i++)
{
BoneWeights.WeightIndex ReIndex(BoneWeights.WeightIndex weightIndex)
{
weightIndex.index = weightIndex.index >= rootBoneIndex ? weightIndex.index - 1 : weightIndex.index;
return weightIndex;
}

nWeights[i] = BoneWeights.Create(weights.weights[i].Select(wi => ReIndex(wi)).ToArray());
}

nmesh.boneWeights = nWeights;
nmesh.bindposes = nmesh.bindposes.Take(rootBoneIndex).Concat(mesh.bindposes.Skip(rootBoneIndex + 1).TakeWhile(b => true)).Take(renderer.bones.Length).ToArray();
renderer.sharedMesh = nmesh;
}

[MenuItem("Assets/Didimo/Create Materials From Texture", true)]
static bool CreateMaterials_Validation()
{
return Selection.objects.Any(o => o as Texture2D);
}

[MenuItem("Assets/Didimo/Create Materials From Texture")]
static void CreateMaterials()
{
Shader shader = GetDefaultShader();
Material CreateAsset(Texture2D texture)
{
var m = new Material(shader) { name = texture.name };
m.mainTexture = texture;
string path = AssetDatabase.GetAssetPath(texture);
string assetPath = Path.GetDirectoryName(path) + "/" + Path.GetFileNameWithoutExtension(path) + ".mat";
Debug.Log("Create material asset at " + assetPath);
AssetDatabase.CreateAsset(m, assetPath);
return m;
}
Material[] materials = Selection.objects.Where(o => o as Texture2D).Select(t => CreateAsset(t as Texture2D)).ToArray();
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Selection.objects = materials;
}

static void SetRendererMaterials(Renderer renderer, Material[] materials) => renderer.sharedMaterials = materials;

static Shader DefaultShader;
static Shader GetDefaultShader()
{
if (!DefaultShader)
{
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
DefaultShader = cube.GetComponent<MeshRenderer>().sharedMaterial.shader;
UnityEngine.Object.DestroyImmediate(cube);
}
return DefaultShader;
}

static Material[] GetSubMeshUVColors(int count, Shader shader) =>
Colors32.OrderBy(c => UnityEngine.Random.value).Take(count).
Select((h, i) => new Material(shader) { name = h[1], color = TryParseHtmlString(h[0], out Color color) ? color : Color.magenta }).ToArray();

public static string[][] Colors32 = new string[][]
{
new [] {"#556b2f","darkolivegreen"},
new [] {"#800000","maroon"},
new [] {"#483d8b","darkslateblue"},
new [] {"#008000","green"},
new [] {"#3cb371","mediumseagreen"},
new [] {"#b8860b","darkgoldenrod"},
new [] {"#008b8b","darkcyan"},
new [] {"#000080","navy"},
new [] {"#32cd32","limegreen"},
new [] {"#8b008b","darkmagenta"},
new [] {"#ff4500","orangered"},
new [] {"#ff8c00","darkorange"},
new [] {"#40e0d0","turquoise"},
new [] {"#00ff00","lime"},
new [] {"#9400d3","darkviolet"},
new [] {"#00fa9a","mediumspringgreen"},
new [] {"#dc143c","crimson"},
new [] {"#00bfff","deepskyblue"},
new [] {"#0000ff","blue"},
new [] {"#adff2f","greenyellow"},
new [] {"#ff00ff","fuchsia"},
new [] {"#1e90ff","dodgerblue"},
new [] {"#db7093","palevioletred"},
new [] {"#f0e68c","khaki"},
new [] {"#ffff54","laserlemon"},
new [] {"#add8e6","lightblue"},
new [] {"#ff1493","deeppink"},
new [] {"#7b68ee","mediumslateblue"},
new [] {"#ffa07a","lightsalmon"},
new [] {"#ee82ee","violet"},
new [] {"#ffc0cb","pink"},
new [] {"#696969","dimgray"},
};
}

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

Loading

0 comments on commit 8565234

Please sign in to comment.