■ 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 |
using System; using System.Windows; using System.Windows.Media.Media3D; namespace TestProject { /// <summary> /// 구체 메시 제너레이터 /// </summary> public class SphereMeshGenerator { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 슬라이스 수 /// </summary> private int sliceCount = 32; /// <summary> /// 스택 수 /// </summary> private int stackCount = 16; /// <summary> /// 중심점 /// </summary> private Point3D centerPoint = new Point3D(); /// <summary> /// 반경 /// </summary> private double radius = 1; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 슬라이스 수 - SliceCount /// <summary> /// 슬라이스 수 /// </summary> public int SliceCount { get { return this.sliceCount; } set { this.sliceCount = value; } } #endregion #region 스택 수 - StackCount /// <summary> /// 스택 수 /// </summary> public int StackCount { set { this.stackCount = value; } get { return this.stackCount; } } #endregion #region 중심점 - CenterPoint /// <summary> /// 중심점 /// </summary> public Point3D CenterPoint { get { return this.centerPoint; } set { this.centerPoint = value; } } #endregion #region 반경 - Radius /// <summary> /// 반경 /// </summary> public double Radius { get { return this.radius; } set { this.radius = value; } } #endregion #region 도형 - Geometry /// <summary> /// 도형 /// </summary> public MeshGeometry3D Geometry { get { MeshGeometry3D mesh = new MeshGeometry3D(); 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); mesh.Normals.Add(normalVector); mesh.Positions.Add(normalVector + CenterPoint); mesh.TextureCoordinates.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) { mesh.TriangleIndices.Add(top + slice ); mesh.TriangleIndices.Add(bottom + slice ); mesh.TriangleIndices.Add(top + slice + 1); } if(stack != StackCount - 1) { mesh.TriangleIndices.Add(top + slice + 1); mesh.TriangleIndices.Add(bottom + slice ); mesh.TriangleIndices.Add(bottom + slice + 1); } } } return mesh; } } #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 |
<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="SphereMeshGeneratorKey" CenterPoint="1 0 0" Radius="0.5" /> </Window.Resources> <Viewport3D> <ModelVisual3D> <ModelVisual3D.Content> <Model3DGroup> <GeometryModel3D Geometry="{Binding Source={StaticResource SphereMeshGeneratorKey}, Path=Geometry}"> <GeometryModel3D.Material> <DiffuseMaterial> <DiffuseMaterial.Brush> <ImageBrush ImageSource="sample.png" /> </DiffuseMaterial.Brush> </DiffuseMaterial> </GeometryModel3D.Material> <GeometryModel3D.Transform> <RotateTransform3D> <RotateTransform3D.Rotation> <AxisAngleRotation3D x:Name="axisAngleRotation3D" Axis="0 1 0" /> </RotateTransform3D.Rotation> </RotateTransform3D> </GeometryModel3D.Transform> </GeometryModel3D> <AmbientLight Color="White" /> </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 TargetName="axisAngleRotation3D" TargetProperty="Angle"> <DoubleAnimation From="0" To="360" Duration="0:0:5" RepeatBehavior="Forever" /> </Storyboard> </BeginStoryboard> </EventTrigger> </Window.Triggers> </Window> |