Skip to content

Instantly share code, notes, and snippets.

@Xevion
Last active July 7, 2020 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xevion/1b1e061a22206e006c71f5822ae8398e to your computer and use it in GitHub Desktop.
Save Xevion/1b1e061a22206e006c71f5822ae8398e to your computer and use it in GitHub Desktop.
// Upgrade NOTE: upgraded instancing buffer 'Props' to new syntax.
Shader "PDT Shaders/TestGrid" {
Properties {
_LineColor ("Line Color", Color) = (1,1,1,1)
_InactiveColor ("Inactive Color", Color) = (0,1,0,1)
_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
}
SubShader {
Tags { "Queue"="AlphaTest" "RenderType"="TransparentCutout" }
LOD 200
CGPROGRAM
// Upgrade NOTE: excluded shader from DX11, OpenGL ES 2.0 because it uses unsized arrays
#pragma exclude_renderers d3d11 gles
// 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 3.0
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
half _Glossiness = 0.0;
half _Metallic = 0.0;
float4 _LineColor; // Line Color
float _LineSize; // Width of Line
// Color gradient endpoints
float4 _InactiveColor; // Color of 0.0 value
float4 _ActiveColor; // Color of 1.0 value
float2 _GridSize; // Integer grid size
float _Values[500]; // Flattened array containing all interpolation
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutputStandard o) {
// Albedo comes from a texture tinted by color
float2 uv = IN.uv_MainTex;
fixed4 c = float4(0.0,0.0,0.0,0.0);
float brightness = 1.0;
// _GridSize = floor(_GridSize);
float gsize = floor(_GridSize) + _LineSize;
// Get grid position
float2 id = float2(floor(uv.x / (1.0 / gsize)), floor(uv.y / (1.0 / gsize)));
float4 color = _InactiveColor;
brightness = _InactiveColor.w;
if (frac(uv.x * gsize) <= _LineSize || frac(uv.y * gsize) <= _LineSize)
{
brightness = _LineColor.w;
color = _LineColor;
} else {
float value = _Values[_GridSize.x * uv.y + uv.x];
// float value = 0.5;
color = lerp(_InactiveColor, _ActiveColor, value);
brightness = color.w;
}
//Clip transparent spots using alpha cutout
if (brightness == 0.0) {
clip(c.a - 1.0);
}
o.Albedo = float4(color.x * brightness, color.y * brightness, color.z * brightness, brightness);
// Metallic and smoothness come from slider variables
o.Metallic = 0.0;
o.Smoothness = 0.0;
o.Alpha = 0.0;
}
ENDCG
}
FallBack "Diffuse"
}
using UnityEngine;
/// <summary>
/// A simple Grid Rendering Controller using MeshRenderer.
/// </summary>
public class GridController : MonoBehaviour {
public int width = 32;
public int height = 32;
public Material gridMaterial;
private float[] values;
private void Start() {
values = new float[width * height];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
SetValue(x, y, Random.value);
}
}
gridMaterial.SetVector("_GridSize", new Vector4(width, height, 0, 0));
}
private void Update() {
gridMaterial.SetFloatArray("_Values", values);
}
public void SetValue(int x, int y, float value) {
values[width * y + x] = value;
}
public float GetValue(int x, int y) {
return values[width * y + x];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment