forked from Custom-Extension-Works/Project-Obsidian
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeshEditTool.cs
More file actions
183 lines (159 loc) · 6.71 KB
/
Copy pathMeshEditTool.cs
File metadata and controls
183 lines (159 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using BepuPhysics.Constraints;
using Elements.Assets;
using Elements.Core;
using FrooxEngine;
using FrooxEngine.UIX;
using Obsidian.Assets.ProceduralMeshes;
using Obsidian.Components.Utility;
namespace Obsidian.Components.Tools;
[Category("Obsidian/Tools")]
public class MeshEditTool : Tool
{
protected readonly DriveRef<PBS_Metallic> _material;
protected readonly DestroyRelayRef<EditableMesh> _currentEditableMesh;
protected readonly SyncRef<TextRenderer> _label;
public override bool UsesLaser => true;
protected override void OnAttach()
{
base.OnAttach();
Slot tip = base.Slot.AddSlot("TipReference");
tip.LocalPosition = float3.Forward * 0.075f;
TipReference.Target = tip;
Slot visual = base.Slot.AddSlot("Visual");
visual.AttachComponent<SphereCollider>().Radius.Value = 0.02f;
visual.LocalRotation = floatQ.Euler(90f, 0f, 0f);
visual.LocalPosition += float3.Forward * 0.05f;
_material.Target = visual.AttachComponent<PBS_Metallic>();
ConeMesh coneMesh = visual.AttachMesh<ConeMesh>(_material.Target);
coneMesh.RadiusTop.Value = 0.0025f;
coneMesh.RadiusBase.Value = 0.015f;
coneMesh.Height.Value = 0.05f;
Slot labelPivot = Slot.AddSlot("Label Pivot");
labelPivot.LocalRotation = floatQ.Euler(0f, 0f, 45f);
Slot slot2 = labelPivot.AddSlot("Label");
TextRenderer label = slot2.AttachComponent<TextRenderer>();
TextUnlitMaterial fontMaterial = slot2.AttachComponent<TextUnlitMaterial>();
fontMaterial.FaceDilate.Value = 0.2f;
fontMaterial.OutlineThickness.Value = 0.2f;
fontMaterial.OutlineColor.Value = colorX.Black;
label.Size.Value *= 0.15f;
label.Material.Target = fontMaterial;
slot2.LocalPosition += float3.Up * 0.05f;
label.Text.Value = "---";
_label.Target = label;
}
protected override void OnChanges()
{
base.OnChanges();
if (_currentEditableMesh.Target != null)
{
_label.Target.Text.Value = $"Editing: {_currentEditableMesh.Target.Slot.Name}";
_label.Target.Color.Value = colorX.White;
_material.Target.AlbedoColor.Value = colorX.Green;
}
else
{
_label.Target.Text.Value = $"---";
_label.Target.Color.Value = colorX.White;
_material.Target.AlbedoColor.Value = colorX.White;
}
}
public override void GenerateMenuItems(InteractionHandler tool, ContextMenu menu)
{
base.GenerateMenuItems(tool, menu);
if (_currentEditableMesh.Target != null)
{
menu.AddItem("Restore", OfficialAssets.Common.Icons.Rubbish, new colorX?(colorX.Red), OnRestore);
menu.AddItem("Bake", OfficialAssets.Graphics.Icons.General.Save, new colorX?(colorX.Green), OnBake);
}
}
[SyncMethod(typeof(Delegate), null)]
private void OnRestore(IButton button, ButtonEventData eventData)
{
Restore();
base.ActiveHandler?.CloseContextMenu();
}
[SyncMethod(typeof(Delegate), null)]
private void OnBake(IButton button, ButtonEventData eventData)
{
Bake();
base.ActiveHandler?.CloseContextMenu();
}
private void Restore()
{
_currentEditableMesh.Target?.Destroy();
_currentEditableMesh.Target = null;
}
private void Bake()
{
_currentEditableMesh.Target?.BakeMesh();
_currentEditableMesh.Target = null;
}
public override void OnPrimaryPress()
{
if (_currentEditableMesh.Target != null)
{
_label.Target.Text.Value = "Already editing!";
_label.Target.Color.Value = colorX.Orange;
RunInSeconds(2.5f, MarkChangeDirty);
return;
}
RaycastHit? potentialHit = GetHit();
if (potentialHit.HasValue)
{
RaycastHit hit = potentialHit.Value;
Slot targetSlot = hit.Collider.Slot;
if (targetSlot.GetComponent<MeshRenderer>() is MeshRenderer meshRenderer)
{
if (targetSlot.GetComponent<EditableMeshControlPoint>() is EditableMeshControlPoint controlPoint)
{
if (controlPoint.EditableMesh?.Slot is Slot editableMeshSlot)
targetSlot = editableMeshSlot;
else
return;
}
if (targetSlot.GetComponent<EditableMesh>() is EditableMesh existingEditableMesh)
{
_currentEditableMesh.Target = existingEditableMesh;
HighlightHelper.FlashHighlight(targetSlot, highlightable => highlightable == meshRenderer, colorX.Green);
return;
}
var hitMesh = meshRenderer.Mesh.Target;
if (hitMesh?.Asset?.Data != null)
{
if (hitMesh is StaticMesh)
{
var editableMesh = targetSlot.AttachComponent<EditableMesh>();
meshRenderer.Mesh.Target = editableMesh;
if (hit.Collider is MeshCollider existingMeshCollider && existingMeshCollider.Mesh.Target == hitMesh)
{
existingMeshCollider.Mesh.Target = editableMesh;
}
else
{
var newCollider = targetSlot.AttachComponent<MeshCollider>();
newCollider.Type.Value = hit.Collider.ColliderType;
newCollider.CharacterCollider.Value = hit.Collider.CharacterCollider;
newCollider.Mesh.Target = editableMesh;
hit.Collider.Enabled = false;
editableMesh.SetColliders(hit.Collider, newCollider);
}
editableMesh.Setup(hitMesh);
_currentEditableMesh.Target = editableMesh;
HighlightHelper.FlashHighlight(targetSlot, highlightable => highlightable == meshRenderer, colorX.Green);
}
else
{
HighlightHelper.FlashHighlight(targetSlot, highlightable => highlightable == meshRenderer, colorX.Red);
UserRoot userroot = World.LocalUser.Root;
NotificationMessage.SpawnTextMessage(World, userroot.HeadPosition + userroot.HeadSlot.Forward * 0.5f, "The target must be a StaticMesh!", colorX.Red);
}
}
}
}
}
}