Read custom properties from glTF elements #368
This commit is contained in:
parent
d449753349
commit
fefee144bd
@ -641,33 +641,41 @@ export class ImporterGltf extends ImporterBase
|
||||
}
|
||||
}
|
||||
|
||||
this.ImportNodes (gltf);
|
||||
this.ImportModelProperties (gltf);
|
||||
this.ImportProperties (this.model, gltf.asset, 'Asset properties');
|
||||
this.ImportScene (gltf);
|
||||
}
|
||||
|
||||
ImportModelProperties (gltf)
|
||||
ImportProperties (modelObject, gltfObject, propertyGroupName)
|
||||
{
|
||||
function ImportProperties (model, propertyGroupName, propertyObject)
|
||||
{
|
||||
let propertyGroup = new PropertyGroup (propertyGroupName);
|
||||
for (let propertyName in propertyObject) {
|
||||
if (Object.prototype.hasOwnProperty.call (propertyObject, propertyName)) {
|
||||
if (typeof propertyObject[propertyName] === 'string') {
|
||||
const property = new Property (PropertyType.Text, propertyName, propertyObject[propertyName]);
|
||||
propertyGroup.AddProperty (property);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (propertyGroup.PropertyCount () > 0) {
|
||||
model.AddPropertyGroup (propertyGroup);
|
||||
}
|
||||
return propertyGroup;
|
||||
if (gltfObject === undefined || gltfObject === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
ImportProperties (this.model, 'Asset properties', gltf.asset);
|
||||
if (gltf.asset['extras']) {
|
||||
ImportProperties (this.model, 'Extras', gltf.asset['extras']);
|
||||
let propertyGroup = new PropertyGroup (propertyGroupName);
|
||||
for (let propertyName in gltfObject) {
|
||||
if (Object.prototype.hasOwnProperty.call (gltfObject, propertyName)) {
|
||||
let property = null;
|
||||
let propertyValue = gltfObject[propertyName];
|
||||
if (typeof propertyValue === 'string') {
|
||||
property = new Property (PropertyType.Text, propertyName, propertyValue);
|
||||
} else if (typeof propertyValue === 'number') {
|
||||
if (Number.isInteger (propertyValue)) {
|
||||
property = new Property (PropertyType.Integer, propertyName, propertyValue);
|
||||
} else {
|
||||
property = new Property (PropertyType.Number, propertyName, propertyValue);
|
||||
}
|
||||
}
|
||||
if (property !== null) {
|
||||
propertyGroup.AddProperty (property);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (propertyGroup.PropertyCount () === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
modelObject.AddPropertyGroup (propertyGroup);
|
||||
}
|
||||
|
||||
GetDefaultScene (gltf)
|
||||
@ -790,6 +798,7 @@ export class ImporterGltf extends ImporterBase
|
||||
ImportMesh (gltf, gltfMesh)
|
||||
{
|
||||
let mesh = new Mesh ();
|
||||
|
||||
this.model.AddMesh (mesh);
|
||||
if (gltfMesh.name !== undefined) {
|
||||
mesh.SetName (gltfMesh.name);
|
||||
@ -799,6 +808,8 @@ export class ImporterGltf extends ImporterBase
|
||||
let primitive = gltfMesh.primitives[i];
|
||||
this.ImportPrimitive (gltf, primitive, mesh);
|
||||
}
|
||||
|
||||
this.ImportProperties (mesh, gltfMesh.extras, 'Mesh properties');
|
||||
}
|
||||
|
||||
ImportPrimitive (gltf, primitive, mesh)
|
||||
@ -982,17 +993,20 @@ export class ImporterGltf extends ImporterBase
|
||||
mesh.AddTriangle (triangle);
|
||||
}
|
||||
|
||||
ImportNodes (gltf)
|
||||
ImportScene (gltf)
|
||||
{
|
||||
let scene = this.GetDefaultScene (gltf);
|
||||
if (scene === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
let rootNode = this.model.GetRootNode ();
|
||||
for (let nodeIndex of scene.nodes) {
|
||||
let gltfNode = gltf.nodes[nodeIndex];
|
||||
this.ImportNode (gltf, gltfNode, rootNode);
|
||||
}
|
||||
|
||||
this.ImportProperties (this.model, scene.extras, 'Scene properties');
|
||||
}
|
||||
|
||||
ImportNode (gltf, gltfNode, parentNode)
|
||||
@ -1046,6 +1060,8 @@ export class ImporterGltf extends ImporterBase
|
||||
if (gltfNode.children === undefined || gltfNode.children.length === 0) {
|
||||
node.SetType (NodeType.MeshNode);
|
||||
}
|
||||
let mesh = this.model.GetMesh (gltfNode.mesh);
|
||||
this.ImportProperties (mesh, gltfNode.extras, 'Node properties');
|
||||
node.AddMeshIndex (gltfNode.mesh);
|
||||
}
|
||||
}
|
||||
|
||||
BIN
test/testfiles/gltf/Properties/properties.glb
Normal file
BIN
test/testfiles/gltf/Properties/properties.glb
Normal file
Binary file not shown.
306
test/testfiles/gltf/Properties/properties.gltf
Normal file
306
test/testfiles/gltf/Properties/properties.gltf
Normal file
@ -0,0 +1,306 @@
|
||||
{
|
||||
"asset" : {
|
||||
"generator" : "Khronos glTF Blender I/O v3.4.49",
|
||||
"version" : "2.0"
|
||||
},
|
||||
"scene" : 0,
|
||||
"scenes" : [
|
||||
{
|
||||
"extras" : {
|
||||
"Scene Property String" : "scene example",
|
||||
"Scene Property Integer" : 1,
|
||||
"Scene Property Float" : 0.5
|
||||
},
|
||||
"name" : "Scene",
|
||||
"nodes" : [
|
||||
0,
|
||||
1,
|
||||
2
|
||||
]
|
||||
}
|
||||
],
|
||||
"nodes" : [
|
||||
{
|
||||
"extras" : {
|
||||
"Node Property Float" : 0.5,
|
||||
"Node Property String" : "node example"
|
||||
},
|
||||
"mesh" : 0,
|
||||
"name" : "Cube 1 Node"
|
||||
},
|
||||
{
|
||||
"mesh" : 1,
|
||||
"name" : "Cube 2 Node",
|
||||
"translation" : [
|
||||
0,
|
||||
0,
|
||||
-3.0047976970672607
|
||||
]
|
||||
},
|
||||
{
|
||||
"mesh" : 2,
|
||||
"name" : "Cube 3 Node",
|
||||
"translation" : [
|
||||
-2.802919626235962,
|
||||
0,
|
||||
0
|
||||
]
|
||||
}
|
||||
],
|
||||
"materials" : [
|
||||
{
|
||||
"doubleSided" : true,
|
||||
"extras" : {
|
||||
"Material Property Float" : 1.0,
|
||||
"Material Property String" : "material example"
|
||||
},
|
||||
"name" : "Material 1",
|
||||
"pbrMetallicRoughness" : {
|
||||
"baseColorFactor" : [
|
||||
0.800000011920929,
|
||||
0,
|
||||
0,
|
||||
1
|
||||
],
|
||||
"metallicFactor" : 0,
|
||||
"roughnessFactor" : 0.5
|
||||
}
|
||||
},
|
||||
{
|
||||
"doubleSided" : true,
|
||||
"name" : "Material 2",
|
||||
"pbrMetallicRoughness" : {
|
||||
"baseColorFactor" : [
|
||||
0,
|
||||
0.800000011920929,
|
||||
0,
|
||||
1
|
||||
],
|
||||
"metallicFactor" : 0,
|
||||
"roughnessFactor" : 0.5
|
||||
}
|
||||
},
|
||||
{
|
||||
"doubleSided" : true,
|
||||
"name" : "Material 3",
|
||||
"pbrMetallicRoughness" : {
|
||||
"baseColorFactor" : [
|
||||
0,
|
||||
0,
|
||||
0.800000011920929,
|
||||
1
|
||||
],
|
||||
"metallicFactor" : 0,
|
||||
"roughnessFactor" : 0.5
|
||||
}
|
||||
}
|
||||
],
|
||||
"meshes" : [
|
||||
{
|
||||
"extras" : {
|
||||
"Mesh Property Float" : 0.5,
|
||||
"Mesh Property String" : "mesh example"
|
||||
},
|
||||
"name" : "Cube 1",
|
||||
"primitives" : [
|
||||
{
|
||||
"attributes" : {
|
||||
"POSITION" : 0,
|
||||
"TEXCOORD_0" : 1,
|
||||
"NORMAL" : 2
|
||||
},
|
||||
"indices" : 3,
|
||||
"material" : 0
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name" : "Cube 2",
|
||||
"primitives" : [
|
||||
{
|
||||
"attributes" : {
|
||||
"POSITION" : 4,
|
||||
"TEXCOORD_0" : 5,
|
||||
"NORMAL" : 6
|
||||
},
|
||||
"indices" : 3,
|
||||
"material" : 1
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name" : "Cube 3",
|
||||
"primitives" : [
|
||||
{
|
||||
"attributes" : {
|
||||
"POSITION" : 7,
|
||||
"TEXCOORD_0" : 8,
|
||||
"NORMAL" : 9
|
||||
},
|
||||
"indices" : 3,
|
||||
"material" : 2
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"accessors" : [
|
||||
{
|
||||
"bufferView" : 0,
|
||||
"componentType" : 5126,
|
||||
"count" : 24,
|
||||
"max" : [
|
||||
1,
|
||||
1,
|
||||
1
|
||||
],
|
||||
"min" : [
|
||||
-1,
|
||||
-1,
|
||||
-1
|
||||
],
|
||||
"type" : "VEC3"
|
||||
},
|
||||
{
|
||||
"bufferView" : 1,
|
||||
"componentType" : 5126,
|
||||
"count" : 24,
|
||||
"type" : "VEC2"
|
||||
},
|
||||
{
|
||||
"bufferView" : 2,
|
||||
"componentType" : 5126,
|
||||
"count" : 24,
|
||||
"type" : "VEC3"
|
||||
},
|
||||
{
|
||||
"bufferView" : 3,
|
||||
"componentType" : 5123,
|
||||
"count" : 36,
|
||||
"type" : "SCALAR"
|
||||
},
|
||||
{
|
||||
"bufferView" : 4,
|
||||
"componentType" : 5126,
|
||||
"count" : 24,
|
||||
"max" : [
|
||||
1,
|
||||
1,
|
||||
1
|
||||
],
|
||||
"min" : [
|
||||
-1,
|
||||
-1,
|
||||
-1
|
||||
],
|
||||
"type" : "VEC3"
|
||||
},
|
||||
{
|
||||
"bufferView" : 5,
|
||||
"componentType" : 5126,
|
||||
"count" : 24,
|
||||
"type" : "VEC2"
|
||||
},
|
||||
{
|
||||
"bufferView" : 6,
|
||||
"componentType" : 5126,
|
||||
"count" : 24,
|
||||
"type" : "VEC3"
|
||||
},
|
||||
{
|
||||
"bufferView" : 7,
|
||||
"componentType" : 5126,
|
||||
"count" : 24,
|
||||
"max" : [
|
||||
1,
|
||||
1,
|
||||
1
|
||||
],
|
||||
"min" : [
|
||||
-1,
|
||||
-1,
|
||||
-1
|
||||
],
|
||||
"type" : "VEC3"
|
||||
},
|
||||
{
|
||||
"bufferView" : 8,
|
||||
"componentType" : 5126,
|
||||
"count" : 24,
|
||||
"type" : "VEC2"
|
||||
},
|
||||
{
|
||||
"bufferView" : 9,
|
||||
"componentType" : 5126,
|
||||
"count" : 24,
|
||||
"type" : "VEC3"
|
||||
}
|
||||
],
|
||||
"bufferViews" : [
|
||||
{
|
||||
"buffer" : 0,
|
||||
"byteLength" : 288,
|
||||
"byteOffset" : 0,
|
||||
"target" : 34962
|
||||
},
|
||||
{
|
||||
"buffer" : 0,
|
||||
"byteLength" : 192,
|
||||
"byteOffset" : 288,
|
||||
"target" : 34962
|
||||
},
|
||||
{
|
||||
"buffer" : 0,
|
||||
"byteLength" : 288,
|
||||
"byteOffset" : 480,
|
||||
"target" : 34962
|
||||
},
|
||||
{
|
||||
"buffer" : 0,
|
||||
"byteLength" : 72,
|
||||
"byteOffset" : 768,
|
||||
"target" : 34963
|
||||
},
|
||||
{
|
||||
"buffer" : 0,
|
||||
"byteLength" : 288,
|
||||
"byteOffset" : 840,
|
||||
"target" : 34962
|
||||
},
|
||||
{
|
||||
"buffer" : 0,
|
||||
"byteLength" : 192,
|
||||
"byteOffset" : 1128,
|
||||
"target" : 34962
|
||||
},
|
||||
{
|
||||
"buffer" : 0,
|
||||
"byteLength" : 288,
|
||||
"byteOffset" : 1320,
|
||||
"target" : 34962
|
||||
},
|
||||
{
|
||||
"buffer" : 0,
|
||||
"byteLength" : 288,
|
||||
"byteOffset" : 1608,
|
||||
"target" : 34962
|
||||
},
|
||||
{
|
||||
"buffer" : 0,
|
||||
"byteLength" : 192,
|
||||
"byteOffset" : 1896,
|
||||
"target" : 34962
|
||||
},
|
||||
{
|
||||
"buffer" : 0,
|
||||
"byteLength" : 288,
|
||||
"byteOffset" : 2088,
|
||||
"target" : 34962
|
||||
}
|
||||
],
|
||||
"buffers" : [
|
||||
{
|
||||
"byteLength" : 2376,
|
||||
"uri" : "data:application/octet-stream;base64,AACAPwAAgD8AAIC/AACAPwAAgD8AAIC/AACAPwAAgD8AAIC/AACAPwAAgL8AAIC/AACAPwAAgL8AAIC/AACAPwAAgL8AAIC/AACAPwAAgD8AAIA/AACAPwAAgD8AAIA/AACAPwAAgD8AAIA/AACAPwAAgL8AAIA/AACAPwAAgL8AAIA/AACAPwAAgL8AAIA/AACAvwAAgD8AAIC/AACAvwAAgD8AAIC/AACAvwAAgD8AAIC/AACAvwAAgL8AAIC/AACAvwAAgL8AAIC/AACAvwAAgL8AAIC/AACAvwAAgD8AAIA/AAC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/AACAvwAAgL8AAIA/AACAvwAAgL8AAIA/AAAgPwAAAD8AACA/AAAAPwAAID8AAAA/AADAPgAAAD8AAMA+AAAAPwAAwD4AAAA/AAAgPwAAgD4AACA/AACAPgAAID8AAIA+AADAPgAAgD4AAMA+AACAPgAAwD4AAIA+AAAgPwAAQD8AACA/AABAPwAAYD8AAAA/AAAAPgAAAD8AAMA+AABAPwAAwD4AAEA/AAAgPwAAAAAAACA/AACAPwAAYD8AAIA+AAAAPgAAgD4AAMA+AAAAAAAAwD4AAIA/AAAAAAAAAAAAAIC/AAAAAAAAgD8AAACAAACAPwAAAAAAAACAAAAAAAAAgL8AAACAAAAAAAAAAAAAAIC/AACAPwAAAAAAAACAAAAAAAAAAAAAAIA/AAAAAAAAgD8AAACAAACAPwAAAAAAAACAAAAAAAAAgL8AAACAAAAAAAAAAAAAAIA/AACAPwAAAAAAAACAAACAvwAAAAAAAACAAAAAAAAAAAAAAIC/AAAAAAAAgD8AAACAAAAAAAAAgL8AAACAAACAvwAAAAAAAACAAAAAAAAAAAAAAIC/AAAAAAAAAAAAAIA/AACAvwAAAAAAAACAAAAAAAAAgD8AAACAAAAAAAAAgL8AAACAAAAAAAAAAAAAAIA/AACAvwAAAAAAAACA"
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -656,6 +656,33 @@ describe ('Gltf Importer', function () {
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it ('Properties', function (done) {
|
||||
let testFileList = [
|
||||
['Properties', 'properties.gltf'],
|
||||
['Properties', 'properties.glb']
|
||||
];
|
||||
let processed = 0;
|
||||
for (let i = 0; i < testFileList.length; i++) {
|
||||
let testFile = testFileList[i];
|
||||
ImportGltfFile (testFile[0], testFile[1], function (model) {
|
||||
assert.ok (OV.CheckModel (model));
|
||||
assert.equal (model.PropertyGroupCount (), 2);
|
||||
assert.equal (model.GetPropertyGroup (0).PropertyCount (), 2);
|
||||
assert.equal (model.GetPropertyGroup (1).PropertyCount (), 3);
|
||||
|
||||
let firstMesh = model.GetMesh (0);
|
||||
assert.equal (firstMesh.PropertyGroupCount (), 2);
|
||||
assert.equal (firstMesh.GetPropertyGroup (0).PropertyCount (), 2);
|
||||
assert.equal (firstMesh.GetPropertyGroup (1).PropertyCount (), 2);
|
||||
|
||||
processed += 1;
|
||||
if (processed == testFileList.length) {
|
||||
done ();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user