Fix image uris and use better naming for separate resources

This commit is contained in:
Sean Lilley 2016-06-23 22:01:36 -04:00
parent 462c80fbc0
commit 139f473785
3 changed files with 27 additions and 12 deletions

View File

@ -104,7 +104,7 @@ function createGltf(data, inputPath, modelName, done) {
vertexByteStride = sizeOfFloat32 * 3; vertexByteStride = sizeOfFloat32 * 3;
} }
var bufferId = 'buffer_' + modelName; var bufferId = modelName + '_buffer';
var bufferViewVertexId = 'bufferView_vertex'; var bufferViewVertexId = 'bufferView_vertex';
var bufferViewIndexId = 'bufferView_index'; var bufferViewIndexId = 'bufferView_index';
var accessorPositionId = 'accessor_position'; var accessorPositionId = 'accessor_position';
@ -131,7 +131,7 @@ function createGltf(data, inputPath, modelName, done) {
} }
function getImageId(image) { function getImageId(image) {
return 'image_' + path.basename(image).substr(0, image.lastIndexOf('.')); return path.basename(image, path.extname(image));
} }
var gltf = { var gltf = {

View File

@ -19,19 +19,39 @@ function getChannels(colorType) {
} }
} }
function getUriType(extension) {
switch (extension) {
case 'png':
return 'data:image/png';
case 'jpg':
return 'data:image/jpeg';
case 'jpeg':
return 'data:image/jpeg';
case 'gif':
return 'data:image/gif';
default:
return 'data:image/' + extension;
}
}
function loadImage(imagePath, done) { function loadImage(imagePath, done) {
fs.readFile(imagePath, function(error, data) { fs.readFile(imagePath, function(error, data) {
if (error) { if (error) {
throw(error); throw(error);
} }
var extension = path.extname(imagePath).slice(1);
var uriType = getUriType(extension);
var uri = uriType + ';base64,' + data.toString('base64');
var info = { var info = {
transparent : false, transparent: false,
channels : 3, channels: 3,
data : data data: data,
uri: uri
}; };
if (path.extname(imagePath) === '.png') { if (path.extname(imagePath) === 'png') {
// Color type is encoded in the 25th bit of the png // Color type is encoded in the 25th bit of the png
var colorType = data[25]; var colorType = data[25];
var channels = getChannels(colorType); var channels = getChannels(colorType);

View File

@ -268,12 +268,7 @@ function getImages(inputPath, materials, done) {
imagePath = path.join(inputPath, image); imagePath = path.join(inputPath, image);
} }
loadImage(imagePath, function(info) { loadImage(imagePath, function(info) {
var uri = 'data:application/octet-stream;base64,' + info.data.toString('base64'); imagesInfo[image] = info;
imagesInfo[image] = {
transparent : info.transparent,
channels : info.channels,
uri : uri
};
callback(); callback();
}); });
}, function (error) { }, function (error) {