-
Notifications
You must be signed in to change notification settings - Fork 226
/
Planet.cs
58 lines (52 loc) · 1.69 KB
/
Planet.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
using UnityEngine;
public class Planet : MonoBehaviour
{
[SerializeField] private Shader _Shader;
[SerializeField] private Camera _Camera;
[System.NonSerialized] private Material _Material;
void Start()
{
_Material = new Material(_Shader);
}
Matrix4x4 GetFrustumCorners(Camera camera)
{
float tangent = Mathf.Tan(camera.fieldOfView * 0.5f * Mathf.Deg2Rad);
Vector3 right = Vector3.right * tangent * camera.aspect;
Vector3 up = Vector3.up * tangent;
Matrix4x4 frustumCorners = Matrix4x4.identity;
frustumCorners.SetRow(0, (-Vector3.forward - right + up));
frustumCorners.SetRow(1, (-Vector3.forward + right + up));
frustumCorners.SetRow(2, (-Vector3.forward + right - up));
frustumCorners.SetRow(3, (-Vector3.forward - right - up));
return frustumCorners;
}
void Blit(RenderTexture source, RenderTexture destination, Material material)
{
RenderTexture.active = destination;
material.SetTexture("_MainTex", source);
GL.PushMatrix();
GL.LoadOrtho();
material.SetPass(0);
GL.Begin(GL.QUADS);
GL.MultiTexCoord3(0, 0.0f, 0.0f, 3.0f);
GL.Vertex3(0.0f, 0.0f, 0.0f);
GL.MultiTexCoord3(0, 1.0f, 0.0f, 2.0f);
GL.Vertex3(1.0f, 0.0f, 0.0f);
GL.MultiTexCoord3(0, 1.0f, 1.0f, 1.0f);
GL.Vertex3(1.0f, 1.0f, 0.0f);
GL.MultiTexCoord3(0, 0.0f, 1.0f, 0.0f);
GL.Vertex3(0.0f, 1.0f, 0.0f);
GL.End();
GL.PopMatrix();
}
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
_Material.SetMatrix("_FrustumCorners", GetFrustumCorners(_Camera));
_Material.SetMatrix("_CameraToWorldMatrix", _Camera.cameraToWorldMatrix);
Blit(source, destination, _Material);
}
void OnDestroy()
{
Destroy(_Material);
}
}