-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplineControlPointGizmo.cs
More file actions
335 lines (292 loc) · 14.6 KB
/
Copy pathSplineControlPointGizmo.cs
File metadata and controls
335 lines (292 loc) · 14.6 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using SplineTest.Rendering;
using Stride.Assets.Presentation.AssetEditors.GameEditor.Game;
using Stride.Core.Mathematics;
using Stride.Engine;
using Stride.Engine.Processors;
using Stride.Engine.Splines.Components;
using Stride.Engine.Splines.Models;
namespace Stride.Assets.Presentation.AssetEditors.Gizmos.Splines;
public enum SplinePointEditingSelectionType
{
None,
ControlPoint,
TangentIn,
TangentOut
}
[Flags]
public enum SplinePointRaycastFilterFlags
{
None = 0,
ControlPoint = 1 << 0,
Tangents = 1 << 1,
All = ControlPoint | Tangents
}
public class SplineControlPointGizmo : GizmoBase
{
private const float GizmoDefaultSize = 48; // the default size of the gizmo on the screen in pixels.
private static readonly Color4 ControlPointFillColor = Color.White.ToColor4();
private static readonly Color4 FirstControlPointFillColor = Color.SkyBlue.ToColor4();
private static readonly Color4 ControlPointOrientationLineColor = Color.Aqua.ToColor4();
private static readonly Color4 ControlPointNormalLineColor = Color.Aqua.ToColor4();
private static readonly Color4 TangentDisabledFillColor = Color.LightGray.ToColor4();
private static readonly Color4 TangentInHandleFillColor = Color.LightCoral.ToColor4();
private static readonly Color4 TangentOutHandleFillColor = Color.LightGreen.ToColor4();
private static readonly Color4 TangentLineColor = Color.WhiteSmoke.ToColor4();
private static readonly Color4 SelectedFillColor = Color.Gold.ToColor4();
private static readonly Color4 SelectedLineColor = Color.Gold.ToColor4();
private IEditorGameCameraService cameraService;
private int controlPointIndex;
private SplineComponent splineComponent;
private Entity controlPointRootGizmoEntity;
private LineVisualizerComponent controlPointLineVisualizerComponent;
private GizmoMarkerSetComponent controlPointGizmoMarkerSetComponent;
private bool isMarkersAndLinesUpdateRequired = true;
private TangentEntityData tangentInEntityData;
private TangentEntityData tangentOutEntityData;
/// <summary>
/// Gets the gizmo default scale in ratio of screen height ( 1 => full screen vertically )
/// </summary>
public float DefaultScale => GizmoDefaultSize / GraphicsDevice.Presenter.BackBuffer.Height;
public SplineControlPointGizmo(int controlPointIndex, SplineComponent splineComponent) : base()
{
RenderGroup = SplineGizmo.GizmoRenderGroup;
this.controlPointIndex = controlPointIndex;
this.splineComponent = splineComponent;
}
private SplinePointEditingSelectionType previousEditingSelectionType;
public SplinePointEditingSelectionType EditingSelectionType { get; set; }
protected override Entity Create()
{
controlPointRootGizmoEntity = new Entity($"SplineControlPoint_{controlPointIndex}");
controlPointLineVisualizerComponent = new LineVisualizerComponent
{
RenderGroup = RenderGroup,
DepthTest = false,
};
controlPointLineVisualizerComponent.LineSet.OccludedStyle = LineOccludedStyle.Checkered;
controlPointRootGizmoEntity.Add(controlPointLineVisualizerComponent);
controlPointGizmoMarkerSetComponent = new GizmoMarkerSetComponent
{
RenderGroup = RenderGroup,
DepthTest = false,
};
controlPointGizmoMarkerSetComponent.GizmoMarkerSet.OccludedStyle = GizmoMarkerOccludedStyle.Checkered;
controlPointRootGizmoEntity.Add(controlPointGizmoMarkerSetComponent);
var controlPointModelEntity = new Entity($"SplineControlPoint_Model_{controlPointIndex}");
controlPointModelEntity.SetParent(controlPointRootGizmoEntity);
tangentInEntityData = new TangentEntityData
{
CurrentLocalPosition = new Vector3(float.MinValue)
};
tangentOutEntityData = new TangentEntityData
{
CurrentLocalPosition = new Vector3(float.MinValue)
};
return controlPointRootGizmoEntity;
}
public void Update()
{
var splineEntity = splineComponent.Entity;
var spline = splineComponent.Spline;
if (splineEntity is null || spline is null
|| controlPointIndex >= spline.Count || spline.Count == 0)
{
return;
}
var splineEval = splineComponent.SplineEvaluator ?? new SplineEvaluator(spline);
var controlPoint = spline[controlPointIndex];
controlPointRootGizmoEntity.Transform.Position = controlPoint.Position;
cameraService ??= Game.EditorServices.Get<IEditorGameCameraService>();
var upVec = splineEntity.Transform.WorldMatrix.Up;
UpdateTangentTransformation(ref tangentInEntityData, controlPoint, controlPoint.TangentInPosition, upVec);
UpdateTangentTransformation(ref tangentOutEntityData, controlPoint, controlPoint.TangentOutPosition, upVec);
// Check if updating color
if (previousEditingSelectionType != EditingSelectionType)
{
isMarkersAndLinesUpdateRequired = true;
previousEditingSelectionType = EditingSelectionType;
}
if (isMarkersAndLinesUpdateRequired)
{
const float ShapeSize = 13;
const float OutlineWidthPx = 1f;
const float ControlPointUpVectorThicknessPx = 2;
const float TangentLineThicknessPx = 3;
const float SelectedGlowWidthPx = 5;
int curveIndex = controlPointIndex; // Same index
var sample = splineEval.EvaluateFromCurve(curveIndex);
var markerSet = controlPointGizmoMarkerSetComponent.GizmoMarkerSet;
markerSet.Markers.Clear();
// Control Point visuals
var ctrlPointMarker = new GizmoMarkerData
{
Shape = GizmoMarkerShape.Circle,
OrientationMode = GizmoMarkerOrientationMode.Billboard,
ScaleMode = GizmoMarkerScaleMode.FixedScreenSize,
Position = Vector3.Zero,
FillColor = controlPointIndex == 0 ? FirstControlPointFillColor : ControlPointFillColor,
SizePx = new Vector2(ShapeSize),
OutlineWidthPx = OutlineWidthPx,
OutlineColor = Color.Black.ToColor4(),
};
if (EditingSelectionType == SplinePointEditingSelectionType.ControlPoint)
{
ctrlPointMarker.FillColor = SelectedFillColor;
ctrlPointMarker.GlowColor = SelectedFillColor;
ctrlPointMarker.GlowWidthPx = SelectedGlowWidthPx;
}
markerSet.Markers.Add(ctrlPointMarker);
var lineSet = controlPointLineVisualizerComponent.LineSet;
lineSet.Segments.Clear();
var ctrlPointOrientationMarker = new GizmoMarkerData
{
Shape = GizmoMarkerShape.Circle,
OrientationMode = GizmoMarkerOrientationMode.World,
ScaleMode = GizmoMarkerScaleMode.FixedScreenSize,
Position = Vector3.Zero,
Rotation = Quaternion.RotationX(MathUtil.PiOverTwo) * sample.Orientation,
FillColor = Color.Transparent.ToColor4(),
SizePx = new Vector2(60),
OutlineWidthPx = 1.5f,
OutlineColor = ControlPointOrientationLineColor,
};
if (EditingSelectionType == SplinePointEditingSelectionType.ControlPoint)
{
ctrlPointOrientationMarker.FillColor = SelectedFillColor with { A = 0.25f };
ctrlPointOrientationMarker.OutlineColor = SelectedLineColor;
}
markerSet.Markers.Add(ctrlPointOrientationMarker);
var ctrlPointUpVec = sample.Orientation * Vector3.UnitY;
var ctrlPointUpVecColor = EditingSelectionType == SplinePointEditingSelectionType.ControlPoint
? SelectedLineColor
: ControlPointNormalLineColor;
lineSet.AddViewScaledLengthLine(Vector3.Zero, ctrlPointUpVec, fixedLengthPx: 60, ctrlPointUpVecColor, ControlPointUpVectorThicknessPx);
// Tangent In visuals
if (controlPoint.Type.IsTangentVisible())
{
lineSet.AddWorldLine(Vector3.Zero, tangentInEntityData.CurrentLocalPosition, TangentLineColor, TangentLineThicknessPx);
var tangentInMarker = new GizmoMarkerData
{
Shape = GizmoMarkerShape.Diamond,
OrientationMode = GizmoMarkerOrientationMode.Billboard,
ScaleMode = GizmoMarkerScaleMode.FixedScreenSize,
Position = tangentInEntityData.CurrentLocalPosition,
FillColor = controlPoint.Type.IsTangentUserControllable() ? TangentInHandleFillColor : TangentDisabledFillColor,
SizePx = new Vector2(ShapeSize),
OutlineWidthPx = OutlineWidthPx,
OutlineColor = Color.Black.ToColor4(),
};
if (EditingSelectionType == SplinePointEditingSelectionType.TangentIn)
{
tangentInMarker.FillColor = SelectedFillColor;
tangentInMarker.GlowColor = SelectedFillColor;
tangentInMarker.GlowWidthPx = SelectedGlowWidthPx;
}
markerSet.Markers.Add(tangentInMarker);
}
// Tangent Out visuals
if (controlPoint.Type.IsTangentVisible())
{
lineSet.AddWorldLine(Vector3.Zero, tangentOutEntityData.CurrentLocalPosition, TangentLineColor, TangentLineThicknessPx);
var tangentOutMarker = new GizmoMarkerData
{
Shape = GizmoMarkerShape.Diamond,
OrientationMode = GizmoMarkerOrientationMode.Billboard,
ScaleMode = GizmoMarkerScaleMode.FixedScreenSize,
Position = tangentOutEntityData.CurrentLocalPosition,
FillColor = controlPoint.Type.IsTangentUserControllable() ? TangentOutHandleFillColor : TangentDisabledFillColor,
SizePx = new Vector2(ShapeSize),
OutlineWidthPx = OutlineWidthPx,
OutlineColor = Color.Black.ToColor4(),
};
if (EditingSelectionType == SplinePointEditingSelectionType.TangentOut)
{
tangentOutMarker.FillColor = SelectedFillColor;
tangentOutMarker.GlowColor = SelectedFillColor;
tangentOutMarker.GlowWidthPx = SelectedGlowWidthPx;
}
markerSet.Markers.Add(tangentOutMarker);
}
isMarkersAndLinesUpdateRequired = false;
}
}
private float GetTargetedScale(IEditorGameCameraService cameraService)
{
var splineEntity = splineComponent.Entity;
if (splineEntity is not null
&& cameraService.Component.Projection == CameraProjectionMode.Perspective)
{
var distanceToSelectedEntity = MathF.Abs(Vector3.TransformCoordinate(splineEntity.Transform.WorldMatrix.TranslationVector, cameraService.ViewMatrix).Z);
return SizeFactor * DefaultScale * 2f * MathF.Tan(MathUtil.DegreesToRadians(cameraService.VerticalFieldOfView / 2)) * distanceToSelectedEntity;
}
return SizeFactor * DefaultScale * cameraService.Component.OrthographicSize;
}
private void UpdateTangentTransformation(ref TangentEntityData tangentEntityData, in SplineControlPoint controlPoint, in Vector3 tangentPosition, in Vector3 upVec)
{
var tangentHandleLocalPosition = tangentPosition - controlPoint.Position;
isMarkersAndLinesUpdateRequired = isMarkersAndLinesUpdateRequired || tangentEntityData.CurrentLocalPosition != tangentHandleLocalPosition;
tangentEntityData.CurrentLocalPosition = tangentHandleLocalPosition;
}
internal bool TryRaycastOnHandle(Ray clickRay, SplinePointRaycastFilterFlags raycastFilterFlags, ref float minHitDistance, ref SplinePointEditingSelectionType newSelection)
{
var spline = splineComponent.Spline;
if (controlPointIndex >= spline.Count || spline.Count == 0)
{
return false;
}
var controlPoint = spline[controlPointIndex];
cameraService ??= Game.EditorServices.Get<IEditorGameCameraService>();
float targetedScale = GetTargetedScale(cameraService);
bool isHit = false;
float controlHitRadius = 0.25f * targetedScale; // TODO should this be part of the gizmo picking?
var controlPointPos = controlPoint.Position;
var controlPointHitSphere = new BoundingSphere(controlPointPos, controlHitRadius);
if ((raycastFilterFlags & SplinePointRaycastFilterFlags.ControlPoint) != 0
&& controlPointHitSphere.Intersects(ref clickRay, out float hitDistance))
{
if (hitDistance < minHitDistance)
{
minHitDistance = hitDistance;
newSelection = SplinePointEditingSelectionType.ControlPoint;
isHit = true;
}
}
if ((raycastFilterFlags & SplinePointRaycastFilterFlags.Tangents) != 0
&& controlPoint.Type.IsTangentUserControllable())
{
var tangentOutPos = tangentOutEntityData.CurrentLocalPosition + controlPoint.Position;
var tangentOutHitSphere = new BoundingSphere(tangentOutPos, controlHitRadius);
if (tangentOutHitSphere.Intersects(ref clickRay, out hitDistance))
{
if (hitDistance < minHitDistance)
{
minHitDistance = hitDistance;
newSelection = SplinePointEditingSelectionType.TangentOut;
isHit = true;
}
}
var tangentInPos = tangentInEntityData.CurrentLocalPosition + controlPoint.Position;
var tangentInHitSphere = new BoundingSphere(tangentInPos, controlHitRadius);
if (tangentInHitSphere.Intersects(ref clickRay, out hitDistance))
{
if (hitDistance < minHitDistance)
{
minHitDistance = hitDistance;
newSelection = SplinePointEditingSelectionType.TangentIn;
isHit = true;
}
}
}
return isHit;
}
internal void InvalidateVisual()
{
isMarkersAndLinesUpdateRequired = true;
}
private struct TangentEntityData
{
public Vector3 CurrentLocalPosition;
}
}