ningshuxia
6 天以前 9eb7f4af097fb41b81fbff725d930cd6ab052c97
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using OpenTK.Mathematics;
 
namespace Yw.WinFrmUI.Hydro
{
    /// <summary>
    /// 2d正交相机
    /// </summary>
    internal class OrthoCamera2d
    {
        /// <summary>
        /// 辅助类
        /// </summary>
        public OrthoCamera2d() { }
 
        /// <summary>
        /// 旋转
        /// </summary>
        public Vector3 Rotation
        {
            get => _rotation;
            set => _rotation = value;
        }
        private Vector3 _rotation = Vector3.Zero;
 
        /// <summary>
        /// 平移
        /// </summary>
        public Vector3 Translation
        {
            get => _translation;
            set => _translation = value;
        }
        private Vector3 _translation = Vector3.Zero;
 
        /// <summary>
        /// 投影矩阵
        /// </summary>
        public Matrix4 ProjectionMatrix
        {
            get => _projectionMatrix;
            set => _projectionMatrix = value;
        }
        private Matrix4 _projectionMatrix = Matrix4.Identity;
 
        /// <summary>
        /// 视图矩阵
        /// </summary>
        public Matrix4 ViewMatrix
        {
            get => _viewMatrix;
            set => _viewMatrix = value;
        }
        private Matrix4 _viewMatrix = Matrix4.Identity;
 
        /// <summary>
        /// 模型矩阵
        /// </summary>
        public Matrix4 ModelMatrix
        {
            get => _modelMatrix;
            set => _modelMatrix = value;
        }
        private Matrix4 _modelMatrix = Matrix4.Identity;
 
        /// <summary>
        /// 缩放
        /// </summary>
        public float Zoom
        {
            get => _zoom;
            set => _zoom = MathHelper.Clamp(value, 0.01f, 100.0f); // 限制缩放范围
        }
        private float _zoom = 1.0f; // 当前缩放系数(1表示原始大小)
 
        private BoundingBox3 _bx = null;
        private Vector3 _center = Vector3.Zero;
        private float _radius = 100f;//包围球半径
        private float _width, _height, _aspect;//视口宽度和高度与宽高比
        private float _pw, _ph;//投影宽度和高度
 
        /// <summary>
        /// 初始化
        /// </summary>
        public void Initial(List<Vector3> vers, int width, int height)
        {
            UpdateViewPort(width, height);
            if (vers == null || vers.Count < 1)
            {
                vers = new List<Vector3>()
                {
                    new Vector3(-_width/2f,-_height/2f,-100f),
                    new Vector3(width/2f,height/2f,100f)
                };
            }
            _bx = new BoundingBox3(vers);
            _center = _bx.Center;
            _radius = _bx.Size.Length / 2f * 1.2f;
            UpdateMatrices();
        }
 
        /// <summary>
        /// 更新视口
        /// </summary>
        public void UpdateViewPort(int width, int height)
        {
            _width = width < 1 ? 1f : width;
            _height = height < 1 ? 1f : height;
            _aspect = _width / _height;
        }
 
        /// <summary>
        /// 更新缩放
        /// </summary>
        public void UpdateZoom(float scale)
        {
            this.Zoom *= scale;
        }
 
        /// <summary>
        /// 更新平移
        /// </summary>
        public void UpdateTranslation(float dx, float dy)
        {
            float xratio = _pw / _width;
            float yratio = _ph / _height;
            _translation.X += dx * xratio;
            _translation.Y -= dy * yratio;
        }
 
        /// <summary>
        /// 更新旋转
        /// </summary>
        public void UpdateRotation(float dx, float dy)
        {
            _rotation.X += dy / _radius;
            _rotation.Y += dx / _radius;
            _rotation.X = NormalizeAngle(_rotation.X);
            _rotation.Y = NormalizeAngle(_rotation.Y);
        }
 
        /// <summary>
        /// 更新模型矩阵
        /// </summary>
        public void UpdateModelMatrix()
        {
            _modelMatrix = Matrix4.CreateRotationX(_rotation.X) *
                Matrix4.CreateRotationY(_rotation.Y) *
                Matrix4.CreateTranslation(_translation * this.Zoom);
        }
 
        /// <summary>
        /// 更新视图矩阵
        /// </summary>
        public void UpdateViewMatrix()
        {
            var eye = new Vector3(_center.X, _center.Y, _center.Z + 2f * _radius);
            var target = _center;
            var up = Vector3.UnitY;
            _viewMatrix = Matrix4.LookAt(eye, target, up);
        }
 
        /// <summary>
        /// 更新投影矩阵
        /// </summary>
        public void UpdateProjectionMatrix()
        {
            _pw = _ph = _radius * 2f;
            if (_aspect > 1)
            {
                _pw *= _aspect;
            }
            else
            {
                _ph /= _aspect;
            }
            _projectionMatrix = Matrix4.CreateOrthographic(_pw * this.Zoom, _ph * this.Zoom, _radius, _radius * 3f);
        }
 
        /// <summary>
        /// 更新举证
        /// </summary>
        public void UpdateMatrices()
        {
            UpdateModelMatrix();
            UpdateViewMatrix();
            UpdateProjectionMatrix();
        }
 
        //标准化旋转弧度
        private static float NormalizeAngle(float angle)
        {
            // 取余操作
            angle %= 2 * MathF.PI;
 
            // 如果结果为负数,加上 2π 使其处于 [0, 2π) 区间
            if (angle < 0)
            {
                angle += 2 * MathF.PI;
            }
 
            return angle;
        }
 
 
 
    }
}