-
Notifications
You must be signed in to change notification settings - Fork 0
/
DrawController.cs
573 lines (465 loc) · 19.5 KB
/
DrawController.cs
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
using UnityEngine;
using Curve;
using VRSketch;
using System.Collections.Generic;
public class DrawController : MonoBehaviour
{
[Header("Stroke prefabs")]
// STROKE
public GameObject inputStrokePrefab;
public GameObject finalStrokePrefab;
[Header("References")]
[SerializeField]
private CASSIEParametersProvider parameters = null;
// BEAUTIFIER
public Beautifier beautifier;
// SCENE DATA
public DrawingCanvas canvas;
public SurfaceManager surfaceManager;
public Grid3D grid;
public MirrorPlane mirrorPlane;
// Reference to check for collisions on stroke start
public BrushCollisions collisionDetector;
[Header("Parameters")]
// PARAMETERS (systems)
public bool Beautification;
private int subdivisionsPerUnit;
private float colliderRadius;
private int finalStrokeID = 0;
private InputStroke currentStroke = null;
private int currentSelectedPatchID = -1;
private void Start()
{
FinalStroke s = finalStrokePrefab.GetComponent<FinalStroke>();
subdivisionsPerUnit = Mathf.CeilToInt(s.SubdivisionsPerUnit * 0.5f); // Reduce resolution by half
colliderRadius = s.BaseCurveWidth * 0.5f; // The collider around the stroke is exactly the same radius as the stroke (BaseCurveWidth gives the diameter)
}
public void Init(bool surfacing)
{
finalStrokeID = 0;
canvas.Init(surfacing);
}
public void SwitchSystem(bool surfacing)
{
// Set parameter
canvas.SwitchSystem(surfacing);
}
public void NewStroke(Vector3 position)
{
GameObject strokeObject = canvas.Create(inputStrokePrefab, Primitive.Stroke);
currentStroke = strokeObject.GetComponent<InputStroke>();
// Check for stroke start point constraint
TryAddGridConstraint(canvas.transform.InverseTransformPoint(position));
collisionDetector.QueryCollision();
if (currentSelectedPatchID != -1)
{
AddInSurfaceConstraint(currentSelectedPatchID, position);
}
}
public void UpdateStroke(Vector3 position, Quaternion rotation, Vector3 velocity, float pressure)
{
if (!currentStroke.ShouldUpdate(position, parameters.Current.MinSamplingDistance))
return;
// Check if current selected patch is still nearby
UpdateSelectedPatch(position);
Vector3 brushNormal = canvas.transform.InverseTransformDirection(rotation * new Vector3(0, 0, 1)); // seems fine
Vector3 relativePos = canvas.transform.InverseTransformPoint(position);
//Debug.DrawLine(samplePos, samplePos + brushNormal * 0.1f, Color.white, 100f);
Sample s = new Sample(relativePos, brushNormal, pressure, velocity);
currentStroke.AddSample(s);
TryAddGridConstraint(relativePos);
if (currentStroke.Samples.Count > 1)
RenderStroke(currentStroke); // Draw current stroke
}
public bool CommitStroke(Vector3 position, out SerializableStroke strokeData, bool mirror = false)
{
// Guard against invalid input
if (!currentStroke.IsValid(parameters.Current.MinStrokeActionTime, parameters.Current.MinStrokeSize))
{
// Destroy input stroke
currentStroke.Destroy();
strokeData = new SerializableStroke(-1);
return false;
}
(Curve.Curve snappedCurve,
IntersectionConstraint[] intersections,
MirrorPlaneConstraint[] mirrorIntersections) =
beautifier.Beautify(
currentStroke,
Beautification,
mirror,
out List<SerializableConstraint> appliedConstraints,
out List<SerializableConstraint> rejectedConstraints,
out bool planar,
out bool onSurface,
out bool onMirror,
out bool closedLoop);
if (!snappedCurve.IsValid(parameters.Current.MinStrokeSize))
{
// Destroy input stroke
currentStroke.Destroy();
strokeData = new SerializableStroke(-1);
return false;
}
// Trim dangling endpoint bits
// Consider all intersections to find the first and last one
// If these intersections are near the stroke endpoint, cut the stroke there
// And correct each on curve parameter t for other intersections
if (intersections.Length > 0 || (mirror && mirrorIntersections.Length > 0))
{
TrimDanglingEndpoints(snappedCurve, intersections, mirrorIntersections);
}
// Create final stroke game object and render the stroke
GameObject strokeObject = canvas.Create(finalStrokePrefab, Primitive.Stroke);
FinalStroke finalStroke = strokeObject.GetComponent<FinalStroke>();
finalStroke.SetID(finalStrokeID);
finalStrokeID++;
finalStroke.SetCurve(snappedCurve, closedLoop: closedLoop);
finalStroke.SaveInputSamples(currentStroke.GetPoints().ToArray());
foreach (var intersection in intersections)
{
// OLD STROKE: Create or fetch node and create new segments if needed
//Debug.Log("intersection param = " + intersection.NewCurveData.t.ToString("F6"));
INode node = intersection.IntersectedStroke.AddIntersectionOldStroke(intersection.OldCurveData, parameters.Current.SnapToExistingNodeThreshold);
// NEW STROKE: Insert node and create new segments if needed
finalStroke.AddIntersectionNewStroke(node, intersection.NewCurveData, parameters.Current.MergeConstraintsThreshold);
Debug.Log("[GRAPH UPDATE] added node with " + node.IncidentCount + " neighbors");
}
// Mirroring
if (mirror)
{
// Create mirrored final stroke game object
GameObject mirrorStrokeObject = canvas.Create(finalStrokePrefab, Primitive.Stroke);
FinalStroke mirrorFinalStroke = mirrorStrokeObject.GetComponent<FinalStroke>();
mirrorFinalStroke.SetID(finalStrokeID);
// Set up mirror stroke curve and intersections
bool mirrorSuccess = mirrorPlane.Mirror(
finalStroke, ref mirrorFinalStroke,
onMirror,
intersections, mirrorIntersections,
closedLoop,
parameters.Current.ProximityThreshold,
parameters.Current.SnapToExistingNodeThreshold,
parameters.Current.MergeConstraintsThreshold,
prevent_extra_mirroring: !Beautification);
if (mirrorSuccess)
{
canvas.Add(mirrorFinalStroke);
RenderStroke(mirrorFinalStroke);
// Generate collider mesh
SolidifyStroke(mirrorFinalStroke);
finalStrokeID++;
}
else
{
// Abort
mirrorFinalStroke.Destroy();
}
}
//finalStroke.TrimDanglingSegments();
canvas.Add(finalStroke);
RenderStroke(finalStroke);
// Generate collider mesh
SolidifyStroke(finalStroke);
// Record stroke data
strokeData = new SerializableStroke(
finalStroke.ID,
finalStroke.GetControlPoints(),
currentStroke.GetPoints(parameters.Current.ExportRDPError),
appliedConstraints,
rejectedConstraints,
onSurface,
planar,
closedLoop
);
// Destroy input stroke
currentStroke.Destroy();
currentStroke = null;
// Stop holding on to patch
DeselectPatch(position);
return true; // success in creating final stroke
}
private void RenderStroke(Stroke s)
{
s.RenderAsLine(canvas.transform.localScale.x);
}
// Called only when stroke is done drawing, to generate its collider
private void SolidifyStroke(FinalStroke s)
{
//Mesh strokeMesh = brush.Solidify(s.Curve, true);
int tubularSegments = Mathf.CeilToInt(s.Curve.GetLength() * subdivisionsPerUnit);
Mesh strokeMesh = Tubular.Tubular.Build(s.Curve, tubularSegments, colliderRadius);
s.UpdateCollider(strokeMesh);
}
public bool AddConstraint(Vector3 collisionPos, GameObject collided)
{
// If is drawing, create a new constraint and add it to the current stroke
if (currentStroke)
{
// Find constraint type and exact position
Constraint constraint;
Vector3 relativePos = canvas.transform.InverseTransformPoint(collisionPos); // position of collision in canvas space
switch (collided.tag)
{
case "GridPoint":
constraint = new Constraint(collided.transform.localPosition); // Grid point pos in canvas space
break;
case "FinalStroke":
FinalStroke stroke = collided.GetComponent<FinalStroke>();
// Check if the curve can serve to create constraints
if (stroke != null)
{
constraint = stroke.GetConstraint(relativePos, parameters.Current.SnapToExistingNodeThreshold);
}
// Otherwise give up this constraint
else
{
constraint = new Constraint(relativePos);
}
break;
case "MirrorPlane":
//Debug.Log("[Mirror] collided with plane");
Vector3 onPlanePos = mirrorPlane.Project(relativePos);
constraint = new MirrorPlaneConstraint(onPlanePos, mirrorPlane.GetNormal());
break;
default:
//constraint = new Constraint(relativePos);
constraint = null;
break;
}
if (constraint != null)
{
currentStroke.AddConstraint(constraint, parameters.Current.MergeConstraintsThreshold);
// Constraint was successfully added
return true;
}
}
// Didn't add constraint
return false;
}
public void OnPatchCollide(int patchID, Vector3 pos)
{
if (currentSelectedPatchID != patchID)
{
if (currentSelectedPatchID != -1)
{
// Check whether we should select new patch or keep old one
if (
!surfaceManager.ProjectOnPatch(patchID, pos, out Vector3 posOnPatch, canvasSpace: true)
|| Vector3.Distance(pos, posOnPatch) > parameters.Current.ProximityThreshold)
{
currentSelectedPatchID = patchID;
// Add constraint of surface ingoing point, if currently drawing
if (currentStroke)
{
AddInSurfaceConstraint(currentSelectedPatchID, pos);
}
}
}
else
{
currentSelectedPatchID = patchID;
// Add constraint of surface ingoing point, if currently drawing
if (currentStroke)
{
AddInSurfaceConstraint(currentSelectedPatchID, pos);
}
}
}
}
public void OnPatchDeselect(Vector3 pos)
{
if (currentStroke)
{
// If we're currently drawing, prevent patch deselect based on pure collision events
// we want to keep drawing projected on the same patch as we started
}
else
{
DeselectPatch(pos);
}
}
private void DeselectPatch(Vector3 pos)
{
// Add constraint of surface outgoing point to draw controller
if (currentStroke)
{
AddOutSurfaceConstraint(currentSelectedPatchID, pos);
}
else
surfaceManager.OnDetailDrawStop(currentSelectedPatchID); // still send stop event to surface patch (to have correct appearance)
currentSelectedPatchID = -1;
}
private bool AddInSurfaceConstraint(int patchID, Vector3 onStrokePos)
{
if (currentStroke)
{
// Check whether we are actually still close to the patch
if (
!surfaceManager.ProjectOnPatch(patchID, onStrokePos, out Vector3 posOnPatch, canvasSpace: false)
|| Vector3.Distance(onStrokePos, posOnPatch) > parameters.Current.ProximityThreshold)
{
//Debug.Log("went too far from patch");
currentSelectedPatchID = -1;
return false;
}
//Debug.Log("add constraint to patch " + patchID);
surfaceManager.OnDetailDrawStart(patchID);
currentStroke.InConstrainToSurface(patchID, canvas.transform.InverseTransformPoint(onStrokePos));
return true;
}
return false;
}
private bool AddOutSurfaceConstraint(int patchID, Vector3 onStrokePos)
{
if (currentStroke)
{
surfaceManager.OnDetailDrawStop(patchID);
currentStroke.OutConstrainToSurface(patchID, canvas.transform.InverseTransformPoint(onStrokePos));
return true;
}
return false;
}
private bool GetProjectedPos(Vector3 brushPos, out Vector3 projectedPos, float maxDist)
{
projectedPos = brushPos;
if (currentSelectedPatchID == -1)
return false;
if (surfaceManager.ProjectOnPatch(currentSelectedPatchID, brushPos, out projectedPos))
{
if (Vector3.Distance(projectedPos, brushPos) < maxDist)
{
return true;
}
}
return false;
}
private void UpdateSelectedPatch(Vector3 brushPos)
{
if (currentSelectedPatchID != -1)
{
// A patch is currently selected,
// Check whether it should still be selected,
// otherwise, explicitly deselect it
Vector3 projectedPos;
if (!GetProjectedPos(brushPos, out projectedPos, maxDist: parameters.Current.ProximityThreshold))
{
//if (currentStroke)
// Debug.Log("got too far from patch while drawing");
DeselectPatch(brushPos);
}
}
}
private bool TryAddGridConstraint(Vector3 pos)
{
if (grid.TryFindConstraint(pos, parameters.Current.ProximityThreshold, out Vector3 gridPointPos))
{
// Check for potential stroke/stroke intersection
Collider[] overlapped = Physics.OverlapSphere(canvas.transform.TransformPoint(gridPointPos), parameters.Current.ProximityThreshold);
Constraint constraint = null;
Collider prioritary = null;
foreach (var obj in overlapped)
{
if (obj.CompareTag("FinalStroke") && obj.GetComponent<FinalStroke>() != null)
{
// Add stroke/stroke constraint insteand
constraint = obj.GetComponent<FinalStroke>().GetConstraint(pos, parameters.Current.SnapToExistingNodeThreshold);
prioritary = obj;
}
// Second prioritary is mirror plane
if (obj.CompareTag("MirrorPlane") && prioritary == null)
{
prioritary = obj;
// Add stroke/mirror constraint insteand
constraint = new MirrorPlaneConstraint(gridPointPos, mirrorPlane.GetNormal());
}
}
if (constraint == null)
constraint = new Constraint(gridPointPos); // Grid point pos is in canvas space
currentStroke.AddConstraint(constraint, parameters.Current.MergeConstraintsThreshold);
//Debug.Log("found grid constraint at " + pos.ToString("F3"));
return true;
}
else
return false;
}
private void TrimDanglingEndpoints(Curve.Curve curve, IntersectionConstraint[] intersections, MirrorPlaneConstraint[] mirrorIntersections)
{
Constraint firstIntersection;
Constraint lastIntersection;
// Initialize intersections
if (intersections.Length > 0)
{
firstIntersection = intersections[0];
lastIntersection = intersections[0];
}
else if (mirrorIntersections.Length > 0)
{
firstIntersection = mirrorIntersections[0];
lastIntersection = mirrorIntersections[0];
}
else
{
// No intersections => won't trim endpoints
return;
}
foreach (var intersection in intersections)
{
if (intersection.NewCurveData.t < firstIntersection.NewCurveData.t)
{
firstIntersection = intersection;
}
else if (intersection.NewCurveData.t > lastIntersection.NewCurveData.t)
{
lastIntersection = intersection;
}
}
foreach (var intersection in mirrorIntersections)
{
if (intersection.NewCurveData.t < firstIntersection.NewCurveData.t)
{
firstIntersection = intersection;
}
else if (intersection.NewCurveData.t > lastIntersection.NewCurveData.t)
{
lastIntersection = intersection;
}
}
float firstIntersectionParam = firstIntersection.NewCurveData.t;
// Total length
float length = curve.GetLength();
// First segment length
float firstSegmentLength = curve.LengthBetween(0f, firstIntersectionParam);
// Cut if first segment is sufficiently small (yet still exists)
if (firstSegmentLength > Constants.eps && firstSegmentLength < parameters.Current.MaxHookSectionStrokeRatio * length && firstSegmentLength < parameters.Current.MaxHookSectionLength)
{
//Debug.Log("cutting stroke at t = " + firstIntersectionParam);
Reparameterization? r = curve.CutAt(firstIntersectionParam, throwBefore: true, snapToExistingAnchorThreshold: parameters.Current.SmallDistance * 0.1f);
foreach (var intersection in intersections)
{
intersection.ReparameterizeNewCurve(r, curve);
}
foreach (var intersection in mirrorIntersections)
{
intersection.ReparameterizeNewCurve(r, curve);
}
}
float lastIntersectionParam = lastIntersection.NewCurveData.t;
length = curve.GetLength();
float lastSegmentLength = curve.LengthBetween(lastIntersectionParam, 1f);
// Cut if last segment is sufficiently small (yet still exists)
if (lastSegmentLength > Constants.eps && lastSegmentLength < parameters.Current.MaxHookSectionStrokeRatio * length && lastSegmentLength < parameters.Current.MaxHookSectionLength)
{
//Debug.Log("cutting stroke at t = " + lastIntersectionParam);
Reparameterization? r = curve.CutAt(lastIntersectionParam, throwBefore: false, snapToExistingAnchorThreshold: parameters.Current.SmallDistance * 0.1f);
foreach (var intersection in intersections)
{
intersection.ReparameterizeNewCurve(r, curve);
}
foreach (var intersection in mirrorIntersections)
{
intersection.ReparameterizeNewCurve(r, curve);
}
}
}
}