├── GridMat.mat ├── GridShader.shader ├── GridShaderUNLIT.shader ├── LICENSE ├── README.md └── SimpleGridShader.unitypackage /GridMat.mat: -------------------------------------------------------------------------------- 1 | %YAML 1.1 2 | %TAG !u! tag:unity3d.com,2011: 3 | --- !u!21 &2100000 4 | Material: 5 | serializedVersion: 6 6 | m_ObjectHideFlags: 0 7 | m_PrefabParentObject: {fileID: 0} 8 | m_PrefabInternal: {fileID: 0} 9 | m_Name: GridMat 10 | m_Shader: {fileID: 4800000, guid: 27a637801b8646a4182d0a32a8d2f4d9, type: 3} 11 | m_ShaderKeywords: 12 | m_LightmapFlags: 4 13 | m_EnableInstancingVariants: 0 14 | m_DoubleSidedGI: 0 15 | m_CustomRenderQueue: -1 16 | stringTagMap: {} 17 | disabledShaderPasses: [] 18 | m_SavedProperties: 19 | serializedVersion: 3 20 | m_TexEnvs: 21 | - _BumpMap: 22 | m_Texture: {fileID: 0} 23 | m_Scale: {x: 1, y: 1} 24 | m_Offset: {x: 0, y: 0} 25 | - _DetailAlbedoMap: 26 | m_Texture: {fileID: 0} 27 | m_Scale: {x: 1, y: 1} 28 | m_Offset: {x: 0, y: 0} 29 | - _DetailMask: 30 | m_Texture: {fileID: 0} 31 | m_Scale: {x: 1, y: 1} 32 | m_Offset: {x: 0, y: 0} 33 | - _DetailNormalMap: 34 | m_Texture: {fileID: 0} 35 | m_Scale: {x: 1, y: 1} 36 | m_Offset: {x: 0, y: 0} 37 | - _EmissionMap: 38 | m_Texture: {fileID: 0} 39 | m_Scale: {x: 1, y: 1} 40 | m_Offset: {x: 0, y: 0} 41 | - _MainTex: 42 | m_Texture: {fileID: 0} 43 | m_Scale: {x: 1, y: 1} 44 | m_Offset: {x: 0, y: 0} 45 | - _MetallicGlossMap: 46 | m_Texture: {fileID: 0} 47 | m_Scale: {x: 1, y: 1} 48 | m_Offset: {x: 0, y: 0} 49 | - _OcclusionMap: 50 | m_Texture: {fileID: 0} 51 | m_Scale: {x: 1, y: 1} 52 | m_Offset: {x: 0, y: 0} 53 | - _ParallaxMap: 54 | m_Texture: {fileID: 0} 55 | m_Scale: {x: 1, y: 1} 56 | m_Offset: {x: 0, y: 0} 57 | m_Floats: 58 | - _BumpScale: 1 59 | - _Cutoff: 0.5 60 | - _DetailNormalMapScale: 1 61 | - _DstBlend: 0 62 | - _GlossMapScale: 1 63 | - _Glossiness: 0 64 | - _GlossyReflections: 1 65 | - _GridSize: 10 66 | - _LineSize: 0.16 67 | - _Metallic: 0 68 | - _Mode: 0 69 | - _OcclusionStrength: 1 70 | - _Parallax: 0.02 71 | - _SelectCell: 1 72 | - _SelectedCellX: 2 73 | - _SelectedCellY: 3 74 | - _SmoothnessTextureChannel: 0 75 | - _SpecularHighlights: 1 76 | - _SrcBlend: 1 77 | - _UVSec: 0 78 | - _ZWrite: 1 79 | m_Colors: 80 | - _CellColor: {r: 1, g: 0, b: 0, a: 0} 81 | - _Color: {r: 0.935151, g: 0.9705882, b: 0.3282872, a: 0} 82 | - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} 83 | - _LineColor: {r: 1, g: 1, b: 1, a: 1} 84 | - _SelectedColor: {r: 0.1586206, g: 1, b: 0, a: 1} 85 | -------------------------------------------------------------------------------- /GridShader.shader: -------------------------------------------------------------------------------- 1 | Shader "PDT Shaders/TestGrid" { 2 | Properties { 3 | _LineColor ("Line Color", Color) = (1,1,1,1) 4 | _CellColor ("Cell Color", Color) = (0,0,0,0) 5 | _SelectedColor ("Selected Color", Color) = (1,0,0,1) 6 | [PerRendererData] _MainTex ("Albedo (RGB)", 2D) = "white" {} 7 | [IntRange] _GridSize("Grid Size", Range(1,100)) = 10 8 | _LineSize("Line Size", Range(0,1)) = 0.15 9 | [IntRange] _SelectCell("Select Cell Toggle ( 0 = False , 1 = True )", Range(0,1)) = 0.0 10 | [IntRange] _SelectedCellX("Selected Cell X", Range(0,100)) = 0.0 11 | [IntRange] _SelectedCellY("Selected Cell Y", Range(0,100)) = 0.0 12 | } 13 | SubShader { 14 | Tags { "Queue"="AlphaTest" "RenderType"="TransparentCutout" } 15 | LOD 200 16 | 17 | 18 | CGPROGRAM 19 | // Physically based Standard lighting model, and enable shadows on all light types 20 | #pragma surface surf Standard fullforwardshadows 21 | 22 | // Use shader model 3.0 target, to get nicer looking lighting 23 | #pragma target 3.0 24 | 25 | sampler2D _MainTex; 26 | 27 | struct Input { 28 | float2 uv_MainTex; 29 | }; 30 | 31 | half _Glossiness = 0.0; 32 | half _Metallic = 0.0; 33 | float4 _LineColor; 34 | float4 _CellColor; 35 | float4 _SelectedColor; 36 | 37 | float _GridSize; 38 | float _LineSize; 39 | 40 | float _SelectCell; 41 | float _SelectedCellX; 42 | float _SelectedCellY; 43 | 44 | // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader. 45 | // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing. 46 | // #pragma instancing_options assumeuniformscaling 47 | UNITY_INSTANCING_CBUFFER_START(Props) 48 | // put more per-instance properties here 49 | UNITY_INSTANCING_CBUFFER_END 50 | 51 | void surf (Input IN, inout SurfaceOutputStandard o) { 52 | // Albedo comes from a texture tinted by color 53 | 54 | float2 uv = IN.uv_MainTex; 55 | 56 | _SelectedCellX = floor(_SelectedCellX); 57 | _SelectedCellY = floor(_SelectedCellY); 58 | 59 | fixed4 c = float4(0.0,0.0,0.0,0.0); 60 | 61 | float brightness = 1.; 62 | 63 | float gsize = floor(_GridSize); 64 | 65 | 66 | 67 | gsize += _LineSize; 68 | 69 | float2 id; 70 | 71 | id.x = floor(uv.x/(1.0/gsize)); 72 | id.y = floor(uv.y/(1.0/gsize)); 73 | 74 | float4 color = _CellColor; 75 | brightness = _CellColor.w; 76 | 77 | //This checks that the cell is currently selected if the Select Cell slider is set to 1 ( True ) 78 | if (round(_SelectCell) == 1.0 && id.x == _SelectedCellX && id.y == _SelectedCellY) 79 | { 80 | brightness = _SelectedColor.w; 81 | color = _SelectedColor; 82 | } 83 | 84 | if (frac(uv.x*gsize) <= _LineSize || frac(uv.y*gsize) <= _LineSize) 85 | { 86 | brightness = _LineColor.w; 87 | color = _LineColor; 88 | } 89 | 90 | 91 | //Clip transparent spots using alpha cutout 92 | if (brightness == 0.0) { 93 | clip(c.a - 1.0); 94 | } 95 | 96 | 97 | o.Albedo = float4( color.x*brightness,color.y*brightness,color.z*brightness,brightness); 98 | // Metallic and smoothness come from slider variables 99 | o.Metallic = 0.0; 100 | o.Smoothness = 0.0; 101 | o.Alpha = 0.0; 102 | } 103 | ENDCG 104 | } 105 | FallBack "Diffuse" 106 | } 107 | -------------------------------------------------------------------------------- /GridShaderUNLIT.shader: -------------------------------------------------------------------------------- 1 | Shader "PDT Shaders/TestGridUNLIT" 2 | { 3 | Properties 4 | { 5 | _LineColor("Line Color", Color) = (1,1,1,1) 6 | _CellColor("Cell Color", Color) = (0,0,0,0) 7 | _SelectedColor("Selected Color", Color) = (1,0,0,1) 8 | [PerRendererData] _MainTex("Albedo (RGB)", 2D) = "white" {} 9 | [IntRange] _GridSize("Grid Size", Range(1,100)) = 10 10 | _LineSize("Line Size", Range(0,1)) = 0.15 11 | [IntRange] _SelectCell("Select Cell Toggle ( 0 = False , 1 = True )", Range(0,1)) = 0.0 12 | [IntRange] _SelectedCellX("Selected Cell X", Range(0,100)) = 0.0 13 | [IntRange] _SelectedCellY("Selected Cell Y", Range(0,100)) = 0.0 14 | } 15 | 16 | SubShader 17 | { 18 | Tags {"Queue" = "Transparent" "RenderType" = "Transparent" } 19 | LOD 100 20 | 21 | ZWrite Off 22 | Blend SrcAlpha OneMinusSrcAlpha 23 | 24 | Pass 25 | { 26 | CGPROGRAM 27 | #pragma vertex vert 28 | #pragma fragment frag 29 | 30 | #include "UnityCG.cginc" 31 | 32 | struct appdata 33 | { 34 | float4 vertex : POSITION; 35 | float2 uv : TEXCOORD0; 36 | }; 37 | 38 | struct v2f 39 | { 40 | float2 uv : TEXCOORD0; 41 | float4 vertex : SV_POSITION; 42 | }; 43 | 44 | half _Glossiness = 0.0; 45 | half _Metallic = 0.0; 46 | float4 _LineColor; 47 | float4 _CellColor; 48 | float4 _SelectedColor; 49 | sampler2D _MainTex; 50 | 51 | float _GridSize; 52 | float _LineSize; 53 | 54 | float _SelectCell; 55 | float _SelectedCellX; 56 | float _SelectedCellY; 57 | 58 | v2f vert(appdata v) 59 | { 60 | v2f o; 61 | o.vertex = UnityObjectToClipPos(v.vertex); 62 | o.uv = v.uv; 63 | return o; 64 | } 65 | 66 | fixed4 frag(v2f i) : SV_Target 67 | { 68 | float2 uv = i.uv; 69 | 70 | _SelectedCellX = floor(_SelectedCellX); 71 | _SelectedCellY = floor(_SelectedCellY); 72 | 73 | fixed4 c = float4(0.0,0.0,0.0,0.0); 74 | 75 | float brightness = 1.; 76 | 77 | float gsize = floor(_GridSize); 78 | 79 | 80 | 81 | gsize += _LineSize; 82 | 83 | float2 id; 84 | 85 | id.x = floor(uv.x / (1.0 / gsize)); 86 | id.y = floor(uv.y / (1.0 / gsize)); 87 | 88 | float4 color = _CellColor; 89 | brightness = _CellColor.w; 90 | 91 | //This checks that the cell is currently selected if the Select Cell slider is set to 1 ( True ) 92 | if (round(_SelectCell) == 1.0 && id.x == _SelectedCellX && id.y == _SelectedCellY) 93 | { 94 | brightness = _SelectedColor.w; 95 | color = _SelectedColor; 96 | } 97 | 98 | if (frac(uv.x*gsize) <= _LineSize || frac(uv.y*gsize) <= _LineSize) 99 | { 100 | brightness = _LineColor.w; 101 | color = _LineColor; 102 | } 103 | 104 | 105 | //Clip transparent spots using alpha cutout 106 | if (brightness == 0.0) { 107 | clip(c.a - 1.0); 108 | } 109 | 110 | 111 | c = fixed4(color.x*brightness,color.y*brightness,color.z*brightness,brightness); 112 | return c; 113 | } 114 | ENDCG 115 | } 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Unity-SimpleGrid-Shader 2 | Simple procedural grid shader with GUI customizable parameters 3 | 4 | # Usage 5 | 6 | Drag and drop both Shader and Material files in your project. The Material can then be edited and also dropped on a gameobject mesh inside Unity. 7 | 8 | To edit the grid material, the following GUI options are available. 9 | 10 | ![Available Material Parameters](https://i.imgur.com/rzwV2OR.png) 11 | 12 | Line Color, Cell Color and Selected Color all represent their respective grid components. Colors use Alpha Cutoff for full transparency by setting the alpha channel to 0. 13 | 14 | Grid Size is the amount of cells in the grid. You must make a simple edit to the shader itself to go above 100. 15 | 16 | Line Size is the size of lines making up the grid. 17 | 18 | Select Cell enables or disables the cell "Selected" by the parameters Selected Cell X and Y. It only colors a certain cell using a different color, but the base code to "Select" cells in the grid can be used in more complex ways. 19 | 20 | The parameters shown above create the following grid. 21 | 22 | ![Example Grid](https://i.imgur.com/Vaeo80i.png) 23 | 24 | # More information 25 | 26 | There is no aspect ratio based scaling by default, to keep cells squared keep the plane mesh square ( or implement your own aspect ratio scaling ) 27 | 28 | The selected cell can also be chosen by editing the material using the SetFloat value, to potentially make the material interactive. ( See official unity docs to learn more ) 29 | 30 | The shader, when applied to new materials, can be found under "PDTShaders/TestGrid". 31 | -------------------------------------------------------------------------------- /SimpleGridShader.unitypackage: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/r3eckon/Unity-SimpleGrid-Shader/bce94d018fa986ad12986390ad2a6f3d5fee9dfd/SimpleGridShader.unitypackage --------------------------------------------------------------------------------