Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes & changes that are not related to specific Issues #1

Merged
merged 9 commits into from
Jun 26, 2022
4 changes: 2 additions & 2 deletions Plugins/Editor/Scripts/Control/BrushUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static void SetPivot(CSGBrush brush, Vector3 newCenter)
var realCenter = transform.position;
var difference = newCenter - realCenter;

if (difference.sqrMagnitude < MathConstants.ConsideredZero)
if (difference.magnitude < MathConstants.ConsideredZero)
return;

transform.position += difference;
Expand All @@ -64,7 +64,7 @@ public static void TranslatePivot(CSGBrush[] brushes, Vector3 offset)
{
if (brushes == null ||
brushes.Length == 0 ||
offset.sqrMagnitude < MathConstants.ConsideredZero)
offset.magnitude < MathConstants.ConsideredZero)
return;

for (int i = 0; i < brushes.Length; i++)
Expand Down
6 changes: 3 additions & 3 deletions Plugins/Editor/Scripts/Control/Helpers/ControlMeshUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public static void MergeVerticesOnEdges(ControlMesh controlMesh, short[] pointIn
var vertex2 = controlMesh.Vertices[vertexIndex2];
var pointOnLine = ProjectPointLine(point, vertex1, vertex2);
var delta = pointOnLine - point;
var distance = delta.sqrMagnitude;
var distance = delta.magnitude;
if (distance < closestDistance)
{
closestEdge = e;
Expand Down Expand Up @@ -3867,7 +3867,7 @@ public static short[] FindDuplicateVerticesToRemove(CSGBrush brush, ControlMeshS
for (var index2 = index1 + 1; index2 < vertices.Length; index2++)
{
var vertex2 = vertices[index2];
if (!((vertex1 - vertex2).sqrMagnitude < MathConstants.EqualityEpsilon))
if (!((vertex1 - vertex2).magnitude < MathConstants.EqualityEpsilon))
continue;

//if ((pointSelectState[index1] & SelectState.Selected) != SelectState.Selected &&
Expand Down Expand Up @@ -3917,7 +3917,7 @@ public static short[] FindDuplicateVerticesToRemove(CSGBrush brush)
for (var index2 = index1 + 1; index2 < vertices.Length; index2++)
{
var vertex2 = vertices[index2];
if (!((vertex1 - vertex2).sqrMagnitude < MathConstants.EqualityEpsilon))
if (!((vertex1 - vertex2).magnitude < MathConstants.EqualityEpsilon))
continue;

var found = false;
Expand Down
10 changes: 5 additions & 5 deletions Plugins/Editor/Scripts/Control/Helpers/ShapePolygonUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ public static void RemoveDuplicatePoints(ref Vector3[] vertices)
// remove any points that are too close to one another
for (int j = vertices.Length - 1, i = vertices.Length - 2; i >= 0; j = i, i--)
{
if ((vertices[j] - vertices[i]).sqrMagnitude < MathConstants.DistanceEpsilon)
if ((vertices[j] - vertices[i]).magnitude < MathConstants.DistanceEpsilon)
{
ArrayUtility.RemoveAt(ref vertices, j);
}
}
while (vertices.Length > 3 && (vertices[0] - vertices[vertices.Length - 1]).sqrMagnitude < MathConstants.DistanceEpsilon)
while (vertices.Length > 3 && (vertices[0] - vertices[vertices.Length - 1]).magnitude < MathConstants.DistanceEpsilon)
{
var lastIndex = vertices.Length - 1;
ArrayUtility.RemoveAt(ref vertices, lastIndex);
Expand All @@ -101,13 +101,13 @@ public static void RemoveDuplicatePoints(ShapePolygon shapePolygon)
// remove any points that are too close to one another
for (int j = vertices.Length - 1, i = vertices.Length - 2; i >= 0; j = i, i--)
{
if ((vertices[j] - vertices[i]).sqrMagnitude < MathConstants.DistanceEpsilon)
if ((vertices[j] - vertices[i]).magnitude < MathConstants.DistanceEpsilon)
{
ArrayUtility.RemoveAt(ref vertices, j);
ArrayUtility.RemoveAt(ref edgeTexgens, j);
}
}
while (vertices.Length > 3 && (vertices[0] - vertices[vertices.Length - 1]).sqrMagnitude < MathConstants.DistanceEpsilon)
while (vertices.Length > 3 && (vertices[0] - vertices[vertices.Length - 1]).magnitude < MathConstants.DistanceEpsilon)
{
var lastIndex = vertices.Length - 1;
ArrayUtility.RemoveAt(ref vertices, lastIndex);
Expand Down Expand Up @@ -159,7 +159,7 @@ private static List<ShapePolygon> CreateCleanSubPolygonsFromVertices(Vector2[] v
{
for (int j = i + 2; j < vertices2d.Length; j++)
{
if ((vertices2d[j] - vertices2d[i]).sqrMagnitude < MathConstants.DistanceEpsilon)
if ((vertices2d[j] - vertices2d[i]).magnitude < MathConstants.DistanceEpsilon)
{
List<ShapePolygon> combined_polygons = null;

Expand Down
7 changes: 7 additions & 0 deletions Plugins/Editor/Scripts/Control/Managers/CSGModelManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ namespace RealtimeCSG
{
public static class CSGModelManager
{
public static bool AllowInEditorPlayMode = false;

public static bool IsInPlayMode
{
get => !AllowInEditorPlayMode && (UnityEditor.EditorApplication.isPlaying || UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode);
nukeandbeans marked this conversation as resolved.
Show resolved Hide resolved
}

public static void ForceRebuild()
{
#if UNITY_EDITOR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static void UndoRedoPerformed()
public static bool skipCheckForChanges = false;
public static void CheckForChanges(bool forceHierarchyUpdate = false)
{
if (EditorApplication.isPlayingOrWillChangePlaymode)
if (RealtimeCSG.CSGModelManager.IsInPlayMode)
return;

if (!forceHierarchyUpdate && skipCheckForChanges)
Expand Down Expand Up @@ -153,5 +153,38 @@ public static void ForceRebuildAll()

}
#endregion

#if UNITY_EDITOR

[UnityEditor.InitializeOnEnterPlayMode]
public static void OnEnterPlayMode()
{
// If saving meshes to scene files, we don't need to dynamically rebuild on scene changes
if (CSGProjectSettings.Instance.SaveMeshesInSceneFiles)
return;

static void sceneLoaded(UnityEngine.SceneManagement.Scene scene, UnityEngine.SceneManagement.LoadSceneMode mode)
nukeandbeans marked this conversation as resolved.
Show resolved Hide resolved
{
RealtimeCSG.CSGModelManager.AllowInEditorPlayMode = true;
InternalCSGModelManager.Shutdown();
InternalCSGModelManager.CheckForChanges(false);
RealtimeCSG.CSGModelManager.AllowInEditorPlayMode = false;
}

static void onPlayModeChange(PlayModeStateChange playMode)
{
if (playMode == PlayModeStateChange.ExitingPlayMode)
{
//UnityEditor.SceneManagement.EditorSceneManager.sceneOpened -= sceneOpened;
UnityEngine.SceneManagement.SceneManager.sceneLoaded -= sceneLoaded;
EditorApplication.playModeStateChanged -= onPlayModeChange;
}
}

EditorApplication.playModeStateChanged += onPlayModeChange;
UnityEngine.SceneManagement.SceneManager.sceneLoaded += sceneLoaded;
//UnityEditor.SceneManagement.EditorSceneManager.sceneOpened += sceneOpened;
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static bool RegisterAllComponents()

Clear();

if (EditorApplication.isPlaying || EditorApplication.isPlayingOrWillChangePlaymode)
if (RealtimeCSG.CSGModelManager.IsInPlayMode)
{
return false;
}
Expand All @@ -105,7 +105,7 @@ static bool RegisterAllComponents()
#region Reset
public static void Reset(CSGNode node)
{
if (EditorApplication.isPlayingOrWillChangePlaymode)
if (RealtimeCSG.CSGModelManager.IsInPlayMode)
{
return;
}
Expand All @@ -122,7 +122,7 @@ public static void Reset(CSGNode node)
#region AddNodeRegistration
static void AddNodeRegistration(CSGNode node)
{
if (EditorApplication.isPlayingOrWillChangePlaymode)
if (RealtimeCSG.CSGModelManager.IsInPlayMode)
{
return;
}
Expand Down Expand Up @@ -461,7 +461,7 @@ public static void Reset(CSGModel component)
#region RegisterChild
static void RegisterChild(ChildNodeData childData)
{
if (EditorApplication.isPlayingOrWillChangePlaymode)
if (RealtimeCSG.CSGModelManager.IsInPlayMode)
return;

// make sure our model has actually been initialized
Expand All @@ -485,7 +485,7 @@ static void RegisterChild(ChildNodeData childData)
#region RegisterBrush
static void RegisterBrush(CSGBrush brush)
{
if (EditorApplication.isPlayingOrWillChangePlaymode ||
if (RealtimeCSG.CSGModelManager.IsInPlayMode ||
External == null ||
!brush ||
!brush.isActiveAndEnabled ||
Expand Down Expand Up @@ -572,7 +572,7 @@ static void RegisterBrush(CSGBrush brush)
#region RegisterOperation
static void RegisterOperation(CSGOperation op)
{
if (EditorApplication.isPlayingOrWillChangePlaymode ||
if (RealtimeCSG.CSGModelManager.IsInPlayMode ||
External == null ||
!op ||
!op.isActiveAndEnabled ||
Expand Down Expand Up @@ -634,7 +634,7 @@ static void RegisterOperation(CSGOperation op)
#region RegisterModel
private static void RegisterModel(CSGModel model)
{
if (EditorApplication.isPlayingOrWillChangePlaymode ||
if (RealtimeCSG.CSGModelManager.IsInPlayMode ||
External == null ||
!model ||
!model.isActiveAndEnabled ||
Expand Down Expand Up @@ -726,7 +726,7 @@ public static void SetBrushMeshSurfaces(CSGBrush brush)
#region UnregisterBrush
static void UnregisterBrush(CSGBrush brush)
{
if (EditorApplication.isPlayingOrWillChangePlaymode)
if (RealtimeCSG.CSGModelManager.IsInPlayMode)
return;

if (External == null)
Expand Down Expand Up @@ -773,7 +773,7 @@ static void UnregisterBrush(CSGBrush brush)
#region UnregisterOperation
static void UnregisterOperation(CSGOperation op)
{
if (EditorApplication.isPlayingOrWillChangePlaymode)
if (RealtimeCSG.CSGModelManager.IsInPlayMode)
return;

if (External == null)
Expand Down Expand Up @@ -820,7 +820,7 @@ static void UnregisterOperation(CSGOperation op)
#region UnregisterModel
static void UnregisterModel(CSGModel model)
{
if (EditorApplication.isPlayingOrWillChangePlaymode)
if (RealtimeCSG.CSGModelManager.IsInPlayMode)
return;

if (!model.IsRegistered)
Expand Down Expand Up @@ -862,7 +862,7 @@ static void UnregisterModel(CSGModel model)
#region EnableModel
private static void EnableModel(CSGModel model)
{
if (EditorApplication.isPlayingOrWillChangePlaymode)
if (RealtimeCSG.CSGModelManager.IsInPlayMode)
return;

if (External == null)
Expand All @@ -889,7 +889,7 @@ private static void EnableModel(CSGModel model)
#region DisableModel
private static void DisableModel(CSGModel model)
{
if (EditorApplication.isPlayingOrWillChangePlaymode)
if (RealtimeCSG.CSGModelManager.IsInPlayMode)
return;

if (External == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public static bool UpdateModelSettings()
#region RefreshMeshes
public static void RefreshMeshes()
{
if (EditorApplication.isPlayingOrWillChangePlaymode)
if (RealtimeCSG.CSGModelManager.IsInPlayMode)
return;

if (skipCheckForChanges)
Expand Down Expand Up @@ -245,6 +245,14 @@ private static bool UpdateMesh(CSGModel model,
ref outputHasGeneratedNormals,
ref sharedMesh,
editorOnly);

if (!CSGProjectSettings.Instance.SaveMeshesInSceneFiles)
{
// If is prefab scene, allow save
if (string.IsNullOrEmpty(model.gameObject.scene.path) && sharedMesh != null)
sharedMesh.hideFlags &= ~HideFlags.DontSaveInEditor;
}

return true;
}

Expand Down Expand Up @@ -409,8 +417,7 @@ public static void RemoveForcedUpdates()
static bool inUpdateMeshes = false;
public static bool UpdateMeshes(System.Text.StringBuilder text = null, bool forceUpdate = false)
{
if (EditorApplication.isPlaying
|| EditorApplication.isPlayingOrWillChangePlaymode)
if (RealtimeCSG.CSGModelManager.IsInPlayMode)
return false;

if (inUpdateMeshes)
Expand Down Expand Up @@ -583,5 +590,31 @@ public static bool UpdateMeshes(System.Text.StringBuilder text = null, bool forc
}
}
#endregion

#region Destroy meshes
/// <summary>
/// Used to destroy any helper surface meshes in the current scene. Useful because they don't get destroyed otherwise, and stay in the Scene file.
/// </summary>
[MenuItem("Edit/Realtime-CSG/Destroy All Helper Surface Meshes In Scene")]
public static void DestroyAllHelperSurfaceCSGMeshes()
{
var allMeshesInScene = UnityEngine.Object.FindObjectsOfType<Mesh>(true);

// regex matches all possible names for the helper surfaces that we can generate
var nameMatch = new System.Text.RegularExpressions.Regex(
nukeandbeans marked this conversation as resolved.
Show resolved Hide resolved
$"^{System.Text.RegularExpressions.Regex.Escape("<")}" +
$"(?:{string.Join("|", System.Linq.Enumerable.Select(renderSurfaceMeshNames, n => System.Text.RegularExpressions.Regex.Escape(n)))})" +
$" generated -?[0-9]+{System.Text.RegularExpressions.Regex.Escape(">")}$",
System.Text.RegularExpressions.RegexOptions.Compiled);

foreach (var mesh in allMeshesInScene)
{
if (nameMatch.IsMatch(mesh.name))
{
Undo.DestroyObjectImmediate(mesh);
}
}
}
#endregion Destroy meshes
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ static bool RemoveWithChildrenIfPossible(Transform transform, HashSet<Transform>

internal static void DestroyAllMeshInstances()
{
if (UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode)
if (RealtimeCSG.CSGModelManager.IsInPlayMode)
return;

for (var sceneIndex = 0; sceneIndex < SceneManager.sceneCount; sceneIndex++)
Expand Down
Loading