forked from KhronosGroup/glTF-Asset-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMesh_PrimitiveRestart.cs
186 lines (171 loc) · 7.25 KB
/
Mesh_PrimitiveRestart.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 AssetGenerator.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
namespace AssetGenerator.ModelGroups
{
internal class Mesh_PrimitiveRestart : ModelGroup
{
public override ModelGroupId Id => ModelGroupId.Mesh_PrimitiveRestart;
public Mesh_PrimitiveRestart(List<string> imageList)
{
// There are no common properties in this model group that are reported in the readme.
NoSampleImages = true;
Model CreateModel(Action<List<Runtime.MeshPrimitive>> setProperties)
{
var meshPrimitives = new List<Runtime.MeshPrimitive>
{
new Runtime.MeshPrimitive(),
new Runtime.MeshPrimitive(),
};
// Apply the properties that are specific to this gltf.
setProperties(meshPrimitives);
var properties = new List<Property>
{
new Property(PropertyName.IndicesComponentType, meshPrimitives[0].Indices.OutputType.ToReadmeString()),
new Property(PropertyName.LeftPrimitiveIndices, meshPrimitives[0].Indices.Values.ToReadmeString()),
new Property(PropertyName.RightPrimitiveIndices, meshPrimitives[1].Indices.Values.ToReadmeString()),
new Property(PropertyName.Mode, meshPrimitives[0].Mode.ToReadmeString())
};
// Create the gltf object
return new Model
{
Properties = properties,
GLTF = CreateGLTF(() => new Scene
{
Nodes = new List<Node>
{
new Node
{
Mesh = new Runtime.Mesh
{
MeshPrimitives = meshPrimitives
}
}
}
}),
Loadable = null
};
}
Vector3[] BuildPositions(DataType type, bool restart)
{
var offset = restart ? -0.6f : 0.6f;
int count;
switch (type)
{
case DataType.UnsignedByte:
count = byte.MaxValue;
break;
case DataType.UnsignedShort:
count = ushort.MaxValue;
break;
case DataType.UnsignedInt:
throw new NotImplementedException();
default:
throw new ArgumentOutOfRangeException(nameof(type), type, null);
}
if (restart) ++count;
var positions = new Vector3[count];
positions[0] = new Vector3(0.5f + offset, -0.5f, 0.0f);
positions[1] = new Vector3(-0.5f + offset, 0.5f, 0.0f);
for (var i = 2; i < count - 1; ++i)
{
positions[i] = Vector3.Zero;
}
positions[count - 1] = new Vector3(-0.5f + offset, -0.5f, 0.0f);
return positions;
}
var typeDelegates = new List<Func<List<Runtime.MeshPrimitive>, DataType>>
{
meshPrimitives =>
{
meshPrimitives[0].Positions = Data.Create(BuildPositions(DataType.UnsignedByte, true));
meshPrimitives[1].Positions = Data.Create(BuildPositions(DataType.UnsignedByte, false));
return DataType.UnsignedByte;
},
meshPrimitives =>
{
meshPrimitives[0].Positions = Data.Create(BuildPositions(DataType.UnsignedShort, true));
meshPrimitives[1].Positions = Data.Create(BuildPositions(DataType.UnsignedShort, false));
return DataType.UnsignedShort;
}
};
var topologyDelegates = new List<Action<Runtime.MeshPrimitive, int, DataType>>
{
(meshPrimitive, maxValue, accessorType) =>
{
meshPrimitive.Mode = MeshPrimitiveMode.Points;
meshPrimitive.Indices = Data.Create(new[]
{
0, 1, maxValue
}, accessorType);
},
(meshPrimitive, maxValue, accessorType) =>
{
meshPrimitive.Mode = MeshPrimitiveMode.Lines;
meshPrimitive.Indices = Data.Create(new[]
{
0, 1, 1, maxValue, maxValue, 0
}, accessorType);
},
(meshPrimitive, maxValue, accessorType) =>
{
meshPrimitive.Mode = MeshPrimitiveMode.LineLoop;
meshPrimitive.Indices = Data.Create(new[]
{
0, 1, maxValue
}, accessorType);
},
(meshPrimitive, maxValue, accessorType) =>
{
meshPrimitive.Mode = MeshPrimitiveMode.LineStrip;
meshPrimitive.Indices = Data.Create(new[]
{
0, 1, maxValue, 0
}, accessorType);
},
(meshPrimitive, maxValue, accessorType) =>
{
meshPrimitive.Mode = MeshPrimitiveMode.Triangles;
meshPrimitive.Indices = Data.Create(new[]
{
0, 1, maxValue
}, accessorType);
},
(meshPrimitive, maxValue, accessorType) =>
{
meshPrimitive.Mode = MeshPrimitiveMode.TriangleStrip;
meshPrimitive.Indices = Data.Create(new[]
{
0, 1, maxValue
}, accessorType);
},
(meshPrimitive, maxValue, accessorType) =>
{
meshPrimitive.Mode = MeshPrimitiveMode.TriangleFan;
meshPrimitive.Indices = Data.Create(new[]
{
0, 1, maxValue
}, accessorType);
}
};
Models = new List<Model>();
foreach (var topologyDelegate in topologyDelegates)
{
foreach (var typeDelegate in typeDelegates)
{
Models.Add(CreateModel(meshPrimitives =>
{
DataType accessorType = typeDelegate(meshPrimitives);
// Models triggering restart
topologyDelegate(meshPrimitives[0], meshPrimitives[0].Positions.Values.Count() - 1, accessorType);
// Models avoiding restart
topologyDelegate(meshPrimitives[1], meshPrimitives[1].Positions.Values.Count() - 1, accessorType);
}));
}
}
GenerateUsedPropertiesList();
}
}
}