Skip to content

Commit

Permalink
Merge pull request #6 from jblattgerste/Object-Conversion
Browse files Browse the repository at this point in the history
New TrainAR object conversion window, including new mesh decimation algorithm choice and improved quality.
  • Loading branch information
jblattgerste authored Apr 7, 2024
2 parents db92fd4 + fe7ff91 commit 1442961
Show file tree
Hide file tree
Showing 1,081 changed files with 219,416 additions and 440 deletions.
4 changes: 4 additions & 0 deletions .idea/.idea.TrainAR/.idea/workspace.xml

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

4 changes: 4 additions & 0 deletions Assembly-CSharp-Editor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,10 @@
<Project>{8e530897-0605-6cd4-3bfe-114dbb14bed6}</Project>
<Name>Whinarn.UnityMeshSimplifier.Runtime</Name>
</ProjectReference>
<ProjectReference Include="UnityMeshDecimation.csproj">
<Project>{ff750720-481d-f431-2cbf-6b672d06727f}</Project>
<Name>UnityMeshDecimation</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
347 changes: 180 additions & 167 deletions Assembly-CSharp-firstpass.csproj

Large diffs are not rendered by default.

349 changes: 182 additions & 167 deletions Assembly-CSharp.csproj

Large diffs are not rendered by default.

98 changes: 74 additions & 24 deletions Assets/Editor/Scripts/ConvertToTrainARObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityMeshDecimation;
using UnityMeshDecimation.Internal;
using UnityMeshSimplifier;
using Mesh = UnityEngine.Mesh;
using MeshCombiner = Others.MeshCombiner;

namespace Editor.Scripts
Expand Down Expand Up @@ -108,47 +111,69 @@ public static void AddConvertionContextItem()
}

/// <summary>
/// Initializes the conversion process for the given object.
/// Finalizes the conversion process for the given object, replacing the original object in the scene.
/// </summary>
/// <param name="selectedObject">The object that is to be converted to a TrainAR Object</param>
/// <param name="originalObject">The original GameObject in the scene.</param>
/// <param name="instantiatedPreviewObject">The instantiated copy of the GameObject.</param>
/// <param name="trainARObjectName">The specified name of the TrainAR Object.</param>
public static void InitConversion(GameObject selectedObject, string trainARObjectName)
/// <param name="pivotPointOffset">The offset of the pivot if it was moved (0,0,0 otherwise)</param>
public static void FinalizeConversion(GameObject originalObject, GameObject instantiatedPreviewObject, string trainARObjectName)
{
// If selected object is a part of a prefab instance, unpack it completely.
if (PrefabUtility.IsPartOfPrefabInstance(selectedObject))
instantiatedPreviewObject = Instantiate(instantiatedPreviewObject);

// Assuming both original and instantiated objects have a Renderer component in the same structure
Renderer originalRenderer = originalObject.GetComponent<Renderer>();
Renderer instantiatedRenderer = instantiatedPreviewObject.GetComponent<Renderer>();

if (originalRenderer != null && instantiatedRenderer != null)
{
PrefabUtility.UnpackPrefabInstance(selectedObject, PrefabUnpackMode.Completely, InteractionMode.UserAction);
// Copy the materials array from the original object to the instantiated object
instantiatedRenderer.sharedMaterials = originalRenderer.sharedMaterials;
}

//Enables the read/write of vertices/indeces of shared meshes
EnableReadWriteOnMeshes(selectedObject);

//Combine all meshes into one
GameObject newSelectedObject = CombineMeshes(selectedObject);
Undo.RegisterCreatedObjectUndo(newSelectedObject.gameObject, "Convert to TrainAR Object");

//Convert the object to a TrainAR object
//TrainARObject.cs automatically imports dependencies and all the other necessary scripts when attached
newSelectedObject.AddComponent<TrainARObject>();
newSelectedObject.tag = "TrainARObject";

// Convert the instantiated object to a TrainAR object
instantiatedPreviewObject.AddComponent<TrainARObject>();
instantiatedPreviewObject.tag = "TrainARObject";

// Apply the TrainAR Object name
newSelectedObject.name = trainARObjectName;

//Reset the selection to the newly converted GameObject
Selection.activeTransform = newSelectedObject.transform;
instantiatedPreviewObject.name = trainARObjectName;

// Replace original object in the scene with the instantiated object
if (originalObject != null)
{
instantiatedPreviewObject.transform.position = originalObject.transform.position;
instantiatedPreviewObject.transform.rotation = originalObject.transform.rotation;
instantiatedPreviewObject.transform.localScale = Vector3.one;

GameObject.DestroyImmediate(originalObject);
}

// Reset the selection to the newly converted GameObject
Selection.activeTransform = instantiatedPreviewObject.transform;
Selection.selectionChanged.Invoke();
Debug.Log("Successfully converted GameObject to TrainAR Object.");
}


