EraserManager.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. Copyright (c) 2020 Omar Duarte
  3. Unauthorized copying of this file, via any medium is strictly prohibited.
  4. Writen by Omar Duarte, 2020.
  5. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  6. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  7. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  8. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  9. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  10. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  11. THE SOFTWARE.
  12. */
  13. using UnityEngine;
  14. using System.Linq;
  15. namespace PluginMaster
  16. {
  17. #region DATA & SETTIGNS
  18. [System.Serializable]
  19. public class EraserSettings : CircleToolBase, ISelectionBrushTool, IModifierTool
  20. {
  21. [SerializeField] private ModifierToolSettings _modifierTool = new ModifierToolSettings();
  22. public EraserSettings() => _modifierTool.OnDataChanged += DataChanged;
  23. public ModifierToolSettings.Command command { get => _modifierTool.command; set => _modifierTool.command = value; }
  24. public bool modifyAllButSelected
  25. {
  26. get => _modifierTool.modifyAllButSelected;
  27. set => _modifierTool.modifyAllButSelected = value;
  28. }
  29. public bool onlyTheClosest
  30. {
  31. get => _modifierTool.onlyTheClosest;
  32. set => _modifierTool.onlyTheClosest = value;
  33. }
  34. public bool outermostPrefabFilter
  35. {
  36. get => _modifierTool.outermostPrefabFilter;
  37. set => _modifierTool.outermostPrefabFilter = value;
  38. }
  39. public override void Copy(IToolSettings other)
  40. {
  41. var otherEraserSettings = other as EraserSettings;
  42. if (otherEraserSettings == null) return;
  43. base.Copy(other);
  44. _modifierTool.Copy(otherEraserSettings);
  45. }
  46. }
  47. [System.Serializable]
  48. public class EraserManager : ToolManagerBase<EraserSettings> { }
  49. #endregion
  50. #region PWBIO
  51. public static partial class PWBIO
  52. {
  53. private static float _lastHitDistance = 20f;
  54. private static Material _transparentRedMaterial = null;
  55. public static Material transparentRedMaterial
  56. {
  57. get
  58. {
  59. if (_transparentRedMaterial == null)
  60. _transparentRedMaterial = new Material(Shader.Find("PluginMaster/TransparentRed"));
  61. return _transparentRedMaterial;
  62. }
  63. }
  64. private static Material _transparentRedMaterial2 = null;
  65. public static Material transparentRedMaterial2
  66. {
  67. get
  68. {
  69. if (_transparentRedMaterial2 == null)
  70. _transparentRedMaterial2 = new Material(Shader.Find("PluginMaster/TransparentRed2"));
  71. return _transparentRedMaterial2;
  72. }
  73. }
  74. private static System.Collections.Generic.HashSet<GameObject> _toErase
  75. = new System.Collections.Generic.HashSet<GameObject>();
  76. private static void EraserMouseEvents()
  77. {
  78. if (Event.current.button == 0 && !Event.current.alt
  79. && (Event.current.type == EventType.MouseDown || Event.current.type == EventType.MouseDrag))
  80. {
  81. Erase();
  82. Event.current.Use();
  83. }
  84. if (Event.current.button == 1)
  85. {
  86. if (Event.current.type == EventType.MouseDown && (Event.current.control || Event.current.shift))
  87. {
  88. _pinned = true;
  89. _pinMouse = Event.current.mousePosition;
  90. Event.current.Use();
  91. }
  92. else if (Event.current.type == EventType.MouseUp) _pinned = false;
  93. }
  94. }
  95. private static void Erase()
  96. {
  97. void EraseObject(GameObject obj)
  98. {
  99. if (obj == null) return;
  100. if (EraserManager.settings.outermostPrefabFilter)
  101. {
  102. var root = UnityEditor.PrefabUtility.GetNearestPrefabInstanceRoot(obj);
  103. if (root != null) obj = root;
  104. }
  105. else
  106. {
  107. var parent = obj.transform.parent.gameObject;
  108. if (parent != null)
  109. {
  110. GameObject outermost = null;
  111. do
  112. {
  113. outermost = UnityEditor.PrefabUtility.GetOutermostPrefabInstanceRoot(obj);
  114. if (outermost == null) break;
  115. if (outermost == obj) break;
  116. UnityEditor.PrefabUtility.UnpackPrefabInstance(outermost,
  117. UnityEditor.PrefabUnpackMode.OutermostRoot, UnityEditor.InteractionMode.UserAction);
  118. } while (outermost != parent);
  119. }
  120. }
  121. PWBCore.DestroyTempCollider(obj.GetInstanceID());
  122. UnityEditor.Undo.DestroyObjectImmediate(obj);
  123. }
  124. foreach (var obj in _toErase) EraseObject(obj);
  125. _toErase.Clear();
  126. }
  127. private static void EraserDuringSceneGUI(UnityEditor.SceneView sceneView)
  128. {
  129. EraserMouseEvents();
  130. var mousePos = Event.current.mousePosition;
  131. if (_pinned) mousePos = _pinMouse;
  132. var mouseRay = UnityEditor.HandleUtility.GUIPointToWorldRay(mousePos);
  133. var center = mouseRay.GetPoint(_lastHitDistance);
  134. if (MouseRaycast(mouseRay, out RaycastHit mouseHit, out GameObject collider,
  135. float.MaxValue, -1, paintOnPalettePrefabs:true, castOnMeshesWithoutCollider:true))
  136. {
  137. _lastHitDistance = mouseHit.distance;
  138. center = mouseHit.point;
  139. PWBCore.UpdateTempCollidersIfHierarchyChanged();
  140. }
  141. DrawCircleTool(center, sceneView.camera, new Color(1f, 0.0f, 0, 1f), EraserManager.settings.radius);
  142. GetCircleToolTargets(mouseRay, sceneView.camera, EraserManager.settings,EraserManager.settings.radius, _toErase);
  143. DrawObjectsToErase(sceneView.camera);
  144. }
  145. private static void DrawObjectsToErase(Camera camera)
  146. {
  147. foreach (var obj in _toErase)
  148. {
  149. var filters = obj.GetComponentsInChildren<MeshFilter>();
  150. foreach (var filter in filters)
  151. {
  152. var mesh = filter.sharedMesh;
  153. if (mesh == null) continue;
  154. for (int subMeshIdx = 0; subMeshIdx < mesh.subMeshCount; ++subMeshIdx)
  155. Graphics.DrawMesh(mesh, filter.transform.localToWorldMatrix,
  156. transparentRedMaterial, 0, camera, subMeshIdx);
  157. }
  158. }
  159. }
  160. }
  161. #endregion
  162. }