Files
2026-02-21 16:45:37 +08:00

74 lines
1.5 KiB
C#

using System;
using UnityEngine;
namespace uNature.Core
{
public static class UNMath
{
private static System.Random rnd = new System.Random();
public static float GetRndRange(float min, float max)
{
return (float)((double)min + rnd.NextDouble() * (double)(max - min));
}
public static float GetHeightAtWorldPoint(float x, float z, Vector3 terrainSize, float[,] heights)
{
x = Mathf.Clamp(x, 0f, terrainSize.x);
z = Mathf.Clamp(z, 0f, terrainSize.z);
return heights[(int)x, (int)z];
}
public static Vector3 GetNormalAtWorldPoint(float x, float z, Vector3 terrainSize, Vector3[,] normals)
{
x = Mathf.Clamp(x, 0f, terrainSize.x);
z = Mathf.Clamp(z, 0f, terrainSize.z);
return normals[(int)x, (int)z];
}
public static bool isBitMasked(this int mask, int bit)
{
return (mask & (1 << bit)) > 0;
}
public static bool CheckMinMaxBounds(Vector2 min, Vector2 max, float offset, Vector2 position)
{
Vector2 vector = new Vector2(offset, offset);
min -= vector;
max += vector;
return position.x > min.x && position.y > min.y && position.x < max.x && position.y < max.y;
}
public static int iClamp(int x, int from, int to)
{
if (x < from)
{
return from;
}
if (x > to)
{
return to;
}
return x;
}
public static int iClampMin(int x, int from)
{
if (x < from)
{
return from;
}
return x;
}
public static int iClampMax(int x, int to)
{
if (x > to)
{
return to;
}
return x;
}
}
}