From 792a8fa452c3852c0bc72f6603984c1ec7ec6397 Mon Sep 17 00:00:00 2001 From: Xevion Date: Fri, 13 Nov 2020 21:39:25 -0600 Subject: [PATCH] support Width and Height properly (rectangles), automatic scaling & fit within Camera boundaries, add timescale clamp + clampedIncrement --- Paths/Assets/Scenes/Default.unity | 10 +++-- Paths/Assets/Scripts/Algorithms/NodeGrid.cs | 1 + Paths/Assets/Scripts/GridController.cs | 27 +++++++----- Paths/Assets/Scripts/Manager.cs | 46 +++++++++++++++------ Paths/Assets/Shaders/GridShader.shader | 13 +++--- 5 files changed, 64 insertions(+), 33 deletions(-) diff --git a/Paths/Assets/Scenes/Default.unity b/Paths/Assets/Scenes/Default.unity index ce99e3e..5e175da 100644 --- a/Paths/Assets/Scenes/Default.unity +++ b/Paths/Assets/Scenes/Default.unity @@ -246,9 +246,12 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: ca142250b9964eb1adf66ac7c5cc3e3e, type: 3} m_Name: m_EditorClassIdentifier: + mainCamera: {fileID: 519420031} + gridObject: {fileID: 2092623184} gridController: {fileID: 2092623185} debugText: {fileID: 1436240699} - speed: 7.18 + speed: 1000 + clampIncrement: 1 --- !u!1 &1436240698 GameObject: m_ObjectHideFlags: 0 @@ -442,7 +445,7 @@ RectTransform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1436240698} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalPosition: {x: 0, y: 0, z: -0.3} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} @@ -576,7 +579,8 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: gridMaterial: {fileID: 2100000, guid: 780d53bea4df7b0418e9c8ed8afd6410, type: 2} - size: 32 + width: 130 + height: 61 --- !u!23 &2092623186 MeshRenderer: m_ObjectHideFlags: 0 diff --git a/Paths/Assets/Scripts/Algorithms/NodeGrid.cs b/Paths/Assets/Scripts/Algorithms/NodeGrid.cs index ab5dcbc..4c058d9 100644 --- a/Paths/Assets/Scripts/Algorithms/NodeGrid.cs +++ b/Paths/Assets/Scripts/Algorithms/NodeGrid.cs @@ -131,6 +131,7 @@ namespace Algorithms { /// /// A instantiated level generator (ILevelGenerator) object public void ApplyGenerator(ILevelGenerator generator) { + generator.Generate(this); } public IEnumerable Iterator() { diff --git a/Paths/Assets/Scripts/GridController.cs b/Paths/Assets/Scripts/GridController.cs index d6edf4a..5589169 100644 --- a/Paths/Assets/Scripts/GridController.cs +++ b/Paths/Assets/Scripts/GridController.cs @@ -1,9 +1,9 @@ using System; -using System.Linq; using UnityEngine; public enum PropertyName { - GridSize, + GridWidth, + GridHeight, ValueLength, Values } @@ -13,8 +13,9 @@ public enum PropertyName { /// public class GridController : MonoBehaviour { public Material gridMaterial; // Maintain reference to the Grid Material the Shader is implanted upon - public int size = 32; // Size of the grid, width and height - + public int width; + public int height; + // Value management private int[] _values; private ComputeBuffer _buffer; @@ -22,10 +23,11 @@ public class GridController : MonoBehaviour { // Get all property IDs private static readonly int ValueLength = Shader.PropertyToID("_valueLength"); private static readonly int Values = Shader.PropertyToID("_values"); - private static readonly int GridSize = Shader.PropertyToID("_GridSize"); + private static readonly int GridWidth = Shader.PropertyToID("_GridWidth"); + private static readonly int GridHeight = Shader.PropertyToID("_GridHeight"); private void Start() { - _values = new int[size * size]; + _values = new int[width * height]; _buffer = new ComputeBuffer((int) Mathf.Pow(2048, 2), 4); // Update all Shader properties @@ -40,8 +42,11 @@ public class GridController : MonoBehaviour { /// public void UpdateShader(PropertyName property) { switch (property) { - case PropertyName.GridSize: - gridMaterial.SetFloat(GridSize, size); + case PropertyName.GridWidth: + gridMaterial.SetFloat(GridWidth, width); + break; + case PropertyName.GridHeight: + gridMaterial.SetFloat(GridHeight, height); break; case PropertyName.Values: _buffer.SetData(_values); @@ -81,7 +86,7 @@ public class GridController : MonoBehaviour { /// the Y coordinate /// the integer value public void SetValue(int x, int y, int value) { - _values[size * y + x] = value; + _values[width * y + x] = value; } /// @@ -91,7 +96,7 @@ public class GridController : MonoBehaviour { /// the Y coordinate /// a integer value public int GetValue(int x, int y) { - return _values[size * y + x]; + return _values[width * y + x]; } /// @@ -101,6 +106,6 @@ public class GridController : MonoBehaviour { /// the Y coordinate /// the integer array index public int GetIndex(int x, int y) { - return size * y + x; + return width * y + x; } } \ No newline at end of file diff --git a/Paths/Assets/Scripts/Manager.cs b/Paths/Assets/Scripts/Manager.cs index 8e40ab4..0dd99e5 100644 --- a/Paths/Assets/Scripts/Manager.cs +++ b/Paths/Assets/Scripts/Manager.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using Algorithms; +using LevelGeneration; using TMPro; using UnityEngine; @@ -15,11 +16,14 @@ public class Manager : MonoBehaviour { private float _lastStart; private float _runtime; + public Camera mainCamera; + public GameObject gridObject; public GridController gridController; public TextMeshPro debugText; public float speed; + public float clampIncrement; - public int CurrentIndex { + private int CurrentIndex { get => (int) _runtime; set => _runtime = value; } @@ -27,15 +31,19 @@ public class Manager : MonoBehaviour { public void Start() { _states = new List(); GeneratePath(); + Resize(); } // public void OnDrawGizmos() { - // float size = (float) (10.0 / gridController.size); - // Gizmos.DrawWireCube(transform.position, new Vector3(size, size, size)); + // float size = (float) (10.0 / gridController.size); + // Gizmos.DrawWireCube(transform.position, new Vector3(size, size, size)); // } public void Update() { - _runtime += Time.deltaTime * speed; + var increment = Time.deltaTime * speed; + if (clampIncrement > 0) + increment = Mathf.Clamp(increment, 0, _states.Count * Time.deltaTime / clampIncrement); + _runtime += increment; if (CurrentIndex < _states.Count) this.LoadNextState(); @@ -52,17 +60,14 @@ public class Manager : MonoBehaviour { } private void GeneratePath() { - var nodeGrid = new NodeGrid(gridController.size, gridController.size); + var nodeGrid = new NodeGrid(gridController.width, gridController.height); _algorithm = new AStar(nodeGrid); Vector2Int start = nodeGrid.RandomPosition(); // Vector2Int start = new Vector2Int(30, 30); Vector2Int end = nodeGrid.RandomPosition(); - - int wallCount = (int) (gridController.size * gridController.size * 0.25); - for (int unused = 0; unused < wallCount; unused++) - nodeGrid.AddRandomWall(); + nodeGrid.ApplyGenerator(new RandomPlacement(0.25f, true)); nodeGrid.GetNode(start).Walkable = true; nodeGrid.GetNode(end).Walkable = true; @@ -70,8 +75,6 @@ public class Manager : MonoBehaviour { _path = _algorithm.FindPath(start, end); _states = _algorithm.GetStates(); - - } private void LoadNextState() { @@ -80,7 +83,24 @@ public class Manager : MonoBehaviour { float change = state.Time - _lastStart; string pathCount = _path != null ? $"{_path.Count}" : "N/A"; - debugText.text = - $"{change * 1000.0:F1}ms\n{this.CurrentIndex:000} / {this._states.Count:000}\nPath: {pathCount} tiles"; + debugText.text = $"{change * 1000.0:F1}ms\n" + + $"{this.CurrentIndex:000} / {this._states.Count:000}\n" + + $"Path: {pathCount} tiles"; + } + + /// + /// Scales the GridController GameObject to fit within the Camera + /// + public void Resize() { + float ratioImage = (float) gridController.width / gridController.height; + float ratioScreen = mainCamera.aspect; + + var orthographicSize = mainCamera.orthographicSize; + var image = new Vector2(gridController.width, gridController.height); + var screen = new Vector2(2 * orthographicSize * mainCamera.aspect, orthographicSize * 2); + + gridObject.transform.localScale = ratioScreen > ratioImage + ? new Vector3(image.x * screen.y / image.y, screen.y, 0.001f) + : new Vector3(screen.x, image.y * screen.x / image.x, 0.001f); } } \ No newline at end of file diff --git a/Paths/Assets/Shaders/GridShader.shader b/Paths/Assets/Shaders/GridShader.shader index 4149d6f..9f97a04 100644 --- a/Paths/Assets/Shaders/GridShader.shader +++ b/Paths/Assets/Shaders/GridShader.shader @@ -49,7 +49,8 @@ Shader "PDT Shaders/TestGrid" float4(166 / 255.0, 2 / 255.0, 51 / 255.0, 1.0) // Path }; - float _GridSize; + float _GridWidth; + float _GridHeight; float _LineSize; // DX11 needed to run shader at high grid sizes @@ -75,18 +76,18 @@ Shader "PDT Shaders/TestGrid" float2 uv = IN.uv_MainTex; fixed4 c = float4(0.0, 0.0, 0.0, 0.0); - float gsize = floor(_GridSize); - gsize += _LineSize; + float gsize_width = floor(_GridWidth) + _LineSize; + float gsize_height = floor(_GridHeight) + _LineSize; float2 id = float2( - floor(uv.x / (1.0 / gsize)), floor(uv.y / (1.0 / gsize)) + floor(uv.x / (1.0 / gsize_width)), floor(uv.y / (1.0 / gsize_height)) ); float4 color = _InactiveColor; float brightness = _InactiveColor.w; // Line Color Check - if (_LineSize > 0.0 && (frac(uv.x * gsize) <= _LineSize || frac(uv.y * gsize) <= _LineSize)) + if (_LineSize > 0.0 && (frac(uv.x * gsize_width) <= _LineSize || frac(uv.y * gsize_height) <= _LineSize)) { color = _LineColor; brightness = color.w; @@ -94,7 +95,7 @@ Shader "PDT Shaders/TestGrid" } else { - float pos = id.y * _GridSize + id.x; + float pos = id.y * _GridWidth + id.x; if (pos < _valueLength) { float index = _values[pos];