Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.6 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. namespace Utility.Meshes
  6. {
  7. public static class Curve
  8. {
  9. // Could do this on the GPU
  10. public static Vector3[] CurveVertices(Vector3[] vertices, Vector3 ringCentre, Vector3 Axis, Transform origin, Vector3 meshCentre)
  11. {
  12. Vector3[] curvedVertices = new Vector3[vertices.Length];
  13. Vector3 down = Vector3.ProjectOnPlane(meshCentre - ringCentre, Axis).normalized;
  14. Vector3 perpendicular = Vector3.Cross(Axis, down).normalized;
  15. float radius = Vector3.ProjectOnPlane(meshCentre - ringCentre, Axis).magnitude;
  16. for (int i = 0; i < vertices.Length; i++)
  17. {
  18. Vector3 position = origin.TransformPoint(vertices[i]);
  19. Vector3 directionFromCentre = Vector3.Project(position - meshCentre, perpendicular);
  20. Vector3 directionAlongAxis = Vector3.Project(position - ringCentre, Axis);
  21. float vertexRadius = Vector3.Project(position - ringCentre, down).magnitude;
  22. float arcLength = directionFromCentre.magnitude;
  23. float angle = (arcLength / radius);
  24. if (Vector3.Dot(directionFromCentre, perpendicular) < 0)
  25. angle = -angle;
  26. Vector3 curvedDirection = Quaternion.AngleAxis(Mathf.Rad2Deg * angle, Axis) * down;
  27. curvedVertices[i] = origin.InverseTransformPoint(ringCentre + (curvedDirection.normalized * (vertexRadius)) + directionAlongAxis);
  28. }
  29. return curvedVertices;
  30. }
  31. }
  32. }