obj2gltf/lib/loadTexture.js

128 lines
3.9 KiB
JavaScript
Raw Permalink Normal View History

2017-03-13 15:28:51 -04:00
'use strict';
2019-02-05 20:59:09 -05:00
const Cesium = require('cesium');
const fsExtra = require('fs-extra');
const jpeg = require('jpeg-js');
const path = require('path');
const PNG = require('pngjs').PNG;
const Promise = require('bluebird');
const Texture = require('./Texture');
2016-07-22 14:09:13 -04:00
2019-02-05 20:59:09 -05:00
const defaultValue = Cesium.defaultValue;
const defined = Cesium.defined;
2017-03-13 15:28:51 -04:00
module.exports = loadTexture;
2015-10-16 17:32:23 -04:00
2017-03-13 15:28:51 -04:00
/**
* Load a texture file.
2017-03-13 15:28:51 -04:00
*
* @param {String} texturePath Path to the texture file.
* @param {Object} [options] An object with the following properties:
2017-04-18 11:56:08 -04:00
* @param {Boolean} [options.checkTransparency=false] Do a more exhaustive check for texture transparency by looking at the alpha channel of each pixel.
* @param {Boolean} [options.decode=false] Whether to decode the texture.
* @returns {Promise} A promise resolving to a Texture object.
2017-03-13 15:28:51 -04:00
*
* @private
*/
function loadTexture(texturePath, options) {
2017-04-18 11:56:08 -04:00
options = defaultValue(options, {});
options.checkTransparency = defaultValue(options.checkTransparency, false);
options.decode = defaultValue(options.decode, false);
return fsExtra.readFile(texturePath)
.then(function(source) {
2019-02-05 20:59:09 -05:00
const name = path.basename(texturePath, path.extname(texturePath));
const extension = path.extname(texturePath).toLowerCase();
const texture = new Texture();
texture.source = source;
texture.name = name;
texture.extension = extension;
texture.path = texturePath;
2017-03-13 15:28:51 -04:00
2019-02-05 20:59:09 -05:00
let decodePromise;
2017-03-13 15:28:51 -04:00
if (extension === '.png') {
decodePromise = decodePng(texture, options);
2017-04-18 11:56:08 -04:00
} else if (extension === '.jpg' || extension === '.jpeg') {
decodePromise = decodeJpeg(texture, options);
2017-07-28 16:56:28 -04:00
}
if (defined(decodePromise)) {
return decodePromise.thenReturn(texture);
2017-03-13 15:28:51 -04:00
}
return texture;
2017-03-13 15:28:51 -04:00
});
}
function hasTransparency(pixels) {
2019-02-05 20:59:09 -05:00
const pixelsLength = pixels.length / 4;
for (let i = 0; i < pixelsLength; ++i) {
2017-04-18 11:56:08 -04:00
if (pixels[i * 4 + 3] < 255) {
return true;
2017-04-10 17:57:56 -04:00
}
}
2017-04-18 11:56:08 -04:00
return false;
}
2015-10-16 17:32:23 -04:00
function getChannels(colorType) {
switch (colorType) {
case 0: // greyscale
return 1;
case 2: // RGB
return 3;
case 4: // greyscale + alpha
return 2;
case 6: // RGB + alpha
return 4;
default:
return 3;
}
}
function parsePng(data) {
return new Promise(function(resolve, reject) {
new PNG().parse(data, function(error, decodedResults) {
if (defined(error)) {
reject(error);
return;
}
resolve(decodedResults);
});
});
}
function decodePng(texture, options) {
2017-04-18 11:56:08 -04:00
// Color type is encoded in the 25th bit of the png
2019-02-05 20:59:09 -05:00
const source = texture.source;
const colorType = source[25];
const channels = getChannels(colorType);
2017-04-18 11:56:08 -04:00
2019-02-05 20:59:09 -05:00
const checkTransparency = (channels === 4 && options.checkTransparency);
const decode = options.decode || checkTransparency;
2017-04-18 11:56:08 -04:00
if (decode) {
2017-07-28 16:56:28 -04:00
return parsePng(source)
.then(function(decodedResults) {
if (options.checkTransparency) {
texture.transparent = hasTransparency(decodedResults.data);
}
if (options.decode) {
texture.pixels = decodedResults.data;
texture.width = decodedResults.width;
texture.height = decodedResults.height;
texture.source = undefined; // Unload resources
}
});
2017-04-18 11:56:08 -04:00
}
}
function decodeJpeg(texture, options) {
2017-04-18 11:56:08 -04:00
if (options.decode) {
2019-02-05 20:59:09 -05:00
const source = texture.source;
const decodedResults = jpeg.decode(source);
texture.pixels = decodedResults.data;
texture.width = decodedResults.width;
texture.height = decodedResults.height;
texture.source = undefined; // Unload resources
2017-03-13 15:28:51 -04:00
}
2015-10-16 17:32:23 -04:00
}