Files
OpticsSimulation.Unity/Assets/Scenes/OpticalSurface2D.cs
T

47 lines
1.1 KiB
C#
Raw Normal View History

2025-05-13 01:04:37 +03:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class OpticalSurface2D : MonoBehaviour
{
[Serializable]
public struct Line {
public Vector2 P1;
public Vector2 P2;
}
public Line[] Shape;
public abstract bool Raycast(Ray2D ray, ref RaycastMath.RaycastResult2D result);
public static Line[] PointsToLines(Vector2[] points, bool closed) {
Line[] result = new Line[points.Length - (closed ? 0 : 1)];
for (int i = 0; i < points.Length - 1; i++) {
result[i] = new Line() { P1 = points[i], P2 = points[i + 1] };
}
if (closed) {
result[result.Length - 1] = new Line() { P1 = result[result.Length - 2].P2, P2 = result[0].P1 };
}
return result;
}
private void OnDrawGizmos() {
if (Shape == null) return;
if (Shape.Length < 1) return;
Gizmos.matrix = transform.localToWorldMatrix;
for (int i = 0; i < Shape.Length; i++) {
Gizmos.DrawLine(Shape[i].P1, Shape[i].P2);
}
Gizmos.matrix = Matrix4x4.identity;
}
}