Skip to content

Instantly share code, notes, and snippets.

@DongguemYoo
Created May 25, 2020 04:30
Show Gist options
  • Save DongguemYoo/0e848cafcaa3e4b549fd21d8fd7bd686 to your computer and use it in GitHub Desktop.
Save DongguemYoo/0e848cafcaa3e4b549fd21d8fd7bd686 to your computer and use it in GitHub Desktop.
Unity gridOverLay
using UnityEngine;
using System.Collections;
public class gridOverlay : MonoBehaviour
{
public GameObject plane;
public bool showMain = true;
public bool showSub = false;
public int gridSizeX;
public int gridSizeY;
public int gridSizeZ;
public float smallStep;
public float largeStep;
public float startX;
public float startY;
public float startZ;
private float offsetY = 0;
private float scrollRate = 0.1f;
private float lastScroll = 0f;
public Material lineMaterial;
private Color mainColor = new Color(0f, 1f, 0f, 1f);
private Color subColor = new Color(0f, 0.5f, 0f, 1f);
public delegate void SetStap(float cam_distance);
public static SetStap del_StepSize;
void Start()
{
//이거를 카메라에 달면 됨 아직 디테일은 수정중
del_StepSize += func_SetSmallStep;
}
public void func_SetSmallStep(float cam_distance)
{
largeStep = cam_distance / 2;
smallStep = cam_distance / 10;
}
void Update()
{
if (lastScroll + scrollRate < Time.time)
{
if (Input.GetKey(KeyCode.KeypadPlus))
{
plane.transform.position = new Vector3(plane.transform.position.x, plane.transform.position.y + smallStep, plane.transform.position.z);
offsetY += smallStep;
lastScroll = Time.time;
}
if (Input.GetKey(KeyCode.KeypadMinus))
{
plane.transform.position = new Vector3(plane.transform.position.x, plane.transform.position.y - smallStep, plane.transform.position.z);
offsetY -= smallStep;
lastScroll = Time.time;
}
}
}
void CreateLineMaterial()
{
if (!lineMaterial)
{
lineMaterial = new Material("Shader \"Lines/Colored Blended\" {" +
"SubShader { Pass { " +
" Blend SrcAlpha OneMinusSrcAlpha " +
" ZWrite Off Cull Off Fog { Mode Off } " +
" BindChannels {" +
" Bind \"vertex\", vertex Bind \"color\", color }" +
"} } }");
lineMaterial.hideFlags = HideFlags.HideAndDontSave;
lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
}
}
void OnPostRender()
{
CreateLineMaterial();
// set the current material
lineMaterial.SetPass(0);
GL.Begin(GL.LINES);
if (showSub)
{
GL.Color(subColor);
//Layers
for (float j = 0; j <= gridSizeY; j += smallStep)
{
//X axis lines
for (float i = 0; i <= gridSizeZ; i += smallStep)
{
GL.Vertex3(startX, j + offsetY, startZ + i);
GL.Vertex3(gridSizeX, j + offsetY, startZ + i);
}
//Z axis lines
for (float i = 0; i <= gridSizeX; i += smallStep)
{
GL.Vertex3(startX + i, j + offsetY, startZ);
GL.Vertex3(startX + i, j + offsetY, gridSizeZ);
}
}
//Y axis lines
for (float i = 0; i <= gridSizeZ; i += smallStep)
{
for (float k = 0; k <= gridSizeX; k += smallStep)
{
GL.Vertex3(startX + k, startY + offsetY, startZ + i);
GL.Vertex3(startX + k, gridSizeY + offsetY, startZ + i);
}
}
}
if (showMain)
{
GL.Color(mainColor);
//Layers
for (float j = 0; j <= gridSizeY; j += largeStep)
{
//X axis lines
for (float i = 0; i <= gridSizeZ; i += largeStep)
{
GL.Vertex3(startX, j + offsetY, startZ + i);
GL.Vertex3(gridSizeX, j + offsetY, startZ + i);
}
//Z axis lines
for (float i = 0; i <= gridSizeX; i += largeStep)
{
GL.Vertex3(startX + i, j + offsetY, startZ);
GL.Vertex3(startX + i, j + offsetY, gridSizeZ);
}
}
//Y axis lines
for (float i = 0; i <= gridSizeZ; i += largeStep)
{
for (float k = 0; k <= gridSizeX; k += largeStep)
{
GL.Vertex3(startX + k, startY + offsetY, startZ + i);
GL.Vertex3(startX + k, gridSizeY + offsetY, startZ + i);
}
}
}
GL.End();
}
}
using UnityEngine;
using System.Collections;
public class gridOverlay : MonoBehaviour
{
public float minval;
public float maxval;
public int rectSize;
public bool isLevel1;
public bool isLevel2;
public bool isLevel3;
public bool isLevel4;
public float level1_Step;
public float level2_Step;
public float level3_Step;
public float level4_Step;
private float offsetY = 0;
private float scrollRate = 0.1f;
private float lastScroll = 0f;
public Material lineMaterial;
private Color Color_level1 = new Color(0f, 0f, 0f, -0.2f);
private Color Color_level2 = new Color(0f, 0f, 0f, 0f);
private Color Color_level3 = new Color(0f, 0f, 0f, 0f);
private Color Color_level4 = new Color(0f, 0f, 0f, 0f);
public delegate void ColorChange(float distance);
public static ColorChange del_ColorChange;
void Start()
{
//lineMaterial.hideFlags = HideFlags.HideAndDontSave;
//lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
level1_Step = 2000;
level2_Step = 1000;
level3_Step = 100f;
level4_Step = 10f;
del_ColorChange += func_ColorChange;
//휠할때마다 다시 그린다? 그것도 개 애바
//StartCoroutine(draw_Line(level1_Step, Color_level1, 0));
}
public void Update()
{
if (Input.GetKeyDown("1"))
{
isLevel1 = true;
}
if (Input.GetKeyDown("2"))
{
}
}
public void func_ColorChange(float dis)
{
//f_setColor(dis, 3, 180, ref Color_level4);
f_setColor(dis, 17000, 70000, ref Color_level1);
f_setColor(dis, 2000, 14000, ref Color_level2);
f_setColor(dis, 100, 2000, ref Color_level3);
f_setColor(dis, 0, 100, ref Color_level4);
f_setActive(dis);
}
void f_setColor(float dis,float min,float max,ref Color color)
{
float tmp_val;
tmp_val = ((1 - ((dis - min) / (max - min))) * -1f);
if (max < dis && color.a !=0)
{
if (max != 70000)
{
//level1 은 사라지지 않게
color = new Color(0f, 0f, 0f, 0f);
}
}
if (min < dis && dis < max)
{
if (max == 70000)
{
//level1 은 사라지지 않게
if (tmp_val < -0.2f)
{
color = new Color(0f, 0f, 0f, tmp_val);
}
}
else
{
if (tmp_val > -0.4f)
color = new Color(0f, 0f, 0f, tmp_val);
}
}
else if (min > dis)
{
color = new Color(0f, 0f, 0f, tmp_val);
}
}
void f_setActive(float dis)
{
if (30000 > dis && dis > 14000)
{
isLevel3 = false;
isLevel4 = false;
}
else if (14000 > dis && dis > 2000)
{
isLevel3 = true;
isLevel4 = false;
}
else if (2000 > dis && dis > 100)
{
isLevel3 = true;
isLevel4 = true;
}
}
IEnumerator draw_Line(float level, Color color, float offset)
{
//Debug.Log("Draw_Action");
lineMaterial.SetPass(0);
GL.Begin(GL.LINES);
GL.Color(color);
//startX = -rectSize;
//startZ = -rectSize;
int count = 0;
for (float i = 0; i <= rectSize; i += level)
{
if (offset == 0)
{
if (count % 5 == 0)
{
GL.Vertex3(-rectSize, offsetY, -rectSize + i);
GL.Vertex3(rectSize, offsetY, -rectSize + i);
//z축
GL.Vertex3(-rectSize + i, offsetY, -rectSize);
GL.Vertex3(-rectSize + i, offsetY, rectSize);
//- 반대편 z축
GL.Vertex3(i, offsetY, -rectSize);
GL.Vertex3(i, offsetY, rectSize);
//- 반대편 x축
GL.Vertex3(-rectSize, offsetY, i);
GL.Vertex3(rectSize, offsetY, i);
}
}
else if (count % offset != 0)
{
if (i != rectSize)
{
//Debug.Log("i" + i);
//x축
GL.Vertex3(-rectSize, offsetY, -rectSize + i);
GL.Vertex3(rectSize, offsetY, -rectSize + i);
//z축
GL.Vertex3(-rectSize + i, offsetY, -rectSize);
GL.Vertex3(-rectSize + i, offsetY, rectSize);
//- 반대편 z축
GL.Vertex3(i, offsetY, -rectSize);
GL.Vertex3(i, offsetY, rectSize);
//- 반대편 x축
GL.Vertex3(-rectSize, offsetY, i);
GL.Vertex3(rectSize, offsetY, i);
}
}
count++;
}
GL.End();
yield return new WaitForEndOfFrame();
}
public void func_DrawLevel(float level, Color color,float offset)
{
//Debug.Log("Draw_Action");
lineMaterial.SetPass(0);
GL.Begin(GL.LINES);
GL.Color(color);
//startX = -rectSize;
//startZ = -rectSize;
int count= 0;
for (float i = 0; i <= rectSize; i += level)
{
if (offset == 0)
{
//level1
if (count % 5 == 0)
{
//x축
GL.Vertex3(-rectSize, offsetY, -rectSize + i);
GL.Vertex3(rectSize, offsetY, -rectSize + i);
//z축
GL.Vertex3(-rectSize + i, offsetY, -rectSize);
GL.Vertex3(-rectSize + i, offsetY, rectSize);
//- 반대편 x축
GL.Vertex3(-rectSize, offsetY, i);
GL.Vertex3(rectSize, offsetY, i);
//- 반대편 z축
GL.Vertex3(i, offsetY, -rectSize);
GL.Vertex3(i, offsetY, rectSize);
}
}
else if (count % offset != 0)
{
//level2,3,4
if (i != rectSize)
{
//Debug.Log("i" + i);
//x축
GL.Vertex3(-rectSize, offsetY, -rectSize + i);
GL.Vertex3(rectSize, offsetY, -rectSize + i);
//z축
GL.Vertex3(-rectSize + i, offsetY, -rectSize);
GL.Vertex3(-rectSize + i, offsetY, rectSize);
//- 반대편 z축
GL.Vertex3(i, offsetY, -rectSize);
GL.Vertex3(i, offsetY, rectSize);
//- 반대편 x축
GL.Vertex3(-rectSize, offsetY, i);
GL.Vertex3(rectSize, offsetY, i);
}
}
count++;
}
GL.End();
}
private void OnPreRender()
{
//if (isLevel1)
//{
// func_DrawLevel(level1_Step, Color_level1, 0);
//}
//if (isLevel2)
//{
// func_DrawLevel(level2_Step, Color_level2, 10);
//}
//if (isLevel3)
//{
// func_DrawLevel(level3_Step, Color_level3, 10);
//}
//if (isLevel4)
//{
// func_DrawLevel(level4_Step, Color_level4, 10);
//}
}
void OnPostRender()
{
if (isLevel1)
{
func_DrawLevel(level1_Step, Color_level1, 0);
}
if (isLevel2)
{
func_DrawLevel(level2_Step, Color_level2, 10);
}
if (isLevel3)
{
func_DrawLevel(level3_Step, Color_level3, 10);
}
if (isLevel4)
{
func_DrawLevel(level4_Step, Color_level4, 10);
}
//if (isLevel1)
//{
// //func_DrawLevel(level1_Step, Color_level1, 0);
// StartCoroutine(draw_Line(level1_Step, Color_level1, 0));
//}
//if (isLevel2)
//{
// //func_DrawLevel(level2_Step, Color_level2, 10);
// StartCoroutine(draw_Line(level2_Step, Color_level2, 10));
//}
//if (isLevel3)
//{
// //func_DrawLevel(level3_Step, Color_level3, 10);
// StartCoroutine(draw_Line(level3_Step, Color_level3, 10));
//}
//if (isLevel4)
//{
// //func_DrawLevel(level4_Step, Color_level4, 10);
// StartCoroutine(draw_Line(level4_Step, Color_level4, 10));
//}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment