45 lines
1.2 KiB
C#
45 lines
1.2 KiB
C#
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
||
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
public class CircleSurface : OpticalSurface2D
|
||
|
|
{
|
||
|
|
|
||
|
|
public float Radius;
|
||
|
|
public int ViewSamples = 15;
|
||
|
|
|
||
|
|
void Start()
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
void Update()
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnValidate() {
|
||
|
|
Shape = PointsToLines(GetGeometry(), true);
|
||
|
|
}
|
||
|
|
|
||
|
|
private Vector2[] GetGeometry() {
|
||
|
|
Vector2[] result = new Vector2[ViewSamples];
|
||
|
|
float da = Mathf.PI * 2 / ViewSamples;
|
||
|
|
for (int i = 0; i < ViewSamples; i++) {
|
||
|
|
float angle = da * i;
|
||
|
|
result[i] = new Vector2(Mathf.Cos(angle) * Radius, Mathf.Sin(angle) * Radius);
|
||
|
|
}
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
public override bool Raycast(Ray2D ray, ref RaycastMath.RaycastResult2D result) {
|
||
|
|
var localSpaceRay = new Ray2D( transform.InverseTransformPoint(ray.origin), transform.InverseTransformVector(ray.direction).normalized);
|
||
|
|
if (RaycastMath.RaycastCircle(localSpaceRay, Radius, ref result)) {
|
||
|
|
result.Point = transform.TransformPoint(result.Point);
|
||
|
|
result.Normal = transform.worldToLocalMatrix.MultiplyVector(result.Normal).normalized;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|