diff --git a/LICENSE.md b/LICENSE.md index 82de349..2a7cb66 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -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 diff --git a/lib/gltf.js b/lib/gltf.js index 0fcee9f..c84df40 100644 --- a/lib/gltf.js +++ b/lib/gltf.js @@ -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, diff --git a/lib/image.js b/lib/image.js index 7f92a47..2387a6a 100644 --- a/lib/image.js +++ b/lib/image.js @@ -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 diff --git a/package.json b/package.json index bdce320..53e3598 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/specs/data/box-complex-material/bump.png b/specs/data/box-complex-material/bump.png index fcc5ba8..16ec3a9 100644 Binary files a/specs/data/box-complex-material/bump.png and b/specs/data/box-complex-material/bump.png differ diff --git a/specs/data/box-objects-groups-materials/box-objects-groups-materials.gltf b/specs/data/box-objects-groups-materials/box-objects-groups-materials.gltf index 0220c2a..1714d03 100644 --- a/specs/data/box-objects-groups-materials/box-objects-groups-materials.gltf +++ b/specs/data/box-objects-groups-materials/box-objects-groups-materials.gltf @@ -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, diff --git a/specs/data/box/box.gltf b/specs/data/box/box.gltf index 0f8ecdb..112389c 100644 --- a/specs/data/box/box.gltf +++ b/specs/data/box/box.gltf @@ -104,6 +104,8 @@ "extensions": { "KHR_materials_common": { "technique": "PHONG", + "transparent": false, + "doubleSided": false, "values": { "ambient": [ 0, diff --git a/specs/lib/imageSpec.js b/specs/lib/imageSpec.js index cb5359c..1d88553 100644 --- a/specs/lib/imageSpec.js +++ b/specs/lib/imageSpec.js @@ -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);