-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnityCameraUtility.cs
More file actions
191 lines (170 loc) · 8.11 KB
/
Copy pathUnityCameraUtility.cs
File metadata and controls
191 lines (170 loc) · 8.11 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class UnityCameraUtility
{
/// <summary>
/// ビュー行列の作成
/// </summary>
///
/// <url> https://forum.unity.com/threads/reproducing-cameras-worldtocameramatrix.365645/ </url>
///
/// <param name="position"> Cameraの座標 </param>
/// <param name="rotation"> Cameraの回転 </param>
public static Matrix4x4 WorldToCameraMatrix(Vector3 position, Quaternion rotation)
{
return (Matrix4x4.TRS(position, rotation, new Vector3(1f, 1f, -1f))).inverse;
}
/// <summary>
/// プロジェクション行列の作成
/// </summary>
///
/// <param name="fov"> Field Of View </param>
/// <param name="width"> 画面の横幅 </param>
/// <param name="height"> 画面の縦幅 </param>
/// <param name="nearClipPlane"> ニアクリッププレーン </param>
/// <param name="farClipPlane"> ファークリッププレーン </param>
public static Matrix4x4 ProjectionMatrix(float fov, float width, float height, float nearClipPlane, float farClipPlane)
{
return Matrix4x4.Perspective(fov, UnityCameraUtility.GetAspect(width, height), nearClipPlane, farClipPlane);
}
/// <summary>
/// ビューポート行列の作成
/// </summary>
///
/// <url> https://edom18.hateblo.jp/entry/2019/01/06/121013 </url>
///
/// <param name="width"> 画面の横幅 </param>
/// <param name="height"> 画面の縦幅 </param>
/// <param name="nearClipPlane"> ニアクリッププレーン </param>
/// <param name="farClipPlane"> ファークリッププレーン </param>
public static Matrix4x4 ViewportMatrix(float width, float height, float nearClipPlane, float farClipPlane)
{
Matrix4x4 mat = Matrix4x4.identity;
mat.m00 = width * 0.5f;
mat.m03 = width * 0.5f;
mat.m11 = height * 0.5f;
mat.m13 = height * 0.5f;
mat.m22 = (farClipPlane - nearClipPlane) * 0.5f;
mat.m23 = (farClipPlane + nearClipPlane) * 0.5f;
return mat;
}
/// <summary>
/// Screen座標からWorld座標への変換
/// </summary>
///
/// <url> https://edom18.hateblo.jp/entry/2019/01/06/121013 </url>
/// <url> https://forum.unity.com/threads/reproducing-cameras-worldtocameramatrix.365645/ </url>
///
/// <param name="screenPosition"> Screen座標 </param>
/// <param name="nearClipPlane"> ニアクリッププレーン </param>
/// <param name="farClipPlane"> ファークリッププレーン </param>
/// <param name="position"> Cameraの座標 </param>
/// <param name="rotation"> Cameraの回転 </param>
/// <param name="fov"> Field Of View </param>
/// <param name="width"> 画面の横幅 </param>
/// <param name="height"> 画面の縦幅 </param>
/// <param name="distance"> カメラからの距離 </param>
///
/// <returns> 変換されたWorld座標 </returns>
public static Vector3 ScreenToWorldPoint(Vector2 screenPosition, float nearClipPlane, float farClipPlane, Vector3 position, Quaternion rotation, float fov, float width, float height, float distance)
{
var customNearClipPlane = nearClipPlane + distance;
Matrix4x4 viewMatrixInverse = WorldToCameraMatrix(position, rotation).inverse;
Matrix4x4 projectionMatrixInverse = ProjectionMatrix(fov, width, height, customNearClipPlane, farClipPlane).inverse;
Matrix4x4 viewportMatrixInverse = ViewportMatrix(width, height, customNearClipPlane, farClipPlane).inverse;
Matrix4x4 matrix = viewMatrixInverse * projectionMatrixInverse * viewportMatrixInverse;
Vector3 worldPosition = new Vector3(screenPosition.x, screenPosition.y, customNearClipPlane);
float x = worldPosition.x * matrix.m00 + worldPosition.y * matrix.m01 + worldPosition.z * matrix.m02 + matrix.m03;
float y = worldPosition.x * matrix.m10 + worldPosition.y * matrix.m11 + worldPosition.z * matrix.m12 + matrix.m13;
float z = worldPosition.x * matrix.m20 + worldPosition.y * matrix.m21 + worldPosition.z * matrix.m22 + matrix.m23;
float w = worldPosition.x * matrix.m30 + worldPosition.y * matrix.m31 + worldPosition.z * matrix.m32 + matrix.m33;
if (w == 0f)
{
return Vector3.zero;
}
x /= w;
y /= w;
z /= w;
return new Vector3(x, y, z);
}
/// <summary>
/// World座標からScreen座標への変換
/// </summary>
///
/// <url> https://stackoverflow.com/questions/8491247/c-opengl-convert-world-coords-to-screen2d-coords </url>
///
/// <param name="worldPosition"> World座標 </param>
/// <param name="nearClipPlane"> ニアクリッププレーン </param>
/// <param name="farClipPlane"> ファークリッププレーン </param>
/// <param name="position"> Cameraの座標 </param>
/// <param name="rotation"> Cameraの回転 </param>
/// <param name="fov"> Field Of View </param>
/// <param name="width"> 画面の横幅 </param>
/// <param name="height"> 画面の縦幅 </param>
///
/// <returns> 変換されたWorld座標 </returns>
public static Vector2 WorldToScreenPoint(Vector3 worldPosition, float nearClipPlane, float farClipPlane, Vector3 position, Quaternion rotation, float fov, float width, float height)
{
Matrix4x4 viewMatrix = WorldToCameraMatrix(position, rotation);
Matrix4x4 projectionMatrix = ProjectionMatrix(fov, width, height, camera.nearClipPlane, camera.farClipPlane);
Matrix4x4 matrix = projectionMatrix * viewMatrix;
Vector4 clipSpacePos = matrix * new Vector4(worldPosition.x, worldPosition.y, worldPosition.z, 1f);
Vector3 ndcSpacePos = clipSpacePos;
if (clipSpacePos.w == 0f)
{
return Vector2.zero;
}
ndcSpacePos.x /= clipSpacePos.w;
ndcSpacePos.y /= clipSpacePos.w;
ndcSpacePos.z /= clipSpacePos.w;
Vector2 windowSpacePos = ndcSpacePos;
windowSpacePos.x = ((ndcSpacePos.x + 1.0f) * 0.5f) * width;
windowSpacePos.y = ((ndcSpacePos.y + 1.0f) * 0.5f) * height;
return windowSpacePos;
}
/// <summary>
/// アスペクト比の取得
/// </summary>
///
/// <param name="width"> 幅 </param>
/// <param name="height"> 高さ </param>
public static float GetAspect(float width, float height)
{
return width / height;
}
/// <summary>
/// 垂直視野とアスペクト比から、水平視野を計算する
/// </summary>
///
/// <param name="fov"> Field Of View </param>
/// <param name="aspect"> アスペクト比 </param>
public static float CalculateHorizontalFoV(float fov, float aspect)
{
return Mathf.Atan(Mathf.Tan(fov / 2f * Mathf.Deg2Rad) * aspect) * 2f * Mathf.Rad2Deg;
}
/// <summary>
/// 水平視野とアスペクト比から、垂直視野を計算する
/// </summary>
///
/// <param name="fov"> Field Of View </param>
/// <param name="aspect"> アスペクト比 </param>
public static float CalculateVerticalFoV(float fov, float aspect)
{
return Mathf.Atan(Mathf.Tan(fov / 2f * Mathf.Deg2Rad) / aspect) * 2f * Mathf.Rad2Deg;
}
/// <summary>
/// 基準の解像度とField Of Viewを元にした、現在の解像度でのField Of Viewの計算
/// </summary>
///
/// <param name="baseFov"> 基準のField Of View </param>
/// <param name="baseFov"> 基準の解像度 </param>
public static float CalculateFov(float baseFov, Vector2 baseResolution)
{
// 基準の垂直視野とアスペクト比から、基準にする水平視野を計算する
float baseHorizontalFoV = UnityCameraUtility.CalculateHorizontalFoV(baseFov, UnityCameraUtility.GetAspect(baseResolution.x, baseResolution.y));
float currentAspect = UnityCameraUtility.GetAspect(Screen.width, Screen.height);
// 基準にする水平視野と現在のアスペクト比から、反映すべき垂直視野を計算する
return UnityCameraUtility.CalculateVerticalFoV(baseHorizontalFoV, currentAspect);
}
}