@@ -14,15 +14,15 @@ namespace AssetGenerator.Runtime
14
14
/// </summary>
15
15
internal class GLTFConverter
16
16
{
17
- private static List < glTFLoader . Schema . Buffer > Buffers { get ; set ; }
18
- private static List < glTFLoader . Schema . BufferView > BufferViews { get ; set ; }
19
- private static List < glTFLoader . Schema . Accessor > Accessors { get ; set ; }
20
- private static List < glTFLoader . Schema . Material > Materials { get ; set ; }
21
- private static List < glTFLoader . Schema . Node > Nodes { get ; set ; }
17
+ private static List < glTFLoader . Schema . Buffer > Buffers { get ; set ; }
18
+ private static List < glTFLoader . Schema . BufferView > BufferViews { get ; set ; }
19
+ private static List < glTFLoader . Schema . Accessor > Accessors { get ; set ; }
20
+ private static List < glTFLoader . Schema . Material > Materials { get ; set ; }
21
+ private static List < glTFLoader . Schema . Node > Nodes { get ; set ; }
22
22
private static List < glTFLoader . Schema . Scene > Scenes { get ; set ; }
23
- private static List < glTFLoader . Schema . Image > Images { get ; set ; }
24
- private static List < glTFLoader . Schema . Sampler > Samplers { get ; set ; }
25
- private static List < glTFLoader . Schema . Texture > Textures { get ; set ; }
23
+ private static List < glTFLoader . Schema . Image > Images { get ; set ; }
24
+ private static List < glTFLoader . Schema . Sampler > Samplers { get ; set ; }
25
+ private static List < glTFLoader . Schema . Texture > Textures { get ; set ; }
26
26
private static List < glTFLoader . Schema . Mesh > Meshes { get ; set ; }
27
27
28
28
/// <summary>
@@ -49,7 +49,7 @@ private static void initBuffers()
49
49
Samplers = new List < glTFLoader . Schema . Sampler > ( ) ;
50
50
Textures = new List < glTFLoader . Schema . Texture > ( ) ;
51
51
Meshes = new List < glTFLoader . Schema . Mesh > ( ) ;
52
- }
52
+ }
53
53
54
54
/// <summary>
55
55
/// Converts Runtime GLTF to Schema GLTF object.
@@ -482,7 +482,7 @@ private static int ConvertNodeToSchema(Runtime.Node runtimeNode, Runtime.GLTF gl
482
482
{
483
483
node . Translation = runtimeNode . Translation . Value . ToArray ( ) ;
484
484
}
485
-
485
+
486
486
if ( runtimeNode . Children != null )
487
487
{
488
488
var childrenIndices = new List < int > ( ) ;
@@ -779,7 +779,7 @@ private static glTFLoader.Schema.Material ConvertMaterialToSchema(Runtime.Materi
779
779
default :
780
780
throw new NotImplementedException ( "Extension schema conversion not implemented for " + extension . Name ) ;
781
781
}
782
-
782
+
783
783
if ( gltf . ExtensionsUsed == null )
784
784
{
785
785
gltf . ExtensionsUsed = new List < string > ( new [ ] { extension . Name } ) ;
@@ -1008,7 +1008,6 @@ private static Dictionary<string, int> InterleaveMeshPrimitiveAttributes(Runtime
1008
1008
}
1009
1009
if ( meshPrimitive . Colors != null )
1010
1010
{
1011
-
1012
1011
// if not multiple of 4, add padding
1013
1012
totalByteLength = Align ( geometryData , totalByteLength , 4 ) ;
1014
1013
int colorOffset = totalByteLength ;
@@ -1044,7 +1043,7 @@ private static Dictionary<string, int> InterleaveMeshPrimitiveAttributes(Runtime
1044
1043
break ;
1045
1044
default :
1046
1045
throw new NotImplementedException ( "Color component type " + meshPrimitive . ColorComponentType + " not supported!" ) ;
1047
-
1046
+
1048
1047
}
1049
1048
var colorAccessor = CreateAccessor ( bufferviewIndex , colorOffset , colorAccessorComponentType , count , "Color Accessor" , null , null , vectorType , normalized ) ;
1050
1049
Accessors . Add ( colorAccessor ) ;
@@ -1054,7 +1053,7 @@ private static Dictionary<string, int> InterleaveMeshPrimitiveAttributes(Runtime
1054
1053
}
1055
1054
// if not multiple of 4, add padding
1056
1055
totalByteLength = Align ( geometryData , totalByteLength , 4 ) ;
1057
-
1056
+
1058
1057
if ( i == 0 )
1059
1058
{
1060
1059
bufferView . ByteStride = totalByteLength ;
@@ -1070,36 +1069,34 @@ private static int WriteTextureCoords(MeshPrimitive meshPrimitive, List<Vector2>
1070
1069
{
1071
1070
int byteLength = 0 ;
1072
1071
int offset = ( int ) geometryData . Writer . BaseStream . Position ;
1073
-
1072
+
1074
1073
int count = max - min + 1 ;
1075
1074
Vector2 [ ] tcs = textureCoordSet . ToArray ( ) ;
1075
+ byteLength = 8 * count ;
1076
1076
switch ( meshPrimitive . TextureCoordsComponentType )
1077
1077
{
1078
1078
case MeshPrimitive . TextureCoordsComponentTypeEnum . FLOAT :
1079
- byteLength = sizeof ( float ) * 2 * count ;
1080
1079
for ( int i = min ; i <= max ; ++ i )
1081
1080
{
1082
1081
geometryData . Writer . Write ( tcs [ i ] ) ;
1083
1082
}
1084
1083
break ;
1085
1084
case MeshPrimitive . TextureCoordsComponentTypeEnum . NORMALIZED_UBYTE :
1086
- byteLength = sizeof ( byte ) * 2 * count ;
1087
1085
for ( int i = min ; i <= max ; ++ i )
1088
1086
{
1089
1087
geometryData . Writer . Write ( Convert . ToByte ( Math . Round ( tcs [ i ] . X * byte . MaxValue ) ) ) ;
1090
1088
geometryData . Writer . Write ( Convert . ToByte ( Math . Round ( tcs [ i ] . Y * byte . MaxValue ) ) ) ;
1089
+ Align ( geometryData , 2 , 4 ) ;
1091
1090
}
1092
1091
break ;
1093
1092
case MeshPrimitive . TextureCoordsComponentTypeEnum . NORMALIZED_USHORT :
1094
- byteLength = sizeof ( ushort ) * 2 * count ;
1095
1093
for ( int i = min ; i <= max ; ++ i )
1096
1094
{
1097
-
1098
1095
geometryData . Writer . Write ( Convert . ToUInt16 ( Math . Round ( tcs [ i ] . X * ushort . MaxValue ) ) ) ;
1099
1096
geometryData . Writer . Write ( Convert . ToUInt16 ( Math . Round ( tcs [ i ] . Y * ushort . MaxValue ) ) ) ;
1100
1097
}
1101
1098
break ;
1102
- default :
1099
+ default :
1103
1100
throw new NotImplementedException ( "Byte length calculation not implemented for TextureCoordsComponentType: " + meshPrimitive . TextureCoordsComponentType ) ;
1104
1101
}
1105
1102
byteLength = ( int ) geometryData . Writer . BaseStream . Position - offset ;
@@ -1112,26 +1109,24 @@ private static int WriteColors(MeshPrimitive meshPrimitive, int min, int max, Da
1112
1109
int byteLength = 0 ;
1113
1110
int count = max - min + 1 ;
1114
1111
int vectorSize = meshPrimitive . ColorType == MeshPrimitive . ColorTypeEnum . VEC3 ? 3 : 4 ;
1112
+ byteLength = 0 ;
1115
1113
1116
1114
switch ( meshPrimitive . ColorComponentType )
1117
1115
{
1118
1116
case MeshPrimitive . ColorComponentTypeEnum . NORMALIZED_UBYTE :
1119
- byteLength = sizeof ( byte ) * vectorSize * count ;
1120
-
1121
1117
for ( int i = min ; i <= max ; ++ i )
1122
1118
{
1123
1119
geometryData . Writer . Write ( Convert . ToByte ( Math . Round ( meshPrimitive . Colors [ i ] . X * byte . MaxValue ) ) ) ;
1124
1120
geometryData . Writer . Write ( Convert . ToByte ( Math . Round ( meshPrimitive . Colors [ i ] . Y * byte . MaxValue ) ) ) ;
1125
1121
geometryData . Writer . Write ( Convert . ToByte ( Math . Round ( meshPrimitive . Colors [ i ] . Z * byte . MaxValue ) ) ) ;
1126
- if ( meshPrimitive . ColorType == MeshPrimitive . ColorTypeEnum . VEC4 )
1122
+ if ( meshPrimitive . ColorType == MeshPrimitive . ColorTypeEnum . VEC4 )
1127
1123
{
1128
1124
geometryData . Writer . Write ( Convert . ToByte ( Math . Round ( meshPrimitive . Colors [ i ] . W * byte . MaxValue ) ) ) ;
1129
1125
}
1126
+ byteLength += Align ( geometryData , vectorSize , 4 ) ;
1130
1127
}
1131
1128
break ;
1132
1129
case MeshPrimitive . ColorComponentTypeEnum . NORMALIZED_USHORT :
1133
- byteLength = sizeof ( ushort ) * vectorSize * count ;
1134
-
1135
1130
for ( int i = min ; i <= max ; ++ i )
1136
1131
{
1137
1132
geometryData . Writer . Write ( Convert . ToUInt16 ( Math . Round ( meshPrimitive . Colors [ i ] . X * ushort . MaxValue ) ) ) ;
@@ -1142,11 +1137,10 @@ private static int WriteColors(MeshPrimitive meshPrimitive, int min, int max, Da
1142
1137
{
1143
1138
geometryData . Writer . Write ( Convert . ToUInt16 ( Math . Round ( meshPrimitive . Colors [ i ] . W * ushort . MaxValue ) ) ) ;
1144
1139
}
1140
+ byteLength += Align ( geometryData , 2 * vectorSize , 4 ) ;
1145
1141
}
1146
1142
break ;
1147
1143
case MeshPrimitive . ColorComponentTypeEnum . FLOAT :
1148
- byteLength = sizeof ( float ) * vectorSize * count ;
1149
-
1150
1144
for ( int i = min ; i <= max ; ++ i )
1151
1145
{
1152
1146
geometryData . Writer . Write ( meshPrimitive . Colors [ i ] . X ) ;
@@ -1157,6 +1151,7 @@ private static int WriteColors(MeshPrimitive meshPrimitive, int min, int max, Da
1157
1151
{
1158
1152
geometryData . Writer . Write ( meshPrimitive . Colors [ i ] . W ) ;
1159
1153
}
1154
+ byteLength += Align ( geometryData , 4 * vectorSize , 4 ) ;
1160
1155
}
1161
1156
break ;
1162
1157
}
@@ -1248,28 +1243,34 @@ private static glTFLoader.Schema.MeshPrimitive ConvertMeshPrimitiveToSchema(Runt
1248
1243
var colorAccessorComponentType = glTFLoader . Schema . Accessor . ComponentTypeEnum . FLOAT ;
1249
1244
var colorAccessorType = runtimeMeshPrimitive . ColorType == MeshPrimitive . ColorTypeEnum . VEC3 ? glTFLoader . Schema . Accessor . TypeEnum . VEC3 : glTFLoader . Schema . Accessor . TypeEnum . VEC4 ;
1250
1245
int vectorSize = runtimeMeshPrimitive . ColorType == MeshPrimitive . ColorTypeEnum . VEC3 ? 3 : 4 ;
1251
- int byteLength = 0 ;
1252
1246
1253
1247
// Create BufferView
1254
1248
int byteOffset = ( int ) geometryData . Writer . BaseStream . Position ;
1255
- byteLength = WriteColors ( runtimeMeshPrimitive , 0 , runtimeMeshPrimitive . Colors . Count ( ) - 1 , geometryData ) ;
1256
1249
1250
+ int byteLength = WriteColors ( runtimeMeshPrimitive , 0 , runtimeMeshPrimitive . Colors . Count ( ) - 1 , geometryData ) ;
1251
+ int ? byteStride = null ;
1257
1252
switch ( runtimeMeshPrimitive . ColorComponentType )
1258
1253
{
1259
1254
case MeshPrimitive . ColorComponentTypeEnum . NORMALIZED_UBYTE :
1260
1255
colorAccessorComponentType = glTFLoader . Schema . Accessor . ComponentTypeEnum . UNSIGNED_BYTE ;
1261
- byteLength = sizeof ( byte ) * vectorSize * runtimeMeshPrimitive . Colors . Count ( ) ;
1256
+ if ( vectorSize == 3 )
1257
+ {
1258
+ byteStride = 4 ;
1259
+ }
1262
1260
break ;
1263
1261
case MeshPrimitive . ColorComponentTypeEnum . NORMALIZED_USHORT :
1264
1262
colorAccessorComponentType = glTFLoader . Schema . Accessor . ComponentTypeEnum . UNSIGNED_SHORT ;
1265
- byteLength = sizeof ( ushort ) * vectorSize * runtimeMeshPrimitive . Colors . Count ( ) ;
1263
+ if ( vectorSize == 3 )
1264
+ {
1265
+ byteStride = 8 ;
1266
+ }
1266
1267
break ;
1267
1268
default : //Default to ColorComponentTypeEnum.FLOAT:
1268
1269
colorAccessorComponentType = glTFLoader . Schema . Accessor . ComponentTypeEnum . FLOAT ;
1269
- byteLength = sizeof ( float ) * vectorSize * runtimeMeshPrimitive . Colors . Count ( ) ;
1270
1270
break ;
1271
1271
}
1272
- var bufferView = CreateBufferView ( bufferIndex , "Colors" , byteLength , byteOffset , null ) ;
1272
+
1273
+ var bufferView = CreateBufferView ( bufferIndex , "Colors" , byteLength , byteOffset , byteStride ) ;
1273
1274
BufferViews . Add ( bufferView ) ;
1274
1275
int bufferviewIndex = BufferViews . Count ( ) - 1 ;
1275
1276
@@ -1282,14 +1283,13 @@ private static glTFLoader.Schema.MeshPrimitive ConvertMeshPrimitiveToSchema(Runt
1282
1283
if ( normalized )
1283
1284
{
1284
1285
// Pad any additional bytes if byteLength is not a multiple of 4
1285
- Align ( geometryData , byteLength , 4 ) ;
1286
+ Align ( geometryData , byteLength , 4 ) ;
1286
1287
}
1287
1288
}
1288
1289
if ( runtimeMeshPrimitive . TextureCoordSets != null )
1289
1290
{
1290
1291
for ( int i = 0 ; i < runtimeMeshPrimitive . TextureCoordSets . Count ; ++ i )
1291
1292
{
1292
-
1293
1293
List < Vector2 > textureCoordSet = runtimeMeshPrimitive . TextureCoordSets [ i ] ;
1294
1294
int byteOffset = ( int ) geometryData . Writer . BaseStream . Position ;
1295
1295
int byteLength = WriteTextureCoords ( runtimeMeshPrimitive , textureCoordSet , 0 , runtimeMeshPrimitive . TextureCoordSets [ i ] . Count ( ) - 1 , geometryData ) ;
@@ -1298,13 +1298,15 @@ private static glTFLoader.Schema.MeshPrimitive ConvertMeshPrimitiveToSchema(Runt
1298
1298
glTFLoader . Schema . Accessor . ComponentTypeEnum accessorComponentType ;
1299
1299
// we normalize only if the texture cood accessor type is not float
1300
1300
bool normalized = runtimeMeshPrimitive . TextureCoordsComponentType != MeshPrimitive . TextureCoordsComponentTypeEnum . FLOAT ;
1301
+ int ? byteStride = null ;
1301
1302
switch ( runtimeMeshPrimitive . TextureCoordsComponentType )
1302
1303
{
1303
1304
case MeshPrimitive . TextureCoordsComponentTypeEnum . FLOAT :
1304
1305
accessorComponentType = glTFLoader . Schema . Accessor . ComponentTypeEnum . FLOAT ;
1305
1306
break ;
1306
1307
case MeshPrimitive . TextureCoordsComponentTypeEnum . NORMALIZED_UBYTE :
1307
1308
accessorComponentType = glTFLoader . Schema . Accessor . ComponentTypeEnum . UNSIGNED_BYTE ;
1309
+ byteStride = 4 ;
1308
1310
break ;
1309
1311
case MeshPrimitive . TextureCoordsComponentTypeEnum . NORMALIZED_USHORT :
1310
1312
accessorComponentType = glTFLoader . Schema . Accessor . ComponentTypeEnum . UNSIGNED_SHORT ;
@@ -1314,7 +1316,7 @@ private static glTFLoader.Schema.MeshPrimitive ConvertMeshPrimitiveToSchema(Runt
1314
1316
break ;
1315
1317
}
1316
1318
1317
- var bufferView = CreateBufferView ( bufferIndex , "Texture Coords " + i , byteLength , byteOffset , null ) ;
1319
+ var bufferView = CreateBufferView ( bufferIndex , "Texture Coords " + i , byteLength , byteOffset , byteStride ) ;
1318
1320
BufferViews . Add ( bufferView ) ;
1319
1321
int bufferviewIndex = BufferViews . Count ( ) - 1 ;
1320
1322
// Create Accessor
@@ -1388,8 +1390,6 @@ private static glTFLoader.Schema.MeshPrimitive ConvertMeshPrimitiveToSchema(Runt
1388
1390
1389
1391
mPrimitive . Indices = Accessors . Count ( ) - 1 ;
1390
1392
}
1391
-
1392
-
1393
1393
1394
1394
mPrimitive . Attributes = attributes ;
1395
1395
if ( runtimeMeshPrimitive . Material != null )
0 commit comments