カメラのaspect を操作する必要がありそうです。たとえば下記のようなスクリプトをカメラにアタッチしてみてはいかがでしょうか?
C#
1 using UnityEngine ;
2 # if UNITY_EDITOR
3 using UnityEditor ;
4 # endif
5
6 [ RequireComponent ( typeof ( Camera ) ) ]
7 [ ExecuteInEditMode ]
8 public class FovModifier : MonoBehaviour
9 {
10 [ SerializeField , Range ( 0.0001f , 179.0f ) ] private float verticalFov ;
11 [ SerializeField , Range ( 0.0001f , 179.0f ) ] private float horizontalFov ;
12
13 private new Camera camera ;
14 private float currentVerticalFov ;
15 private float currentHorizontalFov ;
16 public float Aspect { get ; private set ; }
17
18 public void ResetHorizontalFov ( )
19 {
20 this . camera . ResetAspect ( ) ;
21 this . GetCameraFov ( ) ;
22 this . UpdateFov ( ) ;
23 }
24
25 public void GetCameraFov ( )
26 {
27 this . verticalFov = this . camera . fieldOfView ;
28 this . horizontalFov = Mathf . Atan ( Mathf . Tan ( this . verticalFov * 0.5f * Mathf . Deg2Rad ) * this . camera . aspect ) *
29 2.0f * Mathf . Rad2Deg ;
30 }
31
32 private void UpdateFov ( )
33 {
34 if ( ( this . currentVerticalFov != this . verticalFov ) || ( this . currentHorizontalFov != this . horizontalFov ) )
35 {
36 this . Aspect = Mathf . Tan ( this . horizontalFov * 0.5f * Mathf . Deg2Rad ) /
37 Mathf . Tan ( this . verticalFov * 0.5f * Mathf . Deg2Rad ) ;
38 this . camera . fieldOfView = this . verticalFov ;
39 this . camera . aspect = this . Aspect ;
40 this . currentHorizontalFov = this . horizontalFov ;
41 this . currentVerticalFov = this . verticalFov ;
42 }
43 }
44
45 private void OnEnable ( )
46 {
47 this . camera = this . GetComponent < Camera > ( ) ;
48 this . GetCameraFov ( ) ;
49 this . UpdateFov ( ) ;
50 }
51
52 private void Update ( )
53 {
54 this . UpdateFov ( ) ;
55 }
56 }
57
58 # if UNITY_EDITOR
59 [ CustomEditor ( typeof ( FovModifier ) ) ]
60 public class FovModifierEditor : Editor
61 {
62 private FovModifier fovModifier ;
63
64 /// <inheritdoc />
65 public override void OnInspectorGUI ( )
66 {
67 this . fovModifier . GetCameraFov ( ) ;
68 base . OnInspectorGUI ( ) ;
69 if ( GUILayout . Button ( "Reset Horizontal Fov" ) )
70 {
71 this . fovModifier . ResetHorizontalFov ( ) ;
72 this . serializedObject . ApplyModifiedProperties ( ) ;
73 }
74
75 EditorGUILayout . LabelField ( "Aspect" , this . fovModifier . Aspect . ToString ( "F4" ) ) ;
76 }
77
78 private void OnEnable ( )
79 {
80 this . fovModifier = this . target as FovModifier ;
81 }
82 }
83 # endif
追記
まずaspect
は画面高さに対する画面幅、fieldOfView
および上記コードのverticalFov
は垂直画角、上記コードのhorizontalFov
は水平画角です。
画面上半分・右半分に注目すると、直角三角形が2つあります。
図中の角度と辺の比には下記の関係がありますので...
aspect
とverticalFov
、horizontalFov
の関係はこうなります。
なお、度数法と弧度法の変換は省略しました。実際には画角を三角関数に渡すときには弧度法に変換し、逆三角関数から受け取った角度は度数法に変換する必要があります。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/25 08:37 編集
2019/05/25 22:01
2019/05/28 04:41