toJSON method
dynamic
toJSON()
Implementation
toJSON() {
Map<String, dynamic> data = {
"metadata": {
"version": 4.5,
"type": 'Geometry',
"generator": 'Geometry.toJSON'
}
};
// standard Geometry serialization
data["uuid"] = this.uuid;
data["type"] = this.type;
if (this.name != '') data["name"] = this.name;
print(" Geometry tojson todo ");
// if ( this.parameters != null ) {
// var parameters = this.parameters;
// for ( var key in parameters ) {
// if ( parameters[ key ] != null ) data[ key ] = parameters[ key ];
// }
// return data;
// }
var vertices = [];
for (var i = 0; i < this.vertices.length; i++) {
var vertex = this.vertices[i];
vertices.addAll([vertex.x, vertex.y, vertex.z]);
}
var faces = [];
var normals = [];
var normalsHash = {};
var colors = [];
var colorsHash = {};
var uvs = [];
var uvsHash = {};
setBit(value, position, enabled) {
return enabled ? value | (1 << position) : value & (~(1 << position));
}
getNormalIndex(normal) {
var hash =
normal.x.toString() + normal.y.toString() + normal.z.toString();
if (normalsHash[hash] != null) {
return normalsHash[hash];
}
normalsHash[hash] = normals.length / 3;
normals.addAll([normal.x, normal.y, normal.z]);
return normalsHash[hash];
}
getColorIndex(color) {
var hash = color.r.toString() + color.g.toString() + color.b.toString();
if (colorsHash[hash] != null) {
return colorsHash[hash];
}
colorsHash[hash] = colors.length;
colors.add(color.getHex());
return colorsHash[hash];
}
getUvIndex(uv) {
var hash = uv.x.toString() + uv.y.toString();
if (uvsHash[hash] != null) {
return uvsHash[hash];
}
uvsHash[hash] = uvs.length / 2;
uvs.addAll([uv.x, uv.y]);
return uvsHash[hash];
}
for (var i = 0; i < this.faces.length; i++) {
var face = this.faces[i];
var hasMaterial = true;
var hasFaceUv = false; // deprecated
var hasFaceVertexUv = this.faceVertexUvs[0][i] != null;
var hasFaceNormal = face.normal.length() > 0;
var hasFaceVertexNormal = face.vertexNormals.length > 0;
var hasFaceColor =
face.color.r != 1 || face.color.g != 1 || face.color.b != 1;
var hasFaceVertexColor = face.vertexColors.length > 0;
var faceType = 0;
faceType = setBit(faceType, 0, 0); // isQuad
faceType = setBit(faceType, 1, hasMaterial);
faceType = setBit(faceType, 2, hasFaceUv);
faceType = setBit(faceType, 3, hasFaceVertexUv);
faceType = setBit(faceType, 4, hasFaceNormal);
faceType = setBit(faceType, 5, hasFaceVertexNormal);
faceType = setBit(faceType, 6, hasFaceColor);
faceType = setBit(faceType, 7, hasFaceVertexColor);
faces.add(faceType);
faces.addAll([face.a, face.b, face.c]);
faces.add(face.materialIndex);
if (hasFaceVertexUv) {
var faceVertexUvs = this.faceVertexUvs[0][i];
faces.addAll([
getUvIndex(faceVertexUvs[0]),
getUvIndex(faceVertexUvs[1]),
getUvIndex(faceVertexUvs[2])
]);
}
if (hasFaceNormal) {
faces.add(getNormalIndex(face.normal));
}
if (hasFaceVertexNormal) {
var vertexNormals = face.vertexNormals;
faces.addAll([
getNormalIndex(vertexNormals[0]),
getNormalIndex(vertexNormals[1]),
getNormalIndex(vertexNormals[2])
]);
}
if (hasFaceColor) {
faces.add(getColorIndex(face.color));
}
if (hasFaceVertexColor) {
var vertexColors = face.vertexColors;
faces.addAll([
getColorIndex(vertexColors[0]),
getColorIndex(vertexColors[1]),
getColorIndex(vertexColors[2])
]);
}
}
data["data"] = {};
data["data"].vertices = vertices;
data["data"].normals = normals;
if (colors.length > 0) data["data"].colors = colors;
if (uvs.length > 0)
data["data"].uvs = [uvs]; // temporal backward compatibility
data["data"].faces = faces;
return data;
}