mirror of
https://github.com/CesiumGS/obj2gltf.git
synced 2025-03-03 14:58:54 -05:00
Added flag checkTextureAlpha
This commit is contained in:
parent
0bf726cea7
commit
05b1e3adbd
@ -48,6 +48,7 @@ Using obj2gltf as a command-line tool:
|
|||||||
|`--cesium`|Optimize the glTF for Cesium by using the sun as a default light source.|No, default `false`|
|
|`--cesium`|Optimize the glTF for Cesium by using the sun as a default light source.|No, default `false`|
|
||||||
|`--ao`|Apply ambient occlusion to the converted model.|No, default `false`|
|
|`--ao`|Apply ambient occlusion to the converted model.|No, default `false`|
|
||||||
|`--bypassPipeline`|Bypass the gltf-pipeline for debugging purposes. This option overrides many of the options above and will save the glTF with the KHR_materials_common extension.|No, default `false`|
|
|`--bypassPipeline`|Bypass the gltf-pipeline for debugging purposes. This option overrides many of the options above and will save the glTF with the KHR_materials_common extension.|No, default `false`|
|
||||||
|
|`--checkTextureAlpha`|Do a more exhaustive check for texture transparency by looking at the alpha channel of each pixel. By default textures with an alpha channel are considered to be transparent.|No, default `false`|
|
||||||
|
|
||||||
## Build Instructions
|
## Build Instructions
|
||||||
|
|
||||||
|
@ -79,6 +79,11 @@ var argv = yargs
|
|||||||
describe: 'Bypass the gltf-pipeline for debugging purposes. This option overrides many of the options above and will save the glTF with the KHR_materials_common extension.',
|
describe: 'Bypass the gltf-pipeline for debugging purposes. This option overrides many of the options above and will save the glTF with the KHR_materials_common extension.',
|
||||||
type: 'boolean',
|
type: 'boolean',
|
||||||
default: false
|
default: false
|
||||||
|
},
|
||||||
|
'checkTextureAlpha': {
|
||||||
|
describe: 'Do a more exhaustive check for texture transparency by looking at the alpha channel of each pixel. By default textures with an alpha channel are considered to be transparent.',
|
||||||
|
type: 'boolean',
|
||||||
|
default: false
|
||||||
}
|
}
|
||||||
}).parse(args);
|
}).parse(args);
|
||||||
|
|
||||||
@ -105,7 +110,8 @@ var options = {
|
|||||||
generateNormals : argv.n,
|
generateNormals : argv.n,
|
||||||
ao : argv.ao,
|
ao : argv.ao,
|
||||||
optimizeForCesium : argv.cesium,
|
optimizeForCesium : argv.cesium,
|
||||||
bypassPipeline : argv.bypassPipeline
|
bypassPipeline : argv.bypassPipeline,
|
||||||
|
checkTextureAlpha : argv.checkTextureAlpha
|
||||||
};
|
};
|
||||||
|
|
||||||
console.time('Total');
|
console.time('Total');
|
||||||
|
@ -32,6 +32,7 @@ module.exports = convert;
|
|||||||
* @param {Boolean} [options.ao=false] Apply ambient occlusion to the converted model.
|
* @param {Boolean} [options.ao=false] Apply ambient occlusion to the converted model.
|
||||||
* @param {Boolean} [options.textureCompressionOptions] Options sent to the compressTextures stage of gltf-pipeline.
|
* @param {Boolean} [options.textureCompressionOptions] Options sent to the compressTextures stage of gltf-pipeline.
|
||||||
* @param {Boolean} [options.bypassPipeline=false] Bypass the gltf-pipeline for debugging purposes. This option overrides many of the options above and will save the glTF with the KHR_materials_common extension.
|
* @param {Boolean} [options.bypassPipeline=false] Bypass the gltf-pipeline for debugging purposes. This option overrides many of the options above and will save the glTF with the KHR_materials_common extension.
|
||||||
|
* @param {Boolean} [options.checkTextureAlpha=false] Do a more exhaustive check for texture transparency by looking at the alpha channel of each pixel.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function convert(objPath, gltfPath, options) {
|
function convert(objPath, gltfPath, options) {
|
||||||
@ -81,7 +82,7 @@ function convert(objPath, gltfPath, options) {
|
|||||||
textureCompressionOptions : textureCompressionOptions
|
textureCompressionOptions : textureCompressionOptions
|
||||||
};
|
};
|
||||||
|
|
||||||
return loadObj(objPath)
|
return loadObj(objPath, options)
|
||||||
.then(function(objData) {
|
.then(function(objData) {
|
||||||
return createGltf(objData);
|
return createGltf(objData);
|
||||||
})
|
})
|
||||||
|
15
lib/image.js
15
lib/image.js
@ -7,6 +7,7 @@ var Promise = require('bluebird');
|
|||||||
|
|
||||||
var fsReadFile = Promise.promisify(fs.readFile);
|
var fsReadFile = Promise.promisify(fs.readFile);
|
||||||
|
|
||||||
|
var defaultValue = Cesium.defaultValue;
|
||||||
var WebGLConstants = Cesium.WebGLConstants;
|
var WebGLConstants = Cesium.WebGLConstants;
|
||||||
|
|
||||||
module.exports = loadImage;
|
module.exports = loadImage;
|
||||||
@ -15,11 +16,16 @@ module.exports = loadImage;
|
|||||||
* Load an image file and get information about it.
|
* Load an image file and get information about it.
|
||||||
*
|
*
|
||||||
* @param {String} imagePath Path to the image file.
|
* @param {String} imagePath Path to the image file.
|
||||||
|
* @param {Object} [options] An object with the following properties:
|
||||||
|
* @param {Boolean} [options.checkTextureAlpha=false] Do a more exhaustive check for texture transparency by looking at the alpha channel of each pixel.
|
||||||
* @returns {Promise} A promise resolving to the image information, or undefined if the file doesn't exist.
|
* @returns {Promise} A promise resolving to the image information, or undefined if the file doesn't exist.
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
function loadImage(imagePath) {
|
function loadImage(imagePath, options) {
|
||||||
|
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
|
||||||
|
var checkTextureAlpha = defaultValue(options.checkTextureAlpha, false);
|
||||||
|
|
||||||
return fsReadFile(imagePath)
|
return fsReadFile(imagePath)
|
||||||
.then(function(data) {
|
.then(function(data) {
|
||||||
var extension = path.extname(imagePath);
|
var extension = path.extname(imagePath);
|
||||||
@ -38,8 +44,11 @@ function loadImage(imagePath) {
|
|||||||
info.format = getFormat(channels);
|
info.format = getFormat(channels);
|
||||||
|
|
||||||
if (channels === 4) {
|
if (channels === 4) {
|
||||||
// Need to do a finer grained check over the pixels to see if the image is actually transparent
|
if (checkTextureAlpha) {
|
||||||
info.transparent = isTransparent(data);
|
info.transparent = isTransparent(data);
|
||||||
|
} else {
|
||||||
|
info.transparent = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
14
lib/obj.js
14
lib/obj.js
@ -51,12 +51,14 @@ var facePattern4 = /f( +(-?\d+)\/\/(-?\d+))( +(-?\d+)\/\/(-?\d+))( +(-?\d+)\/\/(
|
|||||||
* Parse an obj file.
|
* Parse an obj file.
|
||||||
*
|
*
|
||||||
* @param {String} objPath Path to the obj file.
|
* @param {String} objPath Path to the obj file.
|
||||||
|
* @param {Object} [options] An object with the following properties:
|
||||||
|
* @param {Boolean} [options.checkTextureAlpha=false] Do a more exhaustive check for texture transparency by looking at the alpha channel of each pixel.
|
||||||
* @returns {Promise} A promise resolving to the obj data.
|
* @returns {Promise} A promise resolving to the obj data.
|
||||||
* @exception {RuntimeError} The file does not have any geometry information in it.
|
* @exception {RuntimeError} The file does not have any geometry information in it.
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
function loadObj(objPath) {
|
function loadObj(objPath, options) {
|
||||||
// Global store of vertex attributes listed in the obj file
|
// Global store of vertex attributes listed in the obj file
|
||||||
var positions = new ArrayStorage(ComponentDatatype.FLOAT);
|
var positions = new ArrayStorage(ComponentDatatype.FLOAT);
|
||||||
var normals = new ArrayStorage(ComponentDatatype.FLOAT);
|
var normals = new ArrayStorage(ComponentDatatype.FLOAT);
|
||||||
@ -273,11 +275,11 @@ function loadObj(objPath) {
|
|||||||
uvs = undefined;
|
uvs = undefined;
|
||||||
|
|
||||||
// Load materials and images
|
// Load materials and images
|
||||||
return finishLoading(nodes, mtlPaths, objPath);
|
return finishLoading(nodes, mtlPaths, objPath, options);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function finishLoading(nodes, mtlPaths, objPath) {
|
function finishLoading(nodes, mtlPaths, objPath, options) {
|
||||||
nodes = cleanNodes(nodes);
|
nodes = cleanNodes(nodes);
|
||||||
if (nodes.length === 0) {
|
if (nodes.length === 0) {
|
||||||
throw new RuntimeError(objPath + ' does not have any geometry data');
|
throw new RuntimeError(objPath + ' does not have any geometry data');
|
||||||
@ -285,7 +287,7 @@ function finishLoading(nodes, mtlPaths, objPath) {
|
|||||||
return loadMaterials(mtlPaths, objPath)
|
return loadMaterials(mtlPaths, objPath)
|
||||||
.then(function(materials) {
|
.then(function(materials) {
|
||||||
var imagePaths = getImagePaths(materials);
|
var imagePaths = getImagePaths(materials);
|
||||||
return loadImages(imagePaths, objPath)
|
return loadImages(imagePaths, options)
|
||||||
.then(function(images) {
|
.then(function(images) {
|
||||||
return {
|
return {
|
||||||
nodes : nodes,
|
nodes : nodes,
|
||||||
@ -316,10 +318,10 @@ function loadMaterials(mtlPaths, objPath) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadImages(imagePaths) {
|
function loadImages(imagePaths, options) {
|
||||||
var images = {};
|
var images = {};
|
||||||
return Promise.map(imagePaths, function(imagePath) {
|
return Promise.map(imagePaths, function(imagePath) {
|
||||||
return loadImage(imagePath)
|
return loadImage(imagePath, options)
|
||||||
.then(function(image) {
|
.then(function(image) {
|
||||||
if (defined(image)) {
|
if (defined(image)) {
|
||||||
images[imagePath] = image;
|
images[imagePath] = image;
|
||||||
|
@ -77,11 +77,18 @@ describe('image', function() {
|
|||||||
|
|
||||||
it('loads image with fully opaque alpha channel', function(done) {
|
it('loads image with fully opaque alpha channel', function(done) {
|
||||||
expect(loadImage(opaqueAlphaImage)
|
expect(loadImage(opaqueAlphaImage)
|
||||||
|
.then(function(info) {
|
||||||
|
expect(info.transparent).toBe(true);
|
||||||
|
}), done).toResolve();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('loads image with fully opaque alpha channel with checkTextureAlpha flag', function(done) {
|
||||||
|
var options = {
|
||||||
|
checkTextureAlpha : true
|
||||||
|
};
|
||||||
|
expect(loadImage(opaqueAlphaImage, options)
|
||||||
.then(function(info) {
|
.then(function(info) {
|
||||||
expect(info.transparent).toBe(false);
|
expect(info.transparent).toBe(false);
|
||||||
expect(info.format).toBe(WebGLConstants.RGBA);
|
|
||||||
expect(info.source).toBeDefined();
|
|
||||||
expect(info.extension).toBe('.png');
|
|
||||||
}), done).toResolve();
|
}), done).toResolve();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user