obj2gltf/lib/loadTexture.js

134 lines
3.8 KiB
JavaScript
Raw Permalink Normal View History

2021-08-02 11:31:59 -04:00
"use strict";
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.
* @param {Boolean} [options.keepSource=false] Whether to keep the source image contents in memory.
* @returns {Promise} A promise resolving to a Texture object.
2017-03-13 15:28:51 -04:00
*
* @private
*/
function loadTexture(texturePath, options) {
2021-08-02 11:31:59 -04:00
options = defaultValue(options, {});
options.checkTransparency = defaultValue(options.checkTransparency, false);
options.decode = defaultValue(options.decode, false);
options.keepSource = defaultValue(options.keepSource, false);
2017-04-18 11:56:08 -04:00
2021-08-02 11:31:59 -04:00
return fsExtra.readFile(texturePath).then(function (source) {
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
2021-08-02 11:31:59 -04:00
let decodePromise;
if (extension === ".png") {
decodePromise = decodePng(texture, options);
} else if (extension === ".jpg" || extension === ".jpeg") {
decodePromise = decodeJpeg(texture, options);
}
2017-07-28 16:56:28 -04:00
2021-08-02 11:31:59 -04:00
if (defined(decodePromise)) {
return decodePromise.then(function () {
return texture;
});
}
2017-03-13 15:28:51 -04:00
2021-08-02 11:31:59 -04:00
return texture;
});
2017-03-13 15:28:51 -04:00
}
function hasTransparency(pixels) {
2021-08-02 11:31:59 -04:00
const pixelsLength = pixels.length / 4;
for (let i = 0; i < pixelsLength; ++i) {
if (pixels[i * 4 + 3] < 255) {
return true;
2017-04-10 17:57:56 -04:00
}
2021-08-02 11:31:59 -04:00
}
return false;
}
2015-10-16 17:32:23 -04:00
function getChannels(colorType) {
2021-08-02 11:31:59 -04:00
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;
}
2015-10-16 17:32:23 -04:00
}
function parsePng(data) {
2021-08-02 11:31:59 -04:00
return new Promise(function (resolve, reject) {
new PNG().parse(data, function (error, decodedResults) {
if (defined(error)) {
reject(error);
return;
}
resolve(decodedResults);
});
2021-08-02 11:31:59 -04:00
});
}
function decodePng(texture, options) {
2021-08-02 11:31:59 -04:00
// Color type is encoded in the 25th bit of the png
const source = texture.source;
const colorType = source[25];
const channels = getChannels(colorType);
2017-04-18 11:56:08 -04:00
2021-08-02 11:31:59 -04:00
const checkTransparency = channels === 4 && options.checkTransparency;
const decode = options.decode || checkTransparency;
2017-04-18 11:56:08 -04:00
2021-08-02 11:31:59 -04:00
if (decode) {
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;
if (!options.keepSource) {
2021-08-02 11:31:59 -04:00
texture.source = undefined; // Unload resources
}
2021-08-02 11:31:59 -04:00
}
});
}
}
function decodeJpeg(texture, options) {
if (options.decode) {
const source = texture.source;
const decodedResults = jpeg.decode(source);
texture.pixels = decodedResults.data;
texture.width = decodedResults.width;
texture.height = decodedResults.height;
if (!options.keepSource) {
texture.source = undefined; // Unload resources
2017-03-13 15:28:51 -04:00
}
2021-08-02 11:31:59 -04:00
}
2015-10-16 17:32:23 -04:00
}