Arpolygon
Arpolygon
Arpolygon
using UnityEngine.EventSystems;
using EnhancedTouch = UnityEngine.InputSystem.EnhancedTouch;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
using UnityEngine.UI;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using TMPro;
undoButton.gameObject.SetActive(false);
resetButton.gameObject.SetActive(false);
rotateSlider.gameObject.SetActive(false);
btnAddToCart.gameObject.SetActive(false);
btnCapturePhoto.gameObject.SetActive(false);
tilePanel.gameObject.SetActive(false);
addButton.gameObject.SetActive(false);
createPolyButton.gameObject.SetActive(false);
}
cartManager = FindObjectOfType<cartManager>();
if (cartManager == null)
{
Debug.LogError("cartManager not found!");
}
}
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(finger.currentTouch.screenPosition);
if (Physics.Raycast(ray, out hit) &&
dots.Contains(hit.transform.gameObject))
{
selectedDot = hit.transform.gameObject;
isDragging = true;
originalDraggedDotPosition = selectedDot.transform.position;
return;
}
if (isMoveActive) return;
StartCoroutine(HandleFingerDownRaycast(finger.currentTouch.screenPosition));
}
// Perform AR Raycast on the main thread and yield control to allow frame
updates
if (raycastManager.Raycast(screenPosition, hits,
TrackableType.PlaneWithinPolygon))
{
Pose hitPose = hits[0].pose;
savedTouchPosition = hitPose.position;
if (dots.Count >= 4)
{
isDialogOpen = true;
isDialogOpen = false;
if (!confirmAddDotResult)
{
savedTouchPosition = null;
yield break;
}
}
if (savedTouchPosition.HasValue)
{
Vector3 newDotPosition = savedTouchPosition.Value;
if (points.Count < 2)
{
SpawnDot(newDotPosition);
savedTouchPosition = null;
if (points.Count > 1) DrawLines();
yield break;
}
if (closestSegmentIndex != -1)
{
InsertDotAtSegment(closestSegmentIndex, closestPointOnSegment);
}
else if (!IsIntersectingExistingLines(lastDotPosition,
newDotPosition))
{
SpawnDot(newDotPosition);
}
else
{
ShowError("New line would intersect an existing line. Dot not
added.");
}
savedTouchPosition = null;
if (points.Count > 1) DrawLines();
if (points.Count >= 3 && !createPolyButton.gameObject.activeSelf)
createPolyButton.gameObject.SetActive(true);
}
}
}
if (dotIndex >= 0)
{
Vector3 prevPoint = (dotIndex > 0) ? points[dotIndex - 1] :
points[points.Count - 1];
Vector3 nextPoint = (dotIndex < points.Count - 1) ? points[dotIndex
+ 1] : points[0];
Vector3 newPosition = selectedDot.transform.position;
private bool DoLinesIntersect(Vector3 p1, Vector3 p2, Vector3 q1, Vector3 q2)
{
return ArePointsOnOppositeSides(p1, p2, q1, q2) &&
ArePointsOnOppositeSides(q1, q2, p1, p2);
}
private bool ArePointsOnOppositeSides(Vector3 a, Vector3 b, Vector3 p, Vector3
q)
{
Vector3 ab = b - a;
Vector3 ap = p - a;
Vector3 aq = q - a;
confirmAddDotResult = tcs.Task.Result;
CalculateTotalTilesNeeded(width, height);
ResetDotPoints();
ActivateUIAfterCreation();
}
tempVertices.RemoveAt(i);
earFound = true;
break;
}
if (!earFound)
{
Debug.LogWarning("Triangulation stopped - unable to find ear. Check
input polygon.");
break;
}
}
}
if (tileSizeInMeters == 0.4f)
{
groutWidth = 0.005f;
groutSpacing = "4-5mm";
}
else if (tileSizeInMeters == 0.3f)
{
groutWidth = 0.004f;
groutSpacing = "3-4mm";
}
else if (tileSizeInMeters == 0.6f)
{
groutWidth = 0.003f;
groutSpacing = "2-3mm";
}
// Add one corner tile for any remaining space in both directions
if (partialWidth > 0 && partialHeight > 0)
{
totalTilesNeeded += 1; // Additional corner tile
partialTileArea += partialWidth * partialHeight;
}
// Output results
Debug.Log($"Total Area: {totalArea} sqm");
Debug.Log($"Total Tiles Needed: {totalTilesNeeded}");
Debug.Log($"Partial Tile Area: {partialTileArea} sqm");
Debug.Log($"Recommended Grout Spacing: {groutSpacing}");
Debug.Log($"Grout Required (kg): {groutRequiredKg}");
Debug.Log($"Adhesive Required (kg): {adhesiveRequiredKg}");
// Display results
resultText.text = $"Number of Tiles: {totalTilesNeeded}\n";
resultText.text += $"Partial Tile Area: {partialTileArea:F2} sqm\n";
resultText.text += $"Recommended Grout Spacing: {groutSpacing}\n";
resultText.text += $"Grout Required: {groutRequiredKg} kg\n";
resultText.text += $"Adhesive Required: {adhesiveRequiredKg} kg";
resultText.gameObject.SetActive(true);
currentTotalTilesNeeded = totalTilesNeeded;
return totalTilesNeeded;
}
points.Clear();
dots.Clear();
if (points.Count > 0)
{
ResetDotPoints();
}
currentPolygon.SetActive(true);
tilePanel.gameObject.SetActive(true);
resultText.gameObject.SetActive(true);
rotateSlider.gameObject.SetActive(true);
btnAddToCart.gameObject.SetActive(true);
btnCapturePhoto.gameObject.SetActive(true);
moveButton.gameObject.SetActive(true);
addButton.gameObject.SetActive(true);
}
if (tileSizeSlider.value == previousSliderValue)
{
return;
}
previousSliderValue = tileSizeSlider.value;
tileSizeInMeters = sizeValues[sizeIndex];
if (currentPolygon != null)
{
Vector3 minBounds =
currentPolygon.GetComponent<MeshFilter>().mesh.bounds.min;
Vector3 maxBounds =
currentPolygon.GetComponent<MeshFilter>().mesh.bounds.max;
CalculateTotalTilesNeeded(maxBounds.x - minBounds.x, maxBounds.z -
minBounds.z);
}
}
Button confirmButton =
dialog.transform.Find("btnConfirm").GetComponent<Button>();
Button cancelButton =
dialog.transform.Find("btnCancel").GetComponent<Button>();
TextMeshProUGUI panelText =
successPanel.GetComponentInChildren<TextMeshProUGUI>();
panelText.text = message;
successPanel.transform.localScale = Vector3.zero;
LeanTween.scale(successPanel, Vector3.one,
0.5f).setEase(LeanTweenType.easeOutBack);
LeanTween.delayedCall(2f, () =>
{
LeanTween.scale(successPanel, Vector3.zero,
0.5f).setEase(LeanTweenType.easeInBack)
.setOnComplete(() => Destroy(successPanel));
});
}
void OnMoveButtonClicked()
{
isMoveActive = true; // Set to true when Move button is clicked
moveButton.gameObject.SetActive(false);
addButton.gameObject.SetActive(true);
Debug.Log("Move Button Active: " + isMoveActive);
void OnAddButtonClicked()
{
isMoveActive = false; // Set to false when Add button is clicked
moveButton.gameObject.SetActive(true);
addButton.gameObject.SetActive(false);
Debug.Log("Move Button Active: " + isMoveActive);
}
}