Hi everyone
I want to write a large amount of gltf models with different attributes using Cesium Geometry.
Previously, I succeeded in generate gltf using Three.js.
What I did is to modify the gltf’s accessors, bufferviews, buffers and other related attributes to match the geometry that created by Three.js. And I created a lot of Cylinder gltf.
But some of the geometry is created more convient by Cesium Geometry than Three.js, e.g. polygon on the surface of the ellipsoid.
But I met problem when I try to generate a sample gltf. my code in nodejs is below:
var Cesium = require(‘Cesium’);
var fs = require(‘fs’);
/* Create a extruded triangle */
var polygon = new Cesium.PolygonGeometry({
polygonHierarchy : new Cesium.PolygonHierarchy(
Cesium.Cartesian3.fromDegreesArray([
0,90,
72,74.2421,
0,74.2421
])
),
extrudedHeight: 10000
});
var geometry = Cesium.PolygonGeometry.createGeometry(polygon);
//load a gltf template, the template itselt works
var templ = fs.readFileSync(‘box.gltf’);
var templobj = JSON.parse(templ);
var obj = templobj;
//write indices accessor and bufferview
var accessor_21 = obj.accessors.accessor_21;
accessor_21.count = geometry.indices.length;
var bufferView_29 = obj.bufferViews.bufferView_29;
bufferView_29.byteLength = geometry.indices.buffer.byteLength;
bufferView_29.byteOffset = 0;
var indexBuffer = new Buffer(geometry.indices.buffer);
var indexBase64 = indexBuffer.toString(‘base64’);
//write position
var accessor_23 = obj.accessors.accessor_23;
accessor_23.count = geometry.attributes.position.values.length;
var positionBuffer = new Buffer(geometry.attributes.position.values.buffer);
var positionBase64 = positionBuffer.toString(‘base64’);
//write normal
var accessor_25 = obj.accessors.accessor_25;
accessor_25.count = geometry.attributes.normal.values.length;
accessor_25.byteOffset = positionBuffer.length;
var normalBuffer = new Buffer(geometry.attributes.normal.values.buffer);
var normalBase64 = normalBuffer.toString(‘base64’);
//write positions and normals bufferView
var bufferView_30 = obj.bufferViews.bufferView_30;
bufferView_30.byteOffset = indexBuffer.length;
bufferView_30.byteLength = positionBuffer.length+normalBuffer.length;
//write Buffer
var buffer = Buffer.concat([indexBuffer,positionBuffer,normalBuffer]);
var bufferBase64 = buffer.toString(‘base64’);
var boxBuffer = obj.buffers.Box;
boxBuffer.byteLength = buffer.length;
boxBuffer.uri = “data:application/octet-stream;base64,”+bufferBase64;
//write into a file
var newgltf = JSON.stringify(obj);
fs.writeFileSync(‘triangle.gltf’, newgltf);
``
My code can generate a gltf file, but it can’t be optimized by gltf-pipeline, and show the errors below:
Unhandled rejection RangeError: Index out of range
at checkOffset (buffer.js:817:11)
at Buffer.readFloatLE (buffer.js:994:5)
at readBufferComponent (G:\host\3d-tiles-creator\gltf-pipeline\lib\readBufferComponent.js:29:27)
at findAccessorMinMax (G:\host\3d-tiles-creator\gltf-pipeline\lib\findAccessorMinMax.js:46:33)
at accessorDefaults (G:\host\3d-tiles-creator\gltf-pipeline\lib\addDefaults.js:27:30)
at addDefaults (G:\host\3d-tiles-creator\gltf-pipeline\lib\addDefaults.js:537:5)
at Function.Pipeline.processJSONWithExtras (G:\host\3d-tiles-creator\gltf-pipeline\lib\Pipeline.js:89:5)
at G:\host\3d-tiles-creator\gltf-pipeline\lib\Pipeline.js:219:29
at tryCatcher (G:\host\3d-tiles-creator\gltf-pipeline\node_modules.3.4.7@bluebird\js\release\util.js:16:23)
at Promise._settlePromiseFromHandler (G:\host\3d-tiles-creator\gltf-pipeline\node_modules.3.4.7@bluebird\js\release\promise.js:510:31)
at Promise._settlePromise (G:\host\3d-tiles-creator\gltf-pipeline\node_modules.3.4.7@bluebird\js\release\promise.js:567:18)
at Promise._settlePromise0 (G:\host\3d-tiles-creator\gltf-pipeline\node_modules.3.4.7@bluebird\js\release\promise.js:612:10)
at Promise._settlePromises (G:\host\3d-tiles-creator\gltf-pipeline\node_modules.3.4.7@bluebird\js\release\promise.js:691:18)
at Async._drainQueue (G:\host\3d-tiles-creator\gltf-pipeline\node_modules.3.4.7@bluebird\js\release\async.js:133:16)
at Async._drainQueues (G:\host\3d-tiles-creator\gltf-pipeline\node_modules.3.4.7@bluebird\js\release\async.js:143:10)
at Immediate.Async.drainQueues (G:\host\3d-tiles-creator\gltf-pipeline\node_modules.3.4.7@bluebird\js\release\async.js:17:14)