Files
OpticsSimulation.Unity/Assets/Scenes/PointLight.cs
T
2025-05-13 01:04:37 +03:00

68 lines
1.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PointLight : MonoBehaviour
{
public int SamplesCount = 5;
public float Angle = 45.0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public Ray2D[] GetRays() {
float angleRad = Mathf.Deg2Rad * Angle;
float halfAngleRad = angleRad / 2.0f;
if(SamplesCount == 1) {
halfAngleRad = 0;
}
float startAngle = 2.5f * Mathf.PI - halfAngleRad;
float endAngle = 2.5f * Mathf.PI + halfAngleRad;
Ray2D[] result = new Ray2D[SamplesCount];
float step = angleRad / (SamplesCount - 1);
if (SamplesCount > 1) {
for (int i = 0; i < SamplesCount; i++) {
float a = startAngle + step * i;
result[i] = new Ray2D(transform.position, transform.TransformVector(new Vector3(Mathf.Cos(a), Mathf.Sin(a), 0.0f)).normalized);
}
} else {
result[0] = new Ray2D(transform.position, transform.TransformVector( new Vector3(Mathf.Cos(startAngle), Mathf.Sin(startAngle), 0.0f)).normalized);
}
return result;
}
public bool DrawGizmo = true;
private void OnDrawGizmos() {
if (!DrawGizmo) {
return;
}
var rays = GetRays();
foreach (var ray in rays) {
Gizmos.DrawLine(ray.origin, ray.origin + ray.direction * 0.5f);
}
}
}