/
ModelHelper.cs
105 lines (91 loc) · 3.76 KB
/
ModelHelper.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;
namespace SkyNet
{
static class ModelHelper
{
//-------------------------------------------------------------------------------------
/// <summary>
/// CreateSphereModel
/// </summary>
//-------------------------------------------------------------------------------------
public static GeometryModel3D CreateSphereModel(double targetRadius)
{
var model = new GeometryModel3D();
var mesh = new MeshGeometry3D();
int latitudes = 40;
double phiStep = Math.PI / latitudes;
int segments = 40;
double thetaStep = Math.PI * 2 / segments;
for (int j = 0; j <= latitudes; j++)
{
double phi = Math.PI / 2 - phiStep * j;
double y = Math.Sin(phi) * targetRadius;
double radius = targetRadius * Math.Cos(phi);
for (int i = 0; i <= segments; i++)
{
var theta = i * thetaStep;
var newPosition = new Point3D(radius * Math.Cos(theta), y, radius * Math.Sin(theta));
mesh.Positions.Add(newPosition);
//mesh.Normals.Add(new Vector3D(newPosition.X, newPosition.Y, newPosition.Z));
var textureX = ((segments) - i) / (double)(segments) * .999;
var textureY = j / (double)latitudes * .999;
mesh.TextureCoordinates.Add(new Point(textureX, textureY));
}
}
int pointsPerLatitude = segments + 1;
for (int j = 1; j <= latitudes; j++)
{
for (int i = 0; i < segments; i++)
{
var p0 = j * pointsPerLatitude + i;
var p1 = (j - 1) * pointsPerLatitude + i;
var p2 = j * pointsPerLatitude + i + 1;
var p3 = (j - 1) * pointsPerLatitude + i + 1;
mesh.TriangleIndices.Add(p0);
mesh.TriangleIndices.Add(p1);
mesh.TriangleIndices.Add(p2);
mesh.TriangleIndices.Add(p3);
mesh.TriangleIndices.Add(p2);
mesh.TriangleIndices.Add(p1);
}
}
model.Geometry = mesh;
return model;
}
//-------------------------------------------------------------------------------------
/// <summary>
/// CreateSatelliteModel
/// </summary>
//-------------------------------------------------------------------------------------
public static GeometryModel3D CreateSatelliteModel(Model3DGroup group)
{
var model2 = new GeometryModel3D();
var mesh2 = new MeshGeometry3D();
mesh2.Positions.Add(new Point3D(0, 0, -.1));
mesh2.Positions.Add(new Point3D(-.1, 0, .1));
mesh2.Positions.Add(new Point3D(.1, 0, .1));
mesh2.Positions.Add(new Point3D(0, .1, 0));
mesh2.TriangleIndices.Add(0);
mesh2.TriangleIndices.Add(1);
mesh2.TriangleIndices.Add(2);
mesh2.TriangleIndices.Add(0);
mesh2.TriangleIndices.Add(3);
mesh2.TriangleIndices.Add(1);
mesh2.TriangleIndices.Add(3);
mesh2.TriangleIndices.Add(2);
mesh2.TriangleIndices.Add(1);
mesh2.TriangleIndices.Add(1);
mesh2.TriangleIndices.Add(1);
mesh2.TriangleIndices.Add(3);
model2.Geometry = mesh2;
return model2;
}
}
}