babylon export fiddles for demo 2

master
wgroeneveld 3 years ago
parent 54b857bd9b
commit ba64bc15c3

@ -1,16 +1,27 @@
/*-----
partially reverse-engineered https://david.blob.core.windows.net/softengine3d/part3/index.html
added compatibility with newer Babylon export formats
try this in your browser:
copy(BABYLON.SceneSerializer.SerializeMesh(BABYLON.MeshBuilder.CreateBox("bla", {}, scene), true, true))
What's possible with the MeshBuilder? See for example https://doc.babylonjs.com/how_to/polyhedra_shapes
------*/
var fs = require('fs');
var jsonObject = JSON.parse(fs.readFileSync('monkey.babylon', 'utf8'));
var args = process.argv.slice(2);
var jsonObject = JSON.parse(fs.readFileSync(args[0], 'utf8'));
var result =
"#include <libgba-sprite-engine/mesh.h>\n" +
"Mesh* createMesh() { \n" +
"\t auto obj = new Mesh();\n";
var meshes = 0;
var vertices = 0;
function addMesh(x, y, z) {
result += `\t obj->add(VectorFx::fromFloat(${x}, ${y}, ${z}));\n`;
meshes++;
vertices++;
}
var faces = 0;
function addFace(a, b, c) {
@ -26,11 +37,18 @@ function done() {
"}\n"
}
// partially reverse-engineered https://david.blob.core.windows.net/softengine3d/part3/index.html
for(var meshIndex = 0; meshIndex < jsonObject.meshes.length; meshIndex++) {
var verticesArray = jsonObject.meshes[meshIndex].vertices;
var indicesArray = jsonObject.meshes[meshIndex].indices;
var uvCount = jsonObject.meshes[meshIndex].uvCount;
const mesh = jsonObject.meshes[meshIndex];
var verticesArray = mesh.vertices;
var indicesArray = mesh.indices;
var uvCount = mesh.uvCount;
if(mesh.geometryId) {
const data = jsonObject.geometries.vertexData.find(v => v.id === mesh.geometryId);
verticesArray = data.positions
indicesArray = data.indices
// also contains .normals and .uvs
}
var verticesStep = 1;
switch(uvCount) {
case 0:
@ -42,6 +60,9 @@ for(var meshIndex = 0; meshIndex < jsonObject.meshes.length; meshIndex++) {
case 2:
verticesStep = 10;
break;
default:
verticesStep = 3;
break;
}
var verticesCount = verticesArray.length / verticesStep;
var facesCount = indicesArray.length / 3;
@ -51,19 +72,30 @@ for(var meshIndex = 0; meshIndex < jsonObject.meshes.length; meshIndex++) {
var y = verticesArray[index * verticesStep + 1];
var z = verticesArray[index * verticesStep + 2];
addMesh(x, y, z);
if(x !== undefined && y !== undefined && z !== undefined) {
addMesh(x, y, z);
} else {
console.log(`WARN; vertices index ${index} with step ${verticesStep} contains invalid data: ${x}, ${y}, ${z}`)
}
}
for(var index = 0; index < facesCount; index++) {
var a = indicesArray[index * 3];
var b = indicesArray[index * 3 + 1];
var c = indicesArray[index * 3 + 2];
addFace(a, b, c);
if(a !== undefined && b !== undefined && c !== undefined) {
addFace(a, b, c);
} else {
console.log(`WARN; indices index ${index} contains invalid data: ${a}, ${b}, ${c}`)
}
}
var position = jsonObject.meshes[meshIndex].position;
var position = mesh.position;
setPosition(position);
}
done();
fs.writeFileSync('src/mesh.cpp', result);
console.log(`mesh.cpp written; ${meshes} meshes and ${faces} faces. GLHF!`)
console.log(`mesh.cpp written; ${vertices} vertices and ${faces} faces. GLHF!`)
if(vertices > 800) {
console.log('WARNING lots of vertices detected, this will not run well...');
}

@ -0,0 +1,327 @@
{
"geometries": {
"boxes": [],
"spheres": [],
"cylinders": [],
"toruses": [],
"grounds": [],
"planes": [],
"torusKnots": [],
"vertexData": [
{
"id": "78db452a-92d6-428a-badf-e5561ecc884e",
"updatable": false,
"positions": [
0.5,
-0.5,
0.5,
-0.5,
-0.5,
0.5,
-0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
-0.5,
-0.5,
0.5,
-0.5,
-0.5,
-0.5,
-0.5,
0.5,
-0.5,
-0.5,
0.5,
0.5,
-0.5,
0.5,
-0.5,
-0.5,
0.5,
-0.5,
0.5,
0.5,
0.5,
0.5,
-0.5,
0.5,
0.5,
-0.5,
-0.5,
0.5,
-0.5,
-0.5,
-0.5,
-0.5,
0.5,
-0.5,
-0.5,
0.5,
0.5,
-0.5,
0.5,
-0.5,
0.5,
0.5,
-0.5,
0.5,
0.5,
0.5,
0.5,
-0.5,
0.5,
0.5,
-0.5,
-0.5,
-0.5,
-0.5,
-0.5,
-0.5,
-0.5,
0.5
],
"normals": [
0,
0,
1,
0,
0,
1,
0,
0,
1,
0,
0,
1,
0,
0,
-1,
0,
0,
-1,
0,
0,
-1,
0,
0,
-1,
1,
0,
0,
1,
0,
0,
1,
0,
0,
1,
0,
0,
-1,
0,
0,
-1,
0,
0,
-1,
0,
0,
-1,
0,
0,
0,
1,
0,
0,
1,
0,
0,
1,
0,
0,
1,
0,
0,
-1,
0,
0,
-1,
0,
0,
-1,
0,
0,
-1,
0
],
"uvs": [
1,
1,
0,
1,
0,
0,
1,
0,
1,
1,
0,
1,
0,
0,
1,
0,
1,
1,
0,
1,
0,
0,
1,
0,
1,
1,
0,
1,
0,
0,
1,
0,
1,
1,
0,
1,
0,
0,
1,
0,
1,
1,
0,
1,
0,
0,
1,
0
],
"indices": [
0,
1,
2,
0,
2,
3,
4,
5,
6,
4,
6,
7,
8,
9,
10,
8,
10,
11,
12,
13,
14,
12,
14,
15,
16,
17,
18,
16,
18,
19,
20,
21,
22,
20,
22,
23
]
}
]
},
"meshes": [
{
"name": "bla",
"id": "bla",
"type": "Mesh",
"position": [
0,
0,
0
],
"rotation": [
0,
0,
0
],
"scaling": [
1,
1,
1
],
"localMatrix": {
"0": 1,
"1": 0,
"2": 0,
"3": 0,
"4": 0,
"5": 1,
"6": 0,
"7": 0,
"8": 0,
"9": 0,
"10": 1,
"11": 0,
"12": 0,
"13": 0,
"14": 0,
"15": 1
},
"isEnabled": true,
"isVisible": true,
"infiniteDistance": false,
"pickable": true,
"receiveShadows": false,
"billboardMode": 0,
"visibility": 1,
"checkCollisions": false,
"isBlocker": false,
"overrideMaterialSideOrientation": null,
"isUnIndexed": false,
"geometryId": "78db452a-92d6-428a-badf-e5561ecc884e",
"subMeshes": [
{
"materialIndex": 0,
"verticesStart": 0,
"verticesCount": 24,
"indexStart": 0,
"indexCount": 36
}
],
"instances": [],
"animations": [],
"ranges": [],
"layerMask": 268435455,
"alphaIndex": 1.7976931348623157e+308,
"hasVertexAlpha": false,
"overlayAlpha": 0.5,
"overlayColor": [
1,
0,
0
],
"applyFog": true
}
]
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1,315 @@
{
"geometries": {
"boxes": [],
"spheres": [],
"cylinders": [],
"toruses": [],
"grounds": [],
"planes": [],
"torusKnots": [],
"vertexData": [
{
"id": "c08fbf04-7758-4154-ab43-744f5d69019e",
"updatable": false,
"positions": [
0,
0,
1.414214,
1.414214,
0,
0,
0,
1.414214,
0,
0,
0,
1.414214,
0,
1.414214,
0,
-1.414214,
0,
0,
0,
0,
1.414214,
-1.414214,
0,
0,
0,
-1.414214,
0,
0,
0,
1.414214,
0,
-1.414214,
0,
1.414214,
0,
0,
1.414214,
0,
0,
0,
-1.414214,
0,
0,
0,
-1.414214,
1.414214,
0,
0,
0,
0,
-1.414214,
0,
1.414214,
0,
0,
1.414214,
0,
0,
0,
-1.414214,
-1.414214,
0,
0,
-1.414214,
0,
0,
0,
0,
-1.414214,
0,
-1.414214,
0
],
"normals": [
0.5773502691896258,
0.5773502691896258,
0.5773502691896258,
0.5773502691896258,
0.5773502691896258,
0.5773502691896258,
0.5773502691896258,
0.5773502691896258,
0.5773502691896258,
-0.5773502691896258,
0.5773502691896258,
0.5773502691896258,
-0.5773502691896258,
0.5773502691896258,
0.5773502691896258,
-0.5773502691896258,
0.5773502691896258,
0.5773502691896258,
-0.5773502691896258,
-0.5773502691896258,
0.5773502691896258,
-0.5773502691896258,
-0.5773502691896258,
0.5773502691896258,
-0.5773502691896258,
-0.5773502691896258,
0.5773502691896258,
0.5773502691896258,
-0.5773502691896258,
0.5773502691896258,
0.5773502691896258,
-0.5773502691896258,
0.5773502691896258,
0.5773502691896258,
-0.5773502691896258,
0.5773502691896258,
0.5773502691896258,
-0.5773502691896258,
-0.5773502691896258,
0.5773502691896258,
-0.5773502691896258,
-0.5773502691896258,
0.5773502691896258,
-0.5773502691896258,
-0.5773502691896258,
0.5773502691896258,
0.5773502691896258,
-0.5773502691896258,
0.5773502691896258,
0.5773502691896258,
-0.5773502691896258,
0.5773502691896258,
0.5773502691896258,
-0.5773502691896258,
-0.5773502691896258,
0.5773502691896258,
-0.5773502691896258,
-0.5773502691896258,
0.5773502691896258,
-0.5773502691896258,
-0.5773502691896258,
0.5773502691896258,
-0.5773502691896258,
-0.5773502691896258,
-0.5773502691896258,
-0.5773502691896258,
-0.5773502691896258,
-0.5773502691896258,
-0.5773502691896258,
-0.5773502691896258,
-0.5773502691896258,
-0.5773502691896258
],
"uvs": [
1.3660254037844384,
0,
-0.3660254037844384,
-1.1102230246251565e-16,
0.4999999999999998,
-1.4999999999999996,
1.3660254037844384,
0,
-0.3660254037844384,
-1.1102230246251565e-16,
0.4999999999999998,
-1.4999999999999996,
1.3660254037844384,
0,
-0.3660254037844384,
-1.1102230246251565e-16,
0.4999999999999998,
-1.4999999999999996,
1.3660254037844384,
0,
-0.3660254037844384,
-1.1102230246251565e-16,
0.4999999999999998,
-1.4999999999999996,
1.3660254037844384,
0,
-0.3660254037844384,
-1.1102230246251565e-16,
0.4999999999999998,
-1.4999999999999996,
1.3660254037844384,
0,
-0.3660254037844384,
-1.1102230246251565e-16,
0.4999999999999998,
-1.4999999999999996,
1.3660254037844384,
0,
-0.3660254037844384,
-1.1102230246251565e-16,
0.4999999999999998,
-1.4999999999999996,
1.3660254037844384,
0,
-0.3660254037844384,
-1.1102230246251565e-16,
0.4999999999999998,
-1.4999999999999996
],
"indices": [
0,
2,
1,
3,
5,
4,
6,
8,
7,
9,
11,
10,
12,
14,
13,
15,
17,
16,
18,
20,
19,
21,
23,
22
]
}
]
},
"meshes": [
{
"name": "oct",
"id": "oct",
"type": "Mesh",
"position": [
0,
0,
0
],
"rotation": [
0,
0,
0
],
"scaling": [
1,
1,
1
],
"localMatrix": {
"0": 1,
"1": 0,
"2": 0,
"3": 0,
"4": 0,
"5": 1,
"6": 0,
"7": 0,
"8": 0,
"9": 0,
"10": 1,
"11": 0,
"12": 0,
"13": 0,
"14": 0,
"15": 1
},
"isEnabled": true,
"isVisible": true,
"infiniteDistance": false,
"pickable": true,
"receiveShadows": false,
"billboardMode": 0,
"visibility": 1,
"checkCollisions": false,
"isBlocker": false,
"overrideMaterialSideOrientation": null,
"isUnIndexed": false,
"geometryId": "c08fbf04-7758-4154-ab43-744f5d69019e",
"subMeshes": [
{
"materialIndex": 0,
"verticesStart": 0,
"verticesCount": 24,
"indexStart": 0,
"indexCount": 24
}
],
"instances": [],
"animations": [],
"ranges": [],
"layerMask": 268435455,
"alphaIndex": 1.7976931348623157e+308,
"hasVertexAlpha": false,
"overlayAlpha": 0.5,
"overlayColor": [
1,
0,
0
],
"applyFog": true
}
]
}

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save