use of org.bimserver.plugins.serializers.SerializerException in project GltfSerializers by opensourceBIM.
the class BinaryGltfSerializer method generateSceneAndBody.
private void generateSceneAndBody() throws SerializerException {
int totalBodyByteLength = 0;
int totalIndicesByteLength = 0;
int totalVerticesByteLength = 0;
int totalNormalsByteLength = 0;
int totalColorsByteLength = 0;
int maxIndexValues = 16389;
for (IfcProduct ifcProduct : model.getAllWithSubTypes(IfcProduct.class)) {
GeometryInfo geometryInfo = ifcProduct.getGeometry();
if (!ifcProduct.eClass().getName().equals("IfcOpeningElement") && geometryInfo != null && geometryInfo.getData().getVertices().length > 0) {
GeometryData data = geometryInfo.getData();
int nrIndicesBytes = data.getIndices().length;
totalIndicesByteLength += nrIndicesBytes / 2;
if (nrIndicesBytes > 4 * maxIndexValues) {
int nrIndices = nrIndicesBytes / 4;
totalVerticesByteLength += nrIndices * 3 * 4;
totalNormalsByteLength += nrIndices * 3 * 4;
if (data.getMaterials() != null) {
totalColorsByteLength += nrIndices * 4 * 4;
}
} else {
totalVerticesByteLength += data.getVertices().length;
totalNormalsByteLength += data.getNormals().length;
if (data.getMaterials() != null) {
totalColorsByteLength += data.getMaterials().length;
}
}
}
}
totalBodyByteLength = totalIndicesByteLength + totalVerticesByteLength + totalNormalsByteLength + totalColorsByteLength;
body = ByteBuffer.allocate(totalBodyByteLength + materialColorFragmentShaderBytes.length + materialColorVertexShaderBytes.length + vertexColorFragmentShaderBytes.length + vertexColorVertexShaderBytes.length);
body.order(ByteOrder.LITTLE_ENDIAN);
ByteBuffer newIndicesBuffer = ByteBuffer.allocate(totalIndicesByteLength);
newIndicesBuffer.order(ByteOrder.LITTLE_ENDIAN);
ByteBuffer newVerticesBuffer = ByteBuffer.allocate(totalVerticesByteLength);
newVerticesBuffer.order(ByteOrder.LITTLE_ENDIAN);
ByteBuffer newNormalsBuffer = ByteBuffer.allocate(totalNormalsByteLength);
newNormalsBuffer.order(ByteOrder.LITTLE_ENDIAN);
ByteBuffer newColorsBuffer = ByteBuffer.allocate(totalColorsByteLength);
newColorsBuffer.order(ByteOrder.LITTLE_ENDIAN);
String indicesBufferView = createBufferView(totalIndicesByteLength, 0, ELEMENT_ARRAY_BUFFER);
String verticesBufferView = createBufferView(totalVerticesByteLength, totalIndicesByteLength, ARRAY_BUFFER);
String normalsBufferView = createBufferView(totalNormalsByteLength, totalIndicesByteLength + totalVerticesByteLength, ARRAY_BUFFER);
String colorsBufferView = null;
scenesNode.set("defaultScene", createDefaultScene());
createModelNode();
for (IfcProduct ifcProduct : model.getAllWithSubTypes(IfcProduct.class)) {
GeometryInfo geometryInfo = ifcProduct.getGeometry();
if (!ifcProduct.eClass().getName().equals("IfcOpeningElement") && geometryInfo != null) {
ByteBuffer matrixByteBuffer = ByteBuffer.wrap(ifcProduct.getGeometry().getTransformation());
matrixByteBuffer.order(ByteOrder.LITTLE_ENDIAN);
DoubleBuffer doubleBuffer = matrixByteBuffer.asDoubleBuffer();
float[] matrix = new float[16];
for (int i = 0; i < doubleBuffer.capacity(); i++) {
matrix[i] = (float) doubleBuffer.get();
}
updateExtends(geometryInfo, matrix);
}
}
float[] offsets = getOffsets();
// This will "normalize" the model by moving it's axis-aligned bounding box center to the 0-point. This will always be the wrong position, but at least the building will be close to the 0-point
modelTranslation.add(-offsets[0]);
modelTranslation.add(-offsets[1]);
modelTranslation.add(-offsets[2]);
for (IfcProduct ifcProduct : model.getAllWithSubTypes(IfcProduct.class)) {
GeometryInfo geometryInfo = ifcProduct.getGeometry();
if (!ifcProduct.eClass().getName().equals("IfcOpeningElement") && geometryInfo != null && geometryInfo.getData().getVertices().length > 0) {
int startPositionIndices = newIndicesBuffer.position();
int startPositionVertices = newVerticesBuffer.position();
int startPositionNormals = newNormalsBuffer.position();
int startPositionColors = newColorsBuffer.position();
GeometryData data = geometryInfo.getData();
ByteBuffer indicesBuffer = ByteBuffer.wrap(data.getIndices());
indicesBuffer.order(ByteOrder.LITTLE_ENDIAN);
IntBuffer indicesIntBuffer = indicesBuffer.asIntBuffer();
ByteBuffer verticesBuffer = ByteBuffer.wrap(data.getVertices());
verticesBuffer.order(ByteOrder.LITTLE_ENDIAN);
FloatBuffer verticesFloatBuffer = verticesBuffer.asFloatBuffer();
ByteBuffer normalsBuffer = ByteBuffer.wrap(data.getNormals());
normalsBuffer.order(ByteOrder.LITTLE_ENDIAN);
FloatBuffer normalsFloatBuffer = normalsBuffer.asFloatBuffer();
FloatBuffer materialsFloatBuffer = null;
if (data.getMaterials() != null) {
ByteBuffer materialsBuffer = ByteBuffer.wrap(data.getMaterials());
materialsBuffer.order(ByteOrder.LITTLE_ENDIAN);
materialsFloatBuffer = materialsBuffer.asFloatBuffer();
}
if (data.getIndices().length > 4 * maxIndexValues) {
int totalNrIndices = indicesIntBuffer.capacity();
int nrParts = (totalNrIndices + maxIndexValues - 1) / maxIndexValues;
ArrayNode primitivesNode = OBJECT_MAPPER.createArrayNode();
for (int part = 0; part < nrParts; part++) {
startPositionIndices = newIndicesBuffer.position();
startPositionVertices = newVerticesBuffer.position();
startPositionNormals = newNormalsBuffer.position();
startPositionColors = newColorsBuffer.position();
short indexCounter = 0;
int upto = Math.min((part + 1) * maxIndexValues, totalNrIndices);
for (int i = part * maxIndexValues; i < upto; i++) {
newIndicesBuffer.putShort(indexCounter++);
}
int nrVertices = upto - part * maxIndexValues;
for (int i = part * maxIndexValues; i < upto; i += 3) {
int oldIndex1 = indicesIntBuffer.get(i);
int oldIndex2 = indicesIntBuffer.get(i + 1);
int oldIndex3 = indicesIntBuffer.get(i + 2);
newVerticesBuffer.putFloat(verticesFloatBuffer.get(oldIndex1 * 3));
newVerticesBuffer.putFloat(verticesFloatBuffer.get(oldIndex1 * 3 + 1));
newVerticesBuffer.putFloat(verticesFloatBuffer.get(oldIndex1 * 3 + 2));
newVerticesBuffer.putFloat(verticesFloatBuffer.get(oldIndex2 * 3));
newVerticesBuffer.putFloat(verticesFloatBuffer.get(oldIndex2 * 3 + 1));
newVerticesBuffer.putFloat(verticesFloatBuffer.get(oldIndex2 * 3 + 2));
newVerticesBuffer.putFloat(verticesFloatBuffer.get(oldIndex3 * 3));
newVerticesBuffer.putFloat(verticesFloatBuffer.get(oldIndex3 * 3 + 1));
newVerticesBuffer.putFloat(verticesFloatBuffer.get(oldIndex3 * 3 + 2));
}
for (int i = part * maxIndexValues; i < upto; i += 3) {
int oldIndex1 = indicesIntBuffer.get(i);
int oldIndex2 = indicesIntBuffer.get(i + 1);
int oldIndex3 = indicesIntBuffer.get(i + 2);
newNormalsBuffer.putFloat(normalsFloatBuffer.get(oldIndex1 * 3));
newNormalsBuffer.putFloat(normalsFloatBuffer.get(oldIndex1 * 3 + 1));
newNormalsBuffer.putFloat(normalsFloatBuffer.get(oldIndex1 * 3 + 2));
newNormalsBuffer.putFloat(normalsFloatBuffer.get(oldIndex2 * 3));
newNormalsBuffer.putFloat(normalsFloatBuffer.get(oldIndex2 * 3 + 1));
newNormalsBuffer.putFloat(normalsFloatBuffer.get(oldIndex2 * 3 + 2));
newNormalsBuffer.putFloat(normalsFloatBuffer.get(oldIndex3 * 3));
newNormalsBuffer.putFloat(normalsFloatBuffer.get(oldIndex3 * 3 + 1));
newNormalsBuffer.putFloat(normalsFloatBuffer.get(oldIndex3 * 3 + 2));
}
if (materialsFloatBuffer != null) {
for (int i = part * maxIndexValues; i < upto; i += 3) {
int oldIndex1 = indicesIntBuffer.get(i);
int oldIndex2 = indicesIntBuffer.get(i + 1);
int oldIndex3 = indicesIntBuffer.get(i + 2);
newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex1 * 4));
newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex1 * 4 + 1));
newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex1 * 4 + 2));
newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex1 * 4 + 3));
newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex2 * 4));
newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex2 * 4 + 1));
newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex2 * 4 + 2));
newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex2 * 4 + 3));
newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex3 * 4));
newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex3 * 4 + 1));
newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex3 * 4 + 2));
newColorsBuffer.putFloat(materialsFloatBuffer.get(oldIndex3 * 4 + 3));
}
}
ObjectNode primitiveNode = OBJECT_MAPPER.createObjectNode();
String indicesAccessor = addIndicesAccessor(ifcProduct, indicesBufferView, startPositionIndices, nrVertices / 3);
String verticesAccessor = addVerticesAccessor(ifcProduct, verticesBufferView, startPositionVertices, nrVertices);
String normalsAccessor = addNormalsAccessor(ifcProduct, normalsBufferView, startPositionNormals, nrVertices);
String colorAccessor = null;
if (data.getMaterials() != null) {
if (colorsBufferView == null) {
colorsBufferView = createBufferView(totalColorsByteLength, totalIndicesByteLength + totalVerticesByteLength + totalNormalsByteLength, ARRAY_BUFFER);
}
colorAccessor = addColorsAccessor(ifcProduct, colorsBufferView, startPositionColors, nrVertices * 4 / 3);
}
primitivesNode.add(primitiveNode);
primitiveNode.put("indices", indicesAccessor);
primitiveNode.put("mode", TRIANGLES);
ObjectNode attributesNode = OBJECT_MAPPER.createObjectNode();
primitiveNode.set("attributes", attributesNode);
attributesNode.put("NORMAL", normalsAccessor);
attributesNode.put("POSITION", verticesAccessor);
if (colorAccessor != null) {
attributesNode.put("COLOR", colorAccessor);
primitiveNode.put("material", VERTEX_COLOR_MATERIAL);
} else {
primitiveNode.put("material", createOrGetMaterial(ifcProduct.eClass().getName(), IfcColors.getDefaultColor(ifcProduct.eClass().getName())));
}
}
String meshName = addMesh(ifcProduct, primitivesNode);
String nodeName = addNode(meshName, ifcProduct);
translationChildrenNode.add(nodeName);
} else {
for (int i = 0; i < indicesIntBuffer.capacity(); i++) {
int index = indicesIntBuffer.get(i);
if (index > Short.MAX_VALUE) {
throw new SerializerException("Index too large to store as short " + index);
}
newIndicesBuffer.putShort((short) (index));
}
newVerticesBuffer.put(data.getVertices());
newNormalsBuffer.put(data.getNormals());
if (data.getMaterials() != null) {
newColorsBuffer.put(data.getMaterials());
}
int totalNrIndices = indicesIntBuffer.capacity();
ArrayNode primitivesNode = OBJECT_MAPPER.createArrayNode();
ObjectNode primitiveNode = OBJECT_MAPPER.createObjectNode();
String indicesAccessor = addIndicesAccessor(ifcProduct, indicesBufferView, startPositionIndices, totalNrIndices);
String verticesAccessor = addVerticesAccessor(ifcProduct, verticesBufferView, startPositionVertices, data.getVertices().length / 4);
String normalsAccessor = addNormalsAccessor(ifcProduct, normalsBufferView, startPositionNormals, data.getNormals().length / 4);
String colorAccessor = null;
if (data.getMaterials() != null) {
if (colorsBufferView == null) {
colorsBufferView = createBufferView(totalColorsByteLength, totalIndicesByteLength + totalVerticesByteLength + totalNormalsByteLength, ARRAY_BUFFER);
}
colorAccessor = addColorsAccessor(ifcProduct, colorsBufferView, startPositionColors, data.getVertices().length / 4);
}
primitivesNode.add(primitiveNode);
primitiveNode.put("indices", indicesAccessor);
primitiveNode.put("mode", TRIANGLES);
ObjectNode attributesNode = OBJECT_MAPPER.createObjectNode();
primitiveNode.set("attributes", attributesNode);
attributesNode.put("NORMAL", normalsAccessor);
attributesNode.put("POSITION", verticesAccessor);
if (colorAccessor != null) {
attributesNode.put("COLOR", colorAccessor);
primitiveNode.put("material", VERTEX_COLOR_MATERIAL);
} else {
primitiveNode.put("material", createOrGetMaterial(ifcProduct.eClass().getName(), IfcColors.getDefaultColor(ifcProduct.eClass().getName())));
}
String meshName = addMesh(ifcProduct, primitivesNode);
String nodeName = addNode(meshName, ifcProduct);
translationChildrenNode.add(nodeName);
}
}
}
if (newIndicesBuffer.position() != newIndicesBuffer.capacity()) {
throw new SerializerException("Not all space used");
}
if (newVerticesBuffer.position() != newVerticesBuffer.capacity()) {
throw new SerializerException("Not all space used");
}
if (newNormalsBuffer.position() != newNormalsBuffer.capacity()) {
throw new SerializerException("Not all space used");
}
if (newColorsBuffer.position() != newColorsBuffer.capacity()) {
throw new SerializerException("Not all space used");
}
newIndicesBuffer.position(0);
newVerticesBuffer.position(0);
newNormalsBuffer.position(0);
newColorsBuffer.position(0);
body.put(newIndicesBuffer);
body.put(newVerticesBuffer);
body.put(newNormalsBuffer);
body.put(newColorsBuffer);
String vertexColorFragmentShaderBufferViewName = createBufferView(vertexColorFragmentShaderBytes.length, body.position());
body.put(vertexColorFragmentShaderBytes);
String vertexColorVertexShaderBufferViewName = createBufferView(vertexColorVertexShaderBytes.length, body.position());
body.put(vertexColorVertexShaderBytes);
String materialColorFragmentShaderBufferViewName = createBufferView(materialColorFragmentShaderBytes.length, body.position());
body.put(materialColorFragmentShaderBytes);
String materialColorVertexShaderBufferViewName = createBufferView(materialColorVertexShaderBytes.length, body.position());
body.put(materialColorVertexShaderBytes);
gltfNode.set("animations", createAnimations());
gltfNode.set("asset", createAsset());
gltfNode.set("programs", createPrograms());
gltfNode.put("scene", "defaultScene");
gltfNode.set("skins", createSkins());
gltfNode.set("techniques", createTechniques());
createVertexColorShaders(vertexColorFragmentShaderBufferViewName, vertexColorVertexShaderBufferViewName);
createMaterialColorShaders(materialColorFragmentShaderBufferViewName, materialColorVertexShaderBufferViewName);
addBuffer("binary_glTF", "arraybuffer", body.capacity());
ArrayNode extensions = OBJECT_MAPPER.createArrayNode();
extensions.add("KHR_binary_glTF");
gltfNode.set("extensionsUsed", extensions);
}
Aggregations