-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShadowMapping.cs
42 lines (39 loc) · 1.37 KB
/
ShadowMapping.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
public class ShadowMapping : MonoBehaviour
{
private Camera CamLight;
public Shader Getz;
public RenderTexture LightDepth;
void Start()
{
LightDepth = new RenderTexture(2048, 2048, 0, RenderTextureFormat.ARGB64);
CamLight = new GameObject().AddComponent<Camera>();
CamLight.name = "DirectionalLight";
CamLight.clearFlags = CameraClearFlags.SolidColor;
CamLight.backgroundColor = Color.black;
CamLight.orthographic = true;
CamLight.orthographicSize = 40;
CamLight.transform.position = transform.position;
CamLight.transform.rotation = transform.rotation;
CamLight.transform.eulerAngles += new Vector3(0, 45, 0);
CamLight.nearClipPlane = 0;
CamLight.farClipPlane = 250;
CamLight.targetTexture = LightDepth;
CamLight.SetReplacementShader(Getz, "RenderType");
}
private void OnPreRender()
{
Proj();
}
void Proj()
{
Matrix4x4 viewM = CamLight.worldToCameraMatrix;
Matrix4x4 projM = GL.GetGPUProjectionMatrix(CamLight.projectionMatrix, false);
Matrix4x4 vp = projM * viewM;
Shader.SetGlobalMatrix("_LIGHT_MATRIX_MVP", vp);
Shader.SetGlobalTexture("_LightDepth", LightDepth);
}
}