implement CustomEditor GUI and most of the new handling

This commit is contained in:
Xevion
2020-05-06 13:34:11 -05:00
parent b2a001a574
commit 0dbbebb975
3 changed files with 2889 additions and 40 deletions

View File

@@ -5,10 +5,11 @@ using UnityEngine;
public enum SamplingTypes { public enum SamplingTypes {
Random, Random,
Poisson, Poisson,
Improved_Poisson ImprovedPoisson
}; };
public class PointRendering : MonoBehaviour { public class PointRendering : MonoBehaviour {
public String seed = "";
[Tooltip("The sampling method used to generate points")] [Tooltip("The sampling method used to generate points")]
public SamplingTypes samplingMethod = SamplingTypes.Random; public SamplingTypes samplingMethod = SamplingTypes.Random;
[Tooltip("The number of points (spheres) placed inside the region")] [Tooltip("The number of points (spheres) placed inside the region")]
@@ -31,9 +32,10 @@ public class PointRendering : MonoBehaviour {
private float _radius; private float _radius;
private int _retryAttempts; 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. // Check to see if point position related values changed. SphereSize is rendering only, so we ignore it.
if (numPoints != _prevNumPoints || _prevRegionSize != regionSize if (numPoints != _prevNumPoints || _prevRegionSize != regionSize
|| samplingMethod != _samplingMethod || radius != _radius || retryAttempts != _retryAttempts) { || samplingMethod != _samplingMethod || radius != _radius || retryAttempts != _retryAttempts) {
@@ -43,28 +45,25 @@ public class PointRendering : MonoBehaviour {
_samplingMethod = samplingMethod; _samplingMethod = samplingMethod;
_radius = radius; _radius = radius;
_retryAttempts = retryAttempts; _retryAttempts = retryAttempts;
}
}
// Handling for different types of point sampling methods public void regeneratePoints() {
float t1 = Time.realtimeSinceStartup;
switch (samplingMethod) { switch (samplingMethod) {
case SamplingTypes.Random: case SamplingTypes.Random:
_points = PointGeneration.random_sampling(numPoints, regionSize); points = PointGeneration.random_sampling(numPoints, regionSize);
break; break;
case SamplingTypes.Poisson: case SamplingTypes.Poisson:
_points = PointGeneration.poisson_sampling(numPoints, regionSize, radius, retryAttempts); points = PointGeneration.poisson_sampling(numPoints, regionSize, radius, retryAttempts);
numPoints = _points.Count; numPoints = points.Count;
break; break;
case SamplingTypes.Improved_Poisson: case SamplingTypes.ImprovedPoisson:
_points = PointGeneration.improved_poisson_sampling(numPoints, regionSize, radius, retryAttempts); points = PointGeneration.improved_poisson_sampling(numPoints, regionSize, radius, retryAttempts);
numPoints = _points.Count; numPoints = points.Count;
break; break;
default: default:
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
} }
float t2 = Time.realtimeSinceStartup;
Debug.Log($"{samplingMethod} generated {_points.Count:n0} points in {t2 - t1:0.0000} seconds");
}
} }
private void OnDrawGizmos() { private void OnDrawGizmos() {
@@ -73,9 +72,9 @@ public class PointRendering : MonoBehaviour {
new Vector3(regionSize.x + sphereSize, regionSize.y + sphereSize, sphereSize)); new Vector3(regionSize.x + sphereSize, regionSize.y + sphereSize, sphereSize));
// Render spheres at every point // Render spheres at every point
if (_points == null) return; if (points == null) return;
var pos = transform.position; 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)); var center = new Vector3(point.x + pos.x - (regionSize.x / 2), point.y + pos.y - (regionSize.y / 2));
Gizmos.DrawSphere(center, Gizmos.DrawSphere(center,
sphereSize); sphereSize);

View File

@@ -1,4 +1,5 @@
using System.Collections; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
@@ -22,22 +23,51 @@ using UnityEngine;
// Sphere Display Size // Sphere Display Size
// Point Minimum Radius (Only for 'Poisson' and 'Improved Poisson' Sampling Methods) // Point Minimum Radius (Only for 'Poisson' and 'Improved Poisson' Sampling Methods)
// Point Retry Attempts (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) // Draw Parent Lines? (Only for 'Poisson' and 'Improved Poisson' Sampling Methods)
// Randomize Seed? // Randomize Seed?
// Choose Point Sphere Radius?
// Generate Button // Generate Button
// Generation Time / Point Count // Generation Time / Point Count
[CustomEditor(typeof(PointRendering))] [CustomEditor(typeof(PointRendering))]
public class SamplingEditor : Editor public class SamplingEditor : Editor {
{ private string _message;
public override void OnInspectorGUI()
{
public override void OnInspectorGUI() {
PointRendering points = (PointRendering) target; PointRendering points = (PointRendering) target;
DrawDefaultInspector();
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) if (points.samplingMethod == SamplingTypes.Random)
EditorGUILayout.HelpBox($"You are using the \"{points.samplingMethod}\" Sampling Method!", MessageType.Error); 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
} }
} }

View File

File diff suppressed because it is too large Load Diff