From e8dc2f2a9fdc70cf86cf0bb41395589999448114 Mon Sep 17 00:00:00 2001 From: Xevion Date: Wed, 8 Jul 2020 14:27:15 -0500 Subject: [PATCH] clean select cell shader prop, change checks for editor, enum shader update funcs, code cleanup --- Climb/Assets/Editor/GridControllerEditor.cs | 31 ++++++--- Climb/Assets/GridShader.shader | 16 ++--- Climb/Assets/Scripts/GridController.cs | 75 +++++++++++++++------ 3 files changed, 79 insertions(+), 43 deletions(-) diff --git a/Climb/Assets/Editor/GridControllerEditor.cs b/Climb/Assets/Editor/GridControllerEditor.cs index c276a73..c9eace9 100644 --- a/Climb/Assets/Editor/GridControllerEditor.cs +++ b/Climb/Assets/Editor/GridControllerEditor.cs @@ -1,15 +1,26 @@ using UnityEditor; -using UnityEngine; +namespace Editor { + [CustomEditor(typeof(GridController))] + public class GridControllerEditor : UnityEditor.Editor { + public override void OnInspectorGUI() { + var controller = (GridController) target; -[CustomEditor(typeof(GridController))] -public class GridControllerEditor : UnityEditor.Editor { - public override void OnInspectorGUI() { - var controller = (GridController) target; - - controller.size = EditorGUILayout.IntSlider("Size", controller.size, 1, 2048); - controller.perlinScale = EditorGUILayout.Slider("Perlin Scale", controller.perlinScale, 0.001f, 0.5f); - controller.offsetChange.x = EditorGUILayout.IntSlider("Horizontal Speed", (int) controller.offsetChange.x, 0, 250); - controller.offsetChange.y = EditorGUILayout.IntSlider("Horizontal Speed", (int) controller.offsetChange.y, 0, 250); + EditorGUI.BeginChangeCheck(); + controller.size = EditorGUILayout.IntSlider("Size", controller.size, 1, 2048); + if (EditorGUI.EndChangeCheck()) { + controller.UpdateShader(PropertyName.GridSize); + controller.RegenerateValues(); + controller.UpdateShader(PropertyName.Values); + controller.UpdateShader(PropertyName.ValueLength); + } + + EditorGUI.BeginChangeCheck(); + controller.perlinScale = EditorGUILayout.Slider("Perlin Scale", controller.perlinScale, 0.001f, 0.5f); + controller.offsetChange.x = EditorGUILayout.IntSlider("Horizontal Speed", (int) controller.offsetChange.x, 0, 250); + controller.offsetChange.y = EditorGUILayout.IntSlider("Horizontal Speed", (int) controller.offsetChange.y, 0, 250); + if(EditorGUI.EndChangeCheck()) + controller.UpdateShader(PropertyName.Values); + } } } \ No newline at end of file diff --git a/Climb/Assets/GridShader.shader b/Climb/Assets/GridShader.shader index 8369cc5..aa9f11d 100644 --- a/Climb/Assets/GridShader.shader +++ b/Climb/Assets/GridShader.shader @@ -3,14 +3,11 @@ Shader "PDT Shaders/TestGrid" { Properties { _LineColor ("Line Color", Color) = (1,1,1,1) - _InactiveColor ("Cell Color", Color) = (0,0,0,0) - _ActiveColor ("Selected Color", Color) = (1,0,0,1) + _InactiveColor ("Inactive Color", Color) = (0,0,0,0) + _ActiveColor ("Active Color", Color) = (1,0,0,1) [PerRendererData] _MainTex ("Albedo (RGB)", 2D) = "white" {} [IntRange] _GridSize("Grid Size", Range(1,100)) = 10 _LineSize("Line Size", Range(0,1)) = 0.15 - [IntRange] _SelectCell("Select Cell Toggle ( 0 = False , 1 = True )", Range(0,1)) = 0.0 - [IntRange] _SelectedCellX("Selected Cell X", Range(0,100)) = 0.0 - [IntRange] _SelectedCellY("Selected Cell Y", Range(0,100)) = 0.0 } SubShader { Tags { "Queue"="AlphaTest" "RenderType"="TransparentCutout" } @@ -20,7 +17,6 @@ Shader "PDT Shaders/TestGrid" { CGPROGRAM // Physically based Standard lighting model, and enable shadows on all light types #pragma surface surf Standard fullforwardshadows - // Use shader model 3.0 target, to get nicer looking lighting #pragma target 5.0 sampler2D _MainTex; @@ -31,6 +27,7 @@ Shader "PDT Shaders/TestGrid" { half _Glossiness = 0.0; half _Metallic = 0.0; + float4 _LineColor; float4 _InactiveColor; float4 _ActiveColor; @@ -38,10 +35,7 @@ Shader "PDT Shaders/TestGrid" { float _GridSize; float _LineSize; - float _SelectCell; - float _SelectedCellX; - float _SelectedCellY; - + // DX11 needed to run shader at high grid sizes #ifdef SHADER_API_D3D11 StructuredBuffer _values; #else @@ -61,8 +55,6 @@ Shader "PDT Shaders/TestGrid" { // Albedo comes from a texture tinted by color float2 uv = IN.uv_MainTex; - _SelectedCellX = floor(_SelectedCellX); - _SelectedCellY = floor(_SelectedCellY); fixed4 c = float4(0.0,0.0,0.0,0.0); float gsize = floor(_GridSize); diff --git a/Climb/Assets/Scripts/GridController.cs b/Climb/Assets/Scripts/GridController.cs index 15d1bb0..460008b 100644 --- a/Climb/Assets/Scripts/GridController.cs +++ b/Climb/Assets/Scripts/GridController.cs @@ -1,50 +1,83 @@ -using UnityEngine; +using System; +using UnityEngine; + +public enum PropertyName { + GridSize, + ValueLength, + Values +} /// /// A simple Grid Rendering Controller using MeshRenderer. /// public class GridController : MonoBehaviour { + public Material gridMaterial; public int size = 32; public float perlinScale = 16; public Vector2 offsetChange = new Vector2(1, 0); - private Vector2 offset; - - public Material gridMaterial; + + private Vector2 _offset; private float[] _values; - private ComputeBuffer buffer; + private ComputeBuffer _buffer; + + // 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 void regenerateValues() { + public void RegenerateValues() { _values = new float[size * size]; for (int x = 0; x < size; x++) { for (int y = 0; y < size; y++) { - SetValue(x, y, Mathf.PerlinNoise((x + offset.x) * perlinScale, (y + offset.y) * perlinScale)); + SetValue(x, y, Mathf.PerlinNoise((x + _offset.x) * perlinScale, (y + _offset.y) * perlinScale)); } } } private void Start() { - offset = new Vector2(0, 0); - buffer = new ComputeBuffer((int) Mathf.Pow(2048, 2), 4); - regenerateValues(); - UpdateShader(); + _offset = new Vector2(0, 0); + _buffer = new ComputeBuffer((int) Mathf.Pow(2048, 2), 4); + RegenerateValues(); + + // Update all Shader properties + foreach(PropertyName property in Enum.GetValues(typeof(PropertyName))) + UpdateShader(property); } - private void UpdateShader() { - gridMaterial.SetFloat("_GridSize", size); - gridMaterial.SetFloat("_valueLength", _values.Length); - buffer.SetData(_values); - gridMaterial.SetBuffer("_values", buffer); + /// + /// Updates Shader material properties. + /// + /// PropertyName item representing Shader property to be updated + /// + public void UpdateShader(PropertyName property) { + switch (property) { + case PropertyName.GridSize: + gridMaterial.SetFloat(GridSize, size); + break; + case PropertyName.Values: + _buffer.SetData(_values); + gridMaterial.SetBuffer(Values, _buffer); + break; + case PropertyName.ValueLength: + gridMaterial.SetFloat(ValueLength, _values.Length); + break; + default: + throw new ArgumentOutOfRangeException(nameof(property), property, null); + } } private void Update() { - regenerateValues(); - UpdateShader(); - offset += offsetChange * Time.deltaTime; + // Regenerate new position then send values to Shader + RegenerateValues(); + UpdateShader(PropertyName.Values); + + // Move offset + _offset += offsetChange * Time.deltaTime; } private void OnApplicationQuit() { - buffer.Release(); + // Release ComputeBuffer memory + _buffer.Release(); } public void SetValue(int x, int y, float value) {