clean select cell shader prop, change checks for editor, enum shader update funcs, code cleanup

This commit is contained in:
Xevion
2020-07-08 14:27:15 -05:00
parent 6324986648
commit e8dc2f2a9f
3 changed files with 79 additions and 43 deletions

View File

@@ -1,15 +1,26 @@
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(GridController))]
public class GridControllerEditor : UnityEditor.Editor {
namespace Editor {
[CustomEditor(typeof(GridController))]
public class GridControllerEditor : UnityEditor.Editor {
public override void OnInspectorGUI() {
var controller = (GridController) target;
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);
}
}
}

View File

@@ -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<float> _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);

View File

@@ -1,50 +1,83 @@
using UnityEngine;
using System;
using UnityEngine;
public enum PropertyName {
GridSize,
ValueLength,
Values
}
/// <summary>
/// A simple Grid Rendering Controller using MeshRenderer.
/// </summary>
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);
/// <summary>
/// Updates Shader material properties.
/// </summary>
/// <param name="property">PropertyName item representing Shader property to be updated</param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
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) {