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

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Utility.Meshes
{
public static class Curve
{
// Could do this on the GPU
public static Vector3[] CurveVertices(Vector3[] vertices, Vector3 ringCentre, Vector3 Axis, Transform origin, Vector3 meshCentre)
{
Vector3[] curvedVertices = new Vector3[vertices.Length];
Vector3 down = Vector3.ProjectOnPlane(meshCentre - ringCentre, Axis).normalized;
Vector3 perpendicular = Vector3.Cross(Axis, down).normalized;
float radius = Vector3.ProjectOnPlane(meshCentre - ringCentre, Axis).magnitude;
for (int i = 0; i < vertices.Length; i++)
{
Vector3 position = origin.TransformPoint(vertices[i]);
Vector3 directionFromCentre = Vector3.Project(position - meshCentre, perpendicular);
Vector3 directionAlongAxis = Vector3.Project(position - ringCentre, Axis);
float vertexRadius = Vector3.Project(position - ringCentre, down).magnitude;
float arcLength = directionFromCentre.magnitude;
float angle = (arcLength / radius);
if (Vector3.Dot(directionFromCentre, perpendicular) < 0)
angle = -angle;
Vector3 curvedDirection = Quaternion.AngleAxis(Mathf.Rad2Deg * angle, Axis) * down;
curvedVertices[i] = origin.InverseTransformPoint(ringCentre + (curvedDirection.normalized * (vertexRadius)) + directionAlongAxis);
}
return curvedVertices;
}
}
}