87 lines
1.7 KiB
C#
87 lines
1.7 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class SpherePointSampler : MonoBehaviour
|
|
{
|
|
[Header("Ustawienia sfery")]
|
|
public float radius = 10f;
|
|
|
|
public Vector3 spacing = new Vector3(1f, 1f, 1f);
|
|
|
|
public LayerMask terrainMask;
|
|
|
|
public LayerMask objectMask;
|
|
|
|
public List<Vector3> Points;
|
|
|
|
public bool isAllPointsInitialized;
|
|
|
|
public int updatePointPerFrame = 100;
|
|
|
|
public bool isDebugModeEnabled;
|
|
|
|
public event Action OnInitialized;
|
|
|
|
public void Generate()
|
|
{
|
|
StartCoroutine(OptimizationPointsDrawing());
|
|
}
|
|
|
|
private IEnumerator OptimizationPointsDrawing()
|
|
{
|
|
int num = updatePointPerFrame;
|
|
Points.Clear();
|
|
for (float x = 0f - radius; x < radius; x += spacing.x)
|
|
{
|
|
for (float z = 0f - radius; z < radius; z += spacing.z)
|
|
{
|
|
for (float y = radius; y > 0f - radius; y -= spacing.y)
|
|
{
|
|
num--;
|
|
if (num == 0)
|
|
{
|
|
yield return null;
|
|
num = updatePointPerFrame;
|
|
}
|
|
Vector3 vector = new Vector3(x, y, z) + base.transform.position;
|
|
if (Physics.Raycast(vector, Vector3.down, spacing.y, terrainMask))
|
|
{
|
|
break;
|
|
}
|
|
if (!(Vector3.Distance(vector, base.transform.position) > radius) && !(vector.y > 0f))
|
|
{
|
|
Points.Add(vector);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
this.OnInitialized();
|
|
isAllPointsInitialized = true;
|
|
}
|
|
|
|
private void OnDrawGizmos()
|
|
{
|
|
if (!isDebugModeEnabled)
|
|
{
|
|
return;
|
|
}
|
|
Gizmos.color = new Color(1f, 1f, 1f, 0.2f);
|
|
Gizmos.DrawSphere(base.transform.position, radius);
|
|
if (Points == null || Points.Count <= 0)
|
|
{
|
|
return;
|
|
}
|
|
foreach (Vector3 point in Points)
|
|
{
|
|
Gizmos.DrawSphere(point, 0.2f);
|
|
}
|
|
}
|
|
|
|
public Vector3 GetRandomPoint()
|
|
{
|
|
return Points[UnityEngine.Random.Range(0, Points.Count)];
|
|
}
|
|
}
|