Skip to content

Commit d70150b

Browse files
RogPodgetarukosukeveleigh
authored
Put pan limits when scaling followup (#9701)
* Put pan limits when scaling * add pan limit tests * refactor: Create ScroollToLimit method * refactor pan test * refactor scale and scroll * add slate jitter test * Differing UV Fix * Differing UV Fix * Differing UV Fix * Update Assets/MRTK/Tests/PlayModeTests/SlateTests.cs * UpdateUV's no longer called twice per scale call * Update Assets/MRTK/SDK/Features/UX/Scripts/Slate/HandInteractionPanZoom.cs Co-authored-by: Kurtis <[email protected]> * PR comments Co-authored-by: Furuta Yusuke <[email protected]> Co-authored-by: Kurtis <[email protected]>
1 parent fa86b66 commit d70150b

File tree

2 files changed

+300
-23
lines changed

2 files changed

+300
-23
lines changed

Assets/MRTK/SDK/Features/UX/Scripts/Slate/HandInteractionPanZoom.cs

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -379,46 +379,72 @@ private void UpdateIdle()
379379

380380
private void UpdateUVMapping()
381381
{
382-
Vector2 tiling = currentMaterial != null ? currentMaterial.mainTextureScale : new Vector2(1.0f, 1.0f);
383-
Vector2 uvTestValue;
384382
mesh.GetUVs(0, uvs);
385383
uvsOrig.Clear();
386384
uvsOrig.AddRange(uvs);
387-
float scaleUVDelta = 0.0f;
388-
Vector2 scaleUVCentroid = Vector2.zero;
389-
float currentContactRatio = 0.0f;
390385

386+
Vector2 offsetUVDelta = new Vector2(-totalUVOffset.x, totalUVOffset.y);
387+
388+
// Scale
391389
if (ScaleActive)
392390
{
393-
scaleUVCentroid = GetDisplayedUVCentroid(uvs);
394-
currentContactRatio = GetUVScaleFromTouches();
395-
scaleUVDelta = currentContactRatio / previousContactRatio;
391+
var scaleUVCentroid = GetDisplayedUVCentroid(uvs);
392+
var currentContactRatio = GetUVScaleFromTouches();
393+
var scaleUVDelta = currentContactRatio / previousContactRatio;
396394
previousContactRatio = currentContactRatio;
397395

398396
currentScale = totalUVScale.x / scaleUVDelta;
399397

400398
// Test for scale limits
401399
if (currentScale > minScale && currentScale < maxScale)
402400
{
403-
// Track total scale
404-
totalUVScale /= scaleUVDelta;
405-
for (int i = 0; i < uvs.Count; ++i)
401+
var scaleAndScrollUVDeltas = new List<Vector2>();
402+
for (int i = 0; i < uvs.Count; i++)
406403
{
407-
// This is where zoom is applied if Active
408-
uvs[i] = ((uvs[i] - scaleUVCentroid) / scaleUVDelta) + scaleUVCentroid;
404+
Vector2 adjustedScaleUVDelta = ((uvs[i] - scaleUVCentroid) / scaleUVDelta) + scaleUVCentroid - uvs[i];
405+
scaleAndScrollUVDeltas.Add(adjustedScaleUVDelta + offsetUVDelta);
409406
}
407+
UpdateUV(uvs, scaleAndScrollUVDeltas);
408+
409+
Vector2 upperLeft = uvs[UpperLeftQuadIndex];
410+
Vector2 upperRight = uvs[UpperRightQuadIndex];
411+
Vector2 lowerLeft = uvs[LowerLeftQuadIndex];
412+
totalUVScale.x = upperRight.x - upperLeft.x;
413+
totalUVScale.y = upperLeft.y - lowerLeft.y;
410414
}
411415
}
416+
else
417+
{
418+
// Scroll
419+
UpdateUVWithScroll(uvs, offsetUVDelta);
420+
}
421+
422+
mesh.SetUVs(0, uvs);
423+
}
424+
425+
private void UpdateUVWithScroll(List<Vector2> uvs, Vector2 uvDelta)
426+
{
427+
var uvDeltas = new List<Vector2>();
428+
for (int i = 0; i < uvs.Count; i++)
429+
{
430+
uvDeltas.Add(uvDelta);
431+
}
432+
433+
UpdateUV(uvs, uvDeltas, true);
434+
}
435+
436+
private void UpdateUV(List<Vector2> uvs, List<Vector2> uvDeltas, bool scrollOnly = false)
437+
{
438+
Vector2 tiling = currentMaterial != null ? currentMaterial.mainTextureScale : new Vector2(1.0f, 1.0f);
412439

413-
// Test for pan limits
414-
Vector2 uvDelta = new Vector2(totalUVOffset.x, -totalUVOffset.y);
415440
if (!unlimitedPan)
416441
{
417442
bool xLimited = false;
418443
bool yLimited = false;
419-
for (int i = 0; i < uvs.Count; ++i)
444+
445+
for (int i = 0; i < uvs.Count; i++)
420446
{
421-
uvTestValue = uvs[i] - uvDelta;
447+
var uvTestValue = uvs[i] + uvDeltas[i];
422448
if (uvTestValue.x > tiling.x * maxPanHorizontal || uvTestValue.x < -(tiling.x * maxPanHorizontal))
423449
{
424450
xLimited = true;
@@ -429,20 +455,28 @@ private void UpdateUVMapping()
429455
}
430456
}
431457

432-
for (int i = 0; i < uvs.Count; ++i)
458+
if (scrollOnly)
459+
{
460+
for (int i = 0; i < uvs.Count; ++i)
461+
{
462+
uvs[i] = new Vector2(xLimited ? uvs[i].x : uvs[i].x + uvDeltas[i].x, yLimited ? uvs[i].y : uvs[i].y + uvDeltas[i].y);
463+
}
464+
}
465+
else if (!xLimited && !yLimited)
433466
{
434-
uvs[i] = new Vector2(xLimited ? uvs[i].x : uvs[i].x - uvDelta.x, yLimited ? uvs[i].y : uvs[i].y - uvDelta.y);
467+
for (int i = 0; i < uvs.Count; ++i)
468+
{
469+
uvs[i] += uvDeltas[i];
470+
}
435471
}
436472
}
437473
else
438474
{
439475
for (int i = 0; i < uvs.Count; ++i)
440476
{
441-
uvs[i] -= uvDelta;
477+
uvs[i] += uvDeltas[i];
442478
}
443479
}
444-
445-
mesh.SetUVs(0, uvs);
446480
}
447481

448482
private float GetUVScaleFromTouches()

0 commit comments

Comments
 (0)