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

80 lines
2.0 KiB
C#
Raw Normal View History

2025-05-13 01:04:37 +03:00
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class SellmeierEquatiionView : MonoBehaviour
{
private SellmeierEquation _equation;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private float[] SamplePlot(SellmeierEquation equation) {
int samplesCount = 30;
float startLength = 0.3f;
float endLength = 0.6f;
float[] result = new float[samplesCount];
for (int i = 0; i < samplesCount; i++) {
if(i > 0) {
float step = ((endLength - startLength) / (samplesCount - 1));
result[i] = equation.Calc(startLength + step * i);
} else {
result[i] = equation.Calc(startLength);
}
}
return result;
}
private void OnDrawGizmos() {
if (_equation == null) {
var coeffs = new SellmeierEquation.Coefficients(
new float[] { 0.99654f, 0.18964f, 0.00411f },
new float[] { 0.00787f, 0.02191f, 3.85727f });
_equation = new SellmeierEquation(coeffs);
}
var samples = SamplePlot(_equation);
float minValue = samples.Min();
float maxValue = samples.Max();
float n365 = _equation.Calc(0.365f);
float n395 = _equation.Calc(0.395f);
float n405 = _equation.Calc(0.405f);
Debug.Log($"n365 = {n365}, n395 = {n395}, n405 = {n405}");
for (int i = 0; i < samples.Length - 1; i++) {
var n1 = samples[i];
var n2 = samples[i + 1];
float n1Scaled = (n1 - minValue) / (maxValue - minValue);
float n2Scaled = (n2 - minValue) / (maxValue - minValue);
Gizmos.DrawLine(transform.position + Vector3.right * (i / (float)samples.Length) + Vector3.up * n1Scaled, transform.position + Vector3.right * ((i + 1) / (float)samples.Length) + Vector3.up * n2Scaled);
}
}
}