-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProjectorInput.cs
67 lines (58 loc) · 2.65 KB
/
ProjectorInput.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
using UnityEngine;
[ExecuteInEditMode]
public class ProjectorInput : MonoBehaviour
{
[SerializeField, Range(0.0001f, 179)]
private float _fieldOfView = 60;
[SerializeField, Range(0.2f, 5.0f)]
private float _aspect = 1.0f;
[SerializeField, Range(0.0001f, 1000.0f)]
private float _nearClipPlane = 0.01f;
[SerializeField, Range(0.0001f, 1000.0f)]
private float _farClipPlane = 100.0f;
[SerializeField]
private bool _orthographic = false;
[SerializeField]
private float _orthographicSize = 1.0f;
[SerializeField]
public bool GPUInstancing = true;
[SerializeField] public Material material;
[System.NonSerialized] public Matrix4x4 viewMatrix;
[System.NonSerialized] public Matrix4x4 projectionMatrix;
[System.NonSerialized] public Vector4 projectorPos;
public void Update()
{
viewMatrix = Matrix4x4.Scale(new Vector3(1, 1, -1)) * transform.worldToLocalMatrix * transform.parent.worldToLocalMatrix.inverse;
if (_orthographic)
{
var orthographicWidth = _orthographicSize * _aspect;
projectionMatrix = Matrix4x4.Ortho(-orthographicWidth, orthographicWidth, -_orthographicSize, _orthographicSize, _nearClipPlane, _farClipPlane);
}
else
{
projectionMatrix = Matrix4x4.Perspective(_fieldOfView, _aspect, _nearClipPlane, _farClipPlane);
}
projectionMatrix = GL.GetGPUProjectionMatrix(projectionMatrix, true);
projectorPos = _orthographic ? transform.parent.InverseTransformVector(transform.forward) : transform.localPosition;
projectorPos.w = _orthographic ? 0 : 1;
material.SetMatrix("_ProjectorMatrixVP", projectionMatrix * viewMatrix);
material.SetVector("_ProjectorPos", projectorPos);
}
private void OnDrawGizmosSelected()
{
var gizmosMatrix = Gizmos.matrix;
Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
if (_orthographic)
{
var orthographicWidth = _orthographicSize * _aspect;
var length = _farClipPlane - _nearClipPlane;
var start = _nearClipPlane + length / 2;
Gizmos.DrawWireCube(Vector3.forward * start * transform.lossyScale.z, new Vector3(orthographicWidth * 2 * transform.lossyScale.x, _orthographicSize * 2 * transform.lossyScale.y, length * transform.lossyScale.z));
}
else
{
Gizmos.DrawFrustum(Vector3.zero, _fieldOfView, _farClipPlane * transform.lossyScale.z, _nearClipPlane * transform.lossyScale.z, _aspect * transform.lossyScale.x / transform.lossyScale.y);
}
Gizmos.matrix = gizmosMatrix;
}
}