make methods private, switch to var type, fix sphere offsets and draw correctly, add sampling method update to OnValidate event

This commit is contained in:
Xevion
2020-05-05 14:20:50 -05:00
parent 97df401b84
commit 0fdc28810a
2 changed files with 12 additions and 11 deletions

View File

@@ -16,7 +16,6 @@ public class CubeRendering : MonoBehaviour {
public int numPoints = 100; public int numPoints = 100;
[Tooltip("The size of the rendered cubes")] [Tooltip("The size of the rendered cubes")]
public float cubeSize = 1; public float cubeSize = 1;
[Tooltip("The Size of the region points are generated in")] [Tooltip("The Size of the region points are generated in")]
public Vector2 regionSize = Vector2.one; public Vector2 regionSize = Vector2.one;
@@ -27,12 +26,14 @@ public class CubeRendering : MonoBehaviour {
private List<Vector2> _points; private List<Vector2> _points;
void OnValidate() { private void OnValidate() {
// Check to see if point position related values changed. CubeSize is rendering only, so we ignore it. // Check to see if point position related values changed. CubeSize is rendering only, so we ignore it.
if (numPoints != _prevNumPoints || _prevRegionSize != regionSize) { if (numPoints != _prevNumPoints || _prevRegionSize != regionSize
|| samplingMethod != _samplingMethod) {
// Update the tracking values // Update the tracking values
_prevNumPoints = numPoints; _prevNumPoints = numPoints;
_prevRegionSize = regionSize; _prevRegionSize = regionSize;
_samplingMethod = samplingMethod;
switch (samplingMethod) { switch (samplingMethod) {
case SamplingTypes.Random: case SamplingTypes.Random:
@@ -47,15 +48,15 @@ public class CubeRendering : MonoBehaviour {
} }
} }
void OnDrawGizmos() { private void OnDrawGizmos() {
// Draw a wireframe around the entire region // Draw a wireframe around the entire region
Gizmos.DrawWireCube(this.transform.position, Gizmos.DrawWireCube(this.transform.position,
new Vector3(regionSize.x * 2 + cubeSize, regionSize.y * 2 + cubeSize, cubeSize)); new Vector3(regionSize.x * 2 + cubeSize, regionSize.y * 2 + cubeSize, cubeSize));
// Render spheres at every point // Render spheres at every point
if (_points == null) return; if (_points == null) return;
foreach (var point in _points) { var pos = transform.position;
Gizmos.DrawSphere(point, cubeSize); foreach (var point in _points)
} Gizmos.DrawSphere(new Vector3(point.x + pos.x, point.y + pos.y, pos.z), cubeSize);
} }
} }

View File

@@ -1,11 +1,11 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class PointGeneration { public static class PointGeneration {
public static List<Vector2> random_sampling(int numPoints, Vector2 range, Vector2 offset) { public static List<Vector2> random_sampling(int numPoints, Vector2 range, Vector2 offset) {
// Create a new empty list of points, and add some randomly // Create a new empty list of points, and add some randomly
List<Vector2> points = new List<Vector2>(); var points = new List<Vector2>();
for (int i = 0; i < numPoints; i++) { for (var i = 0; i < numPoints; i++) {
points.Add( points.Add(
new Vector2( new Vector2(
Random.Range(-range.x, range.x), Random.Range(-range.x, range.x),