■ MeshGeometry3D 클래스를 사용해 수레바퀴를 만드는 방법을 보여준다.
▶ MeshGeneratorBase.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 |
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 abstract class MeshGeneratorBase : Animatable { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 도형 키 /// </summary> private static DependencyPropertyKey GeometryKey = DependencyProperty.RegisterReadOnly ( "Geometry", typeof(MeshGeometry3D), typeof(MeshGeneratorBase), new PropertyMetadata(new MeshGeometry3D()) ); #endregion //////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 명칭 속성 /// </summary> public static readonly DependencyProperty NameProperty = DependencyProperty.Register ( "Name", typeof(string), typeof(MeshGeneratorBase) ); /// <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 도형 - Geometry /// <summary> /// 도형 /// </summary> public MeshGeometry3D Geometry { get { return (MeshGeometry3D)GetValue(GeometryProperty); } protected set { SetValue(GeometryKey, value); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MeshGeneratorBase() /// <summary> /// 생성자 /// </summary> public MeshGeneratorBase() { Geometry = new MeshGeometry3D(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Protected #region 속성 변경시 처리하기 - PropertyChanged(d, e) /// <summary> /// 속성 변경시 처리하기 /// </summary> /// <param name="d">의존 객체</param> /// <param name="e">이벤트 인자</param> protected static void PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { (d as MeshGeneratorBase).PropertyChanged(e); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Protected #region 삼각형 구성하기 - Triangulate(e, vertexCollection, normalCollection, indexCollection, textureCollection) /// <summary> /// 삼각형 구성하기 /// </summary> /// <param name="e">이벤트 인자</param> /// <param name="vertexCollection">꼭지점 컬렉션</param> /// <param name="normalCollection">법선 컬렉션</param> /// <param name="indexCollection">인덱스 컬렉션</param> /// <param name="textureCollection">텍스쳐 컬렉션</param> protected abstract void Triangulate(DependencyPropertyChangedEventArgs e, Point3DCollection vertexCollection, Vector3DCollection normalCollection, Int32Collection indexCollection, PointCollection textureCollection); #endregion #region 속성 변경시 처리하기 - PropertyChanged(e) /// <summary> /// 속성 변경시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected virtual void PropertyChanged(DependencyPropertyChangedEventArgs e) { MeshGeometry3D mesh = Geometry; Point3DCollection vertexCollection = 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; Triangulate(e, vertexCollection, normalCollection, indexCollection, textureCollection); mesh.TextureCoordinates = textureCollection; mesh.TriangleIndices = indexCollection; mesh.Normals = normalCollection; mesh.Positions = vertexCollection; } #endregion } } |
▶ CylindricalMeshBase.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 |
using System.Windows; namespace TestProject { /// <summary> /// 실린더 메쉬 베이스 /// </summary> public abstract class CylindricalMeshBase : MeshGeneratorBase { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 길이 속성 /// </summary> public static readonly DependencyProperty LengthProperty = DependencyProperty.Register ( "Length", typeof(double), typeof(CylindricalMeshBase), new PropertyMetadata(1.0, PropertyChanged) ); /// <summary> /// 반경 /// </summary> public static readonly DependencyProperty RadiusProperty = DependencyProperty.Register ( "Radius", typeof(double), typeof(CylindricalMeshBase), new PropertyMetadata(1.0, PropertyChanged) ); /// <summary> /// 슬라이스 수 속성 /// </summary> public static readonly DependencyProperty SliceCountProperty = DependencyProperty.Register ( "SliceCount", typeof(int), typeof(CylindricalMeshBase), new PropertyMetadata(36, PropertyChanged), ValidateSliceCount ); /// <summary> /// 스택 수 속성 /// </summary> public static readonly DependencyProperty StackCountProperty = DependencyProperty.Register ( "StackCount", typeof(int), typeof(CylindricalMeshBase), new PropertyMetadata(1, PropertyChanged), ValidateStackCount ); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 길이 - Length /// <summary> /// 길이 /// </summary> public double Length { set { SetValue(LengthProperty, value); } get { return (double)GetValue(LengthProperty); } } #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 //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 슬라이스 수 무결성 체크하기 - ValidateSliceCount(sourceObject) /// <summary> /// 슬라이스 수 무결성 체크하기 /// </summary> /// <param name="sourceObject">소스 객체</param> /// <returns>처리 결과</returns> private static bool ValidateSliceCount(object sourceObject) { return (int)sourceObject > 2; } #endregion #region 스택 수 무결성 체크하기 - ValidateStackCount(sourceObject) /// <summary> /// 스택 수 무결성 체크하기 /// </summary> /// <param name="sourceObject">소스 객체</param> /// <returns>처리 결과</returns> private static bool ValidateStackCount(object sourceObject) { return (int)sourceObject > 0; } #endregion } } |
▶ SphereMesh.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 |
using System; using System.Windows; using System.Windows.Media; using System.Windows.Media.Media3D; namespace TestProject { /// <summary> /// 구체 메쉬 /// </summary> public class SphereMesh : MeshGeneratorBase { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 중심점 속성 /// </summary> public static readonly DependencyProperty CenterPointProperty = DependencyProperty.Register ( "CenterPoint", typeof(Point3D), typeof(SphereMesh), new PropertyMetadata(new Point3D(), PropertyChanged) ); /// <summary> /// 반경 속성 /// </summary> public static readonly DependencyProperty RadiusProperty = DependencyProperty.Register ( "Radius", typeof(double), typeof(SphereMesh), new PropertyMetadata(1.0, PropertyChanged) ); /// <summary> /// 슬라이스 수 속성 /// </summary> public static readonly DependencyProperty SliceCountProperty = DependencyProperty.Register ( "SliceCount", typeof(int), typeof(SphereMesh), new PropertyMetadata(36, PropertyChanged), ValidateSliceCount ); /// <summary> /// 스택 수 속성 /// </summary> public static readonly DependencyProperty StackCountProperty = DependencyProperty.Register ( "StackCount", typeof(int), typeof(SphereMesh), new PropertyMetadata(18, PropertyChanged), ValidateStackCount ); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #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 //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - SphereMesh() /// <summary> /// 생성자 /// </summary> public SphereMesh() { PropertyChanged(new DependencyPropertyChangedEventArgs()); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 슬라이스 수 무결성 체크하기 - ValidateSliceCount(sourceObject) /// <summary> /// 슬라이스 수 무결성 체크하기 /// </summary> /// <param name="sourceObject">소스 객체</param> /// <returns>처리 결과</returns> private static bool ValidateSliceCount(object sourceObject) { return (int)sourceObject > 2; } #endregion #region 스택 수 무결성 체크하기 - ValidateStackCount(sourceObject) /// <summary> /// 스택 수 무결성 체크하기 /// </summary> /// <param name="sourceObject">소스 객체</param> /// <returns>처리 결과</returns> private static bool ValidateStackCount(object sourceObject) { return (int)sourceObject > 1; } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Protected #region 삼각형 구성하기 - Triangulate(e, vertexCollection, normalCollection, indexCollection, textureCollection) /// <summary> /// 삼각형 구성하기 /// </summary> /// <param name="e">이벤트 인자</param> /// <param name="vertexCollection">꼭지점 컬렉션</param> /// <param name="normalCollection">법선 컬렉션</param> /// <param name="indexCollection">인덱스 컬렉션</param> /// <param name="textureCollection">텍스처 컬렉션</param> protected override void Triangulate(DependencyPropertyChangedEventArgs e, Point3DCollection vertexCollection, Vector3DCollection normalCollection, Int32Collection indexCollection, PointCollection textureCollection) { vertexCollection.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 normal = new Vector3D(x, y, z); normalCollection.Add(normal); vertexCollection.Add(normal + CenterPoint); textureCollection.Add(new Point((double)slice / SliceCount, (double)stack / StackCount)); } } for(int stack = 0; stack < StackCount; stack++) { for(int slice = 0; slice < SliceCount; slice++) { if(stack != 0) { indexCollection.Add((stack + 0) * (SliceCount + 1) + slice ); indexCollection.Add((stack + 1) * (SliceCount + 1) + slice ); indexCollection.Add((stack + 0) * (SliceCount + 1) + slice + 1); } if(stack != StackCount - 1) { indexCollection.Add((stack + 0) * (SliceCount + 1) + slice + 1); indexCollection.Add((stack + 1) * (SliceCount + 1) + slice ); indexCollection.Add((stack + 1) * (SliceCount + 1) + slice + 1); } } } } #endregion #region 인스턴스 생성하기 (코어) - CreateInstanceCore() /// <summary> /// 인스턴스 생성하기 (코어) /// </summary> /// <returns>인스턴스</returns> protected override Freezable CreateInstanceCore() { return new SphereMesh(); } #endregion } } |
▶ HollowCylinderMesh.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 |
using System; using System.Windows; using System.Windows.Media; using System.Windows.Media.Media3D; namespace TestProject { /// <summary> /// 빈 실린더 메쉬 /// </summary> public class HollowCylinderMesh : CylindricalMeshBase { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - HollowCylinderMesh() /// <summary> /// 생성자 /// </summary> public HollowCylinderMesh() { PropertyChanged(new DependencyPropertyChangedEventArgs()); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 삼각형 구성하기 - Triangulate(e, vertexCollection, normalCollection, indexCollection, textureCollection) /// <summary> /// 삼각형 구성하기 /// </summary> /// <param name="e">이벤트 인자</param> /// <param name="vertexCollection">꼭지점 컬렉션</param> /// <param name="normalCollection">법선 컬렉션</param> /// <param name="indexCollection">인덱스 컬렉션</param> /// <param name="textureCollection">텍스처 컬렉션</param> protected override void Triangulate(DependencyPropertyChangedEventArgs e, Point3DCollection vertexCollection, Vector3DCollection normalCollection, Int32Collection indexCollection, PointCollection textureCollection) { vertexCollection.Clear(); normalCollection.Clear(); indexCollection.Clear(); textureCollection.Clear(); for(int stack = 0; stack <= StackCount; stack++) { double y = Length - stack * Length / StackCount; for(int slice = 0; slice <= SliceCount; slice++) { double theta = slice * 2 * Math.PI / SliceCount; double x = -Radius * Math.Sin(theta); double z = -Radius * Math.Cos(theta); normalCollection.Add(new Vector3D(x, 0, z)); vertexCollection.Add(new Point3D(x, y, z)); textureCollection.Add(new Point((double)slice / SliceCount, (double)stack / StackCount)); } } for(int stack = 0; stack < StackCount; stack++) { for(int slice = 0; slice < SliceCount; slice++) { indexCollection.Add((stack + 0) * (SliceCount + 1) + slice ); indexCollection.Add((stack + 1) * (SliceCount + 1) + slice ); indexCollection.Add((stack + 0) * (SliceCount + 1) + slice + 1); indexCollection.Add((stack + 0) * (SliceCount + 1) + slice + 1); indexCollection.Add((stack + 1) * (SliceCount + 1) + slice ); indexCollection.Add((stack + 1) * (SliceCount + 1) + slice + 1); } } } #endregion #region 인스턴스 생성하기 (코어) - CreateInstanceCore() /// <summary> /// 인스턴스 생성하기 (코어) /// </summary> /// <returns>인스턴스</returns> protected override Freezable CreateInstanceCore() { return new HollowCylinderMesh(); } #endregion } } |
▶ TorusMesh.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 |
using System; using System.Windows; using System.Windows.Media; using System.Windows.Media.Media3D; namespace TestProject { /// <summary> /// 원환체 메쉬 /// </summary> public class TorusMesh : MeshGeneratorBase { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 반경 속성 /// </summary> public static readonly DependencyProperty RadiusProperty = DependencyProperty.Register ( "Radius", typeof(double), typeof(TorusMesh), new PropertyMetadata(1.0, PropertyChanged) ); /// <summary> /// 튜브 반경 속성 /// </summary> public static readonly DependencyProperty TubeRadiusProperty = DependencyProperty.Register ( "TubeRadius", typeof(double), typeof(TorusMesh), new PropertyMetadata(0.25, PropertyChanged) ); /// <summary> /// 슬라이스 수 속성 /// </summary> public static readonly DependencyProperty SliceCountProperty = DependencyProperty.Register ( "SliceCount", typeof(int), typeof(TorusMesh), new PropertyMetadata(18, PropertyChanged), ValidateSliceCount ); /// <summary> /// 스택 수 속성 /// </summary> public static readonly DependencyProperty StackCountProperty = DependencyProperty.Register ( "StackCount", typeof(int), typeof(TorusMesh), new PropertyMetadata(36, PropertyChanged), ValidateStackCount ); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 반경 - Radius /// <summary> /// 반경 /// </summary> public double Radius { set { SetValue(RadiusProperty, value); } get { return (double)GetValue(RadiusProperty); } } #endregion #region 튜브 반경 - TubeRadius /// <summary> /// 튜브 반경 /// </summary> public double TubeRadius { set { SetValue(TubeRadiusProperty, value); } get { return (double)GetValue(TubeRadiusProperty); } } #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 //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - TorusMesh() /// <summary> /// 생성자 /// </summary> public TorusMesh() { PropertyChanged(new DependencyPropertyChangedEventArgs()); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 슬라이스 수 무결성 체크하기 - ValidateSliceCount(sourceObject) /// <summary> /// 슬라이스 수 무결성 체크하기 /// </summary> /// <param name="sourceObject">소스 객체</param> /// <returns>처리 결과</returns> private static bool ValidateSliceCount(object sourceObject) { return (int)sourceObject > 2; } #endregion #region 스택 수 무결성 체크하기 - ValidateStackCount(sourceObject) /// <summary> /// 스택 수 무결성 체크하기 /// </summary> /// <param name="sourceObject">소스 객체</param> /// <returns>처리 결과</returns> private static bool ValidateStackCount(object sourceObject) { return (int)sourceObject > 2; } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Protected #region 삼각형 구성하기 - Triangulate(e, vertexCollection, normalCollection, indexCollection, textureCollection) /// <summary> /// 삼각형 구성하기 /// </summary> /// <param name="e">이벤트 인자</param> /// <param name="vertexCollection">꼭지점 컬렉션</param> /// <param name="normalCollection">법선 컬렉션</param> /// <param name="indexCollection">인덱스 컬렉션</param> /// <param name="textureCollection">텍스처 컬렉션</param> protected override void Triangulate(DependencyPropertyChangedEventArgs e, Point3DCollection vertexCollection, Vector3DCollection normalCollection, Int32Collection indexCollection, PointCollection textureCollection) { vertexCollection.Clear(); normalCollection.Clear(); indexCollection.Clear(); textureCollection.Clear(); for(int stack = 0; stack <= StackCount; stack++) { double phi = stack * 2 * Math.PI / StackCount; double xCenter = Radius * Math.Sin(phi); double yCenter = Radius * Math.Cos(phi); Point3D centerPoint = new Point3D(xCenter, yCenter, 0); for(int slice = 0; slice <= SliceCount; slice++) { double theta = slice * 2 * Math.PI / SliceCount + Math.PI; double x = (Radius + TubeRadius * Math.Cos(theta)) * Math.Sin(phi); double y = (Radius + TubeRadius * Math.Cos(theta)) * Math.Cos(phi); double z = -TubeRadius * Math.Sin(theta); Point3D point = new Point3D(x, y, z); vertexCollection.Add(point); normalCollection.Add(point - centerPoint); textureCollection.Add(new Point((double)slice / SliceCount, (double)stack / StackCount)); } } for(int stack = 0; stack < StackCount; stack++) { for(int slice = 0; slice < SliceCount; slice++) { indexCollection.Add((stack + 0) * (SliceCount + 1) + slice ); indexCollection.Add((stack + 1) * (SliceCount + 1) + slice ); indexCollection.Add((stack + 0) * (SliceCount + 1) + slice + 1); indexCollection.Add((stack + 0) * (SliceCount + 1) + slice + 1); indexCollection.Add((stack + 1) * (SliceCount + 1) + slice ); indexCollection.Add((stack + 1) * (SliceCount + 1) + slice + 1); } } } #endregion #region 인스턴스 생성하기 (코어) - CreateInstanceCore() /// <summary> /// 인스턴스 생성하기 (코어) /// </summary> /// <returns>인스턴스</returns> protected override Freezable CreateInstanceCore() { return new TorusMesh(); } #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 |
<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:SphereMesh x:Key="hubSphereMeshKey" Radius="0.2" /> <local:HollowCylinderMesh x:Key="spokeHollowCylinderMeshKey" Radius="0.03" Length="1" /> <local:TorusMesh x:Key="tireTorusMeshKey" Radius="1" TubeRadius="0.2" SliceCount="144" StackCount="96" /> <DiffuseMaterial x:Key="steelDiffuseMaterialKey" Brush="SteelBlue" /> <DiffuseMaterial x:Key="tireDiffuseMaterialKey" Brush="DarkGray" /> <Model3DGroup x:Key="spokeModel3DGroupKey"> <GeometryModel3D Geometry="{Binding Source={StaticResource spokeHollowCylinderMeshKey}, Path=Geometry}" Material="{StaticResource steelDiffuseMaterialKey}" /> <GeometryModel3D Geometry="{Binding Source={StaticResource spokeHollowCylinderMeshKey}, Path=Geometry}" Material="{StaticResource steelDiffuseMaterialKey}"> <GeometryModel3D.Transform> <RotateTransform3D> <RotateTransform3D.Rotation> <AxisAngleRotation3D Axis="0 0 -1" Angle="30" /> </RotateTransform3D.Rotation> </RotateTransform3D> </GeometryModel3D.Transform> </GeometryModel3D> <GeometryModel3D Geometry="{Binding Source={StaticResource spokeHollowCylinderMeshKey}, Path=Geometry}" Material="{StaticResource steelDiffuseMaterialKey}"> <GeometryModel3D.Transform> <RotateTransform3D> <RotateTransform3D.Rotation> <AxisAngleRotation3D Axis="0 0 -1" Angle="60" /> </RotateTransform3D.Rotation> </RotateTransform3D> </GeometryModel3D.Transform> </GeometryModel3D> </Model3DGroup> <Model3DGroup x:Key="wheelModel3DGroupKey"> <GeometryModel3D Geometry="{Binding Source={StaticResource hubSphereMeshKey}, Path=Geometry}" Material="{StaticResource steelDiffuseMaterialKey}" /> <StaticResource ResourceKey="spokeModel3DGroupKey" /> <Model3DGroup> <StaticResource ResourceKey="spokeModel3DGroupKey" /> <Model3DGroup.Transform> <RotateTransform3D> <RotateTransform3D.Rotation> <AxisAngleRotation3D Axis="0 0 -1" Angle="90" /> </RotateTransform3D.Rotation> </RotateTransform3D> </Model3DGroup.Transform> </Model3DGroup> <Model3DGroup> <StaticResource ResourceKey="spokeModel3DGroupKey" /> <Model3DGroup.Transform> <RotateTransform3D> <RotateTransform3D.Rotation> <AxisAngleRotation3D Axis="0 0 -1" Angle="180" /> </RotateTransform3D.Rotation> </RotateTransform3D> </Model3DGroup.Transform> </Model3DGroup> <Model3DGroup> <StaticResource ResourceKey="spokeModel3DGroupKey" /> <Model3DGroup.Transform> <RotateTransform3D> <RotateTransform3D.Rotation> <AxisAngleRotation3D Axis="0 0 -1" Angle="270" /> </RotateTransform3D.Rotation> </RotateTransform3D> </Model3DGroup.Transform> </Model3DGroup> <GeometryModel3D Geometry="{Binding Source={StaticResource tireTorusMeshKey}, Path=Geometry}" Material="{StaticResource tireDiffuseMaterialKey}" /> </Model3DGroup> </Window.Resources> <Viewport3D> <ModelVisual3D Content="{StaticResource wheelModel3DGroupKey}"> <ModelVisual3D.Transform> <RotateTransform3D> <RotateTransform3D.Rotation> <AxisAngleRotation3D x:Name="axisAngleRotation3D" Axis="0 0 -1" /> </RotateTransform3D.Rotation> </RotateTransform3D> </ModelVisual3D.Transform> </ModelVisual3D> <ModelVisual3D> <ModelVisual3D.Content> <Model3DGroup> <AmbientLight Color="#404040" /> <DirectionalLight Color="#C0C0C0" Direction="2 -3 -1" /> </Model3DGroup> </ModelVisual3D.Content> </ModelVisual3D> <Viewport3D.Camera> <PerspectiveCamera Position="-4 0 4" LookDirection="4 0 -4" UpDirection="0 1 0" FieldOfView="45" /> </Viewport3D.Camera> </Viewport3D> <Window.Triggers> <EventTrigger RoutedEvent="Window.Loaded"> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetName="axisAngleRotation3D" Storyboard.TargetProperty="Angle" From="0" To="360" Duration="0:0:5" RepeatBehavior="Forever" /> </Storyboard> </BeginStoryboard> </EventTrigger> </Window.Triggers> </Window> |