/// <summary>
/// Combines all meshes of the selected object (e.g. all meshes in child structures) into a singular mesh,
/// saving it into the Models folder and deleting all the children to make the mesh structure work with TrainAR.
/// </summary>
/// <param name="objectToCombineAllMeshesFor">The GameObject which meshes (parent and child) should be combined</param>
/// <returns></returns>
private static GameObject CombineMeshes(GameObject objectToCombineAllMeshesFor)
public static GameObject CombineMeshes(GameObject objectToCombineAllMeshesFor)
{
Undo.RegisterCreatedObjectUndo(objectToCombineAllMeshesFor.gameObject, "Convert to TrainAR Object");

// If selected object is a part of a prefab instance, unpack it completely.
if (PrefabUtility.IsPartOfPrefabInstance(objectToCombineAllMeshesFor))
{
PrefabUtility.UnpackPrefabInstance(objectToCombineAllMeshesFor, PrefabUnpackMode.Completely, InteractionMode.UserAction);
}

//Enables the read/write of vertices/indeces of shared meshes
EnableReadWriteOnMeshes(objectToCombineAllMeshesFor);

//Create a new empty parent for the combination
GameObject newGameObjectWithCombinedMeshes = new GameObject(objectToCombineAllMeshesFor.name, typeof(MeshFilter), typeof(MeshRenderer));
newGameObjectWithCombinedMeshes.transform.SetPositionAndRotation(objectToCombineAllMeshesFor.transform.position, objectToCombineAllMeshesFor.transform.rotation);
Expand Down Expand Up @@ -186,8 +211,33 @@ private static GameObject CombineMeshes(GameObject objectToCombineAllMeshesFor)
return newGameObjectWithCombinedMeshes;
}

/// <summary>
/// Simplifies one given mesh (careful, this is different to the SimplifyMeshesUsingQuadrics method, which simplifies all meshes of a given object)
/// to the given target polygon count using the Tridecimator method from the UnityMeshDecimation package.
/// </summary>
/// <param name="providedMesh">The Mesh to simplify</param>
/// <param name="targetMeshPolygons">the target polygon count to simplify to</param>
/// <returns>The simplified mesh</returns>
public static Mesh SimplifyMeshesUsingTridecimator(Mesh providedMesh, int targetMeshPolygons)
{
var conditions= new TargetConditions
{
faceCount = targetMeshPolygons
};
var parameter = new EdgeCollapseParameter
{
UsedProperty = VertexProperty.UV0
};

var meshDecimation = new UnityMeshDecimation.UnityMeshDecimation();
meshDecimation.Execute(providedMesh, parameter, conditions);
return meshDecimation.ToMesh();
}

/// <summary>
/// Uses the Meshsimplifier to decimate the mesh of the passed Gameobject as well as all of it's children's meshes.
///
/// Note: In the current implementation this only simplifies one single mesh but it works so i leave this as it is here.
/// </summary>
/// <param name="currentSelectedObject">The current Gameobject, to which the mesh changes are applied to</param>
/// <param name="quality">The desired quality of the simplification. Must be between 0 and 1.</param>
Expand All @@ -197,7 +247,7 @@ private static GameObject CombineMeshes(GameObject objectToCombineAllMeshesFor)
/// <param name="preserveSurfaceCurvature">Optional parameter: Should surface curvature be preserved?</param>
/// <param name="preserveUVSeamEdges">Optional parameter: Should UV seam edges be preserved?</param>
/// <param name="preserveUVFoldoverEdges">Optional parameter: Should UV foldover edges be preserved?</param>
public static void SimplifyMeshes(IEnumerable<Mesh> originalMeshes, GameObject currentSelectedObject, float quality,
public static void SimplifyMeshesUsingQuadrics(IEnumerable<Mesh> originalMeshes, GameObject currentSelectedObject, float quality,
bool preserveBorderEdges = false, bool preserveSurfaceCurvature = false, bool preserveUVSeamEdges = false, bool preserveUVFoldoverEdges = false)
{
// Create instance of Unity Mesh Simplifier
Expand Down
3 changes: 3 additions & 0 deletions Assets/Editor/Scripts/TrainAREditorMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public static void SwitchToTrainARMode()

//Force Unity into 3D mode
SceneView.lastActiveSceneView.in2DMode = false;

//Force Unity Scene into Pivot mode
Tools.pivotMode = PivotMode.Pivot;

Debug.Log("Successfully switched to TrainAR authoring tool. ");
}
Expand Down
Loading

0 comments on commit 1442961

Please sign in to comment.