■ MeshGeometry3D 클래스에서 리소스를 사용하는 방법을 보여준다.
▶ SphereMeshGenerator.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 |
using System; using System.Windows; using System.Windows.Markup; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Media3D; namespace TestProject { /// <summary> /// 구체 메쉬 생성자 /// </summary> [RuntimeNameProperty("Name")] public class SphereMeshGenerator : Animatable { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 도형 키 /// </summary> private static readonly DependencyPropertyKey GeometryKey = DependencyProperty.RegisterReadOnly ( "Geometry", typeof(MeshGeometry3D), typeof(SphereMeshGenerator), new PropertyMetadata(new MeshGeometry3D()) ); #endregion //////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 명칭 속성 /// </summary> public static readonly DependencyProperty NameProperty = DependencyProperty.Register ( "Name", typeof(string), typeof(SphereMeshGenerator) ); /// <summary> /// 중심점 속성 /// </summary> public static readonly DependencyProperty CenterPointProperty = DependencyProperty.Register ( "CenterPoint", typeof(Point3D), typeof(SphereMeshGenerator), new PropertyMetadata(new Point3D(), PropertyChangedCallback) ); /// <summary> /// 반경 속성 /// </summary> public static readonly DependencyProperty RadiusProperty = DependencyProperty.Register ( "Radius", typeof(double), typeof(SphereMeshGenerator), new PropertyMetadata(1.0, PropertyChangedCallback) ); /// <summary> /// 슬라이스 수 속성 /// </summary> public static readonly DependencyProperty SliceCountProperty = DependencyProperty.Register ( "SliceCount", typeof(int), typeof(SphereMeshGenerator), new PropertyMetadata(32, PropertyChangedCallback) ); /// <summary> /// 스택 수 속성 /// </summary> public static readonly DependencyProperty StackCountProperty = DependencyProperty.Register ( "StackCount", typeof(int), typeof(SphereMeshGenerator), new PropertyMetadata(16, PropertyChangedCallback) ); /// <summary> /// 도형 속성 /// </summary> public static readonly DependencyProperty GeometryProperty = GeometryKey.DependencyProperty; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 명칭 - Name /// <summary> /// 명칭 /// </summary> public string Name { set { SetValue(NameProperty, value); } get { return (string)GetValue(NameProperty); } } #endregion #region 중심점 - CenterPoint /// <summary> /// 중심점 /// </summary> public Point3D CenterPoint { set { SetValue(CenterPointProperty, value); } get { return (Point3D)GetValue(CenterPointProperty); } } #endregion #region 반경 - Radius /// <summary> /// 반경 /// </summary> public double Radius { set { SetValue(RadiusProperty, value); } get { return (double)GetValue(RadiusProperty); } } #endregion #region 슬라이스 수 - SliceCount /// <summary> /// 슬라이스 수 /// </summary> public int SliceCount { set { SetValue(SliceCountProperty, value); } get { return (int)GetValue(SliceCountProperty); } } #endregion #region 스택 수 - StackCount /// <summary> /// 스택 수 /// </summary> public int StackCount { set { SetValue(StackCountProperty, value); } get { return (int)GetValue(StackCountProperty); } } #endregion #region 도형 - Geometry /// <summary> /// 도형 /// </summary> public MeshGeometry3D Geometry { protected set { SetValue(GeometryKey, value); } get { return (MeshGeometry3D)GetValue(GeometryProperty); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - SphereMeshGenerator() /// <summary> /// 생성자 /// </summary> public SphereMeshGenerator() { Geometry = Geometry.Clone(); PropertyChanged(new DependencyPropertyChangedEventArgs()); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 속성 변경시 콜백 처리하기 - PropertyChangedCallback(d, e) /// <summary> /// 속성 변경시 콜백 처리하기 /// </summary> /// <param name="d">의존 객체</param> /// <param name="e">이벤트 인자</param> private static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { (d as SphereMeshGenerator).PropertyChanged(e); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Protected #region 인스턴스 생성하기 (CORE) - CreateInstanceCore() /// <summary> /// 인스턴스 생성하기 (CORE) /// </summary> /// <returns>Freezable 객체</returns> protected override Freezable CreateInstanceCore() { return new SphereMeshGenerator(); } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region 속성 변경시 처리하기 - PropertyChanged(e) /// <summary> /// 속성 변경시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> private void PropertyChanged(DependencyPropertyChangedEventArgs e) { MeshGeometry3D mesh = Geometry; Point3DCollection positionCollection = mesh.Positions; Vector3DCollection normalCollection = mesh.Normals; Int32Collection indexCollection = mesh.TriangleIndices; PointCollection textureCollection = mesh.TextureCoordinates; mesh.Positions = null; mesh.Normals = null; mesh.TriangleIndices = null; mesh.TextureCoordinates = null; positionCollection.Clear(); normalCollection.Clear(); indexCollection.Clear(); textureCollection.Clear(); for(int stack = 0; stack <= StackCount; stack++) { double phi = Math.PI / 2 - stack * Math.PI / StackCount; double y = Radius * Math.Sin(phi); double scale = -Radius * Math.Cos(phi); for(int slice = 0; slice <= SliceCount; slice++) { double theta = slice * 2 * Math.PI / SliceCount; double x = scale * Math.Sin(theta); double z = scale * Math.Cos(theta); Vector3D normalVector = new Vector3D(x, y, z); normalCollection.Add(normalVector); positionCollection.Add(normalVector + CenterPoint); textureCollection.Add(new Point((double)slice / SliceCount, (double)stack / StackCount)); } } for(int stack = 0; stack < StackCount; stack++) { int top = (stack + 0) * (SliceCount + 1); int bottom = (stack + 1) * (SliceCount + 1); for(int slice = 0; slice < SliceCount; slice++) { if(stack != 0) { indexCollection.Add(top + slice ); indexCollection.Add(bottom + slice ); indexCollection.Add(top + slice + 1); } if(stack != StackCount - 1) { indexCollection.Add(top + slice + 1); indexCollection.Add(bottom + slice ); indexCollection.Add(bottom + slice + 1); } } } mesh.TextureCoordinates = textureCollection; mesh.TriangleIndices = indexCollection; mesh.Normals = normalCollection; mesh.Positions = positionCollection; } #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 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestProject" Width="800" Height="600" Title="MeshGeometry3D 클래스 : 리소스 사용하기" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <local:SphereMeshGenerator x:Key="SphereMeshGeneratorKey1" x:Name="sphereMeshGenerator1" CenterPoint="-0.25 0 0" /> <local:SphereMeshGenerator x:Key="SphereMeshGeneratorKey2" x:Name="sphereMeshGenerator2" CenterPoint="0.25 0 0" /> </Window.Resources> <Viewport3D> <ModelVisual3D> <ModelVisual3D.Content> <Model3DGroup> <GeometryModel3D Geometry="{Binding Source={StaticResource SphereMeshGeneratorKey1}, Path=Geometry}"> <GeometryModel3D.Material> <DiffuseMaterial Brush="RoyalBlue" /> </GeometryModel3D.Material> </GeometryModel3D> <GeometryModel3D Geometry="{Binding Source={StaticResource SphereMeshGeneratorKey2}, Path=Geometry}"> <GeometryModel3D.Material> <DiffuseMaterial Brush="Red" /> </GeometryModel3D.Material> </GeometryModel3D> <AmbientLight Color="#404040" /> <DirectionalLight Color="#c0c0c0" Direction="2 -3 -1" /> </Model3DGroup> </ModelVisual3D.Content> </ModelVisual3D> <Viewport3D.Camera> <PerspectiveCamera Position="0 0 4" LookDirection="0 0 -1" UpDirection="0 1 0" FieldOfView="45" /> </Viewport3D.Camera> </Viewport3D> <Window.Triggers> <EventTrigger RoutedEvent="Window.Loaded"> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetName="sphereMeshGenerator1" Storyboard.TargetProperty="Radius" From="0" To="0.5" Duration="0:0:3" AutoReverse="True" RepeatBehavior="Forever" /> <DoubleAnimation Storyboard.TargetName="sphereMeshGenerator2" Storyboard.TargetProperty="Radius" From="0.5" To="0" Duration="0:0:3" AutoReverse="True" RepeatBehavior="Forever" /> </Storyboard> </BeginStoryboard> </EventTrigger> </Window.Triggers> </Window> |
※ 리소스에서 Name 또는 x:Name 속성의 사용이 닷넷 프레임워크 3.5 버전까지만 에러없이 실행된다.