■ 3D 큐브 애니메이션을 만드는 방법을 보여준다.
▶ Trackball.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 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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
using System.Collections.Generic; using System.Windows; using System.Windows.Input; using System.Windows.Controls; using System.Windows.Media.Media3D; namespace TestProject { /// <summary> /// 트랙볼 /// </summary> public class Trackball { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 뷰포트 리스트 /// </summary> private List<Viewport3D> viewportList; /// <summary> /// 이용 가능 여부 /// </summary> private bool enabled; /// <summary> /// 드래그 최초 포인트 /// </summary> private Point dragInitialPoint; /// <summary> /// 회전 쿼터니언 /// </summary> private Quaternion rotateQuaternion; /// <summary> /// 회전 여부 /// </summary> private bool isRotating; /// <summary> /// 스케일링 여부 /// </summary> private bool scaling; /// <summary> /// 회전 델타 쿼터니언 /// </summary> private Quaternion rotateDeltaQuaternion; /// <summary> /// 이동 델타 벡터 /// </summary> private Vector3D translateDeltaVector; /// <summary> /// 이동 벡터 /// </summary> private Vector3D translateVector; /// <summary> /// 스케일 /// </summary> private double scale; /// <summary> /// 스케일 델타 /// </summary> private double scaleDelta; /// <summary> /// 중심 여부 /// </summary> private bool isCentered; /// <summary> /// 중심 벡터 /// </summary> private Vector3D centerVector; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 뷰포트 리스트 - ViewportList /// <summary> /// 뷰포트 리스트 /// </summary> public List<Viewport3D> ViewportList { get { if(this.viewportList == null) { this.viewportList = new List<Viewport3D>(); } return this.viewportList; } set { this.viewportList = value; } } #endregion #region 이용 가능 여부 - Enabled /// <summary> /// 이용 가능 여부 /// </summary> public bool Enabled { get { return this.enabled && (this.viewportList != null) && (this.viewportList.Count > 0); } set { this.enabled = value; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Trackball() /// <summary> /// 생성자 /// </summary> public Trackball() { Reset(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 접착하기 - Attach(element) /// <summary> /// 접착하기 /// </summary> /// <param name="element">프레임워크 엘리먼트</param> public void Attach(FrameworkElement element) { element.MouseMove += element_MouseMove; element.MouseRightButtonDown += element_MouseRightButtonDown; element.MouseRightButtonUp += element_MouseRightButtonUp; element.MouseWheel += element_MouseWheel; } #endregion #region 탈착하기 - Detach(element) /// <summary> /// 탈착하기 /// </summary> /// <param name="element">프레임워크 엘리먼트</param> public void Detach(FrameworkElement element) { element.MouseMove -= element_MouseMove; element.MouseRightButtonDown -= element_MouseRightButtonDown; element.MouseRightButtonUp -= element_MouseRightButtonUp; element.MouseWheel -= element_MouseWheel; } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 엘리먼트 마우스 이동시 처리하기 - element_MouseMove(sender, e) /// <summary> /// 엘리먼트 마우스 이동시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void element_MouseMove(object sender, MouseEventArgs e) { if(!Enabled) { return; } e.Handled = true; UIElement element = sender as UIElement; if(element.IsMouseCaptured) { Vector delta = this.dragInitialPoint - e.MouseDevice.GetPosition(element); Vector3D verctor = new Vector3D(); delta /= 2; Quaternion quaternion = this.rotateQuaternion; if(this.isRotating == true) { Vector3D mouseVector = new Vector3D(delta.X, -delta.Y, 0); Vector3D axisVector = Vector3D.CrossProduct(mouseVector, new Vector3D(0, 0, 1)); double axisVectorLength = axisVector.Length; if(axisVectorLength < 0.00001 || this.scaling) { this.rotateDeltaQuaternion = new Quaternion(new Vector3D(0, 0, 1), 0); } else { this.rotateDeltaQuaternion = new Quaternion(axisVector, axisVectorLength); } quaternion = this.rotateDeltaQuaternion * this.rotateQuaternion; } else { delta /= 20; this.translateDeltaVector.X = delta.X * -1; this.translateDeltaVector.Y = delta.Y; } verctor = this.translateVector + this.translateDeltaVector; UpdateViewportList(quaternion, this.scale * this.scaleDelta, verctor); } } #endregion #region 엘리먼트 마우스 오른쪽 버튼 DOWN 처리하기 - element_MouseRightButtonDown(sender, e) /// <summary> /// 엘리먼트 마우스 오른쪽 버튼 DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void element_MouseRightButtonDown(object sender, MouseButtonEventArgs e) { if(!Enabled) { return; } e.Handled = true; if(Keyboard.IsKeyDown(Key.F1) == true) { Reset(); return; } UIElement element = sender as UIElement; this.dragInitialPoint = e.MouseDevice.GetPosition(element); if(!this.isCentered) { ProjectionCamera camera = (ProjectionCamera)this.viewportList[0].Camera; this.centerVector = camera.LookDirection; this.isCentered = true; } this.scaling = (e.MiddleButton == MouseButtonState.Pressed); if(Keyboard.IsKeyDown(Key.Space) == false) { this.isRotating = true; } else { this.isRotating = false; } element.CaptureMouse(); } #endregion #region 엘리먼트 마우스 오른쪽 버튼 UP 처리하기 - element_MouseRightButtonUp(sender, e) /// <summary> /// 엘리먼트 마우스 오른쪽 버튼 UP 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void element_MouseRightButtonUp(object sender, MouseButtonEventArgs e) { if(!this.enabled) { return; } e.Handled = true; if(this.isRotating == true) { this.rotateQuaternion = this.rotateDeltaQuaternion * this.rotateQuaternion; } else { this.translateVector += this.translateDeltaVector; this.translateDeltaVector.X = 0; this.translateDeltaVector.Y = 0; } UIElement element = sender as UIElement; element.ReleaseMouseCapture(); } #endregion #region 엘리먼트 마우스 휠 처리하기 - element_MouseWheel(sender, e) /// <summary> /// 엘리먼트 마우스 휠 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void element_MouseWheel(object sender, MouseWheelEventArgs e) { e.Handled = true; this.scaleDelta += (double)((double)e.Delta / (double)1000); Quaternion quaternion = this.rotateQuaternion; UpdateViewportList(quaternion, this.scale * this.scaleDelta, this.translateVector); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 뷰포트 리스트 업데이트하기 - UpdateViewportList(quaternion, scale, vector) /// <summary> /// 뷰포트 리스트 업데이트하기 /// </summary> /// <param name="quaternion">쿼터니언</param> /// <param name="scale">스케일</param> /// <param name="vector">벡터</param> private void UpdateViewportList(Quaternion quaternion, double scale, Vector3D vector) { if(this.viewportList != null) { foreach(Viewport3D viewport in this.viewportList) { ModelVisual3D modelVisual = viewport.Children[0] as ModelVisual3D; Transform3DGroup transformGroup = modelVisual.Transform as Transform3DGroup; ScaleTransform3D groupScaleTransform = transformGroup.Children[0] as ScaleTransform3D; RotateTransform3D groupRotateTransform = transformGroup.Children[1] as RotateTransform3D; TranslateTransform3D groupTranslateTransform = transformGroup.Children[2] as TranslateTransform3D; groupScaleTransform.ScaleX = scale; groupScaleTransform.ScaleY = scale; groupScaleTransform.ScaleZ = scale; groupRotateTransform.Rotation = new AxisAngleRotation3D(quaternion.Axis, quaternion.Angle); groupTranslateTransform.OffsetX = vector.X; groupTranslateTransform.OffsetY = vector.Y; groupTranslateTransform.OffsetZ = vector.Z; } } } #endregion #region 리셋하기 - Reset() /// <summary> /// 리셋하기 /// </summary> private void Reset() { this.rotateQuaternion = new Quaternion (0, 0, 0, 1); this.scale = 1; this.translateVector.X = 0; this.translateVector.Y = 0; this.translateVector.Z = 0; this.translateDeltaVector.X = 0; this.translateDeltaVector.Y = 0; this.translateDeltaVector.Z = 0; this.rotateDeltaQuaternion = Quaternion.Identity; this.scaleDelta = 1; UpdateViewportList(this.rotateQuaternion, this.scale, this.translateVector); } #endregion } } |
▶ MainWindow.xaml
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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="3D 큐브 애니메이션 만들기" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <Storyboard x:Key="RotateStoryboardKey"> <ParallelTimeline Storyboard.TargetName="axisAngleRotation" Storyboard.TargetProperty="Angle" RepeatBehavior="Forever"> <DoubleAnimation From="0" To="360" Duration="0:0:30" /> </ParallelTimeline> </Storyboard> <MeshGeometry3D x:Key="CubeMeshKey" TriangleIndices="0 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" Normals="0 0 -1 0 0 -1 0 0 -1 0 0 -1 0 0 -1 0 0 -1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 -1 0 0 -1 0 0 -1 0 0 -1 0 0 -1 0 0 -1 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 -1 0 0 -1 0 0 -1 0 0 -1 0 0 -1 0 0 -1 0 0" TextureCoordinates="1 0 1 1 0 1 0 1 0 0 1 0 0 0 1 0 1 1 1 1 0 1 0 0 0 0 1 0 1 1 1 1 0 1 0 0 1 0 1 1 0 1 0 1 0 0 1 0 1 1 0 1 0 0 0 0 1 0 1 1 0 1 0 0 1 0 1 0 1 1 0 1" Positions="-0.5 -0.5 -0.5 -0.5 0.5 -0.5 0.5 0.5 -0.5 0.5 0.5 -0.5 0.5 -0.5 -0.5 -0.5 -0.5 -0.5 -0.5 -0.5 0.5 0.5 -0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 -0.5 0.5 0.5 -0.5 -0.5 0.5 -0.5 -0.5 -0.5 0.5 -0.5 -0.5 0.5 -0.5 0.5 0.5 -0.5 0.5 -0.5 -0.5 0.5 -0.5 -0.5 -0.5 0.5 -0.5 -0.5 0.5 0.5 -0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 -0.5 0.5 0.5 -0.5 -0.5 0.5 0.5 -0.5 -0.5 0.5 -0.5 -0.5 0.5 0.5 -0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 -0.5 -0.5 0.5 -0.5 -0.5 -0.5 -0.5 -0.5 -0.5 0.5 -0.5 -0.5 0.5 -0.5 0.5 0.5 -0.5 0.5 -0.5" /> <Transform3DGroup x:Key="CubeMeshTransformKey1"> <Transform3DGroup.Children> <ScaleTransform3D ScaleX="10" ScaleY="10" ScaleZ="10" /> </Transform3DGroup.Children> </Transform3DGroup> <Transform3DGroup x:Key="CubeMeshTransformKey2"> <Transform3DGroup.Children> <ScaleTransform3D ScaleX="12" ScaleY="12" ScaleZ="12" /> </Transform3DGroup.Children> </Transform3DGroup> </Window.Resources> <Grid Background="Blue"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <StackPanel> <Button Name="animateButton" Margin="10" Width="100" Height="30" Content="애니메이션" /> </StackPanel> <Viewport3D Name="viewport3D" Grid.Row="0" Grid.Column="1" Focusable="true" ClipToBounds="true"> <Viewport3D.Camera> <PerspectiveCamera Position="0 0 3.0" LookDirection="0 0 -1" UpDirection="0 1 0" NearPlaneDistance="0.25" FarPlaneDistance="20" FieldOfView="60" /> </Viewport3D.Camera> <ModelVisual3D> <ModelVisual3D.Transform> <Transform3DGroup> <Transform3DGroup.Children> <Transform3DCollection > <ScaleTransform3D ScaleX="1" ScaleY="1" ScaleZ="1" /> <RotateTransform3D> <RotateTransform3D.Rotation> <AxisAngleRotation3D Axis="0 1 0" Angle="0" /> </RotateTransform3D.Rotation> </RotateTransform3D> <TranslateTransform3D OffsetX="0" OffsetY="0" OffsetZ="0" /> </Transform3DCollection> </Transform3DGroup.Children> </Transform3DGroup> </ModelVisual3D.Transform> <ModelVisual3D.Content> <Model3DGroup > <Model3DGroup.Transform> <Transform3DGroup> <Transform3DGroup.Children> <Transform3DCollection > <ScaleTransform3D ScaleX="0.1" ScaleY="0.1" ScaleZ="0.1" /> <RotateTransform3D> <RotateTransform3D.Rotation> <AxisAngleRotation3D Axis="0 1 0" Angle="0" /> </RotateTransform3D.Rotation> </RotateTransform3D> <TranslateTransform3D OffsetX="0" OffsetY="0" OffsetZ="0" /> </Transform3DCollection> </Transform3DGroup.Children> </Transform3DGroup> </Model3DGroup.Transform> <Model3DGroup.Children> <Model3DGroup > <Model3DGroup.Transform> <Transform3DGroup> <Transform3DGroup.Children> <Transform3DCollection > <ScaleTransform3D ScaleX="1" ScaleY="1" ScaleZ="1" /> <RotateTransform3D > <RotateTransform3D.Rotation > <AxisAngleRotation3D x:Name="axisAngleRotation" Axis="1 0 0" Angle="0" /> </RotateTransform3D.Rotation> </RotateTransform3D> <TranslateTransform3D OffsetX="0" OffsetY="0" OffsetZ="0" /> </Transform3DCollection> </Transform3DGroup.Children> </Transform3DGroup> </Model3DGroup.Transform> <Model3DGroup.Children> <GeometryModel3D Transform="{StaticResource CubeMeshTransformKey1}" Geometry="{StaticResource CubeMeshKey}"> <GeometryModel3D.Material> <MaterialGroup> <MaterialGroup.Children> <EmissiveMaterial> <EmissiveMaterial.Brush> <ImageBrush ImageSource="Image\roundcornersheet.png"> <ImageBrush.Transform> <TransformGroup> <TransformGroup.Children> <RotateTransform CenterX="0.5" CenterY="0.5" Angle="0" /> </TransformGroup.Children> </TransformGroup> </ImageBrush.Transform> </ImageBrush> </EmissiveMaterial.Brush> </EmissiveMaterial> </MaterialGroup.Children> </MaterialGroup> </GeometryModel3D.Material> <GeometryModel3D.BackMaterial> <MaterialGroup> <MaterialGroup.Children> <EmissiveMaterial> <EmissiveMaterial.Brush> <ImageBrush ImageSource="Image\roundcornersheet.png"> <ImageBrush.Transform> <TransformGroup> <TransformGroup.Children> <RotateTransform CenterX="0.5" CenterY="0.5" Angle="0" /> </TransformGroup.Children> </TransformGroup> </ImageBrush.Transform> </ImageBrush> </EmissiveMaterial.Brush> </EmissiveMaterial> </MaterialGroup.Children> </MaterialGroup> </GeometryModel3D.BackMaterial> </GeometryModel3D> <GeometryModel3D Transform="{StaticResource CubeMeshTransformKey2}" Geometry="{StaticResource CubeMeshKey}"> <GeometryModel3D.Material> <MaterialGroup> <MaterialGroup.Children> <EmissiveMaterial> <EmissiveMaterial.Brush> <ImageBrush ImageSource="Image\roundcornersheet.png"> <ImageBrush.Transform> <TransformGroup> <TransformGroup.Children> <RotateTransform CenterX="0.5" CenterY="0.5" Angle="0" /> </TransformGroup.Children> </TransformGroup> </ImageBrush.Transform> </ImageBrush> </EmissiveMaterial.Brush> </EmissiveMaterial> </MaterialGroup.Children> </MaterialGroup> </GeometryModel3D.Material> <GeometryModel3D.BackMaterial> <MaterialGroup> <MaterialGroup.Children> <EmissiveMaterial> <EmissiveMaterial.Brush> <ImageBrush ImageSource="Image\roundcornersheet.png"> <ImageBrush.Transform> <TransformGroup> <TransformGroup.Children> <RotateTransform CenterX="0.5" CenterY="0.5" Angle="0" /> </TransformGroup.Children> </TransformGroup> </ImageBrush.Transform> </ImageBrush> </EmissiveMaterial.Brush> </EmissiveMaterial> </MaterialGroup.Children> </MaterialGroup> </GeometryModel3D.BackMaterial> </GeometryModel3D> </Model3DGroup.Children> </Model3DGroup> <Model3DGroup> <Model3DGroup.Children> <AmbientLight Color="#ffcccccc" /> <DirectionalLight Color="LightGray" Direction="-1 -1 -1" /> </Model3DGroup.Children> </Model3DGroup> </Model3DGroup.Children> </Model3DGroup> </ModelVisual3D.Content> </ModelVisual3D> </Viewport3D> </Grid> </Window> |
▶ MainWindow.xaml.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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
using System; using System.Windows; using System.Windows.Media.Animation; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 트랙 볼 /// </summary> private Trackball trackball; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); Loaded += Window_Loaded; this.animateButton.Click += animateButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e) /// <summary> /// 윈도우 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_Loaded(object sender, EventArgs e) { this.trackball = new Trackball(); this.trackball.Attach(this); this.trackball.ViewportList.Add(this.viewport3D); this.trackball.Enabled = true; } #endregion #region 애니메이션 버튼 클릭시 처리하기 - animateButton_Click(sender, e) /// <summary> /// 애니메이션 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void animateButton_Click(object sender, RoutedEventArgs e) { Storyboard storyboard = (Storyboard)FindResource("RotateStoryboardKey"); BeginStoryboard(storyboard); } #endregion } } |