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 UnityEditor;
using UnityEngine;
namespace Editor {
[CustomEditor(typeof(GridController))] [CustomEditor(typeof(GridController))]
public class GridControllerEditor : UnityEditor.Editor { public class GridControllerEditor : UnityEditor.Editor {
public override void OnInspectorGUI() { public override void OnInspectorGUI() {
var controller = (GridController) target; var controller = (GridController) target;
EditorGUI.BeginChangeCheck();
controller.size = EditorGUILayout.IntSlider("Size", controller.size, 1, 2048); 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.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.x = EditorGUILayout.IntSlider("Horizontal Speed", (int) controller.offsetChange.x, 0, 250);
controller.offsetChange.y = EditorGUILayout.IntSlider("Horizontal Speed", (int) controller.offsetChange.y, 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" { Shader "PDT Shaders/TestGrid" {
Properties { Properties {
_LineColor ("Line Color", Color) = (1,1,1,1) _LineColor ("Line Color", Color) = (1,1,1,1)
_InactiveColor ("Cell Color", Color) = (0,0,0,0) _InactiveColor ("Inactive Color", Color) = (0,0,0,0)
_ActiveColor ("Selected Color", Color) = (1,0,0,1) _ActiveColor ("Active Color", Color) = (1,0,0,1)
[PerRendererData] _MainTex ("Albedo (RGB)", 2D) = "white" {} [PerRendererData] _MainTex ("Albedo (RGB)", 2D) = "white" {}
[IntRange] _GridSize("Grid Size", Range(1,100)) = 10 [IntRange] _GridSize("Grid Size", Range(1,100)) = 10
_LineSize("Line Size", Range(0,1)) = 0.15 _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 { SubShader {
Tags { "Queue"="AlphaTest" "RenderType"="TransparentCutout" } Tags { "Queue"="AlphaTest" "RenderType"="TransparentCutout" }
@@ -20,7 +17,6 @@ Shader "PDT Shaders/TestGrid" {
CGPROGRAM CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types // Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows #pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 5.0 #pragma target 5.0
sampler2D _MainTex; sampler2D _MainTex;
@@ -31,6 +27,7 @@ Shader "PDT Shaders/TestGrid" {
half _Glossiness = 0.0; half _Glossiness = 0.0;
half _Metallic = 0.0; half _Metallic = 0.0;
float4 _LineColor; float4 _LineColor;
float4 _InactiveColor; float4 _InactiveColor;
float4 _ActiveColor; float4 _ActiveColor;
@@ -38,10 +35,7 @@ Shader "PDT Shaders/TestGrid" {
float _GridSize; float _GridSize;
float _LineSize; float _LineSize;
float _SelectCell; // DX11 needed to run shader at high grid sizes
float _SelectedCellX;
float _SelectedCellY;
#ifdef SHADER_API_D3D11 #ifdef SHADER_API_D3D11
StructuredBuffer<float> _values; StructuredBuffer<float> _values;
#else #else
@@ -61,8 +55,6 @@ Shader "PDT Shaders/TestGrid" {
// Albedo comes from a texture tinted by color // Albedo comes from a texture tinted by color
float2 uv = IN.uv_MainTex; float2 uv = IN.uv_MainTex;
_SelectedCellX = floor(_SelectedCellX);
_SelectedCellY = floor(_SelectedCellY);
fixed4 c = float4(0.0,0.0,0.0,0.0); fixed4 c = float4(0.0,0.0,0.0,0.0);
float gsize = floor(_GridSize); float gsize = floor(_GridSize);

View File

@@ -1,50 +1,83 @@
using UnityEngine; using System;
using UnityEngine;
public enum PropertyName {
GridSize,
ValueLength,
Values
}
/// <summary> /// <summary>
/// A simple Grid Rendering Controller using MeshRenderer. /// A simple Grid Rendering Controller using MeshRenderer.
/// </summary> /// </summary>
public class GridController : MonoBehaviour { public class GridController : MonoBehaviour {
public Material gridMaterial;
public int size = 32; public int size = 32;
public float perlinScale = 16; public float perlinScale = 16;
public Vector2 offsetChange = new Vector2(1, 0); public Vector2 offsetChange = new Vector2(1, 0);
private Vector2 offset;
public Material gridMaterial; private Vector2 _offset;
private float[] _values; 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]; _values = new float[size * size];
for (int x = 0; x < size; x++) { for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) { 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() { private void Start() {
offset = new Vector2(0, 0); _offset = new Vector2(0, 0);
buffer = new ComputeBuffer((int) Mathf.Pow(2048, 2), 4); _buffer = new ComputeBuffer((int) Mathf.Pow(2048, 2), 4);
regenerateValues(); RegenerateValues();
UpdateShader();
// Update all Shader properties
foreach(PropertyName property in Enum.GetValues(typeof(PropertyName)))
UpdateShader(property);
} }
private void UpdateShader() { /// <summary>
gridMaterial.SetFloat("_GridSize", size); /// Updates Shader material properties.
gridMaterial.SetFloat("_valueLength", _values.Length); /// </summary>
buffer.SetData(_values); /// <param name="property">PropertyName item representing Shader property to be updated</param>
gridMaterial.SetBuffer("_values", buffer); /// <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() { private void Update() {
regenerateValues(); // Regenerate new position then send values to Shader
UpdateShader(); RegenerateValues();
offset += offsetChange * Time.deltaTime; UpdateShader(PropertyName.Values);
// Move offset
_offset += offsetChange * Time.deltaTime;
} }
private void OnApplicationQuit() { private void OnApplicationQuit() {
buffer.Release(); // Release ComputeBuffer memory
_buffer.Release();
} }
public void SetValue(int x, int y, float value) { public void SetValue(int x, int y, float value) {