Check for transparent pixels and fix for khr_materials_common

This commit is contained in:
Sean Lilley 2017-03-15 13:05:32 -04:00
parent 41956dd70d
commit 5f6d84581f
8 changed files with 71 additions and 10 deletions

View File

@ -110,6 +110,31 @@ https://www.npmjs.com/package/gltf-pipeline
>
> Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
### pngjs
https://www.npmjs.com/package/pngjs
> pngjs2 original work Copyright (c) 2015 Luke Page & Original Contributors
> pngjs derived work Copyright (c) 2012 Kuba Niegowski
>
> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
> in the Software without restriction, including without limitation the rights
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> copies of the Software, and to permit persons to whom the Software is
> furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in
> all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
> THE SOFTWARE.
### yargs
https://www.npmjs.com/package/yargs

View File

@ -93,11 +93,18 @@ function createGltf(objData) {
diffuse = [0, 0, 0, 1];
}
// It's not completely clear whether transparent and doubleSided belong under values or KHR_materials_common
// Put under both for now to handle both situations.
// https://github.com/KhronosGroup/glTF/tree/master/extensions/Khronos/KHR_materials_common
// https://github.com/KhronosGroup/glTF/issues/632
var technique = hasNormals ? (hasSpecular ? 'PHONG' : 'LAMBERT') : 'CONSTANT';
return {
extensions : {
KHR_materials_common : {
technique : technique,
transparent : transparent,
doubleSided : doubleSided,
values : {
ambient : ambient,
diffuse : diffuse,

View File

@ -2,6 +2,7 @@
var Cesium = require('cesium');
var fs = require('fs-extra');
var path = require('path');
var PNG = require('pngjs').PNG;
var Promise = require('bluebird');
var fsReadFile = Promise.promisify(fs.readFile);
@ -27,7 +28,6 @@ function loadImage(imagePath) {
var info = {
transparent : false,
channels : 3,
data : data,
uri : uri,
format : getFormat(3),
@ -38,9 +38,12 @@ function loadImage(imagePath) {
// Color type is encoded in the 25th bit of the png
var colorType = data[25];
var channels = getChannels(colorType);
info.channels = channels;
info.transparent = (channels === 4);
info.format = getFormat(channels);
if (channels === 4) {
// Need to do a finer grained check over the pixels to see if the image is actually transparent
info.transparent = isTransparent(data);
}
}
return info;
@ -51,6 +54,18 @@ function loadImage(imagePath) {
});
}
function isTransparent(data) {
var decoded = PNG.sync.read(data);
var pixels = decoded.data;
var pixelsLength = decoded.width * decoded.height;
for (var i = 0; i < pixelsLength; ++i) {
if (pixels[i * 4 + 3] < 255) {
return true;
}
}
return false;
}
function getChannels(colorType) {
switch (colorType) {
case 0: // greyscale

View File

@ -31,6 +31,7 @@
"event-stream": "^3.3.4",
"fs-extra": "^2.0.0",
"gltf-pipeline": "^0.1.0-alpha11",
"pngjs": "^3.0.1",
"yargs": "^7.0.1"
},
"devDependencies": {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.6 KiB

After

Width:  |  Height:  |  Size: 4.9 KiB

View File

@ -278,6 +278,8 @@
"extensions": {
"KHR_materials_common": {
"technique": "PHONG",
"transparent": false,
"doubleSided": false,
"values": {
"ambient": [
0,
@ -315,6 +317,8 @@
"extensions": {
"KHR_materials_common": {
"technique": "PHONG",
"transparent": false,
"doubleSided": false,
"values": {
"ambient": [
0,
@ -352,6 +356,8 @@
"extensions": {
"KHR_materials_common": {
"technique": "PHONG",
"transparent": false,
"doubleSided": false,
"values": {
"ambient": [
0,

View File

@ -104,6 +104,8 @@
"extensions": {
"KHR_materials_common": {
"technique": "PHONG",
"transparent": false,
"doubleSided": false,
"values": {
"ambient": [
0,

View File

@ -11,6 +11,7 @@ var jpegImage = 'specs/data/box-complex-material/specular.jpeg';
var gifImage = 'specs/data/box-complex-material/ambient.gif';
var grayscaleImage = 'specs/data/box-complex-material/alpha.png';
var transparentImage = 'specs/data/box-complex-material/diffuse.png';
var opaqueAlphaImage = 'specs/data/box-complex-material/bump.png';
var invalidImage = 'invalid.png';
describe('image', function() {
@ -18,7 +19,6 @@ describe('image', function() {
expect(loadImage(pngImage)
.then(function(info) {
expect(info.transparent).toBe(false);
expect(info.channels).toBe(3);
expect(info.data).toBeDefined();
expect(info.uri.indexOf('data:image/png') === 0).toBe(true);
expect(info.format).toBe(WebGLConstants.RGB);
@ -29,7 +29,6 @@ describe('image', function() {
expect(loadImage(jpgImage)
.then(function(info) {
expect(info.transparent).toBe(false);
expect(info.channels).toBe(3);
expect(info.data).toBeDefined();
expect(info.uri.indexOf('data:image/jpeg') === 0).toBe(true);
expect(info.format).toBe(WebGLConstants.RGB);
@ -40,7 +39,6 @@ describe('image', function() {
expect(loadImage(jpegImage)
.then(function(info) {
expect(info.transparent).toBe(false);
expect(info.channels).toBe(3);
expect(info.data).toBeDefined();
expect(info.uri.indexOf('data:image/jpeg') === 0).toBe(true);
expect(info.format).toBe(WebGLConstants.RGB);
@ -51,7 +49,6 @@ describe('image', function() {
expect(loadImage(gifImage)
.then(function(info) {
expect(info.transparent).toBe(false);
expect(info.channels).toBe(3);
expect(info.data).toBeDefined();
expect(info.uri.indexOf('data:image/gif') === 0).toBe(true);
expect(info.format).toBe(WebGLConstants.RGB);
@ -62,18 +59,26 @@ describe('image', function() {
expect(loadImage(grayscaleImage)
.then(function(info) {
expect(info.transparent).toBe(false);
expect(info.channels).toBe(1);
expect(info.data).toBeDefined();
expect(info.uri.indexOf('data:image/png') === 0).toBe(true);
expect(info.format).toBe(WebGLConstants.ALPHA);
}), done).toResolve();
});
it('loads transparentImage image', function(done) {
it('loads transparent image', function(done) {
expect(loadImage(transparentImage)
.then(function(info) {
expect(info.transparent).toBe(true);
expect(info.channels).toBe(4);
expect(info.data).toBeDefined();
expect(info.uri.indexOf('data:image/png') === 0).toBe(true);
expect(info.format).toBe(WebGLConstants.RGBA);
}), done).toResolve();
});
it('loads image with fully opaque alpha channel', function(done) {
expect(loadImage(opaqueAlphaImage)
.then(function(info) {
expect(info.transparent).toBe(false);
expect(info.data).toBeDefined();
expect(info.uri.indexOf('data:image/png') === 0).toBe(true);
expect(info.format).toBe(WebGLConstants.RGBA);