-
Notifications
You must be signed in to change notification settings - Fork 254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Web3d quantized attributes #56
Changes from 38 commits
0960548
78caba2
14b5491
ff81e8f
f83a5df
21ab7a6
8588875
4b83b4d
0f66720
8eaedab
36e5e07
8596b8e
9a893aa
4b2c2b5
c5c4845
9183718
92280c8
cc98b05
9ad9112
e045dd3
d0569ce
6a17276
0397d54
f4be044
466a802
d0c4214
f134385
be8790e
e228777
09d39da
deb1c54
5d7e99f
c4d3626
6071c9e
84dbc41
24dca8b
2ab58d3
74f46e8
539fc23
05d8ad5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use strict'; | ||
var Cesium = require('cesium'); | ||
var defined = Cesium.defined; | ||
|
||
module.exports = addExtensionsUsed; | ||
|
||
function addExtensionsUsed(gltf, extension) { | ||
var extensionsUsed = gltf.extensionsUsed; | ||
if (!defined(extensionsUsed)) { | ||
extensionsUsed = []; | ||
gltf.extensionsUsed = extensionsUsed; | ||
} | ||
if (extensionsUsed.indexOf(extension) < 0) { | ||
extensionsUsed.push(extension); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
'use strict'; | ||
var Cesium = require('cesium'); | ||
var defined = Cesium.defined; | ||
var defaultValue = Cesium.defaultValue; | ||
var sizeOf = require('image-size'); | ||
var mime = require('mime'); | ||
var mergeBuffers = require('./mergeBuffers'); | ||
var removePipelineExtras = require('./removePipelineExtras'); | ||
|
||
module.exports = getBinaryGltf; | ||
|
||
//Update object with KHR_binary_glTF properties and add to body and bufferViews | ||
function updateBinaryObject(gltf, pipelineExtras, name, state) { | ||
var bufferViews = gltf.bufferViews; | ||
var objects = gltf[name]; | ||
if (defined(objects)) { | ||
for (var objectId in objects) { | ||
if (objects.hasOwnProperty(objectId)) { | ||
var object = objects[objectId]; | ||
|
||
//Update object with binary format | ||
object.uri = 'data:,'; | ||
object.extensions = defaultValue(object.extensions, {}); | ||
object.extensions.KHR_binary_glTF = defaultValue(object.extensions.KHR_binary_glTF, {}); | ||
var KHR_binary_glTF = object.extensions.KHR_binary_glTF; | ||
|
||
//Create a bufferView based on the byte length and current offset | ||
var bufferViewKeys = Object.keys(bufferViews); | ||
while (bufferViewKeys.indexOf('binary_bufferView' + state.currentBinaryView) != -1) { | ||
state.currentBinaryView++; | ||
} | ||
|
||
var objectSource = object.extras._pipeline.source; | ||
var bufferViewId = 'binary_bufferView' + state.currentBinaryView; | ||
KHR_binary_glTF.bufferView = bufferViewId; //Create bufferview | ||
bufferViews[bufferViewId] = { | ||
buffer : 'binary_glTF', | ||
byteLength : objectSource.length, | ||
byteOffset : state.offset | ||
}; | ||
state.offset += objectSource.length; | ||
|
||
//Append the object source to the binary body | ||
pipelineExtras.source = Buffer.concat([pipelineExtras.source, objectSource]); | ||
|
||
//Add additional properties for images | ||
if (name === 'images') { | ||
KHR_binary_glTF.mimeType = mime.lookup(object.extras._pipeline.extension); | ||
|
||
var dimensions = sizeOf(object.extras._pipeline.source); | ||
KHR_binary_glTF.width = dimensions.width; | ||
KHR_binary_glTF.height = dimensions.height; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
function getBinaryGltf(gltf, callback) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there unit tests for this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are unit tests for writeBinaryGltf which uses this directly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good, can you double check the coverage and update if needed? |
||
//Create the special binary buffer from the existing buffers | ||
gltf.bufferViews = defaultValue(gltf.bufferViews, {}); | ||
gltf.buffers = defaultValue(gltf.buffers, {}); | ||
mergeBuffers(gltf, 'binary_glTF'); | ||
|
||
var buffers = gltf.buffers; | ||
var currentOffset = buffers.binary_glTF.byteLength; | ||
var pipelineExtras = buffers.binary_glTF.extras._pipeline; | ||
var state = { | ||
offset : currentOffset, | ||
currentBinaryView : 0 | ||
}; | ||
|
||
updateBinaryObject(gltf, pipelineExtras, 'shaders', state); | ||
updateBinaryObject(gltf, pipelineExtras, 'images', state); | ||
|
||
var body = buffers.binary_glTF.extras._pipeline.source; | ||
buffers.binary_glTF.byteLength = state.offset; | ||
|
||
//Remove extras objects before writing | ||
removePipelineExtras(gltf); | ||
|
||
//Create padded binary scene string and calculate total length | ||
var sceneString = JSON.stringify(gltf); | ||
var sceneLength = Buffer.byteLength(sceneString); | ||
sceneLength += 4 - (sceneLength % 4); | ||
var padding = new Array(sceneLength + 1).join(' '); | ||
sceneString = (sceneString + padding).substring(0, sceneLength); | ||
var bodyOffset = 20 + sceneLength; | ||
var glbLength = bodyOffset + body.length; | ||
|
||
//Write binary glTF header (magic, version, length, sceneLength, sceneFormat) | ||
var header = new Buffer(20); | ||
header.write('glTF', 0); | ||
header.writeUInt32LE(1, 4); | ||
header.writeUInt32LE(glbLength, 8); | ||
header.writeUInt32LE(sceneLength, 12); | ||
header.writeUInt32LE(0, 16); | ||
|
||
//Create scene buffer and overall buffer | ||
var scene = new Buffer(sceneString); | ||
var glb = Buffer.concat([header, scene, body], glbLength); | ||
|
||
if (callback) { | ||
process.nextTick(function () { | ||
callback(header, scene, body); | ||
}); | ||
} | ||
return glb; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also update README.md please.