-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathFogOfWar.cs
89 lines (80 loc) · 2.06 KB
/
FogOfWar.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Edit -> Project Settings -> Graphics -> Always Included Shader -> Hidden/FogOfWar
using UnityEngine;
public class FogOfWar : MonoBehaviour
{
public Texture2D Map;
public int Resolution = 1024;
public float Radius = 0.02f;
Material material;
RenderTexture input, output;
Vector4 center = new Vector4(0.5f,0.5f,0.0f,0.0f);
bool swap = true;
void Load()
{
material = new Material(Shader.Find("Hidden/FogOfWar"));
input = new RenderTexture(Resolution, Resolution, 0, RenderTextureFormat.R8);
output = new RenderTexture(Resolution, Resolution, 0, RenderTextureFormat.R8);
GetComponent<Renderer>().material = material;
material.SetTexture("_Map", Map);
}
void Blit(RenderTexture source, RenderTexture destination, Material mat)
{
RenderTexture.active = destination;
mat.SetTexture("_MainTex", source);
GL.PushMatrix();
GL.LoadOrtho();
GL.invertCulling = true;
mat.SetPass(0);
GL.Begin(GL.QUADS);
GL.MultiTexCoord2(0, 0.0f, 0.0f);
GL.Vertex3(0.0f, 0.0f, 0.0f);
GL.MultiTexCoord2(0, 1.0f, 0.0f);
GL.Vertex3(1.0f, 0.0f, 0.0f);
GL.MultiTexCoord2(0, 1.0f, 1.0f);
GL.Vertex3(1.0f, 1.0f, 1.0f);
GL.MultiTexCoord2(0, 0.0f, 1.0f);
GL.Vertex3(0.0f, 1.0f, 0.0f);
GL.End();
GL.invertCulling = false;
GL.PopMatrix();
}
void GenerateFogOfWar ()
{
material.SetVector("Center",center);
material.SetFloat("Radius",Radius);
if (swap)
{
material.SetTexture("_MainTex", input);
Blit(input,output,material);
material.SetTexture("_Buffer", output);
}
else
{
material.SetTexture("_MainTex", output);
Blit(output,input,material);
material.SetTexture("_Buffer", input);
}
swap = !swap;
}
void Movement()
{
if (Input.GetKey("a")) center.x -= Time.deltaTime * 0.05f;
if (Input.GetKey("d")) center.x += Time.deltaTime * 0.05f;
if (Input.GetKey("s")) center.y -= Time.deltaTime * 0.05f;
if (Input.GetKey("w")) center.y += Time.deltaTime * 0.05f;
}
void Start ()
{
Load();
}
void Update ()
{
GenerateFogOfWar();
Movement();
}
void OnDestroy ()
{
input.Release();
output.Release();
}
}