mirror of
https://github.com/Xevion/procedural-placement.git
synced 2025-12-06 05:16:05 -06:00
implement CustomEditor GUI and most of the new handling
This commit is contained in:
@@ -5,10 +5,11 @@ using UnityEngine;
|
||||
public enum SamplingTypes {
|
||||
Random,
|
||||
Poisson,
|
||||
Improved_Poisson
|
||||
ImprovedPoisson
|
||||
};
|
||||
|
||||
public class PointRendering : MonoBehaviour {
|
||||
public String seed = "";
|
||||
[Tooltip("The sampling method used to generate points")]
|
||||
public SamplingTypes samplingMethod = SamplingTypes.Random;
|
||||
[Tooltip("The number of points (spheres) placed inside the region")]
|
||||
@@ -31,9 +32,10 @@ public class PointRendering : MonoBehaviour {
|
||||
private float _radius;
|
||||
private int _retryAttempts;
|
||||
|
||||
private List<Vector2> _points;
|
||||
public List<Vector2> points;
|
||||
|
||||
private void OnValidate() {
|
||||
public void OnValidate() {
|
||||
Debug.Log("Firing onValidate!");
|
||||
// Check to see if point position related values changed. SphereSize is rendering only, so we ignore it.
|
||||
if (numPoints != _prevNumPoints || _prevRegionSize != regionSize
|
||||
|| samplingMethod != _samplingMethod || radius != _radius || retryAttempts != _retryAttempts) {
|
||||
@@ -43,28 +45,25 @@ public class PointRendering : MonoBehaviour {
|
||||
_samplingMethod = samplingMethod;
|
||||
_radius = radius;
|
||||
_retryAttempts = retryAttempts;
|
||||
}
|
||||
}
|
||||
|
||||
// Handling for different types of point sampling methods
|
||||
float t1 = Time.realtimeSinceStartup;
|
||||
public void regeneratePoints() {
|
||||
switch (samplingMethod) {
|
||||
case SamplingTypes.Random:
|
||||
_points = PointGeneration.random_sampling(numPoints, regionSize);
|
||||
points = PointGeneration.random_sampling(numPoints, regionSize);
|
||||
break;
|
||||
case SamplingTypes.Poisson:
|
||||
_points = PointGeneration.poisson_sampling(numPoints, regionSize, radius, retryAttempts);
|
||||
numPoints = _points.Count;
|
||||
points = PointGeneration.poisson_sampling(numPoints, regionSize, radius, retryAttempts);
|
||||
numPoints = points.Count;
|
||||
break;
|
||||
case SamplingTypes.Improved_Poisson:
|
||||
_points = PointGeneration.improved_poisson_sampling(numPoints, regionSize, radius, retryAttempts);
|
||||
numPoints = _points.Count;
|
||||
case SamplingTypes.ImprovedPoisson:
|
||||
points = PointGeneration.improved_poisson_sampling(numPoints, regionSize, radius, retryAttempts);
|
||||
numPoints = points.Count;
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
float t2 = Time.realtimeSinceStartup;
|
||||
Debug.Log($"{samplingMethod} generated {_points.Count:n0} points in {t2 - t1:0.0000} seconds");
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDrawGizmos() {
|
||||
@@ -73,9 +72,9 @@ public class PointRendering : MonoBehaviour {
|
||||
new Vector3(regionSize.x + sphereSize, regionSize.y + sphereSize, sphereSize));
|
||||
|
||||
// Render spheres at every point
|
||||
if (_points == null) return;
|
||||
if (points == null) return;
|
||||
var pos = transform.position;
|
||||
foreach (var point in _points) {
|
||||
foreach (var point in points) {
|
||||
var center = new Vector3(point.x + pos.x - (regionSize.x / 2), point.y + pos.y - (regionSize.y / 2));
|
||||
Gizmos.DrawSphere(center,
|
||||
sphereSize);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
@@ -22,22 +23,51 @@ using UnityEngine;
|
||||
// Sphere Display Size
|
||||
// Point Minimum Radius (Only for 'Poisson' and 'Improved Poisson' Sampling Methods)
|
||||
// Point Retry Attempts (Only for 'Poisson' and 'Improved Poisson' Sampling Methods)
|
||||
// Draw Sphere?
|
||||
// Autogenerate?
|
||||
// Draw Radius?
|
||||
// Draw Parent Lines? (Only for 'Poisson' and 'Improved Poisson' Sampling Methods)
|
||||
// Randomize Seed?
|
||||
// Choose Point Sphere Radius?
|
||||
// Generate Button
|
||||
// Generation Time / Point Count
|
||||
|
||||
[CustomEditor(typeof(PointRendering))]
|
||||
public class SamplingEditor : Editor
|
||||
{
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
public class SamplingEditor : Editor {
|
||||
private string _message;
|
||||
|
||||
public override void OnInspectorGUI() {
|
||||
PointRendering points = (PointRendering) target;
|
||||
DrawDefaultInspector();
|
||||
|
||||
if(points.samplingMethod == SamplingTypes.Random)
|
||||
EditorGUILayout.HelpBox($"You are using the \"{points.samplingMethod}\" Sampling Method!", MessageType.Error);
|
||||
EditorGUI.BeginChangeCheck();
|
||||
points.samplingMethod = (SamplingTypes) EditorGUILayout.EnumPopup("Sampling Method", points.samplingMethod);
|
||||
points.seed = EditorGUILayout.TextField("Seed Value", points.seed);
|
||||
// Simple checks for point regeneration and point
|
||||
|
||||
if (points.samplingMethod == SamplingTypes.Random)
|
||||
points.numPoints = EditorGUILayout.IntField("Number of Points", points.numPoints);
|
||||
else {
|
||||
points.radius = EditorGUILayout.Slider("Point Minimum Radius", points.radius, 0.1f, 50f);
|
||||
points.retryAttempts = EditorGUILayout.IntSlider("Point Retry Attempts", points.retryAttempts, 1, 500);
|
||||
}
|
||||
bool regenerate = EditorGUI.EndChangeCheck();
|
||||
|
||||
// Check if redraw is needed
|
||||
EditorGUI.BeginChangeCheck();
|
||||
points.sphereSize = EditorGUILayout.FloatField("Sphere Display Size", points.sphereSize);
|
||||
bool redraw = EditorGUI.EndChangeCheck();
|
||||
|
||||
// Handling for different types of point sampling methods
|
||||
if (regenerate) {
|
||||
float t1 = Time.realtimeSinceStartup;
|
||||
points.regeneratePoints();
|
||||
float t2 = Time.realtimeSinceStartup;
|
||||
this._message = $"{points.points.Count:n0} points in {t2 - t1:0.0000}s";
|
||||
}
|
||||
|
||||
// Display timings in Editor GUI
|
||||
EditorGUILayout.HelpBox(_message, MessageType.Info);
|
||||
|
||||
if (regenerate || redraw)
|
||||
EditorUtility.SetDirty(target); // Redraw Gizmos
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user