diff --git a/CHANGES.md b/CHANGES.md index a1584ef..2e45e30 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,147 +1,146 @@ -Change Log -========== +# Change Log ### 3.?.? - 2021-??-?? -* Removed `minFilter` and `magFilter` from generated samplers so that runtime engines can use their preferred texture filtering. [#240](https://github.com/CesiumGS/obj2gltf/pull/240) -* Triangle winding order sanitization is longer done by default. Use the `--triangle-winding-order-sanitization` option. [#236](https://github.com/CesiumGS/obj2gltf/pull/236) +- Removed `minFilter` and `magFilter` from generated samplers so that runtime engines can use their preferred texture filtering. [#240](https://github.com/CesiumGS/obj2gltf/pull/240) +- Triangle winding order sanitization is longer done by default. Use the `--triangle-winding-order-sanitization` option. [#236](https://github.com/CesiumGS/obj2gltf/pull/236) ### 3.1.1 - 2021-06-22 -* Fixed security warnings by updating outdated npm dependencies. [#254](https://github.com/CesiumGS/obj2gltf/pull/254) +- Fixed security warnings by updating outdated npm dependencies. [#254](https://github.com/CesiumGS/obj2gltf/pull/254) ### 3.1.0 - 2020-03-13 -* Added back `inputUpAxis` and `outputUpAxis`. [#211](https://github.com/CesiumGS/obj2gltf/pull/211) -* Fixed handling of mtl and texture absolute paths. [#219](https://github.com/CesiumGS/obj2gltf/pull/219) -* Fixed specular image not being decoded when referenced by other textures. [#217](https://github.com/CesiumGS/obj2gltf/pull/217) -* Fixed parsing faces that reference non-existing attributes. [#218](https://github.com/CesiumGS/obj2gltf/pull/218) +- Added back `inputUpAxis` and `outputUpAxis`. [#211](https://github.com/CesiumGS/obj2gltf/pull/211) +- Fixed handling of mtl and texture absolute paths. [#219](https://github.com/CesiumGS/obj2gltf/pull/219) +- Fixed specular image not being decoded when referenced by other textures. [#217](https://github.com/CesiumGS/obj2gltf/pull/217) +- Fixed parsing faces that reference non-existing attributes. [#218](https://github.com/CesiumGS/obj2gltf/pull/218) ### 3.0.4 - 2019-07-22 -* No longer printing texture decode warning if the diffuse and alpha textures are the same. [#205](https://github.com/CesiumGS/obj2gltf/pull/205) +- No longer printing texture decode warning if the diffuse and alpha textures are the same. [#205](https://github.com/CesiumGS/obj2gltf/pull/205) ### 3.0.3 2019-06-26 -* Fixed parsing of negative face indices. [#191](https://github.com/CesiumGS/obj2gltf/pull/191) +- Fixed parsing of negative face indices. [#191](https://github.com/CesiumGS/obj2gltf/pull/191) ### 3.0.2 2019-03-21 -* Fixed a crash when saving separate resources that would exceed the Node buffer size limit. [#173](https://github.com/CesiumGS/obj2gltf/pull/173) +- Fixed a crash when saving separate resources that would exceed the Node buffer size limit. [#173](https://github.com/CesiumGS/obj2gltf/pull/173) ### 3.0.1 2019-03-08 -* Fixed handling of materials that don't have names. [#173](https://github.com/CesiumGS/obj2gltf/pull/173) +- Fixed handling of materials that don't have names. [#173](https://github.com/CesiumGS/obj2gltf/pull/173) ### 3.0.0 2018-12-05 -* Breaking changes - * The `--materialsCommon` flag has been removed. Use `--unlit` instead which uses the `KHR_materials_unlit` extension. [#152](https://github.com/CesiumGS/obj2gltf/pull/152) +- Breaking changes + - The `--materialsCommon` flag has been removed. Use `--unlit` instead which uses the `KHR_materials_unlit` extension. [#152](https://github.com/CesiumGS/obj2gltf/pull/152) ### 2.3.2 2018-11-02 -* Improved handling of primitives with different attributes using the same material. Materials are now duplicated. [#162](https://github.com/CesiumGS/obj2gltf/pull/162) -* Fixed a bug where primitives without texture coordinates could use materials containing textures. Those textures are now removed. [#162](https://github.com/CesiumGS/obj2gltf/pull/162) -* Improved parsing of faces with mismatching attributes. [#161](https://github.com/CesiumGS/obj2gltf/pull/161) +- Improved handling of primitives with different attributes using the same material. Materials are now duplicated. [#162](https://github.com/CesiumGS/obj2gltf/pull/162) +- Fixed a bug where primitives without texture coordinates could use materials containing textures. Those textures are now removed. [#162](https://github.com/CesiumGS/obj2gltf/pull/162) +- Improved parsing of faces with mismatching attributes. [#161](https://github.com/CesiumGS/obj2gltf/pull/161) ### 2.3.1 2018-10-16 -* Improved parsing models with concave or n-sided faces. [#157](https://github.com/CesiumGS/obj2gltf/pull/157) -* Fixed handling of objs with interleaved materials. [#155](https://github.com/CesiumGS/obj2gltf/pull/155) +- Improved parsing models with concave or n-sided faces. [#157](https://github.com/CesiumGS/obj2gltf/pull/157) +- Fixed handling of objs with interleaved materials. [#155](https://github.com/CesiumGS/obj2gltf/pull/155) ### 2.3.0 2018-09-19 -* Fixed handling of objs with mismatching attribute layouts. [#153](https://github.com/CesiumGS/obj2gltf/pull/153) -* Fixed normalization of Windows paths when running the converter on Linux. [#150](https://github.com/CesiumGS/obj2gltf/pull/150) -* Added ability to use the first material in the mtl file when the obj is missing `usemtl`. [#133](https://github.com/CesiumGS/obj2gltf/pull/133) -* Fixed handling of unnormalized input normals. [#136](https://github.com/CesiumGS/obj2gltf/pull/136) +- Fixed handling of objs with mismatching attribute layouts. [#153](https://github.com/CesiumGS/obj2gltf/pull/153) +- Fixed normalization of Windows paths when running the converter on Linux. [#150](https://github.com/CesiumGS/obj2gltf/pull/150) +- Added ability to use the first material in the mtl file when the obj is missing `usemtl`. [#133](https://github.com/CesiumGS/obj2gltf/pull/133) +- Fixed handling of unnormalized input normals. [#136](https://github.com/CesiumGS/obj2gltf/pull/136) ### 2.2.0 2018-01-29 -* Fixed handling of materials where the diffuse and ambient texture are the same. [#127](https://github.com/CesiumGS/obj2gltf/pull/127) -* Added ability to load alpha textures. [#124](https://github.com/CesiumGS/obj2gltf/pull/124) -* Fixed handling of `usemtl` when appearing before an `o` or `g` token. [#123](https://github.com/CesiumGS/obj2gltf/pull/123) -* Fixed output name when running from the command line. [#126](https://github.com/CesiumGS/obj2gltf/pull/126) +- Fixed handling of materials where the diffuse and ambient texture are the same. [#127](https://github.com/CesiumGS/obj2gltf/pull/127) +- Added ability to load alpha textures. [#124](https://github.com/CesiumGS/obj2gltf/pull/124) +- Fixed handling of `usemtl` when appearing before an `o` or `g` token. [#123](https://github.com/CesiumGS/obj2gltf/pull/123) +- Fixed output name when running from the command line. [#126](https://github.com/CesiumGS/obj2gltf/pull/126) ### 2.1.0 2017-12-28 -* Fixed loading faces that contain less than 3 vertices. [#120](https://github.com/CesiumGS/obj2gltf/pull/120) -* Attempt to load missing materials and textures from within the same directory as the obj. [#117](https://github.com/CesiumGS/obj2gltf/pull/117) -* Fixed loading mtllib paths that contain spaces. [#116](https://github.com/CesiumGS/obj2gltf/pull/116) -* Fixed checking for transparency when the diffuse texture is used in another texture slot. [#115](https://github.com/CesiumGS/obj2gltf/pull/115) -* Fixed parsing mtl textures that contain texture map options. [#109](https://github.com/CesiumGS/obj2gltf/pull/109) -* Added back support for the `CONSTANT` technique when a model uses the `KHR_materials_common` extension and has no normals. [#108](https://github.com/CesiumGS/obj2gltf/pull/108) -* Improved handling of materials with alpha. If the alpha value is 0.0 it is now treated as 1.0. [#107](https://github.com/CesiumGS/obj2gltf/pull/107) +- Fixed loading faces that contain less than 3 vertices. [#120](https://github.com/CesiumGS/obj2gltf/pull/120) +- Attempt to load missing materials and textures from within the same directory as the obj. [#117](https://github.com/CesiumGS/obj2gltf/pull/117) +- Fixed loading mtllib paths that contain spaces. [#116](https://github.com/CesiumGS/obj2gltf/pull/116) +- Fixed checking for transparency when the diffuse texture is used in another texture slot. [#115](https://github.com/CesiumGS/obj2gltf/pull/115) +- Fixed parsing mtl textures that contain texture map options. [#109](https://github.com/CesiumGS/obj2gltf/pull/109) +- Added back support for the `CONSTANT` technique when a model uses the `KHR_materials_common` extension and has no normals. [#108](https://github.com/CesiumGS/obj2gltf/pull/108) +- Improved handling of materials with alpha. If the alpha value is 0.0 it is now treated as 1.0. [#107](https://github.com/CesiumGS/obj2gltf/pull/107) ### 2.0.0 2017-08-11 -* Breaking changes - * Obj models now convert to glTF 2.0. Possible material profiles are `metallicRoughness`, `specGlossiness` (using the `KHR_materials_pbrSpecularGlossiness` extension), and `materialsCommon` (using the `KHR_materials_common` extension). - * Removed `gltf-pipeline` dependency. The following options have been removed: `compress`, `optimize`, `generateNormals`, `optimizeForCesium`, `ao`, and `bypassPipeline`. - * Removed `inputUpAxis` and `outputUpAxis`. This stage will be incorporated into `gltf-pipeline` instead. - * `obj2gltf` no longer takes a `gltfPath` argument and saves a glTF file. Instead it returns a promise that resolves to the glTF JSON or glb buffer. +- Breaking changes + - Obj models now convert to glTF 2.0. Possible material profiles are `metallicRoughness`, `specGlossiness` (using the `KHR_materials_pbrSpecularGlossiness` extension), and `materialsCommon` (using the `KHR_materials_common` extension). + - Removed `gltf-pipeline` dependency. The following options have been removed: `compress`, `optimize`, `generateNormals`, `optimizeForCesium`, `ao`, and `bypassPipeline`. + - Removed `inputUpAxis` and `outputUpAxis`. This stage will be incorporated into `gltf-pipeline` instead. + - `obj2gltf` no longer takes a `gltfPath` argument and saves a glTF file. Instead it returns a promise that resolves to the glTF JSON or glb buffer. ### 1.3.0 2017-08-11 -* Fixed parsing models with concave or n-sided faces. [#85](https://github.com/CesiumGS/obj2gltf/pull/85) -* Fixed parsing models with line breaks. [#85](https://github.com/CesiumGS/obj2gltf/pull/85) +- Fixed parsing models with concave or n-sided faces. [#85](https://github.com/CesiumGS/obj2gltf/pull/85) +- Fixed parsing models with line breaks. [#85](https://github.com/CesiumGS/obj2gltf/pull/85) ### 1.2.0 2017-07-11 -* Change texture sampling to use `NEAREST_MIPMAP_LINEAR` by default. [#83](https://github.com/CesiumGS/obj2gltf/pull/83). -* Fixed lighting when generating normals. [#89](https://github.com/CesiumGS/obj2gltf/pull/89) +- Change texture sampling to use `NEAREST_MIPMAP_LINEAR` by default. [#83](https://github.com/CesiumGS/obj2gltf/pull/83). +- Fixed lighting when generating normals. [#89](https://github.com/CesiumGS/obj2gltf/pull/89) ### 1.1.1 2017-04-25 -* Fixed `CHANGES.md` formatting. +- Fixed `CHANGES.md` formatting. ### 1.1.0 2017-04-25 -* Added ability to convert the up-axis of the obj model. [#68](https://github.com/CesiumGS/obj2gltf/pull/68) -* Fixed issues with an extra .bin file being saved when using `--separate`. [#62](https://github.com/CesiumGS/obj2gltf/pull/62) -* Fixed issue where an ambient color of `[1, 1, 1]` overly brightens the converted model. [#70](https://github.com/CesiumGS/obj2gltf/pull/70) +- Added ability to convert the up-axis of the obj model. [#68](https://github.com/CesiumGS/obj2gltf/pull/68) +- Fixed issues with an extra .bin file being saved when using `--separate`. [#62](https://github.com/CesiumGS/obj2gltf/pull/62) +- Fixed issue where an ambient color of `[1, 1, 1]` overly brightens the converted model. [#70](https://github.com/CesiumGS/obj2gltf/pull/70) ### 1.0.0 2017-04-13 -* Breaking changes - * To use `obj2gltf` as a library, call `require('obj2gltf')(input, output, options)`. The previous calling code was `require('obj2gltf').convert(input, output, options)`. - * Many library options and command-line parameters have been renamed. -* Project cleanup. [#49](https://github.com/CesiumGS/obj2gltf/pull/49) - * Speed improvements, especially for larger models. - * Preserves the objects and groups in the obj. - * Added documentation and tests. - * Material fixes. +- Breaking changes + - To use `obj2gltf` as a library, call `require('obj2gltf')(input, output, options)`. The previous calling code was `require('obj2gltf').convert(input, output, options)`. + - Many library options and command-line parameters have been renamed. +- Project cleanup. [#49](https://github.com/CesiumGS/obj2gltf/pull/49) + - Speed improvements, especially for larger models. + - Preserves the objects and groups in the obj. + - Added documentation and tests. + - Material fixes. ### 0.1.7 2017-01-06 -* Update gltf-pipeline to 0.1.0-alpha9 -* Added command to generate documentation (npm run jsdoc) +- Update gltf-pipeline to 0.1.0-alpha9 +- Added command to generate documentation (npm run jsdoc) ### 0.1.6 2016-09-07 -* Changed obj2gltf.js line endings from CRLF to LF in npm package. +- Changed obj2gltf.js line endings from CRLF to LF in npm package. ### 0.1.5 2016-08-26 -* Fixed incorrect parameter to the gltf-pipeline. +- Fixed incorrect parameter to the gltf-pipeline. ### 0.1.4 2016-08-25 -* Added compression flag for quantizing positions, compressing texture coordinates, and oct-encoding normals. +- Added compression flag for quantizing positions, compressing texture coordinates, and oct-encoding normals. ### 0.1.3 - 2016-08-08 -* Fixed a bug causing models with no mtl file to not convert. +- Fixed a bug causing models with no mtl file to not convert. ### 0.1.2 - 2016-07-25 -* Converted the API to now use promises instead of callbacks. [#21](https://github.com/CesiumGS/OBJ2GLTF/pull/21) -* Added the ability to optimize the converted glTF for CesiumJS by using the sun as a default light source. +- Converted the API to now use promises instead of callbacks. [#21](https://github.com/CesiumGS/OBJ2GLTF/pull/21) +- Added the ability to optimize the converted glTF for CesiumJS by using the sun as a default light source. ### 0.1.1 - 2016-07-21 -* Updated to use gltf-pipeline 0.1.0-alpha2. +- Updated to use gltf-pipeline 0.1.0-alpha2. ### 0.1.0 - 2016-07-20 -* Initial release. +- Initial release. diff --git a/LICENSE.md b/LICENSE.md index 9fc5377..9ce539a 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -4,180 +4,180 @@ Copyright 2016-2020 Cesium GS, Inc. and Contributors Version 2.0, January 2004 http://www.apache.org/licenses/ - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - 1. Definitions. +1. Definitions. - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. - END OF TERMS AND CONDITIONS +END OF TERMS AND CONDITIONS - APPENDIX: How to apply the Apache License to your work. +APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" @@ -188,23 +188,21 @@ Copyright 2016-2020 Cesium GS, Inc. and Contributors same "printed page" as the copyright notice for easier identification within third-party archives. - Copyright 2016-2020 Cesium GS, Inc. and Contributors +Copyright 2016-2020 Cesium GS, Inc. and Contributors - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 - 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. +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. - -Third-Party Code -================ +# Third-Party Code obj2gltf includes the following third-party code. @@ -226,7 +224,7 @@ obj2gltf includes the following third-party code. > > 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 +> 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 @@ -283,17 +281,17 @@ https://www.npmjs.com/package/gltf-pipeline https://www.npmjs.com/package/mime > Copyright (c) 2010 Benjamin Thomas, Robert Kieffer -> +> > 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 @@ -308,17 +306,17 @@ 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 diff --git a/README.md b/README.md index c2f20ec..3403b4d 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ Convert OBJ assets to [glTF](https://www.khronos.org/gltf) 2.0. ## Getting Started Install [Node.js](https://nodejs.org/en/) if you don't already have it, and then: + ``` npm install -g obj2gltf ``` @@ -22,27 +23,25 @@ npm install -g obj2gltf #### Converting an obj model to gltf: ```javascript -const obj2gltf = require('obj2gltf'); -const fs = require('fs'); -obj2gltf('model.obj') - .then(function(gltf) { - const data = Buffer.from(JSON.stringify(gltf)); - fs.writeFileSync('model.gltf', data); - }); +const obj2gltf = require("obj2gltf"); +const fs = require("fs"); +obj2gltf("model.obj").then(function (gltf) { + const data = Buffer.from(JSON.stringify(gltf)); + fs.writeFileSync("model.gltf", data); +}); ``` #### Converting an obj model to glb ```javascript -const obj2gltf = require('obj2gltf'); -const fs = require('fs'); +const obj2gltf = require("obj2gltf"); +const fs = require("fs"); const options = { - binary : true -} -obj2gltf('model.obj', options) - .then(function(glb) { - fs.writeFileSync('model.glb', glb); - }); + binary: true, +}; +obj2gltf("model.obj", options).then(function (glb) { + fs.writeFileSync("model.glb", glb); +}); ``` ## Material types @@ -52,13 +51,13 @@ materials. There are three shading models supported by `obj2gltf`: -* Metallic roughness PBR -* Specular glossiness PBR (via `KHR_materials_pbrSpecularGlossiness` extension) -* Unlit materials (via `KHR_materials_unlit` extension) +- Metallic roughness PBR +- Specular glossiness PBR (via `KHR_materials_pbrSpecularGlossiness` extension) +- Unlit materials (via `KHR_materials_unlit` extension) If the material type is known in advance, it should be specified with either the `metallicRoughness` or `specularGlossiness` flag. -If lighting information is already present in the model, the `unlit` flag should be used. This will save the glTF with the `KHR_materials_unlit` extension. +If lighting information is already present in the model, the `unlit` flag should be used. This will save the glTF with the `KHR_materials_unlit` extension. If the model is created with PBR textures, either the `metallicRoughness` or `specularGlossiness` flag should be passed in. See the table below for more information about how to specify PBR values inside the .mtl file. @@ -71,72 +70,79 @@ As a convenience the PBR textures may be supplied directly to the command line. **Mapping of mtl slots to shading models** -|Slot| Metallic roughness|Specular glossiness| -|----|-------------------|-------------------| -|Ka|occlusion value|occlusion value| -|Ke|emissive color|emissive color| -|Kd|base color|diffuse color| -|Ks|metallic value|specular color| -|Ns|roughness value|glossiness value| -|d|alpha|alpha| -|Tr|1.0 - alpha|1.0 - alpha| -|map_Ka|occlusion texture|occlusion texture| -|map_Ke|emissive texture|emissive texture| -|map_Kd|base color texture|diffuse texture| -|map_Ks|metallic texture|specular texture| -|map_Ns|roughness texture|glossiness texture| -|map_Bump|normal texture|normal texture| +| Slot | Metallic roughness | Specular glossiness | +| -------- | ------------------ | ------------------- | +| Ka | occlusion value | occlusion value | +| Ke | emissive color | emissive color | +| Kd | base color | diffuse color | +| Ks | metallic value | specular color | +| Ns | roughness value | glossiness value | +| d | alpha | alpha | +| Tr | 1.0 - alpha | 1.0 - alpha | +| map_Ka | occlusion texture | occlusion texture | +| map_Ke | emissive texture | emissive texture | +| map_Kd | base color texture | diffuse texture | +| map_Ks | metallic texture | specular texture | +| map_Ns | roughness texture | glossiness texture | +| map_Bump | normal texture | normal texture | ## Usage ### Command line flags: -|Flag|Description|Required| -|----|-----------|--------| -|`-h`, `--help`|Display help.|No| -|`-i`, `--input`|Path to the obj file.| :white_check_mark: Yes| -|`-o`, `--output`|Path of the converted glTF or glb file.|No| -|`-b`, `--binary`|Save as binary glTF (.glb).|No, default `false`| -|`-s`, `--separate`|Writes out separate buffers and textures instead of embedding them in the glTF file.|No, default `false`| -|`-t`, `--separateTextures`|Write out separate textures only.|No, default `false`| -|`--checkTransparency`|Do a more exhaustive check for texture transparency by looking at the alpha channel of each pixel. By default textures are considered to be opaque.|No, default `false`| -|`--secure`|Prevent the converter from reading texture or mtl files outside of the input obj directory.|No, default `false`| -|`--packOcclusion`|Pack the occlusion texture in the red channel of metallic-roughness texture.|No, default `false`| -|`--metallicRoughness`|The values in the mtl file are already metallic-roughness PBR values and no conversion step should be applied. Metallic is stored in the Ks and map_Ks slots and roughness is stored in the Ns and map_Ns slots.|No, default `false`| -|`--specularGlossiness`|The values in the mtl file are already specular-glossiness PBR values and no conversion step should be applied. Specular is stored in the Ks and map_Ks slots and glossiness is stored in the Ns and map_Ns slots. The glTF will be saved with the `KHR_materials_pbrSpecularGlossiness` extension.|No, default `false`| -|`--unlit`|The glTF will be saved with the KHR_materials_unlit extension.|No, default `false`| -|`--metallicRoughnessOcclusionTexture`|Path to the metallic-roughness-occlusion texture that should override textures in the .mtl file, where occlusion is stored in the red channel, roughness is stored in the green channel, and metallic is stored in the blue channel. The model will be saved with a pbrMetallicRoughness material. This is often convenient in workflows where the .mtl does not exist or is not set up to use PBR materials. Intended for models with a single material.|No| -|`--specularGlossinessTexture`|Path to the specular-glossiness texture that should override textures in the .mtl file, where specular color is stored in the red, green, and blue channels and specular glossiness is stored in the alpha channel. The model will be saved with a material using the KHR_materials_pbrSpecularGlossiness extension.|No| -|`--occlusionTexture`|Path to the occlusion texture that should override textures in the .mtl file.|No| -|`--normalTexture`|Path to the normal texture that should override textures in the .mtl file.|No| -|`--baseColorTexture`|Path to the baseColor/diffuse texture that should override textures in the .mtl file.|No| -|`--emissiveTexture`|Path to the emissive texture that should override textures in the .mtl file.|No| -|`--alphaTexture`|Path to the alpha texture that should override textures in the .mtl file.|No| -|`--input-up-axis`|Up axis of the obj.|No| -|`--output-up-axis`|Up axis of the converted glTF.|No| -|`--triangle-winding-order-sanitization`|Apply triangle winding order sanitization.|No| +| Flag | Description | Required | +| --------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------- | +| `-h`, `--help` | Display help. | No | +| `-i`, `--input` | Path to the obj file. | :white_check_mark: Yes | +| `-o`, `--output` | Path of the converted glTF or glb file. | No | +| `-b`, `--binary` | Save as binary glTF (.glb). | No, default `false` | +| `-s`, `--separate` | Writes out separate buffers and textures instead of embedding them in the glTF file. | No, default `false` | +| `-t`, `--separateTextures` | Write out separate textures only. | No, default `false` | +| `--checkTransparency` | Do a more exhaustive check for texture transparency by looking at the alpha channel of each pixel. By default textures are considered to be opaque. | No, default `false` | +| `--secure` | Prevent the converter from reading texture or mtl files outside of the input obj directory. | No, default `false` | +| `--packOcclusion` | Pack the occlusion texture in the red channel of metallic-roughness texture. | No, default `false` | +| `--metallicRoughness` | The values in the mtl file are already metallic-roughness PBR values and no conversion step should be applied. Metallic is stored in the Ks and map_Ks slots and roughness is stored in the Ns and map_Ns slots. | No, default `false` | +| `--specularGlossiness` | The values in the mtl file are already specular-glossiness PBR values and no conversion step should be applied. Specular is stored in the Ks and map_Ks slots and glossiness is stored in the Ns and map_Ns slots. The glTF will be saved with the `KHR_materials_pbrSpecularGlossiness` extension. | No, default `false` | +| `--unlit` | The glTF will be saved with the KHR_materials_unlit extension. | No, default `false` | +| `--metallicRoughnessOcclusionTexture` | Path to the metallic-roughness-occlusion texture that should override textures in the .mtl file, where occlusion is stored in the red channel, roughness is stored in the green channel, and metallic is stored in the blue channel. The model will be saved with a pbrMetallicRoughness material. This is often convenient in workflows where the .mtl does not exist or is not set up to use PBR materials. Intended for models with a single material. | No | +| `--specularGlossinessTexture` | Path to the specular-glossiness texture that should override textures in the .mtl file, where specular color is stored in the red, green, and blue channels and specular glossiness is stored in the alpha channel. The model will be saved with a material using the KHR_materials_pbrSpecularGlossiness extension. | No | +| `--occlusionTexture` | Path to the occlusion texture that should override textures in the .mtl file. | No | +| `--normalTexture` | Path to the normal texture that should override textures in the .mtl file. | No | +| `--baseColorTexture` | Path to the baseColor/diffuse texture that should override textures in the .mtl file. | No | +| `--emissiveTexture` | Path to the emissive texture that should override textures in the .mtl file. | No | +| `--alphaTexture` | Path to the alpha texture that should override textures in the .mtl file. | No | +| `--input-up-axis` | Up axis of the obj. | No | +| `--output-up-axis` | Up axis of the converted glTF. | No | +| `--triangle-winding-order-sanitization` | Apply triangle winding order sanitization. | No | ## Build Instructions Run the tests: + ``` npm run test ``` + To run ESLint on the entire codebase, run: + ``` npm run eslint ``` + To run ESLint automatically when a file is saved, run the following and leave it open in a console window: + ``` npm run eslint-watch ``` ## Running Test Coverage -Coverage uses [nyc](https://github.com/istanbuljs/nyc). Run: +Coverage uses [nyc](https://github.com/istanbuljs/nyc). Run: + ``` npm run coverage ``` + For complete coverage details, open `coverage/lcov-report/index.html`. The tests and coverage covers the Node.js module; it does not cover the command-line interface, which is tiny. @@ -144,6 +150,7 @@ The tests and coverage covers the Node.js module; it does not cover the command- ## Generating Documentation To generate the documentation: + ``` npm run jsdoc ``` @@ -152,11 +159,12 @@ The documentation will be placed in the `doc` folder. ## Contributions -Pull requests are appreciated. Please use the same [Contributor License Agreement (CLA)](https://github.com/CesiumGS/cesium/blob/main/CONTRIBUTING.md) used for [CesiumJS](https://cesium.com/cesiumjs/). +Pull requests are appreciated. Please use the same [Contributor License Agreement (CLA)](https://github.com/CesiumGS/cesium/blob/main/CONTRIBUTING.md) used for [CesiumJS](https://cesium.com/cesiumjs/). --- Developed by the Cesium team. +

diff --git a/bin/obj2gltf.js b/bin/obj2gltf.js index b8ec374..e1d1abe 100644 --- a/bin/obj2gltf.js +++ b/bin/obj2gltf.js @@ -1,10 +1,10 @@ #!/usr/bin/env node -'use strict'; -const Cesium = require('cesium'); -const fsExtra = require('fs-extra'); -const path = require('path'); -const yargs = require('yargs'); -const obj2gltf = require('../lib/obj2gltf'); +"use strict"; +const Cesium = require("cesium"); +const fsExtra = require("fs-extra"); +const path = require("path"); +const yargs = require("yargs"); +const obj2gltf = require("../lib/obj2gltf"); const defaultValue = Cesium.defaultValue; const defined = Cesium.defined; @@ -14,148 +14,170 @@ const defaults = obj2gltf.defaults; const args = process.argv; const argv = yargs - .usage('Usage: node $0 -i inputPath -o outputPath') - .example('node $0 -i ./specs/data/box/box.obj -o box.gltf') - .help('h') - .alias('h', 'help') - .options({ - input : { - alias : 'i', - describe : 'Path to the obj file.', - type : 'string', - demandOption : true, - coerce : function (p) { - if (!defined(p)) { - return undefined; - } - if (p.length === 0) { - throw new Error('Input path must be a file name'); - } - return path.resolve(p); - } - }, - output : { - alias : 'o', - describe : 'Path of the converted glTF or glb file.', - type : 'string', - coerce : function (p) { - if (!defined(p)) { - return undefined; - } - if (p.length === 0) { - throw new Error('Output path must be a file name'); - } - return path.resolve(p); - } - }, - binary : { - alias : 'b', - describe : 'Save as binary glTF (.glb)', - type : 'boolean', - default : defaults.binary - }, - separate : { - alias : 's', - describe : 'Write separate buffers and textures instead of embedding them in the glTF.', - type : 'boolean', - default : defaults.separate - }, - separateTextures : { - alias : 't', - describe : 'Write out separate textures only.', - type : 'boolean', - default : defaults.separateTextures - }, - checkTransparency : { - describe : 'Do a more exhaustive check for texture transparency by looking at the alpha channel of each pixel. By default textures are considered to be opaque.', - type : 'boolean', - default : defaults.checkTransparency - }, - secure : { - describe : 'Prevent the converter from reading textures or mtl files outside of the input obj directory.', - type : 'boolean', - default : defaults.secure - }, - packOcclusion : { - describe : 'Pack the occlusion texture in the red channel of metallic-roughness texture.', - type : 'boolean', - default : defaults.packOcclusion - }, - metallicRoughness : { - describe : 'The values in the .mtl file are already metallic-roughness PBR values and no conversion step should be applied. Metallic is stored in the Ks and map_Ks slots and roughness is stored in the Ns and map_Ns slots.', - type : 'boolean', - default : defaults.metallicRoughness - }, - specularGlossiness : { - describe : 'The values in the .mtl file are already specular-glossiness PBR values and no conversion step should be applied. Specular is stored in the Ks and map_Ks slots and glossiness is stored in the Ns and map_Ns slots. The glTF will be saved with the KHR_materials_pbrSpecularGlossiness extension.', - type : 'boolean', - default : defaults.specularGlossiness - }, - unlit : { - describe : 'The glTF will be saved with the KHR_materials_unlit extension.', - type : 'boolean', - default : defaults.unlit - }, - metallicRoughnessOcclusionTexture : { - describe : 'Path to the metallic-roughness-occlusion texture that should override textures in the .mtl file, where occlusion is stored in the red channel, roughness is stored in the green channel, and metallic is stored in the blue channel. The model will be saved with a pbrMetallicRoughness material. This is often convenient in workflows where the .mtl does not exist or is not set up to use PBR materials. Intended for models with a single material', - type : 'string', - normalize : true - }, - specularGlossinessTexture : { - describe : 'Path to the specular-glossiness texture that should override textures in the .mtl file, where specular color is stored in the red, green, and blue channels and specular glossiness is stored in the alpha channel. The model will be saved with a material using the KHR_materials_pbrSpecularGlossiness extension.', - type : 'string', - normalize : true - }, - occlusionTexture : { - describe : 'Path to the occlusion texture that should override textures in the .mtl file.', - type : 'string', - normalize : true - }, - normalTexture : { - describe : 'Path to the normal texture that should override textures in the .mtl file.', - type : 'string', - normalize : true - }, - baseColorTexture : { - describe : 'Path to the baseColor/diffuse texture that should override textures in the .mtl file.', - type : 'string', - normalize : true - }, - emissiveTexture : { - describe : 'Path to the emissive texture that should override textures in the .mtl file.', - type : 'string', - normalize : true - }, - alphaTexture : { - describe : 'Path to the alpha texture that should override textures in the .mtl file.' - }, - inputUpAxis : { - describe: 'Up axis of the obj.', - choices: ['X', 'Y', 'Z'], - type: 'string', - default: 'Y' - }, - outputUpAxis : { - describe: 'Up axis of the converted glTF.', - choices: ['X', 'Y', 'Z'], - type: 'string', - default: 'Y' - }, - triangleWindingOrderSanitization : { - describe: 'Apply triangle winding order sanitization.', - type: 'boolean', - default: defaults.triangleWindingOrderSanitization + .usage("Usage: node $0 -i inputPath -o outputPath") + .example("node $0 -i ./specs/data/box/box.obj -o box.gltf") + .help("h") + .alias("h", "help") + .options({ + input: { + alias: "i", + describe: "Path to the obj file.", + type: "string", + demandOption: true, + coerce: function (p) { + if (!defined(p)) { + return undefined; } - }).parse(args); + if (p.length === 0) { + throw new Error("Input path must be a file name"); + } + return path.resolve(p); + }, + }, + output: { + alias: "o", + describe: "Path of the converted glTF or glb file.", + type: "string", + coerce: function (p) { + if (!defined(p)) { + return undefined; + } + if (p.length === 0) { + throw new Error("Output path must be a file name"); + } + return path.resolve(p); + }, + }, + binary: { + alias: "b", + describe: "Save as binary glTF (.glb)", + type: "boolean", + default: defaults.binary, + }, + separate: { + alias: "s", + describe: + "Write separate buffers and textures instead of embedding them in the glTF.", + type: "boolean", + default: defaults.separate, + }, + separateTextures: { + alias: "t", + describe: "Write out separate textures only.", + type: "boolean", + default: defaults.separateTextures, + }, + checkTransparency: { + describe: + "Do a more exhaustive check for texture transparency by looking at the alpha channel of each pixel. By default textures are considered to be opaque.", + type: "boolean", + default: defaults.checkTransparency, + }, + secure: { + describe: + "Prevent the converter from reading textures or mtl files outside of the input obj directory.", + type: "boolean", + default: defaults.secure, + }, + packOcclusion: { + describe: + "Pack the occlusion texture in the red channel of metallic-roughness texture.", + type: "boolean", + default: defaults.packOcclusion, + }, + metallicRoughness: { + describe: + "The values in the .mtl file are already metallic-roughness PBR values and no conversion step should be applied. Metallic is stored in the Ks and map_Ks slots and roughness is stored in the Ns and map_Ns slots.", + type: "boolean", + default: defaults.metallicRoughness, + }, + specularGlossiness: { + describe: + "The values in the .mtl file are already specular-glossiness PBR values and no conversion step should be applied. Specular is stored in the Ks and map_Ks slots and glossiness is stored in the Ns and map_Ns slots. The glTF will be saved with the KHR_materials_pbrSpecularGlossiness extension.", + type: "boolean", + default: defaults.specularGlossiness, + }, + unlit: { + describe: + "The glTF will be saved with the KHR_materials_unlit extension.", + type: "boolean", + default: defaults.unlit, + }, + metallicRoughnessOcclusionTexture: { + describe: + "Path to the metallic-roughness-occlusion texture that should override textures in the .mtl file, where occlusion is stored in the red channel, roughness is stored in the green channel, and metallic is stored in the blue channel. The model will be saved with a pbrMetallicRoughness material. This is often convenient in workflows where the .mtl does not exist or is not set up to use PBR materials. Intended for models with a single material", + type: "string", + normalize: true, + }, + specularGlossinessTexture: { + describe: + "Path to the specular-glossiness texture that should override textures in the .mtl file, where specular color is stored in the red, green, and blue channels and specular glossiness is stored in the alpha channel. The model will be saved with a material using the KHR_materials_pbrSpecularGlossiness extension.", + type: "string", + normalize: true, + }, + occlusionTexture: { + describe: + "Path to the occlusion texture that should override textures in the .mtl file.", + type: "string", + normalize: true, + }, + normalTexture: { + describe: + "Path to the normal texture that should override textures in the .mtl file.", + type: "string", + normalize: true, + }, + baseColorTexture: { + describe: + "Path to the baseColor/diffuse texture that should override textures in the .mtl file.", + type: "string", + normalize: true, + }, + emissiveTexture: { + describe: + "Path to the emissive texture that should override textures in the .mtl file.", + type: "string", + normalize: true, + }, + alphaTexture: { + describe: + "Path to the alpha texture that should override textures in the .mtl file.", + }, + inputUpAxis: { + describe: "Up axis of the obj.", + choices: ["X", "Y", "Z"], + type: "string", + default: "Y", + }, + outputUpAxis: { + describe: "Up axis of the converted glTF.", + choices: ["X", "Y", "Z"], + type: "string", + default: "Y", + }, + triangleWindingOrderSanitization: { + describe: "Apply triangle winding order sanitization.", + type: "boolean", + default: defaults.triangleWindingOrderSanitization, + }, + }) + .parse(args); if (argv.metallicRoughness + argv.specularGlossiness > 1) { - console.error('Only one material type may be set from [--metallicRoughness, --specularGlossiness].'); - process.exit(1); + console.error( + "Only one material type may be set from [--metallicRoughness, --specularGlossiness]." + ); + process.exit(1); } -if (defined(argv.metallicRoughnessOcclusionTexture) && defined(argv.specularGlossinessTexture)) { - console.error('--metallicRoughnessOcclusionTexture and --specularGlossinessTexture cannot both be set.'); - process.exit(1); +if ( + defined(argv.metallicRoughnessOcclusionTexture) && + defined(argv.specularGlossinessTexture) +) { + console.error( + "--metallicRoughnessOcclusionTexture and --specularGlossinessTexture cannot both be set." + ); + process.exit(1); } const objPath = argv.input; @@ -164,55 +186,55 @@ let gltfPath = argv.output; const filename = defaultValue(gltfPath, objPath); const name = path.basename(filename, path.extname(filename)); const outputDirectory = path.dirname(filename); -const binary = argv.binary || path.extname(filename).toLowerCase() === '.glb'; -const extension = binary ? '.glb' : '.gltf'; +const binary = argv.binary || path.extname(filename).toLowerCase() === ".glb"; +const extension = binary ? ".glb" : ".gltf"; gltfPath = path.join(outputDirectory, name + extension); const overridingTextures = { - metallicRoughnessOcclusionTexture : argv.metallicRoughnessOcclusionTexture, - specularGlossinessTexture : argv.specularGlossinessTexture, - occlusionTexture : argv.occlusionTexture, - normalTexture : argv.normalTexture, - baseColorTexture : argv.baseColorTexture, - emissiveTexture : argv.emissiveTexture, - alphaTexture : argv.alphaTexture + metallicRoughnessOcclusionTexture: argv.metallicRoughnessOcclusionTexture, + specularGlossinessTexture: argv.specularGlossinessTexture, + occlusionTexture: argv.occlusionTexture, + normalTexture: argv.normalTexture, + baseColorTexture: argv.baseColorTexture, + emissiveTexture: argv.emissiveTexture, + alphaTexture: argv.alphaTexture, }; const options = { - binary : binary, - separate : argv.separate, - separateTextures : argv.separateTextures, - checkTransparency : argv.checkTransparency, - secure : argv.secure, - packOcclusion : argv.packOcclusion, - metallicRoughness : argv.metallicRoughness, - specularGlossiness : argv.specularGlossiness, - unlit : argv.unlit, - overridingTextures : overridingTextures, - outputDirectory : outputDirectory, - inputUpAxis : argv.inputUpAxis, - outputUpAxis : argv.outputUpAxis, - triangleWindingOrderSanitization: argv.triangleWindingOrderSanitization + binary: binary, + separate: argv.separate, + separateTextures: argv.separateTextures, + checkTransparency: argv.checkTransparency, + secure: argv.secure, + packOcclusion: argv.packOcclusion, + metallicRoughness: argv.metallicRoughness, + specularGlossiness: argv.specularGlossiness, + unlit: argv.unlit, + overridingTextures: overridingTextures, + outputDirectory: outputDirectory, + inputUpAxis: argv.inputUpAxis, + outputUpAxis: argv.outputUpAxis, + triangleWindingOrderSanitization: argv.triangleWindingOrderSanitization, }; -console.time('Total'); +console.time("Total"); obj2gltf(objPath, options) - .then(function(gltf) { - if (binary) { - // gltf is a glb buffer - return fsExtra.outputFile(gltfPath, gltf); - } - const jsonOptions = { - spaces : 2 - }; - return fsExtra.outputJson(gltfPath, gltf, jsonOptions); - }) - .then(function() { - console.timeEnd('Total'); - }) - .catch(function(error) { - console.log(error.message); - process.exit(1); - }); + .then(function (gltf) { + if (binary) { + // gltf is a glb buffer + return fsExtra.outputFile(gltfPath, gltf); + } + const jsonOptions = { + spaces: 2, + }; + return fsExtra.outputJson(gltfPath, gltf, jsonOptions); + }) + .then(function () { + console.timeEnd("Total"); + }) + .catch(function (error) { + console.log(error.message); + process.exit(1); + }); diff --git a/gulpfile.js b/gulpfile.js index 7a8742e..6e1b9c1 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,108 +1,119 @@ -'use strict'; +"use strict"; -const Cesium = require('cesium'); -const Promise = require('bluebird'); -const child_process = require('child_process'); -const fsExtra = require('fs-extra'); -const gulp = require('gulp'); -const Jasmine = require('jasmine'); -const JasmineSpecReporter = require('jasmine-spec-reporter').SpecReporter; -const open = require('open'); -const path = require('path'); -const yargs = require('yargs'); +const Cesium = require("cesium"); +const Promise = require("bluebird"); +const child_process = require("child_process"); +const fsExtra = require("fs-extra"); +const gulp = require("gulp"); +const Jasmine = require("jasmine"); +const JasmineSpecReporter = require("jasmine-spec-reporter").SpecReporter; +const open = require("open"); +const path = require("path"); +const yargs = require("yargs"); const defined = Cesium.defined; const argv = yargs.argv; // Add third-party node module binaries to the system path // since some tasks need to call them directly. -const environmentSeparator = process.platform === 'win32' ? ';' : ':'; -const nodeBinaries = path.join(__dirname, 'node_modules', '.bin'); +const environmentSeparator = process.platform === "win32" ? ";" : ":"; +const nodeBinaries = path.join(__dirname, "node_modules", ".bin"); process.env.PATH += environmentSeparator + nodeBinaries; -const specFiles = ['**/*.js', '!node_modules/**', '!coverage/**', '!doc/**', '!bin/**']; +const specFiles = [ + "**/*.js", + "!node_modules/**", + "!coverage/**", + "!doc/**", + "!bin/**", +]; module.exports = { - test: test, - 'test-watch': testWatch, - coverage: coverage, - cloc: cloc + test: test, + "test-watch": testWatch, + coverage: coverage, + cloc: cloc, }; function test(done) { - const jasmine = new Jasmine(); - jasmine.loadConfigFile('specs/jasmine.json'); - jasmine.addReporter(new JasmineSpecReporter({ - displaySuccessfulSpec: !defined(argv.suppressPassed) || !argv.suppressPassed - })); - jasmine.execute(); - jasmine.onComplete(function (passed) { - done(argv.failTaskOnError && !passed ? 1 : 0); - }); + const jasmine = new Jasmine(); + jasmine.loadConfigFile("specs/jasmine.json"); + jasmine.addReporter( + new JasmineSpecReporter({ + displaySuccessfulSpec: + !defined(argv.suppressPassed) || !argv.suppressPassed, + }) + ); + jasmine.execute(); + jasmine.onComplete(function (passed) { + done(argv.failTaskOnError && !passed ? 1 : 0); + }); } function testWatch() { - return gulp.watch(specFiles).on('change', function () { - // We can't simply depend on the test task because Jasmine - // does not like being run multiple times in the same process. - try { - child_process.execSync('jasmine JASMINE_CONFIG_PATH=specs/jasmine.json', { - stdio: [process.stdin, process.stdout, process.stderr] - }); - } catch (exception) { - console.log('Tests failed to execute.'); - } - }); + return gulp.watch(specFiles).on("change", function () { + // We can't simply depend on the test task because Jasmine + // does not like being run multiple times in the same process. + try { + child_process.execSync("jasmine JASMINE_CONFIG_PATH=specs/jasmine.json", { + stdio: [process.stdin, process.stdout, process.stderr], + }); + } catch (exception) { + console.log("Tests failed to execute."); + } + }); } async function coverage() { - fsExtra.removeSync('coverage/server'); - child_process.execSync('nyc' + - ' --all' + - ' --reporter=lcov' + - ' --dir coverage' + - ' -x "specs/**" -x "coverage/**" -x "doc/**" -x "bin/**" -x "index.js" -x "gulpfile.js"' + - ' node_modules/jasmine/bin/jasmine.js' + - ' JASMINE_CONFIG_PATH=specs/jasmine.json', { - stdio: [process.stdin, process.stdout, process.stderr] - }); - open('coverage/lcov-report/index.html'); + fsExtra.removeSync("coverage/server"); + child_process.execSync( + "nyc" + + " --all" + + " --reporter=lcov" + + " --dir coverage" + + ' -x "specs/**" -x "coverage/**" -x "doc/**" -x "bin/**" -x "index.js" -x "gulpfile.js"' + + " node_modules/jasmine/bin/jasmine.js" + + " JASMINE_CONFIG_PATH=specs/jasmine.json", + { + stdio: [process.stdin, process.stdout, process.stderr], + } + ); + open("coverage/lcov-report/index.html"); } function cloc() { - let cmdLine; - const clocPath = path.join('node_modules', 'cloc', 'lib', 'cloc'); + let cmdLine; + const clocPath = path.join("node_modules", "cloc", "lib", "cloc"); - //Run cloc on primary Source files only - const source = new Promise(function(resolve, reject) { - cmdLine = 'perl ' + clocPath + ' --quiet --progress-rate=0' + - ' lib/ bin/'; + //Run cloc on primary Source files only + const source = new Promise(function (resolve, reject) { + cmdLine = "perl " + clocPath + " --quiet --progress-rate=0" + " lib/ bin/"; - child_process.exec(cmdLine, function(error, stdout, stderr) { - if (error) { - console.log(stderr); - return reject(error); - } - console.log('Source:'); - console.log(stdout); - resolve(); - }); + child_process.exec(cmdLine, function (error, stdout, stderr) { + if (error) { + console.log(stderr); + return reject(error); + } + console.log("Source:"); + console.log(stdout); + resolve(); }); + }); - //If running cloc on source succeeded, also run it on the tests. - return source.then(function() { - return new Promise(function(resolve, reject) { - cmdLine = 'perl ' + clocPath + ' --quiet --progress-rate=0' + - ' specs/lib/'; - child_process.exec(cmdLine, function(error, stdout, stderr) { - if (error) { - console.log(stderr); - return reject(error); - } - console.log('Specs:'); - console.log(stdout); - resolve(); - }); - }); + //If running cloc on source succeeded, also run it on the tests. + return source.then(function () { + return new Promise(function (resolve, reject) { + cmdLine = + "perl " + clocPath + " --quiet --progress-rate=0" + " specs/lib/"; + child_process.exec(cmdLine, function (error, stdout, stderr) { + if (error) { + console.log(stderr); + return reject(error); + } + console.log("Specs:"); + console.log(stdout); + resolve(); + }); }); + }); } diff --git a/index.js b/index.js index b157a83..2d64194 100644 --- a/index.js +++ b/index.js @@ -1,2 +1,2 @@ -'use strict'; -module.exports = require('./lib/obj2gltf'); +"use strict"; +module.exports = require("./lib/obj2gltf"); diff --git a/lib/ArrayStorage.js b/lib/ArrayStorage.js index 3cac2a3..6b0c1d5 100644 --- a/lib/ArrayStorage.js +++ b/lib/ArrayStorage.js @@ -1,5 +1,5 @@ -'use strict'; -const Cesium = require('cesium'); +"use strict"; +const Cesium = require("cesium"); const ComponentDatatype = Cesium.ComponentDatatype; @@ -18,89 +18,92 @@ const fixedExpansionLength = 33554432; // 2^25 (~134 MB for a Float32Array) * @private */ function ArrayStorage(componentDatatype) { - this.componentDatatype = componentDatatype; - this.typedArray = ComponentDatatype.createTypedArray(componentDatatype, 0); - this.length = 0; + this.componentDatatype = componentDatatype; + this.typedArray = ComponentDatatype.createTypedArray(componentDatatype, 0); + this.length = 0; } function resize(storage, length) { - const typedArray = ComponentDatatype.createTypedArray(storage.componentDatatype, length); - typedArray.set(storage.typedArray); - storage.typedArray = typedArray; + const typedArray = ComponentDatatype.createTypedArray( + storage.componentDatatype, + length + ); + typedArray.set(storage.typedArray); + storage.typedArray = typedArray; } -ArrayStorage.prototype.push = function(value) { - const length = this.length; - const typedArrayLength = this.typedArray.length; +ArrayStorage.prototype.push = function (value) { + const length = this.length; + const typedArrayLength = this.typedArray.length; - if (length === 0) { - resize(this, initialLength); - } else if (length === typedArrayLength) { - if (length < doublingThreshold) { - resize(this, typedArrayLength * 2); - } else { - resize(this, typedArrayLength + fixedExpansionLength); - } + if (length === 0) { + resize(this, initialLength); + } else if (length === typedArrayLength) { + if (length < doublingThreshold) { + resize(this, typedArrayLength * 2); + } else { + resize(this, typedArrayLength + fixedExpansionLength); } + } - this.typedArray[this.length++] = value; + this.typedArray[this.length++] = value; }; -ArrayStorage.prototype.get = function(index) { - return this.typedArray[index]; +ArrayStorage.prototype.get = function (index) { + return this.typedArray[index]; }; const sizeOfUint16 = 2; const sizeOfUint32 = 4; const sizeOfFloat = 4; -ArrayStorage.prototype.toUint16Buffer = function() { - const length = this.length; - const typedArray = this.typedArray; - const paddedLength = length + ((length % 2 === 0) ? 0 : 1); // Round to next multiple of 2 - const buffer = Buffer.alloc(paddedLength * sizeOfUint16); - for (let i = 0; i < length; ++i) { - buffer.writeUInt16LE(typedArray[i], i * sizeOfUint16); - } - return buffer; +ArrayStorage.prototype.toUint16Buffer = function () { + const length = this.length; + const typedArray = this.typedArray; + const paddedLength = length + (length % 2 === 0 ? 0 : 1); // Round to next multiple of 2 + const buffer = Buffer.alloc(paddedLength * sizeOfUint16); + for (let i = 0; i < length; ++i) { + buffer.writeUInt16LE(typedArray[i], i * sizeOfUint16); + } + return buffer; }; -ArrayStorage.prototype.toUint32Buffer = function() { - const length = this.length; - const typedArray = this.typedArray; - const buffer = Buffer.alloc(length * sizeOfUint32); - for (let i = 0; i < length; ++i) { - buffer.writeUInt32LE(typedArray[i], i * sizeOfUint32); - } - return buffer; +ArrayStorage.prototype.toUint32Buffer = function () { + const length = this.length; + const typedArray = this.typedArray; + const buffer = Buffer.alloc(length * sizeOfUint32); + for (let i = 0; i < length; ++i) { + buffer.writeUInt32LE(typedArray[i], i * sizeOfUint32); + } + return buffer; }; -ArrayStorage.prototype.toFloatBuffer = function() { - const length = this.length; - const typedArray = this.typedArray; - const buffer = Buffer.alloc(length * sizeOfFloat); - for (let i = 0; i < length; ++i) { - buffer.writeFloatLE(typedArray[i], i * sizeOfFloat); - } - return buffer; +ArrayStorage.prototype.toFloatBuffer = function () { + const length = this.length; + const typedArray = this.typedArray; + const buffer = Buffer.alloc(length * sizeOfFloat); + for (let i = 0; i < length; ++i) { + buffer.writeFloatLE(typedArray[i], i * sizeOfFloat); + } + return buffer; }; -ArrayStorage.prototype.getMinMax = function(components) { - const length = this.length; - const typedArray = this.typedArray; - const count = length / components; - const min = new Array(components).fill(Number.POSITIVE_INFINITY); - const max = new Array(components).fill(Number.NEGATIVE_INFINITY); - for (let i = 0; i < count; ++i) { - for (let j = 0; j < components; ++j) { - const index = i * components + j; - const value = typedArray[index]; - min[j] = Math.min(min[j], value); - max[j] = Math.max(max[j], value); - } +ArrayStorage.prototype.getMinMax = function (components) { + const length = this.length; + const typedArray = this.typedArray; + const count = length / components; + const min = new Array(components).fill(Number.POSITIVE_INFINITY); + const max = new Array(components).fill(Number.NEGATIVE_INFINITY); + for (let i = 0; i < count; ++i) { + for (let j = 0; j < components; ++j) { + const index = i * components + j; + const value = typedArray[index]; + min[j] = Math.min(min[j], value); + max[j] = Math.max(max[j], value); } - return { - min : min, - max : max - }; + } + return { + min: min, + max: max, + }; }; diff --git a/lib/Texture.js b/lib/Texture.js index aa44053..4df8a52 100644 --- a/lib/Texture.js +++ b/lib/Texture.js @@ -1,4 +1,4 @@ -'use strict'; +"use strict"; module.exports = Texture; @@ -8,12 +8,12 @@ module.exports = Texture; * @private */ function Texture() { - this.transparent = false; - this.source = undefined; - this.name = undefined; - this.extension = undefined; - this.path = undefined; - this.pixels = undefined; - this.width = undefined; - this.height = undefined; + this.transparent = false; + this.source = undefined; + this.name = undefined; + this.extension = undefined; + this.path = undefined; + this.pixels = undefined; + this.width = undefined; + this.height = undefined; } diff --git a/lib/createGltf.js b/lib/createGltf.js index 4d98382..bc2c28b 100644 --- a/lib/createGltf.js +++ b/lib/createGltf.js @@ -1,9 +1,9 @@ -'use strict'; -const BUFFER_MAX_BYTE_LENGTH = require('buffer').constants.MAX_LENGTH; -const Cesium = require('cesium'); -const getBufferPadded = require('./getBufferPadded'); -const getDefaultMaterial = require('./loadMtl').getDefaultMaterial; -const Texture = require('./Texture'); +"use strict"; +const BUFFER_MAX_BYTE_LENGTH = require("buffer").constants.MAX_LENGTH; +const Cesium = require("cesium"); +const getBufferPadded = require("./getBufferPadded"); +const getDefaultMaterial = require("./loadMtl").getDefaultMaterial; +const Texture = require("./Texture"); const defaultValue = Cesium.defaultValue; const defined = Cesium.defined; @@ -21,522 +21,690 @@ module.exports = createGltf; * @private */ function createGltf(objData, options) { - const nodes = objData.nodes; - let materials = objData.materials; - const name = objData.name; + const nodes = objData.nodes; + let materials = objData.materials; + const name = objData.name; - // Split materials used by primitives with different types of attributes - materials = splitIncompatibleMaterials(nodes, materials, options); + // Split materials used by primitives with different types of attributes + materials = splitIncompatibleMaterials(nodes, materials, options); - const gltf = { - accessors : [], - asset : {}, - buffers : [], - bufferViews : [], - extensionsUsed : [], - extensionsRequired : [], - images : [], - materials : [], - meshes : [], - nodes : [], - samplers : [], - scene : 0, - scenes : [], - textures : [] - }; + const gltf = { + accessors: [], + asset: {}, + buffers: [], + bufferViews: [], + extensionsUsed: [], + extensionsRequired: [], + images: [], + materials: [], + meshes: [], + nodes: [], + samplers: [], + scene: 0, + scenes: [], + textures: [], + }; - gltf.asset = { - generator : 'obj2gltf', - version: '2.0' - }; + gltf.asset = { + generator: "obj2gltf", + version: "2.0", + }; - gltf.scenes.push({ - nodes : [] + gltf.scenes.push({ + nodes: [], + }); + + const bufferState = { + positionBuffers: [], + normalBuffers: [], + uvBuffers: [], + indexBuffers: [], + positionAccessors: [], + normalAccessors: [], + uvAccessors: [], + indexAccessors: [], + }; + + const uint32Indices = requiresUint32Indices(nodes); + + const nodesLength = nodes.length; + for (let i = 0; i < nodesLength; ++i) { + const node = nodes[i]; + const meshes = node.meshes; + const meshesLength = meshes.length; + + if (meshesLength === 1) { + const meshIndex = addMesh( + gltf, + materials, + bufferState, + uint32Indices, + meshes[0], + options + ); + addNode(gltf, node.name, meshIndex, undefined); + } else { + // Add meshes as child nodes + const parentIndex = addNode(gltf, node.name); + for (let j = 0; j < meshesLength; ++j) { + const mesh = meshes[j]; + const meshIndex = addMesh( + gltf, + materials, + bufferState, + uint32Indices, + mesh, + options + ); + addNode(gltf, mesh.name, meshIndex, parentIndex); + } + } + } + + if (gltf.images.length > 0) { + gltf.samplers.push({ + wrapS: WebGLConstants.REPEAT, + wrapT: WebGLConstants.REPEAT, }); + } - const bufferState = { - positionBuffers : [], - normalBuffers : [], - uvBuffers : [], - indexBuffers : [], - positionAccessors : [], - normalAccessors : [], - uvAccessors : [], - indexAccessors : [] - }; + addBuffers(gltf, bufferState, name, options.separate); - const uint32Indices = requiresUint32Indices(nodes); + if (options.specularGlossiness) { + gltf.extensionsUsed.push("KHR_materials_pbrSpecularGlossiness"); + gltf.extensionsRequired.push("KHR_materials_pbrSpecularGlossiness"); + } - const nodesLength = nodes.length; - for (let i = 0; i < nodesLength; ++i) { - const node = nodes[i]; - const meshes = node.meshes; - const meshesLength = meshes.length; + if (options.unlit) { + gltf.extensionsUsed.push("KHR_materials_unlit"); + gltf.extensionsRequired.push("KHR_materials_unlit"); + } - if (meshesLength === 1) { - const meshIndex = addMesh(gltf, materials, bufferState, uint32Indices, meshes[0], options); - addNode(gltf, node.name, meshIndex, undefined); - } else { - // Add meshes as child nodes - const parentIndex = addNode(gltf, node.name); - for (let j = 0; j < meshesLength; ++j) { - const mesh = meshes[j]; - const meshIndex = addMesh(gltf, materials, bufferState, uint32Indices, mesh, options); - addNode(gltf, mesh.name, meshIndex, parentIndex); - } - } - } - - if (gltf.images.length > 0) { - gltf.samplers.push({ - wrapS : WebGLConstants.REPEAT, - wrapT : WebGLConstants.REPEAT - }); - } - - addBuffers(gltf, bufferState, name, options.separate); - - if (options.specularGlossiness) { - gltf.extensionsUsed.push('KHR_materials_pbrSpecularGlossiness'); - gltf.extensionsRequired.push('KHR_materials_pbrSpecularGlossiness'); - } - - if (options.unlit) { - gltf.extensionsUsed.push('KHR_materials_unlit'); - gltf.extensionsRequired.push('KHR_materials_unlit'); - } - - return gltf; + return gltf; } function addCombinedBufferView(gltf, buffers, accessors, byteStride, target) { - const length = buffers.length; - if (length === 0) { - return; - } - const bufferViewIndex = gltf.bufferViews.length; - const previousBufferView = gltf.bufferViews[bufferViewIndex - 1]; - const byteOffset = defined(previousBufferView) ? previousBufferView.byteOffset + previousBufferView.byteLength : 0; - let byteLength = 0; - for (let i = 0; i < length; ++i) { - const accessor = gltf.accessors[accessors[i]]; - accessor.bufferView = bufferViewIndex; - accessor.byteOffset = byteLength; - byteLength += buffers[i].length; - } - gltf.bufferViews.push({ - name : 'bufferView_' + bufferViewIndex, - buffer : 0, - byteLength : byteLength, - byteOffset : byteOffset, - byteStride : byteStride, - target : target - }); + const length = buffers.length; + if (length === 0) { + return; + } + const bufferViewIndex = gltf.bufferViews.length; + const previousBufferView = gltf.bufferViews[bufferViewIndex - 1]; + const byteOffset = defined(previousBufferView) + ? previousBufferView.byteOffset + previousBufferView.byteLength + : 0; + let byteLength = 0; + for (let i = 0; i < length; ++i) { + const accessor = gltf.accessors[accessors[i]]; + accessor.bufferView = bufferViewIndex; + accessor.byteOffset = byteLength; + byteLength += buffers[i].length; + } + gltf.bufferViews.push({ + name: "bufferView_" + bufferViewIndex, + buffer: 0, + byteLength: byteLength, + byteOffset: byteOffset, + byteStride: byteStride, + target: target, + }); } function addCombinedBuffers(gltf, bufferState, name) { - addCombinedBufferView(gltf, bufferState.positionBuffers, bufferState.positionAccessors, 12, WebGLConstants.ARRAY_BUFFER); - addCombinedBufferView(gltf, bufferState.normalBuffers, bufferState.normalAccessors, 12, WebGLConstants.ARRAY_BUFFER); - addCombinedBufferView(gltf, bufferState.uvBuffers, bufferState.uvAccessors, 8, WebGLConstants.ARRAY_BUFFER); - addCombinedBufferView(gltf, bufferState.indexBuffers, bufferState.indexAccessors, undefined, WebGLConstants.ELEMENT_ARRAY_BUFFER); + addCombinedBufferView( + gltf, + bufferState.positionBuffers, + bufferState.positionAccessors, + 12, + WebGLConstants.ARRAY_BUFFER + ); + addCombinedBufferView( + gltf, + bufferState.normalBuffers, + bufferState.normalAccessors, + 12, + WebGLConstants.ARRAY_BUFFER + ); + addCombinedBufferView( + gltf, + bufferState.uvBuffers, + bufferState.uvAccessors, + 8, + WebGLConstants.ARRAY_BUFFER + ); + addCombinedBufferView( + gltf, + bufferState.indexBuffers, + bufferState.indexAccessors, + undefined, + WebGLConstants.ELEMENT_ARRAY_BUFFER + ); - let buffers = []; - buffers = buffers.concat(bufferState.positionBuffers, bufferState.normalBuffers, bufferState.uvBuffers, bufferState.indexBuffers); - const buffer = getBufferPadded(Buffer.concat(buffers)); + let buffers = []; + buffers = buffers.concat( + bufferState.positionBuffers, + bufferState.normalBuffers, + bufferState.uvBuffers, + bufferState.indexBuffers + ); + const buffer = getBufferPadded(Buffer.concat(buffers)); - gltf.buffers.push({ - name : name, - byteLength : buffer.length, - extras : { - _obj2gltf : { - source : buffer - } - } - }); + gltf.buffers.push({ + name: name, + byteLength: buffer.length, + extras: { + _obj2gltf: { + source: buffer, + }, + }, + }); } -function addSeparateBufferView(gltf, buffer, accessor, byteStride, target, name) { - const bufferIndex = gltf.buffers.length; - const bufferViewIndex = gltf.bufferViews.length; +function addSeparateBufferView( + gltf, + buffer, + accessor, + byteStride, + target, + name +) { + const bufferIndex = gltf.buffers.length; + const bufferViewIndex = gltf.bufferViews.length; - gltf.buffers.push({ - name : name + '_' + bufferIndex, - byteLength : buffer.length, - extras : { - _obj2gltf : { - source : buffer - } - } - }); + gltf.buffers.push({ + name: name + "_" + bufferIndex, + byteLength: buffer.length, + extras: { + _obj2gltf: { + source: buffer, + }, + }, + }); - gltf.bufferViews.push({ - buffer : bufferIndex, - byteLength : buffer.length, - byteOffset : 0, - byteStride : byteStride, - target : target - }); + gltf.bufferViews.push({ + buffer: bufferIndex, + byteLength: buffer.length, + byteOffset: 0, + byteStride: byteStride, + target: target, + }); - gltf.accessors[accessor].bufferView = bufferViewIndex; - gltf.accessors[accessor].byteOffset = 0; + gltf.accessors[accessor].bufferView = bufferViewIndex; + gltf.accessors[accessor].byteOffset = 0; } -function addSeparateBufferViews(gltf, buffers, accessors, byteStride, target, name) { - const length = buffers.length; - for (let i = 0; i < length; ++i) { - addSeparateBufferView(gltf, buffers[i], accessors[i], byteStride, target, name); - } +function addSeparateBufferViews( + gltf, + buffers, + accessors, + byteStride, + target, + name +) { + const length = buffers.length; + for (let i = 0; i < length; ++i) { + addSeparateBufferView( + gltf, + buffers[i], + accessors[i], + byteStride, + target, + name + ); + } } function addSeparateBuffers(gltf, bufferState, name) { - addSeparateBufferViews(gltf, bufferState.positionBuffers, bufferState.positionAccessors, 12, WebGLConstants.ARRAY_BUFFER, name); - addSeparateBufferViews(gltf, bufferState.normalBuffers, bufferState.normalAccessors, 12, WebGLConstants.ARRAY_BUFFER, name); - addSeparateBufferViews(gltf, bufferState.uvBuffers, bufferState.uvAccessors, 8, WebGLConstants.ARRAY_BUFFER, name); - addSeparateBufferViews(gltf, bufferState.indexBuffers, bufferState.indexAccessors, undefined, WebGLConstants.ELEMENT_ARRAY_BUFFER, name); + addSeparateBufferViews( + gltf, + bufferState.positionBuffers, + bufferState.positionAccessors, + 12, + WebGLConstants.ARRAY_BUFFER, + name + ); + addSeparateBufferViews( + gltf, + bufferState.normalBuffers, + bufferState.normalAccessors, + 12, + WebGLConstants.ARRAY_BUFFER, + name + ); + addSeparateBufferViews( + gltf, + bufferState.uvBuffers, + bufferState.uvAccessors, + 8, + WebGLConstants.ARRAY_BUFFER, + name + ); + addSeparateBufferViews( + gltf, + bufferState.indexBuffers, + bufferState.indexAccessors, + undefined, + WebGLConstants.ELEMENT_ARRAY_BUFFER, + name + ); } function addBuffers(gltf, bufferState, name, separate) { - const buffers = bufferState.positionBuffers.concat(bufferState.normalBuffers, bufferState.uvBuffers, bufferState.indexBuffers); - const buffersLength = buffers.length; - let buffersByteLength = 0; - for (let i = 0; i < buffersLength; ++i) { - buffersByteLength += buffers[i].length; - } + const buffers = bufferState.positionBuffers.concat( + bufferState.normalBuffers, + bufferState.uvBuffers, + bufferState.indexBuffers + ); + const buffersLength = buffers.length; + let buffersByteLength = 0; + for (let i = 0; i < buffersLength; ++i) { + buffersByteLength += buffers[i].length; + } - if (separate && (buffersByteLength > createGltf._getBufferMaxByteLength())) { - // Don't combine buffers if the combined buffer will exceed the Node limit. - addSeparateBuffers(gltf, bufferState, name); - } else { - addCombinedBuffers(gltf, bufferState, name); - } + if (separate && buffersByteLength > createGltf._getBufferMaxByteLength()) { + // Don't combine buffers if the combined buffer will exceed the Node limit. + addSeparateBuffers(gltf, bufferState, name); + } else { + addCombinedBuffers(gltf, bufferState, name); + } } function addTexture(gltf, texture) { - const imageName = texture.name; - const textureName = texture.name; - const imageIndex = gltf.images.length; - const textureIndex = gltf.textures.length; + const imageName = texture.name; + const textureName = texture.name; + const imageIndex = gltf.images.length; + const textureIndex = gltf.textures.length; - gltf.images.push({ - name : imageName, - extras : { - _obj2gltf : texture - } - }); + gltf.images.push({ + name: imageName, + extras: { + _obj2gltf: texture, + }, + }); - gltf.textures.push({ - name : textureName, - sampler : 0, - source : imageIndex - }); + gltf.textures.push({ + name: textureName, + sampler: 0, + source: imageIndex, + }); - return textureIndex; + return textureIndex; } function getTexture(gltf, texture) { - let textureIndex; - const images = gltf.images; - const length = images.length; - for (let i = 0; i < length; ++i) { - if (images[i].extras._obj2gltf === texture) { - textureIndex = i; - break; - } + let textureIndex; + const images = gltf.images; + const length = images.length; + for (let i = 0; i < length; ++i) { + if (images[i].extras._obj2gltf === texture) { + textureIndex = i; + break; } + } - if (!defined(textureIndex)) { - textureIndex = addTexture(gltf, texture); - } + if (!defined(textureIndex)) { + textureIndex = addTexture(gltf, texture); + } - return { - index : textureIndex - }; + return { + index: textureIndex, + }; } function cloneMaterial(material, removeTextures) { - if (typeof material !== 'object') { - return material; - } else if (material instanceof Texture) { - if (removeTextures) { - return undefined; - } - return material; - } else if (Array.isArray(material)) { - const length = material.length; - const clonedArray = new Array(length); - for (let i = 0; i < length; ++i) { - clonedArray[i] = cloneMaterial(material[i], removeTextures); - } - return clonedArray; + if (typeof material !== "object") { + return material; + } else if (material instanceof Texture) { + if (removeTextures) { + return undefined; } - const clonedObject = {}; - for (const name in material) { - if (Object.prototype.hasOwnProperty.call(material, name)) { - clonedObject[name] = cloneMaterial(material[name], removeTextures); - } + return material; + } else if (Array.isArray(material)) { + const length = material.length; + const clonedArray = new Array(length); + for (let i = 0; i < length; ++i) { + clonedArray[i] = cloneMaterial(material[i], removeTextures); } - return clonedObject; + return clonedArray; + } + const clonedObject = {}; + for (const name in material) { + if (Object.prototype.hasOwnProperty.call(material, name)) { + clonedObject[name] = cloneMaterial(material[name], removeTextures); + } + } + return clonedObject; } function resolveTextures(gltf, material) { - for (const name in material) { - if (Object.prototype.hasOwnProperty.call(material, name)) { - const property = material[name]; - if (property instanceof Texture) { - material[name] = getTexture(gltf, property); - } else if (!Array.isArray(property) && (typeof property === 'object')) { - resolveTextures(gltf, property); - } - } + for (const name in material) { + if (Object.prototype.hasOwnProperty.call(material, name)) { + const property = material[name]; + if (property instanceof Texture) { + material[name] = getTexture(gltf, property); + } else if (!Array.isArray(property) && typeof property === "object") { + resolveTextures(gltf, property); + } } + } } function addGltfMaterial(gltf, material, options) { - resolveTextures(gltf, material); - const materialIndex = gltf.materials.length; - if (options.unlit) { - if (!defined(material.extensions)) { - material.extensions = {}; - } - material.extensions.KHR_materials_unlit = {}; + resolveTextures(gltf, material); + const materialIndex = gltf.materials.length; + if (options.unlit) { + if (!defined(material.extensions)) { + material.extensions = {}; } - gltf.materials.push(material); - return materialIndex; + material.extensions.KHR_materials_unlit = {}; + } + gltf.materials.push(material); + return materialIndex; } function getMaterialByName(materials, materialName) { - const materialsLength = materials.length; - for (let i = 0; i < materialsLength; ++i) { - if (materials[i].name === materialName) { - return materials[i]; - } + const materialsLength = materials.length; + for (let i = 0; i < materialsLength; ++i) { + if (materials[i].name === materialName) { + return materials[i]; } + } } function getMaterialIndex(materials, materialName) { - const materialsLength = materials.length; - for (let i = 0; i < materialsLength; ++i) { - if (materials[i].name === materialName) { - return i; - } + const materialsLength = materials.length; + for (let i = 0; i < materialsLength; ++i) { + if (materials[i].name === materialName) { + return i; } + } } function getOrCreateGltfMaterial(gltf, materials, materialName, options) { - const material = getMaterialByName(materials, materialName); - let materialIndex = getMaterialIndex(gltf.materials, materialName); + const material = getMaterialByName(materials, materialName); + let materialIndex = getMaterialIndex(gltf.materials, materialName); - if (!defined(materialIndex)) { - materialIndex = addGltfMaterial(gltf, material, options); - } + if (!defined(materialIndex)) { + materialIndex = addGltfMaterial(gltf, material, options); + } - return materialIndex; + return materialIndex; } function primitiveInfoMatch(a, b) { - return a.hasUvs === b.hasUvs && - a.hasNormals === b.hasNormals; + return a.hasUvs === b.hasUvs && a.hasNormals === b.hasNormals; } -function getSplitMaterialName(originalMaterialName, primitiveInfo, primitiveInfoByMaterial) { - let splitMaterialName = originalMaterialName; - let suffix = 2; - while (defined(primitiveInfoByMaterial[splitMaterialName])) { - if (primitiveInfoMatch(primitiveInfo, primitiveInfoByMaterial[splitMaterialName])) { - break; - } - splitMaterialName = originalMaterialName + '-' + suffix++; +function getSplitMaterialName( + originalMaterialName, + primitiveInfo, + primitiveInfoByMaterial +) { + let splitMaterialName = originalMaterialName; + let suffix = 2; + while (defined(primitiveInfoByMaterial[splitMaterialName])) { + if ( + primitiveInfoMatch( + primitiveInfo, + primitiveInfoByMaterial[splitMaterialName] + ) + ) { + break; } - return splitMaterialName; + splitMaterialName = originalMaterialName + "-" + suffix++; + } + return splitMaterialName; } function splitIncompatibleMaterials(nodes, materials, options) { - const splitMaterials = []; - const primitiveInfoByMaterial = {}; - const nodesLength = nodes.length; - for (let i = 0; i < nodesLength; ++i) { - const meshes = nodes[i].meshes; - const meshesLength = meshes.length; - for (let j = 0; j < meshesLength; ++j) { - const primitives = meshes[j].primitives; - const primitivesLength = primitives.length; - for (let k = 0; k < primitivesLength; ++k) { - const primitive = primitives[k]; - const hasUvs = primitive.uvs.length > 0; - const hasNormals = primitive.normals.length > 0; - const primitiveInfo = { - hasUvs : hasUvs, - hasNormals : hasNormals - }; - const originalMaterialName = defaultValue(primitive.material, 'default'); - const splitMaterialName = getSplitMaterialName(originalMaterialName, primitiveInfo, primitiveInfoByMaterial); - primitive.material = splitMaterialName; - primitiveInfoByMaterial[splitMaterialName] = primitiveInfo; + const splitMaterials = []; + const primitiveInfoByMaterial = {}; + const nodesLength = nodes.length; + for (let i = 0; i < nodesLength; ++i) { + const meshes = nodes[i].meshes; + const meshesLength = meshes.length; + for (let j = 0; j < meshesLength; ++j) { + const primitives = meshes[j].primitives; + const primitivesLength = primitives.length; + for (let k = 0; k < primitivesLength; ++k) { + const primitive = primitives[k]; + const hasUvs = primitive.uvs.length > 0; + const hasNormals = primitive.normals.length > 0; + const primitiveInfo = { + hasUvs: hasUvs, + hasNormals: hasNormals, + }; + const originalMaterialName = defaultValue( + primitive.material, + "default" + ); + const splitMaterialName = getSplitMaterialName( + originalMaterialName, + primitiveInfo, + primitiveInfoByMaterial + ); + primitive.material = splitMaterialName; + primitiveInfoByMaterial[splitMaterialName] = primitiveInfo; - let splitMaterial = getMaterialByName(splitMaterials, splitMaterialName); - if (defined(splitMaterial)) { - continue; - } - - const originalMaterial = getMaterialByName(materials, originalMaterialName); - if (defined(originalMaterial)) { - splitMaterial = cloneMaterial(originalMaterial, !hasUvs); - } else { - splitMaterial = getDefaultMaterial(options); - } - splitMaterial.name = splitMaterialName; - splitMaterials.push(splitMaterial); - } + let splitMaterial = getMaterialByName( + splitMaterials, + splitMaterialName + ); + if (defined(splitMaterial)) { + continue; } + + const originalMaterial = getMaterialByName( + materials, + originalMaterialName + ); + if (defined(originalMaterial)) { + splitMaterial = cloneMaterial(originalMaterial, !hasUvs); + } else { + splitMaterial = getDefaultMaterial(options); + } + splitMaterial.name = splitMaterialName; + splitMaterials.push(splitMaterial); + } } - return splitMaterials; + } + return splitMaterials; } function addVertexAttribute(gltf, array, components, name) { - const count = array.length / components; - const minMax = array.getMinMax(components); - const type = (components === 3 ? 'VEC3' : 'VEC2'); + const count = array.length / components; + const minMax = array.getMinMax(components); + const type = components === 3 ? "VEC3" : "VEC2"; - const accessor = { - name : name, - componentType : WebGLConstants.FLOAT, - count : count, - min : minMax.min, - max : minMax.max, - type : type - }; + const accessor = { + name: name, + componentType: WebGLConstants.FLOAT, + count: count, + min: minMax.min, + max: minMax.max, + type: type, + }; - const accessorIndex = gltf.accessors.length; - gltf.accessors.push(accessor); - return accessorIndex; + const accessorIndex = gltf.accessors.length; + gltf.accessors.push(accessor); + return accessorIndex; } function addIndexArray(gltf, array, uint32Indices, name) { - const componentType = uint32Indices ? WebGLConstants.UNSIGNED_INT : WebGLConstants.UNSIGNED_SHORT; - const count = array.length; - const minMax = array.getMinMax(1); + const componentType = uint32Indices + ? WebGLConstants.UNSIGNED_INT + : WebGLConstants.UNSIGNED_SHORT; + const count = array.length; + const minMax = array.getMinMax(1); - const accessor = { - name : name, - componentType : componentType, - count : count, - min : minMax.min, - max : minMax.max, - type : 'SCALAR' - }; + const accessor = { + name: name, + componentType: componentType, + count: count, + min: minMax.min, + max: minMax.max, + type: "SCALAR", + }; - const accessorIndex = gltf.accessors.length; - gltf.accessors.push(accessor); - return accessorIndex; + const accessorIndex = gltf.accessors.length; + gltf.accessors.push(accessor); + return accessorIndex; } function requiresUint32Indices(nodes) { - const nodesLength = nodes.length; - for (let i = 0; i < nodesLength; ++i) { - const meshes = nodes[i].meshes; - const meshesLength = meshes.length; - for (let j = 0; j < meshesLength; ++j) { - const primitives = meshes[j].primitives; - const primitivesLength = primitives.length; - for (let k = 0; k < primitivesLength; ++k) { - // Reserve the 65535 index for primitive restart - const vertexCount = primitives[k].positions.length / 3; - if (vertexCount > 65534) { - return true; - } - } + const nodesLength = nodes.length; + for (let i = 0; i < nodesLength; ++i) { + const meshes = nodes[i].meshes; + const meshesLength = meshes.length; + for (let j = 0; j < meshesLength; ++j) { + const primitives = meshes[j].primitives; + const primitivesLength = primitives.length; + for (let k = 0; k < primitivesLength; ++k) { + // Reserve the 65535 index for primitive restart + const vertexCount = primitives[k].positions.length / 3; + if (vertexCount > 65534) { + return true; } + } } - return false; + } + return false; } -function addPrimitive(gltf, materials, bufferState, uint32Indices, mesh, primitive, index, options) { - const hasPositions = primitive.positions.length > 0; - const hasNormals = primitive.normals.length > 0; - const hasUVs = primitive.uvs.length > 0; +function addPrimitive( + gltf, + materials, + bufferState, + uint32Indices, + mesh, + primitive, + index, + options +) { + const hasPositions = primitive.positions.length > 0; + const hasNormals = primitive.normals.length > 0; + const hasUVs = primitive.uvs.length > 0; - const attributes = {}; - if (hasPositions) { - const accessorIndex = addVertexAttribute(gltf, primitive.positions, 3, mesh.name + '_' + index + '_positions'); - attributes.POSITION = accessorIndex; - bufferState.positionBuffers.push(primitive.positions.toFloatBuffer()); - bufferState.positionAccessors.push(accessorIndex); - } - if (hasNormals) { - const accessorIndex = addVertexAttribute(gltf, primitive.normals, 3, mesh.name + '_' + index + '_normals'); - attributes.NORMAL = accessorIndex; - bufferState.normalBuffers.push(primitive.normals.toFloatBuffer()); - bufferState.normalAccessors.push(accessorIndex); - } - if (hasUVs) { - const accessorIndex = addVertexAttribute(gltf, primitive.uvs, 2, mesh.name + '_' + index + '_texcoords'); - attributes.TEXCOORD_0 = accessorIndex; - bufferState.uvBuffers.push(primitive.uvs.toFloatBuffer()); - bufferState.uvAccessors.push(accessorIndex); - } + const attributes = {}; + if (hasPositions) { + const accessorIndex = addVertexAttribute( + gltf, + primitive.positions, + 3, + mesh.name + "_" + index + "_positions" + ); + attributes.POSITION = accessorIndex; + bufferState.positionBuffers.push(primitive.positions.toFloatBuffer()); + bufferState.positionAccessors.push(accessorIndex); + } + if (hasNormals) { + const accessorIndex = addVertexAttribute( + gltf, + primitive.normals, + 3, + mesh.name + "_" + index + "_normals" + ); + attributes.NORMAL = accessorIndex; + bufferState.normalBuffers.push(primitive.normals.toFloatBuffer()); + bufferState.normalAccessors.push(accessorIndex); + } + if (hasUVs) { + const accessorIndex = addVertexAttribute( + gltf, + primitive.uvs, + 2, + mesh.name + "_" + index + "_texcoords" + ); + attributes.TEXCOORD_0 = accessorIndex; + bufferState.uvBuffers.push(primitive.uvs.toFloatBuffer()); + bufferState.uvAccessors.push(accessorIndex); + } - const indexAccessorIndex = addIndexArray(gltf, primitive.indices, uint32Indices, mesh.name + '_' + index + '_indices'); - const indexBuffer = uint32Indices ? primitive.indices.toUint32Buffer() : primitive.indices.toUint16Buffer(); - bufferState.indexBuffers.push(indexBuffer); - bufferState.indexAccessors.push(indexAccessorIndex); + const indexAccessorIndex = addIndexArray( + gltf, + primitive.indices, + uint32Indices, + mesh.name + "_" + index + "_indices" + ); + const indexBuffer = uint32Indices + ? primitive.indices.toUint32Buffer() + : primitive.indices.toUint16Buffer(); + bufferState.indexBuffers.push(indexBuffer); + bufferState.indexAccessors.push(indexAccessorIndex); - // Unload resources - primitive.positions = undefined; - primitive.normals = undefined; - primitive.uvs = undefined; - primitive.indices = undefined; + // Unload resources + primitive.positions = undefined; + primitive.normals = undefined; + primitive.uvs = undefined; + primitive.indices = undefined; - const materialIndex = getOrCreateGltfMaterial(gltf, materials, primitive.material, options); + const materialIndex = getOrCreateGltfMaterial( + gltf, + materials, + primitive.material, + options + ); - return { - attributes : attributes, - indices : indexAccessorIndex, - material : materialIndex, - mode : WebGLConstants.TRIANGLES - }; + return { + attributes: attributes, + indices: indexAccessorIndex, + material: materialIndex, + mode: WebGLConstants.TRIANGLES, + }; } function addMesh(gltf, materials, bufferState, uint32Indices, mesh, options) { - const gltfPrimitives = []; - const primitives = mesh.primitives; - const primitivesLength = primitives.length; - for (let i = 0; i < primitivesLength; ++i) { - gltfPrimitives.push(addPrimitive(gltf, materials, bufferState, uint32Indices, mesh, primitives[i], i, options)); - } + const gltfPrimitives = []; + const primitives = mesh.primitives; + const primitivesLength = primitives.length; + for (let i = 0; i < primitivesLength; ++i) { + gltfPrimitives.push( + addPrimitive( + gltf, + materials, + bufferState, + uint32Indices, + mesh, + primitives[i], + i, + options + ) + ); + } - const gltfMesh = { - name : mesh.name, - primitives : gltfPrimitives - }; + const gltfMesh = { + name: mesh.name, + primitives: gltfPrimitives, + }; - const meshIndex = gltf.meshes.length; - gltf.meshes.push(gltfMesh); - return meshIndex; + const meshIndex = gltf.meshes.length; + gltf.meshes.push(gltfMesh); + return meshIndex; } function addNode(gltf, name, meshIndex, parentIndex) { - const node = { - name : name, - mesh : meshIndex - }; + const node = { + name: name, + mesh: meshIndex, + }; - const nodeIndex = gltf.nodes.length; - gltf.nodes.push(node); + const nodeIndex = gltf.nodes.length; + gltf.nodes.push(node); - if (defined(parentIndex)) { - const parentNode = gltf.nodes[parentIndex]; - if (!defined(parentNode.children)) { - parentNode.children = []; - } - parentNode.children.push(nodeIndex); - } else { - gltf.scenes[gltf.scene].nodes.push(nodeIndex); + if (defined(parentIndex)) { + const parentNode = gltf.nodes[parentIndex]; + if (!defined(parentNode.children)) { + parentNode.children = []; } + parentNode.children.push(nodeIndex); + } else { + gltf.scenes[gltf.scene].nodes.push(nodeIndex); + } - return nodeIndex; + return nodeIndex; } // Exposed for testing -createGltf._getBufferMaxByteLength = function() { - return BUFFER_MAX_BYTE_LENGTH; +createGltf._getBufferMaxByteLength = function () { + return BUFFER_MAX_BYTE_LENGTH; }; diff --git a/lib/getBufferPadded.js b/lib/getBufferPadded.js index be824e3..4942263 100644 --- a/lib/getBufferPadded.js +++ b/lib/getBufferPadded.js @@ -1,4 +1,4 @@ -'use strict'; +"use strict"; module.exports = getBufferPadded; /** @@ -10,13 +10,13 @@ module.exports = getBufferPadded; * @private */ function getBufferPadded(buffer) { - const boundary = 4; - const byteLength = buffer.length; - const remainder = byteLength % boundary; - if (remainder === 0) { - return buffer; - } - const padding = (remainder === 0) ? 0 : boundary - remainder; - const emptyBuffer = Buffer.alloc(padding); - return Buffer.concat([buffer, emptyBuffer]); + const boundary = 4; + const byteLength = buffer.length; + const remainder = byteLength % boundary; + if (remainder === 0) { + return buffer; + } + const padding = remainder === 0 ? 0 : boundary - remainder; + const emptyBuffer = Buffer.alloc(padding); + return Buffer.concat([buffer, emptyBuffer]); } diff --git a/lib/getJsonBufferPadded.js b/lib/getJsonBufferPadded.js index 6cb6fc9..ff4d873 100644 --- a/lib/getJsonBufferPadded.js +++ b/lib/getJsonBufferPadded.js @@ -1,4 +1,4 @@ -'use strict'; +"use strict"; module.exports = getJsonBufferPadded; /** @@ -13,17 +13,17 @@ module.exports = getJsonBufferPadded; * @private */ function getJsonBufferPadded(json) { - let string = JSON.stringify(json); + let string = JSON.stringify(json); - const boundary = 4; - const byteLength = Buffer.byteLength(string); - const remainder = byteLength % boundary; - const padding = (remainder === 0) ? 0 : boundary - remainder; - let whitespace = ''; - for (let i = 0; i < padding; ++i) { - whitespace += ' '; - } - string += whitespace; + const boundary = 4; + const byteLength = Buffer.byteLength(string); + const remainder = byteLength % boundary; + const padding = remainder === 0 ? 0 : boundary - remainder; + let whitespace = ""; + for (let i = 0; i < padding; ++i) { + whitespace += " "; + } + string += whitespace; - return Buffer.from(string); + return Buffer.from(string); } diff --git a/lib/gltfToGlb.js b/lib/gltfToGlb.js index 3f83309..b4268a0 100644 --- a/lib/gltfToGlb.js +++ b/lib/gltfToGlb.js @@ -1,6 +1,6 @@ -'use strict'; -const Cesium = require('cesium'); -const getJsonBufferPadded = require('./getJsonBufferPadded'); +"use strict"; +const Cesium = require("cesium"); +const getJsonBufferPadded = require("./getJsonBufferPadded"); const defined = Cesium.defined; @@ -18,44 +18,44 @@ module.exports = gltfToGlb; * @private */ function gltfToGlb(gltf, binaryBuffer) { - const buffer = gltf.buffers[0]; - if (defined(buffer.uri)) { - binaryBuffer = Buffer.alloc(0); - } + const buffer = gltf.buffers[0]; + if (defined(buffer.uri)) { + binaryBuffer = Buffer.alloc(0); + } - // Create padded binary scene string - const jsonBuffer = getJsonBufferPadded(gltf); + // Create padded binary scene string + const jsonBuffer = getJsonBufferPadded(gltf); - // Allocate buffer (Global header) + (JSON chunk header) + (JSON chunk) + (Binary chunk header) + (Binary chunk) - const glbLength = 12 + 8 + jsonBuffer.length + 8 + binaryBuffer.length; - const glb = Buffer.alloc(glbLength); + // Allocate buffer (Global header) + (JSON chunk header) + (JSON chunk) + (Binary chunk header) + (Binary chunk) + const glbLength = 12 + 8 + jsonBuffer.length + 8 + binaryBuffer.length; + const glb = Buffer.alloc(glbLength); - // Write binary glTF header (magic, version, length) - let byteOffset = 0; - glb.writeUInt32LE(0x46546C67, byteOffset); - byteOffset += 4; - glb.writeUInt32LE(2, byteOffset); - byteOffset += 4; - glb.writeUInt32LE(glbLength, byteOffset); - byteOffset += 4; + // Write binary glTF header (magic, version, length) + let byteOffset = 0; + glb.writeUInt32LE(0x46546c67, byteOffset); + byteOffset += 4; + glb.writeUInt32LE(2, byteOffset); + byteOffset += 4; + glb.writeUInt32LE(glbLength, byteOffset); + byteOffset += 4; - // Write JSON Chunk header (length, type) - glb.writeUInt32LE(jsonBuffer.length, byteOffset); - byteOffset += 4; - glb.writeUInt32LE(0x4E4F534A, byteOffset); // JSON - byteOffset += 4; + // Write JSON Chunk header (length, type) + glb.writeUInt32LE(jsonBuffer.length, byteOffset); + byteOffset += 4; + glb.writeUInt32LE(0x4e4f534a, byteOffset); // JSON + byteOffset += 4; - // Write JSON Chunk - jsonBuffer.copy(glb, byteOffset); - byteOffset += jsonBuffer.length; + // Write JSON Chunk + jsonBuffer.copy(glb, byteOffset); + byteOffset += jsonBuffer.length; - // Write Binary Chunk header (length, type) - glb.writeUInt32LE(binaryBuffer.length, byteOffset); - byteOffset += 4; - glb.writeUInt32LE(0x004E4942, byteOffset); // BIN - byteOffset += 4; + // Write Binary Chunk header (length, type) + glb.writeUInt32LE(binaryBuffer.length, byteOffset); + byteOffset += 4; + glb.writeUInt32LE(0x004e4942, byteOffset); // BIN + byteOffset += 4; - // Write Binary Chunk - binaryBuffer.copy(glb, byteOffset); - return glb; + // Write Binary Chunk + binaryBuffer.copy(glb, byteOffset); + return glb; } diff --git a/lib/loadMtl.js b/lib/loadMtl.js index 7a3d00d..b0fe34f 100644 --- a/lib/loadMtl.js +++ b/lib/loadMtl.js @@ -1,11 +1,11 @@ -'use strict'; -const Cesium = require('cesium'); -const path = require('path'); -const Promise = require('bluebird'); -const loadTexture = require('./loadTexture'); -const outsideDirectory = require('./outsideDirectory'); -const readLines = require('./readLines'); -const Texture = require('./Texture'); +"use strict"; +const Cesium = require("cesium"); +const path = require("path"); +const Promise = require("bluebird"); +const loadTexture = require("./loadTexture"); +const outsideDirectory = require("./outsideDirectory"); +const readLines = require("./readLines"); +const Texture = require("./Texture"); const CesiumMath = Cesium.Math; const clone = Cesium.clone; @@ -31,725 +31,989 @@ module.exports = loadMtl; * @private */ function loadMtl(mtlPath, options) { - let material; - let values; - let value; + let material; + let values; + let value; - const mtlDirectory = path.dirname(mtlPath); - const materials = []; - const texturePromiseMap = {}; // Maps texture paths to load promises so that no texture is loaded twice - const texturePromises = []; + const mtlDirectory = path.dirname(mtlPath); + const materials = []; + const texturePromiseMap = {}; // Maps texture paths to load promises so that no texture is loaded twice + const texturePromises = []; - const overridingTextures = options.overridingTextures; - const overridingSpecularTexture = defaultValue(overridingTextures.metallicRoughnessOcclusionTexture, overridingTextures.specularGlossinessTexture); - const overridingSpecularShininessTexture = defaultValue(overridingTextures.metallicRoughnessOcclusionTexture, overridingTextures.specularGlossinessTexture); - const overridingAmbientTexture = defaultValue(overridingTextures.metallicRoughnessOcclusionTexture, overridingTextures.occlusionTexture); - const overridingNormalTexture = overridingTextures.normalTexture; - const overridingDiffuseTexture = overridingTextures.baseColorTexture; - const overridingEmissiveTexture = overridingTextures.emissiveTexture; - const overridingAlphaTexture = overridingTextures.alphaTexture; + const overridingTextures = options.overridingTextures; + const overridingSpecularTexture = defaultValue( + overridingTextures.metallicRoughnessOcclusionTexture, + overridingTextures.specularGlossinessTexture + ); + const overridingSpecularShininessTexture = defaultValue( + overridingTextures.metallicRoughnessOcclusionTexture, + overridingTextures.specularGlossinessTexture + ); + const overridingAmbientTexture = defaultValue( + overridingTextures.metallicRoughnessOcclusionTexture, + overridingTextures.occlusionTexture + ); + const overridingNormalTexture = overridingTextures.normalTexture; + const overridingDiffuseTexture = overridingTextures.baseColorTexture; + const overridingEmissiveTexture = overridingTextures.emissiveTexture; + const overridingAlphaTexture = overridingTextures.alphaTexture; - // Textures that are packed into PBR textures need to be decoded first - const decodeOptions = { - decode : true - }; + // Textures that are packed into PBR textures need to be decoded first + const decodeOptions = { + decode: true, + }; - const diffuseTextureOptions = { - checkTransparency : options.checkTransparency - }; + const diffuseTextureOptions = { + checkTransparency: options.checkTransparency, + }; - const ambientTextureOptions = defined(overridingAmbientTexture) ? undefined : (options.packOcclusion ? decodeOptions : undefined); - const specularTextureOptions = defined(overridingSpecularTexture) ? undefined : decodeOptions; - const specularShinessTextureOptions = defined(overridingSpecularShininessTexture) ? undefined : decodeOptions; - const emissiveTextureOptions = undefined; - const normalTextureOptions = undefined; - const alphaTextureOptions = { - decode : true - }; + const ambientTextureOptions = defined(overridingAmbientTexture) + ? undefined + : options.packOcclusion + ? decodeOptions + : undefined; + const specularTextureOptions = defined(overridingSpecularTexture) + ? undefined + : decodeOptions; + const specularShinessTextureOptions = defined( + overridingSpecularShininessTexture + ) + ? undefined + : decodeOptions; + const emissiveTextureOptions = undefined; + const normalTextureOptions = undefined; + const alphaTextureOptions = { + decode: true, + }; - function createMaterial(name) { - material = new Material(); - material.name = name; - material.specularShininess = options.metallicRoughness ? 1.0 : 0.0; - material.specularTexture = overridingSpecularTexture; - material.specularShininessTexture = overridingSpecularShininessTexture; - material.diffuseTexture = overridingDiffuseTexture; - material.ambientTexture = overridingAmbientTexture; - material.normalTexture = overridingNormalTexture; - material.emissiveTexture = overridingEmissiveTexture; - material.alphaTexture = overridingAlphaTexture; - materials.push(material); + function createMaterial(name) { + material = new Material(); + material.name = name; + material.specularShininess = options.metallicRoughness ? 1.0 : 0.0; + material.specularTexture = overridingSpecularTexture; + material.specularShininessTexture = overridingSpecularShininessTexture; + material.diffuseTexture = overridingDiffuseTexture; + material.ambientTexture = overridingAmbientTexture; + material.normalTexture = overridingNormalTexture; + material.emissiveTexture = overridingEmissiveTexture; + material.alphaTexture = overridingAlphaTexture; + materials.push(material); + } + + function normalizeTexturePath(texturePath, mtlDirectory) { + // Removes texture options from texture name + // Assumes no spaces in texture name + const re = /-(bm|t|s|o|blendu|blendv|boost|mm|texres|clamp|imfchan|type)/; + if (re.test(texturePath)) { + texturePath = texturePath.split(/\s+/).pop(); + } + texturePath = texturePath.replace(/\\/g, "/"); + return path.normalize(path.resolve(mtlDirectory, texturePath)); + } + + function parseLine(line) { + line = line.trim(); + if (/^newmtl/i.test(line)) { + const name = line.substring(7).trim(); + createMaterial(name); + } else if (/^Ka /i.test(line)) { + values = line.substring(3).trim().split(" "); + material.ambientColor = [ + parseFloat(values[0]), + parseFloat(values[1]), + parseFloat(values[2]), + 1.0, + ]; + } else if (/^Ke /i.test(line)) { + values = line.substring(3).trim().split(" "); + material.emissiveColor = [ + parseFloat(values[0]), + parseFloat(values[1]), + parseFloat(values[2]), + 1.0, + ]; + } else if (/^Kd /i.test(line)) { + values = line.substring(3).trim().split(" "); + material.diffuseColor = [ + parseFloat(values[0]), + parseFloat(values[1]), + parseFloat(values[2]), + 1.0, + ]; + } else if (/^Ks /i.test(line)) { + values = line.substring(3).trim().split(" "); + material.specularColor = [ + parseFloat(values[0]), + parseFloat(values[1]), + parseFloat(values[2]), + 1.0, + ]; + } else if (/^Ns /i.test(line)) { + value = line.substring(3).trim(); + material.specularShininess = parseFloat(value); + } else if (/^d /i.test(line)) { + value = line.substring(2).trim(); + material.alpha = correctAlpha(parseFloat(value)); + } else if (/^Tr /i.test(line)) { + value = line.substring(3).trim(); + material.alpha = correctAlpha(1.0 - parseFloat(value)); + } else if (/^map_Ka /i.test(line)) { + if (!defined(overridingAmbientTexture)) { + material.ambientTexture = normalizeTexturePath( + line.substring(7).trim(), + mtlDirectory + ); + } + } else if (/^map_Ke /i.test(line)) { + if (!defined(overridingEmissiveTexture)) { + material.emissiveTexture = normalizeTexturePath( + line.substring(7).trim(), + mtlDirectory + ); + } + } else if (/^map_Kd /i.test(line)) { + if (!defined(overridingDiffuseTexture)) { + material.diffuseTexture = normalizeTexturePath( + line.substring(7).trim(), + mtlDirectory + ); + } + } else if (/^map_Ks /i.test(line)) { + if (!defined(overridingSpecularTexture)) { + material.specularTexture = normalizeTexturePath( + line.substring(7).trim(), + mtlDirectory + ); + } + } else if (/^map_Ns /i.test(line)) { + if (!defined(overridingSpecularShininessTexture)) { + material.specularShininessTexture = normalizeTexturePath( + line.substring(7).trim(), + mtlDirectory + ); + } + } else if (/^map_Bump /i.test(line)) { + if (!defined(overridingNormalTexture)) { + material.normalTexture = normalizeTexturePath( + line.substring(9).trim(), + mtlDirectory + ); + } + } else if (/^map_d /i.test(line)) { + if (!defined(overridingAlphaTexture)) { + material.alphaTexture = normalizeTexturePath( + line.substring(6).trim(), + mtlDirectory + ); + } + } + } + + function loadMaterialTextures(material) { + // If an alpha texture is present the diffuse texture needs to be decoded so they can be packed together + const diffuseAlphaTextureOptions = defined(material.alphaTexture) + ? alphaTextureOptions + : diffuseTextureOptions; + + if (material.diffuseTexture === material.ambientTexture) { + // OBJ models are often exported with the same texture in the diffuse and ambient slots but this is typically not desirable, particularly + // when saving with PBR materials where the ambient texture is treated as the occlusion texture. + material.ambientTexture = undefined; } - function normalizeTexturePath(texturePath, mtlDirectory) { - // Removes texture options from texture name - // Assumes no spaces in texture name - const re = /-(bm|t|s|o|blendu|blendv|boost|mm|texres|clamp|imfchan|type)/; - if (re.test(texturePath)) { - texturePath = texturePath.split(/\s+/).pop(); + const textureNames = [ + "diffuseTexture", + "ambientTexture", + "emissiveTexture", + "specularTexture", + "specularShininessTexture", + "normalTexture", + "alphaTexture", + ]; + const textureOptions = [ + diffuseAlphaTextureOptions, + ambientTextureOptions, + emissiveTextureOptions, + specularTextureOptions, + specularShinessTextureOptions, + normalTextureOptions, + alphaTextureOptions, + ]; + + const sharedOptions = {}; + textureNames.forEach(function (name, index) { + const texturePath = material[name]; + const originalOptions = textureOptions[index]; + if (defined(texturePath) && defined(originalOptions)) { + if (!defined(sharedOptions[texturePath])) { + sharedOptions[texturePath] = clone(originalOptions); } - texturePath = texturePath.replace(/\\/g, '/'); - return path.normalize(path.resolve(mtlDirectory, texturePath)); - } + const options = sharedOptions[texturePath]; + options.checkTransparency = + options.checkTransparency || originalOptions.checkTransparency; + options.decode = options.decode || originalOptions.decode; + options.keepSource = + options.keepSource || + !originalOptions.decode || + !originalOptions.checkTransparency; + } + }); - function parseLine(line) { - line = line.trim(); - if (/^newmtl/i.test(line)) { - const name = line.substring(7).trim(); - createMaterial(name); - } else if (/^Ka /i.test(line)) { - values = line.substring(3).trim().split(' '); - material.ambientColor = [ - parseFloat(values[0]), - parseFloat(values[1]), - parseFloat(values[2]), - 1.0 - ]; - } else if (/^Ke /i.test(line)) { - values = line.substring(3).trim().split(' '); - material.emissiveColor = [ - parseFloat(values[0]), - parseFloat(values[1]), - parseFloat(values[2]), - 1.0 - ]; - } else if (/^Kd /i.test(line)) { - values = line.substring(3).trim().split(' '); - material.diffuseColor = [ - parseFloat(values[0]), - parseFloat(values[1]), - parseFloat(values[2]), - 1.0 - ]; - } else if (/^Ks /i.test(line)) { - values = line.substring(3).trim().split(' '); - material.specularColor = [ - parseFloat(values[0]), - parseFloat(values[1]), - parseFloat(values[2]), - 1.0 - ]; - } else if (/^Ns /i.test(line)) { - value = line.substring(3).trim(); - material.specularShininess = parseFloat(value); - } else if (/^d /i.test(line)) { - value = line.substring(2).trim(); - material.alpha = correctAlpha(parseFloat(value)); - } else if (/^Tr /i.test(line)) { - value = line.substring(3).trim(); - material.alpha = correctAlpha(1.0 - parseFloat(value)); - } else if (/^map_Ka /i.test(line)) { - if (!defined(overridingAmbientTexture)) { - material.ambientTexture = normalizeTexturePath(line.substring(7).trim(), mtlDirectory); - } - } else if (/^map_Ke /i.test(line)) { - if (!defined(overridingEmissiveTexture)) { - material.emissiveTexture = normalizeTexturePath(line.substring(7).trim(), mtlDirectory); - } - } else if (/^map_Kd /i.test(line)) { - if (!defined(overridingDiffuseTexture)) { - material.diffuseTexture = normalizeTexturePath(line.substring(7).trim(), mtlDirectory); - } - } else if (/^map_Ks /i.test(line)) { - if (!defined(overridingSpecularTexture)) { - material.specularTexture = normalizeTexturePath(line.substring(7).trim(), mtlDirectory); - } - } else if (/^map_Ns /i.test(line)) { - if (!defined(overridingSpecularShininessTexture)) { - material.specularShininessTexture = normalizeTexturePath(line.substring(7).trim(), mtlDirectory); - } - } else if (/^map_Bump /i.test(line)) { - if (!defined(overridingNormalTexture)) { - material.normalTexture = normalizeTexturePath(line.substring(9).trim(), mtlDirectory); - } - } else if (/^map_d /i.test(line)) { - if (!defined(overridingAlphaTexture)) { - material.alphaTexture = normalizeTexturePath(line.substring(6).trim(), mtlDirectory); - } - } - } + textureNames.forEach(function (name) { + const texturePath = material[name]; + if (defined(texturePath)) { + loadMaterialTexture( + material, + name, + sharedOptions[texturePath], + mtlDirectory, + texturePromiseMap, + texturePromises, + options + ); + } + }); + } - function loadMaterialTextures(material) { - // If an alpha texture is present the diffuse texture needs to be decoded so they can be packed together - const diffuseAlphaTextureOptions = defined(material.alphaTexture) ? alphaTextureOptions : diffuseTextureOptions; - - if (material.diffuseTexture === material.ambientTexture) { - // OBJ models are often exported with the same texture in the diffuse and ambient slots but this is typically not desirable, particularly - // when saving with PBR materials where the ambient texture is treated as the occlusion texture. - material.ambientTexture = undefined; - } - - const textureNames = ['diffuseTexture', 'ambientTexture', 'emissiveTexture', 'specularTexture', 'specularShininessTexture', 'normalTexture', 'alphaTexture']; - const textureOptions = [diffuseAlphaTextureOptions, ambientTextureOptions, emissiveTextureOptions, specularTextureOptions, specularShinessTextureOptions, normalTextureOptions, alphaTextureOptions]; - - const sharedOptions = {}; - textureNames.forEach(function(name, index) { - const texturePath = material[name]; - const originalOptions = textureOptions[index]; - if (defined(texturePath) && defined(originalOptions)) { - if (!defined(sharedOptions[texturePath])) { - sharedOptions[texturePath] = clone(originalOptions); - } - const options = sharedOptions[texturePath]; - options.checkTransparency = options.checkTransparency || originalOptions.checkTransparency; - options.decode = options.decode || originalOptions.decode; - options.keepSource = options.keepSource || !originalOptions.decode || !originalOptions.checkTransparency; - } - }); - - textureNames.forEach(function(name) { - const texturePath = material[name]; - if (defined(texturePath)) { - loadMaterialTexture(material, name, sharedOptions[texturePath], mtlDirectory, texturePromiseMap, texturePromises, options); - } - }); - } - - return readLines(mtlPath, parseLine) - .then(function() { - const length = materials.length; - for (let i = 0; i < length; ++i) { - loadMaterialTextures(materials[i]); - } - return Promise.all(texturePromises); - }) - .then(function() { - return convertMaterials(materials, options); - }); -} - -function correctAlpha(alpha) { - // An alpha of 0.0 usually implies a problem in the export, change to 1.0 instead - return alpha === 0.0 ? 1.0 : alpha; -} - -function Material() { - this.name = undefined; - this.ambientColor = [0.0, 0.0, 0.0, 1.0]; // Ka - this.emissiveColor = [0.0, 0.0, 0.0, 1.0]; // Ke - this.diffuseColor = [0.5, 0.5, 0.5, 1.0]; // Kd - this.specularColor = [0.0, 0.0, 0.0, 1.0]; // Ks - this.specularShininess = 0.0; // Ns - this.alpha = 1.0; // d / Tr - this.ambientTexture = undefined; // map_Ka - this.emissiveTexture = undefined; // map_Ke - this.diffuseTexture = undefined; // map_Kd - this.specularTexture = undefined; // map_Ks - this.specularShininessTexture = undefined; // map_Ns - this.normalTexture = undefined; // map_Bump - this.alphaTexture = undefined; // map_d -} - -loadMtl.getDefaultMaterial = function(options) { - return convertMaterial(new Material(), options); -}; - -// Exposed for testing -loadMtl._createMaterial = function(materialOptions, options) { - return convertMaterial(combine(materialOptions, new Material()), options); -}; - -function loadMaterialTexture(material, name, textureOptions, mtlDirectory, texturePromiseMap, texturePromises, options) { - const texturePath = material[name]; - if (!defined(texturePath)) { - return; - } - - let texturePromise = texturePromiseMap[texturePath]; - if (!defined(texturePromise)) { - const shallowPath = path.join(mtlDirectory, path.basename(texturePath)); - if (options.secure && outsideDirectory(texturePath, mtlDirectory)) { - // Try looking for the texture in the same directory as the obj - options.logger('Texture file is outside of the mtl directory and the secure flag is true. Attempting to read the texture file from within the obj directory instead.'); - texturePromise = loadTexture(shallowPath, textureOptions) - .catch(function(error) { - options.logger(error.message); - options.logger('Could not read texture file at ' + shallowPath + '. This texture will be ignored'); - }); - } else { - texturePromise = loadTexture(texturePath, textureOptions) - .catch(function(error) { - // Try looking for the texture in the same directory as the obj - options.logger(error.message); - options.logger('Could not read texture file at ' + texturePath + '. Attempting to read the texture file from within the obj directory instead.'); - return loadTexture(shallowPath, textureOptions); - }) - .catch(function(error) { - options.logger(error.message); - options.logger('Could not read texture file at ' + shallowPath + '. This texture will be ignored.'); - }); - } - texturePromiseMap[texturePath] = texturePromise; - } - - texturePromises.push(texturePromise - .then(function(texture) { - material[name] = texture; - })); -} - -function convertMaterial(material, options) { - if (options.specularGlossiness) { - return createSpecularGlossinessMaterial(material, options); - } else if (options.metallicRoughness) { - return createMetallicRoughnessMaterial(material, options); - } - // No material type specified, convert the material to metallic roughness - convertTraditionalToMetallicRoughness(material); - return createMetallicRoughnessMaterial(material, options); -} - -function convertMaterials(materials, options) { - return materials.map(function(material) { - return convertMaterial(material, options); + return readLines(mtlPath, parseLine) + .then(function () { + const length = materials.length; + for (let i = 0; i < length; ++i) { + loadMaterialTextures(materials[i]); + } + return Promise.all(texturePromises); + }) + .then(function () { + return convertMaterials(materials, options); }); } -function resizeChannel(sourcePixels, sourceWidth, sourceHeight, targetPixels, targetWidth, targetHeight) { - // Nearest neighbor sampling - const widthRatio = sourceWidth / targetWidth; - const heightRatio = sourceHeight / targetHeight; +function correctAlpha(alpha) { + // An alpha of 0.0 usually implies a problem in the export, change to 1.0 instead + return alpha === 0.0 ? 1.0 : alpha; +} - for (let y = 0; y < targetHeight; ++y) { - for (let x = 0; x < targetWidth; ++x) { - const targetIndex = y * targetWidth + x; - const sourceY = Math.round(y * heightRatio); - const sourceX = Math.round(x * widthRatio); - const sourceIndex = sourceY * sourceWidth + sourceX; - const sourceValue = sourcePixels.readUInt8(sourceIndex); - targetPixels.writeUInt8(sourceValue, targetIndex); - } +function Material() { + this.name = undefined; + this.ambientColor = [0.0, 0.0, 0.0, 1.0]; // Ka + this.emissiveColor = [0.0, 0.0, 0.0, 1.0]; // Ke + this.diffuseColor = [0.5, 0.5, 0.5, 1.0]; // Kd + this.specularColor = [0.0, 0.0, 0.0, 1.0]; // Ks + this.specularShininess = 0.0; // Ns + this.alpha = 1.0; // d / Tr + this.ambientTexture = undefined; // map_Ka + this.emissiveTexture = undefined; // map_Ke + this.diffuseTexture = undefined; // map_Kd + this.specularTexture = undefined; // map_Ks + this.specularShininessTexture = undefined; // map_Ns + this.normalTexture = undefined; // map_Bump + this.alphaTexture = undefined; // map_d +} + +loadMtl.getDefaultMaterial = function (options) { + return convertMaterial(new Material(), options); +}; + +// Exposed for testing +loadMtl._createMaterial = function (materialOptions, options) { + return convertMaterial(combine(materialOptions, new Material()), options); +}; + +function loadMaterialTexture( + material, + name, + textureOptions, + mtlDirectory, + texturePromiseMap, + texturePromises, + options +) { + const texturePath = material[name]; + if (!defined(texturePath)) { + return; + } + + let texturePromise = texturePromiseMap[texturePath]; + if (!defined(texturePromise)) { + const shallowPath = path.join(mtlDirectory, path.basename(texturePath)); + if (options.secure && outsideDirectory(texturePath, mtlDirectory)) { + // Try looking for the texture in the same directory as the obj + options.logger( + "Texture file is outside of the mtl directory and the secure flag is true. Attempting to read the texture file from within the obj directory instead." + ); + texturePromise = loadTexture(shallowPath, textureOptions).catch(function ( + error + ) { + options.logger(error.message); + options.logger( + "Could not read texture file at " + + shallowPath + + ". This texture will be ignored" + ); + }); + } else { + texturePromise = loadTexture(texturePath, textureOptions) + .catch(function (error) { + // Try looking for the texture in the same directory as the obj + options.logger(error.message); + options.logger( + "Could not read texture file at " + + texturePath + + ". Attempting to read the texture file from within the obj directory instead." + ); + return loadTexture(shallowPath, textureOptions); + }) + .catch(function (error) { + options.logger(error.message); + options.logger( + "Could not read texture file at " + + shallowPath + + ". This texture will be ignored." + ); + }); } - return targetPixels; + texturePromiseMap[texturePath] = texturePromise; + } + + texturePromises.push( + texturePromise.then(function (texture) { + material[name] = texture; + }) + ); +} + +function convertMaterial(material, options) { + if (options.specularGlossiness) { + return createSpecularGlossinessMaterial(material, options); + } else if (options.metallicRoughness) { + return createMetallicRoughnessMaterial(material, options); + } + // No material type specified, convert the material to metallic roughness + convertTraditionalToMetallicRoughness(material); + return createMetallicRoughnessMaterial(material, options); +} + +function convertMaterials(materials, options) { + return materials.map(function (material) { + return convertMaterial(material, options); + }); +} + +function resizeChannel( + sourcePixels, + sourceWidth, + sourceHeight, + targetPixels, + targetWidth, + targetHeight +) { + // Nearest neighbor sampling + const widthRatio = sourceWidth / targetWidth; + const heightRatio = sourceHeight / targetHeight; + + for (let y = 0; y < targetHeight; ++y) { + for (let x = 0; x < targetWidth; ++x) { + const targetIndex = y * targetWidth + x; + const sourceY = Math.round(y * heightRatio); + const sourceX = Math.round(x * widthRatio); + const sourceIndex = sourceY * sourceWidth + sourceX; + const sourceValue = sourcePixels.readUInt8(sourceIndex); + targetPixels.writeUInt8(sourceValue, targetIndex); + } + } + return targetPixels; } let scratchResizeChannel; -function getTextureChannel(texture, index, targetWidth, targetHeight, targetChannel) { - const pixels = texture.pixels; // RGBA - const sourceWidth = texture.width; - const sourceHeight = texture.height; - const sourcePixelsLength = sourceWidth * sourceHeight; - const targetPixelsLength = targetWidth * targetHeight; +function getTextureChannel( + texture, + index, + targetWidth, + targetHeight, + targetChannel +) { + const pixels = texture.pixels; // RGBA + const sourceWidth = texture.width; + const sourceHeight = texture.height; + const sourcePixelsLength = sourceWidth * sourceHeight; + const targetPixelsLength = targetWidth * targetHeight; - // Allocate the scratchResizeChannel on demand if the texture needs to be resized - let sourceChannel = targetChannel; - if (sourcePixelsLength > targetPixelsLength) { - if (!defined(scratchResizeChannel) || (sourcePixelsLength > scratchResizeChannel.length)) { - scratchResizeChannel = Buffer.alloc(sourcePixelsLength); - } - sourceChannel = scratchResizeChannel; + // Allocate the scratchResizeChannel on demand if the texture needs to be resized + let sourceChannel = targetChannel; + if (sourcePixelsLength > targetPixelsLength) { + if ( + !defined(scratchResizeChannel) || + sourcePixelsLength > scratchResizeChannel.length + ) { + scratchResizeChannel = Buffer.alloc(sourcePixelsLength); } + sourceChannel = scratchResizeChannel; + } - for (let i = 0; i < sourcePixelsLength; ++i) { - const value = pixels.readUInt8(i * 4 + index); - sourceChannel.writeUInt8(value, i); - } + for (let i = 0; i < sourcePixelsLength; ++i) { + const value = pixels.readUInt8(i * 4 + index); + sourceChannel.writeUInt8(value, i); + } - if (sourcePixelsLength > targetPixelsLength) { - resizeChannel(sourceChannel, sourceWidth, sourceHeight, targetChannel, targetWidth, targetHeight); - } + if (sourcePixelsLength > targetPixelsLength) { + resizeChannel( + sourceChannel, + sourceWidth, + sourceHeight, + targetChannel, + targetWidth, + targetHeight + ); + } - return targetChannel; + return targetChannel; } function writeChannel(pixels, channel, index) { - const pixelsLength = pixels.length / 4; - for (let i = 0; i < pixelsLength; ++i) { - const value = channel.readUInt8(i); - pixels.writeUInt8(value, i * 4 + index); - } + const pixelsLength = pixels.length / 4; + for (let i = 0; i < pixelsLength; ++i) { + const value = channel.readUInt8(i); + pixels.writeUInt8(value, i * 4 + index); + } } function getMinimumDimensions(textures, options) { - let width = Number.POSITIVE_INFINITY; - let height = Number.POSITIVE_INFINITY; + let width = Number.POSITIVE_INFINITY; + let height = Number.POSITIVE_INFINITY; - const length = textures.length; - for (let i = 0; i < length; ++i) { - const texture = textures[i]; - width = Math.min(texture.width, width); - height = Math.min(texture.height, height); + const length = textures.length; + for (let i = 0; i < length; ++i) { + const texture = textures[i]; + width = Math.min(texture.width, width); + height = Math.min(texture.height, height); + } + + for (let i = 0; i < length; ++i) { + const texture = textures[i]; + if (texture.width !== width || texture.height !== height) { + options.logger( + "Texture " + + texture.path + + " will be scaled from " + + texture.width + + "x" + + texture.height + + " to " + + width + + "x" + + height + + "." + ); } + } - for (let i = 0; i < length; ++i) { - const texture = textures[i]; - if (texture.width !== width || texture.height !== height) { - options.logger('Texture ' + texture.path + ' will be scaled from ' + texture.width + 'x' + texture.height + ' to ' + width + 'x' + height + '.'); - } - } - - return [width, height]; + return [width, height]; } function isChannelSingleColor(buffer) { - const first = buffer.readUInt8(0); - const length = buffer.length; - for (let i = 1; i < length; ++i) { - if (buffer[i] !== first) { - return false; - } + const first = buffer.readUInt8(0); + const length = buffer.length; + for (let i = 1; i < length; ++i) { + if (buffer[i] !== first) { + return false; } - return true; + } + return true; } function createDiffuseAlphaTexture(diffuseTexture, alphaTexture, options) { - const packDiffuse = defined(diffuseTexture); - const packAlpha = defined(alphaTexture); + const packDiffuse = defined(diffuseTexture); + const packAlpha = defined(alphaTexture); - if (!packDiffuse) { - return undefined; - } + if (!packDiffuse) { + return undefined; + } - if (!packAlpha) { - return diffuseTexture; - } + if (!packAlpha) { + return diffuseTexture; + } - if (diffuseTexture === alphaTexture) { - return diffuseTexture; - } + if (diffuseTexture === alphaTexture) { + return diffuseTexture; + } - if (!defined(diffuseTexture.pixels) || !defined(alphaTexture.pixels)) { - options.logger('Could not get decoded texture data for ' + diffuseTexture.path + ' or ' + alphaTexture.path + '. The material will be created without an alpha texture.'); - return diffuseTexture; - } + if (!defined(diffuseTexture.pixels) || !defined(alphaTexture.pixels)) { + options.logger( + "Could not get decoded texture data for " + + diffuseTexture.path + + " or " + + alphaTexture.path + + ". The material will be created without an alpha texture." + ); + return diffuseTexture; + } - const packedTextures = [diffuseTexture, alphaTexture]; - const dimensions = getMinimumDimensions(packedTextures, options); - const width = dimensions[0]; - const height = dimensions[1]; - const pixelsLength = width * height; - const pixels = Buffer.alloc(pixelsLength * 4, 0xFF); // Initialize with 4 channels - const scratchChannel = Buffer.alloc(pixelsLength); + const packedTextures = [diffuseTexture, alphaTexture]; + const dimensions = getMinimumDimensions(packedTextures, options); + const width = dimensions[0]; + const height = dimensions[1]; + const pixelsLength = width * height; + const pixels = Buffer.alloc(pixelsLength * 4, 0xff); // Initialize with 4 channels + const scratchChannel = Buffer.alloc(pixelsLength); + // Write into the R, G, B channels + const redChannel = getTextureChannel( + diffuseTexture, + 0, + width, + height, + scratchChannel + ); + writeChannel(pixels, redChannel, 0); + const greenChannel = getTextureChannel( + diffuseTexture, + 1, + width, + height, + scratchChannel + ); + writeChannel(pixels, greenChannel, 1); + const blueChannel = getTextureChannel( + diffuseTexture, + 2, + width, + height, + scratchChannel + ); + writeChannel(pixels, blueChannel, 2); + + // First try reading the alpha component from the alpha channel, but if it is a single color read from the red channel instead. + let alphaChannel = getTextureChannel( + alphaTexture, + 3, + width, + height, + scratchChannel + ); + if (isChannelSingleColor(alphaChannel)) { + alphaChannel = getTextureChannel( + alphaTexture, + 0, + width, + height, + scratchChannel + ); + } + writeChannel(pixels, alphaChannel, 3); + + const texture = new Texture(); + texture.name = diffuseTexture.name; + texture.extension = ".png"; + texture.pixels = pixels; + texture.width = width; + texture.height = height; + texture.transparent = true; + + return texture; +} + +function createMetallicRoughnessTexture( + metallicTexture, + roughnessTexture, + occlusionTexture, + options +) { + if (defined(options.overridingTextures.metallicRoughnessOcclusionTexture)) { + return metallicTexture; + } + + const packMetallic = defined(metallicTexture); + const packRoughness = defined(roughnessTexture); + const packOcclusion = defined(occlusionTexture) && options.packOcclusion; + + if (!packMetallic && !packRoughness) { + return undefined; + } + + if (packMetallic && !defined(metallicTexture.pixels)) { + options.logger( + "Could not get decoded texture data for " + + metallicTexture.path + + ". The material will be created without a metallicRoughness texture." + ); + return undefined; + } + + if (packRoughness && !defined(roughnessTexture.pixels)) { + options.logger( + "Could not get decoded texture data for " + + roughnessTexture.path + + ". The material will be created without a metallicRoughness texture." + ); + return undefined; + } + + if (packOcclusion && !defined(occlusionTexture.pixels)) { + options.logger( + "Could not get decoded texture data for " + + occlusionTexture.path + + ". The occlusion texture will not be packed in the metallicRoughness texture." + ); + return undefined; + } + + const packedTextures = [ + metallicTexture, + roughnessTexture, + occlusionTexture, + ].filter(function (texture) { + return defined(texture) && defined(texture.pixels); + }); + + const dimensions = getMinimumDimensions(packedTextures, options); + const width = dimensions[0]; + const height = dimensions[1]; + const pixelsLength = width * height; + const pixels = Buffer.alloc(pixelsLength * 4, 0xff); // Initialize with 4 channels, unused channels will be white + const scratchChannel = Buffer.alloc(pixelsLength); + + if (packMetallic) { + // Write into the B channel + const metallicChannel = getTextureChannel( + metallicTexture, + 0, + width, + height, + scratchChannel + ); + writeChannel(pixels, metallicChannel, 2); + } + + if (packRoughness) { + // Write into the G channel + const roughnessChannel = getTextureChannel( + roughnessTexture, + 0, + width, + height, + scratchChannel + ); + writeChannel(pixels, roughnessChannel, 1); + } + + if (packOcclusion) { + // Write into the R channel + const occlusionChannel = getTextureChannel( + occlusionTexture, + 0, + width, + height, + scratchChannel + ); + writeChannel(pixels, occlusionChannel, 0); + } + + const length = packedTextures.length; + const names = new Array(length); + for (let i = 0; i < length; ++i) { + names[i] = packedTextures[i].name; + } + const name = names.join("_"); + + const texture = new Texture(); + texture.name = name; + texture.extension = ".png"; + texture.pixels = pixels; + texture.width = width; + texture.height = height; + + return texture; +} + +function createSpecularGlossinessTexture( + specularTexture, + glossinessTexture, + options +) { + if (defined(options.overridingTextures.specularGlossinessTexture)) { + return specularTexture; + } + + const packSpecular = defined(specularTexture); + const packGlossiness = defined(glossinessTexture); + + if (!packSpecular && !packGlossiness) { + return undefined; + } + + if (packSpecular && !defined(specularTexture.pixels)) { + options.logger( + "Could not get decoded texture data for " + + specularTexture.path + + ". The material will be created without a specularGlossiness texture." + ); + return undefined; + } + + if (packGlossiness && !defined(glossinessTexture.pixels)) { + options.logger( + "Could not get decoded texture data for " + + glossinessTexture.path + + ". The material will be created without a specularGlossiness texture." + ); + return undefined; + } + + const packedTextures = [specularTexture, glossinessTexture].filter(function ( + texture + ) { + return defined(texture) && defined(texture.pixels); + }); + + const dimensions = getMinimumDimensions(packedTextures, options); + const width = dimensions[0]; + const height = dimensions[1]; + const pixelsLength = width * height; + const pixels = Buffer.alloc(pixelsLength * 4, 0xff); // Initialize with 4 channels, unused channels will be white + const scratchChannel = Buffer.alloc(pixelsLength); + + if (packSpecular) { // Write into the R, G, B channels - const redChannel = getTextureChannel(diffuseTexture, 0, width, height, scratchChannel); + const redChannel = getTextureChannel( + specularTexture, + 0, + width, + height, + scratchChannel + ); writeChannel(pixels, redChannel, 0); - const greenChannel = getTextureChannel(diffuseTexture, 1, width, height, scratchChannel); + const greenChannel = getTextureChannel( + specularTexture, + 1, + width, + height, + scratchChannel + ); writeChannel(pixels, greenChannel, 1); - const blueChannel = getTextureChannel(diffuseTexture, 2, width, height, scratchChannel); + const blueChannel = getTextureChannel( + specularTexture, + 2, + width, + height, + scratchChannel + ); writeChannel(pixels, blueChannel, 2); + } - // First try reading the alpha component from the alpha channel, but if it is a single color read from the red channel instead. - let alphaChannel = getTextureChannel(alphaTexture, 3, width, height, scratchChannel); - if (isChannelSingleColor(alphaChannel)) { - alphaChannel = getTextureChannel(alphaTexture, 0, width, height, scratchChannel); - } - writeChannel(pixels, alphaChannel, 3); + if (packGlossiness) { + // Write into the A channel + const glossinessChannel = getTextureChannel( + glossinessTexture, + 0, + width, + height, + scratchChannel + ); + writeChannel(pixels, glossinessChannel, 3); + } - const texture = new Texture(); - texture.name = diffuseTexture.name; - texture.extension = '.png'; - texture.pixels = pixels; - texture.width = width; - texture.height = height; - texture.transparent = true; + const length = packedTextures.length; + const names = new Array(length); + for (let i = 0; i < length; ++i) { + names[i] = packedTextures[i].name; + } + const name = names.join("_"); - return texture; -} + const texture = new Texture(); + texture.name = name; + texture.extension = ".png"; + texture.pixels = pixels; + texture.width = width; + texture.height = height; -function createMetallicRoughnessTexture(metallicTexture, roughnessTexture, occlusionTexture, options) { - if (defined(options.overridingTextures.metallicRoughnessOcclusionTexture)) { - return metallicTexture; - } - - const packMetallic = defined(metallicTexture); - const packRoughness = defined(roughnessTexture); - const packOcclusion = defined(occlusionTexture) && options.packOcclusion; - - if (!packMetallic && !packRoughness) { - return undefined; - } - - if (packMetallic && !defined(metallicTexture.pixels)) { - options.logger('Could not get decoded texture data for ' + metallicTexture.path + '. The material will be created without a metallicRoughness texture.'); - return undefined; - } - - if (packRoughness && !defined(roughnessTexture.pixels)) { - options.logger('Could not get decoded texture data for ' + roughnessTexture.path + '. The material will be created without a metallicRoughness texture.'); - return undefined; - } - - if (packOcclusion && !defined(occlusionTexture.pixels)) { - options.logger('Could not get decoded texture data for ' + occlusionTexture.path + '. The occlusion texture will not be packed in the metallicRoughness texture.'); - return undefined; - } - - const packedTextures = [metallicTexture, roughnessTexture, occlusionTexture].filter(function(texture) { - return defined(texture) && defined(texture.pixels); - }); - - const dimensions = getMinimumDimensions(packedTextures, options); - const width = dimensions[0]; - const height = dimensions[1]; - const pixelsLength = width * height; - const pixels = Buffer.alloc(pixelsLength * 4, 0xFF); // Initialize with 4 channels, unused channels will be white - const scratchChannel = Buffer.alloc(pixelsLength); - - if (packMetallic) { - // Write into the B channel - const metallicChannel = getTextureChannel(metallicTexture, 0, width, height, scratchChannel); - writeChannel(pixels, metallicChannel, 2); - } - - if (packRoughness) { - // Write into the G channel - const roughnessChannel = getTextureChannel(roughnessTexture, 0, width, height, scratchChannel); - writeChannel(pixels, roughnessChannel, 1); - } - - if (packOcclusion) { - // Write into the R channel - const occlusionChannel = getTextureChannel(occlusionTexture, 0, width, height, scratchChannel); - writeChannel(pixels, occlusionChannel, 0); - } - - const length = packedTextures.length; - const names = new Array(length); - for (let i = 0; i < length; ++i) { - names[i] = packedTextures[i].name; - } - const name = names.join('_'); - - const texture = new Texture(); - texture.name = name; - texture.extension = '.png'; - texture.pixels = pixels; - texture.width = width; - texture.height = height; - - return texture; -} - -function createSpecularGlossinessTexture(specularTexture, glossinessTexture, options) { - if (defined(options.overridingTextures.specularGlossinessTexture)) { - return specularTexture; - } - - const packSpecular = defined(specularTexture); - const packGlossiness = defined(glossinessTexture); - - if (!packSpecular && !packGlossiness) { - return undefined; - } - - if (packSpecular && !defined(specularTexture.pixels)) { - options.logger('Could not get decoded texture data for ' + specularTexture.path + '. The material will be created without a specularGlossiness texture.'); - return undefined; - } - - if (packGlossiness && !defined(glossinessTexture.pixels)) { - options.logger('Could not get decoded texture data for ' + glossinessTexture.path + '. The material will be created without a specularGlossiness texture.'); - return undefined; - } - - const packedTextures = [specularTexture, glossinessTexture].filter(function(texture) { - return defined(texture) && defined(texture.pixels); - }); - - const dimensions = getMinimumDimensions(packedTextures, options); - const width = dimensions[0]; - const height = dimensions[1]; - const pixelsLength = width * height; - const pixels = Buffer.alloc(pixelsLength * 4, 0xFF); // Initialize with 4 channels, unused channels will be white - const scratchChannel = Buffer.alloc(pixelsLength); - - if (packSpecular) { - // Write into the R, G, B channels - const redChannel = getTextureChannel(specularTexture, 0, width, height, scratchChannel); - writeChannel(pixels, redChannel, 0); - const greenChannel = getTextureChannel(specularTexture, 1, width, height, scratchChannel); - writeChannel(pixels, greenChannel, 1); - const blueChannel = getTextureChannel(specularTexture, 2, width, height, scratchChannel); - writeChannel(pixels, blueChannel, 2); - } - - if (packGlossiness) { - // Write into the A channel - const glossinessChannel = getTextureChannel(glossinessTexture, 0, width, height, scratchChannel); - writeChannel(pixels, glossinessChannel, 3); - } - - const length = packedTextures.length; - const names = new Array(length); - for (let i = 0; i < length; ++i) { - names[i] = packedTextures[i].name; - } - const name = names.join('_'); - - const texture = new Texture(); - texture.name = name; - texture.extension = '.png'; - texture.pixels = pixels; - texture.width = width; - texture.height = height; - - return texture; + return texture; } function createSpecularGlossinessMaterial(material, options) { - const emissiveTexture = material.emissiveTexture; - const normalTexture = material.normalTexture; - const occlusionTexture = material.ambientTexture; - const diffuseTexture = material.diffuseTexture; - const alphaTexture = material.alphaTexture; - const specularTexture = material.specularTexture; - const glossinessTexture = material.specularShininessTexture; - const specularGlossinessTexture = createSpecularGlossinessTexture(specularTexture, glossinessTexture, options); - const diffuseAlphaTexture = createDiffuseAlphaTexture(diffuseTexture, alphaTexture, options); + const emissiveTexture = material.emissiveTexture; + const normalTexture = material.normalTexture; + const occlusionTexture = material.ambientTexture; + const diffuseTexture = material.diffuseTexture; + const alphaTexture = material.alphaTexture; + const specularTexture = material.specularTexture; + const glossinessTexture = material.specularShininessTexture; + const specularGlossinessTexture = createSpecularGlossinessTexture( + specularTexture, + glossinessTexture, + options + ); + const diffuseAlphaTexture = createDiffuseAlphaTexture( + diffuseTexture, + alphaTexture, + options + ); - let emissiveFactor = material.emissiveColor.slice(0, 3); - let diffuseFactor = material.diffuseColor; - let specularFactor = material.specularColor.slice(0, 3); - let glossinessFactor = material.specularShininess; + let emissiveFactor = material.emissiveColor.slice(0, 3); + let diffuseFactor = material.diffuseColor; + let specularFactor = material.specularColor.slice(0, 3); + let glossinessFactor = material.specularShininess; - if (defined(emissiveTexture)) { - emissiveFactor = [1.0, 1.0, 1.0]; - } + if (defined(emissiveTexture)) { + emissiveFactor = [1.0, 1.0, 1.0]; + } - if (defined(diffuseTexture)) { - diffuseFactor = [1.0, 1.0, 1.0, 1.0]; - } + if (defined(diffuseTexture)) { + diffuseFactor = [1.0, 1.0, 1.0, 1.0]; + } - if (defined(specularTexture)) { - specularFactor = [1.0, 1.0, 1.0]; - } + if (defined(specularTexture)) { + specularFactor = [1.0, 1.0, 1.0]; + } - if (defined(glossinessTexture)) { - glossinessFactor = 1.0; - } + if (defined(glossinessTexture)) { + glossinessFactor = 1.0; + } - let transparent = false; - if (defined(alphaTexture)) { - transparent = true; - } else { - const alpha = material.alpha; - diffuseFactor[3] = alpha; - transparent = alpha < 1.0; - } + let transparent = false; + if (defined(alphaTexture)) { + transparent = true; + } else { + const alpha = material.alpha; + diffuseFactor[3] = alpha; + transparent = alpha < 1.0; + } - if (defined(diffuseTexture)) { - transparent = transparent || diffuseTexture.transparent; - } + if (defined(diffuseTexture)) { + transparent = transparent || diffuseTexture.transparent; + } - const doubleSided = transparent; - const alphaMode = transparent ? 'BLEND' : 'OPAQUE'; + const doubleSided = transparent; + const alphaMode = transparent ? "BLEND" : "OPAQUE"; - return { - name : material.name, - extensions : { - KHR_materials_pbrSpecularGlossiness: { - diffuseTexture : diffuseAlphaTexture, - specularGlossinessTexture : specularGlossinessTexture, - diffuseFactor : diffuseFactor, - specularFactor : specularFactor, - glossinessFactor : glossinessFactor - } - }, - emissiveTexture : emissiveTexture, - normalTexture : normalTexture, - occlusionTexture : occlusionTexture, - emissiveFactor : emissiveFactor, - alphaMode : alphaMode, - doubleSided : doubleSided - }; + return { + name: material.name, + extensions: { + KHR_materials_pbrSpecularGlossiness: { + diffuseTexture: diffuseAlphaTexture, + specularGlossinessTexture: specularGlossinessTexture, + diffuseFactor: diffuseFactor, + specularFactor: specularFactor, + glossinessFactor: glossinessFactor, + }, + }, + emissiveTexture: emissiveTexture, + normalTexture: normalTexture, + occlusionTexture: occlusionTexture, + emissiveFactor: emissiveFactor, + alphaMode: alphaMode, + doubleSided: doubleSided, + }; } function createMetallicRoughnessMaterial(material, options) { - const emissiveTexture = material.emissiveTexture; - const normalTexture = material.normalTexture; - let occlusionTexture = material.ambientTexture; - const baseColorTexture = material.diffuseTexture; - const alphaTexture = material.alphaTexture; - const metallicTexture = material.specularTexture; - const roughnessTexture = material.specularShininessTexture; - const metallicRoughnessTexture = createMetallicRoughnessTexture(metallicTexture, roughnessTexture, occlusionTexture, options); - const diffuseAlphaTexture = createDiffuseAlphaTexture(baseColorTexture, alphaTexture, options); + const emissiveTexture = material.emissiveTexture; + const normalTexture = material.normalTexture; + let occlusionTexture = material.ambientTexture; + const baseColorTexture = material.diffuseTexture; + const alphaTexture = material.alphaTexture; + const metallicTexture = material.specularTexture; + const roughnessTexture = material.specularShininessTexture; + const metallicRoughnessTexture = createMetallicRoughnessTexture( + metallicTexture, + roughnessTexture, + occlusionTexture, + options + ); + const diffuseAlphaTexture = createDiffuseAlphaTexture( + baseColorTexture, + alphaTexture, + options + ); - if (options.packOcclusion) { - occlusionTexture = metallicRoughnessTexture; - } + if (options.packOcclusion) { + occlusionTexture = metallicRoughnessTexture; + } - let emissiveFactor = material.emissiveColor.slice(0, 3); - let baseColorFactor = material.diffuseColor; - let metallicFactor = material.specularColor[0]; - let roughnessFactor = material.specularShininess; + let emissiveFactor = material.emissiveColor.slice(0, 3); + let baseColorFactor = material.diffuseColor; + let metallicFactor = material.specularColor[0]; + let roughnessFactor = material.specularShininess; - if (defined(emissiveTexture)) { - emissiveFactor = [1.0, 1.0, 1.0]; - } + if (defined(emissiveTexture)) { + emissiveFactor = [1.0, 1.0, 1.0]; + } - if (defined(baseColorTexture)) { - baseColorFactor = [1.0, 1.0, 1.0, 1.0]; - } + if (defined(baseColorTexture)) { + baseColorFactor = [1.0, 1.0, 1.0, 1.0]; + } - if (defined(metallicTexture)) { - metallicFactor = 1.0; - } + if (defined(metallicTexture)) { + metallicFactor = 1.0; + } - if (defined(roughnessTexture)) { - roughnessFactor = 1.0; - } + if (defined(roughnessTexture)) { + roughnessFactor = 1.0; + } - let transparent = false; - if (defined(alphaTexture)) { - transparent = true; - } else { - const alpha = material.alpha; - baseColorFactor[3] = alpha; - transparent = alpha < 1.0; - } + let transparent = false; + if (defined(alphaTexture)) { + transparent = true; + } else { + const alpha = material.alpha; + baseColorFactor[3] = alpha; + transparent = alpha < 1.0; + } - if (defined(baseColorTexture)) { - transparent = transparent || baseColorTexture.transparent; - } + if (defined(baseColorTexture)) { + transparent = transparent || baseColorTexture.transparent; + } - const doubleSided = transparent; - const alphaMode = transparent ? 'BLEND' : 'OPAQUE'; + const doubleSided = transparent; + const alphaMode = transparent ? "BLEND" : "OPAQUE"; - return { - name : material.name, - pbrMetallicRoughness : { - baseColorTexture : diffuseAlphaTexture, - metallicRoughnessTexture : metallicRoughnessTexture, - baseColorFactor : baseColorFactor, - metallicFactor : metallicFactor, - roughnessFactor : roughnessFactor - }, - emissiveTexture : emissiveTexture, - normalTexture : normalTexture, - occlusionTexture : occlusionTexture, - emissiveFactor : emissiveFactor, - alphaMode : alphaMode, - doubleSided : doubleSided - }; + return { + name: material.name, + pbrMetallicRoughness: { + baseColorTexture: diffuseAlphaTexture, + metallicRoughnessTexture: metallicRoughnessTexture, + baseColorFactor: baseColorFactor, + metallicFactor: metallicFactor, + roughnessFactor: roughnessFactor, + }, + emissiveTexture: emissiveTexture, + normalTexture: normalTexture, + occlusionTexture: occlusionTexture, + emissiveFactor: emissiveFactor, + alphaMode: alphaMode, + doubleSided: doubleSided, + }; } function luminance(color) { - return color[0] * 0.2125 + color[1] * 0.7154 + color[2] * 0.0721; + return color[0] * 0.2125 + color[1] * 0.7154 + color[2] * 0.0721; } function convertTraditionalToMetallicRoughness(material) { - // Translate the blinn-phong model to the pbr metallic-roughness model - // Roughness factor is a combination of specular intensity and shininess - // Metallic factor is 0.0 - // Textures are not converted for now - const specularIntensity = luminance(material.specularColor); + // Translate the blinn-phong model to the pbr metallic-roughness model + // Roughness factor is a combination of specular intensity and shininess + // Metallic factor is 0.0 + // Textures are not converted for now + const specularIntensity = luminance(material.specularColor); - // Transform from 0-1000 range to 0-1 range. Then invert. - let roughnessFactor = material.specularShininess; - roughnessFactor = roughnessFactor / 1000.0; - roughnessFactor = 1.0 - roughnessFactor; - roughnessFactor = CesiumMath.clamp(roughnessFactor, 0.0, 1.0); + // Transform from 0-1000 range to 0-1 range. Then invert. + let roughnessFactor = material.specularShininess; + roughnessFactor = roughnessFactor / 1000.0; + roughnessFactor = 1.0 - roughnessFactor; + roughnessFactor = CesiumMath.clamp(roughnessFactor, 0.0, 1.0); - // Low specular intensity values should produce a rough material even if shininess is high. - if (specularIntensity < 0.1) { - roughnessFactor *= (1.0 - specularIntensity); - } + // Low specular intensity values should produce a rough material even if shininess is high. + if (specularIntensity < 0.1) { + roughnessFactor *= 1.0 - specularIntensity; + } - const metallicFactor = 0.0; + const metallicFactor = 0.0; - material.specularColor = [metallicFactor, metallicFactor, metallicFactor, 1.0]; - material.specularShininess = roughnessFactor; + material.specularColor = [ + metallicFactor, + metallicFactor, + metallicFactor, + 1.0, + ]; + material.specularShininess = roughnessFactor; } diff --git a/lib/loadObj.js b/lib/loadObj.js index 66c636d..c0e5a6f 100644 --- a/lib/loadObj.js +++ b/lib/loadObj.js @@ -1,12 +1,12 @@ -'use strict'; -const Cesium = require('cesium'); -const path = require('path'); -const Promise = require('bluebird'); +"use strict"; +const Cesium = require("cesium"); +const path = require("path"); +const Promise = require("bluebird"); -const ArrayStorage = require('./ArrayStorage'); -const loadMtl = require('./loadMtl'); -const outsideDirectory = require('./outsideDirectory'); -const readLines = require('./readLines'); +const ArrayStorage = require("./ArrayStorage"); +const loadMtl = require("./loadMtl"); +const outsideDirectory = require("./outsideDirectory"); +const readLines = require("./readLines"); const Axis = Cesium.Axis; const Cartesian3 = Cesium.Cartesian3; @@ -26,28 +26,30 @@ module.exports = loadObj; // Material name (usemtl) -> primitive function Node() { - this.name = undefined; - this.meshes = []; + this.name = undefined; + this.meshes = []; } function Mesh() { - this.name = undefined; - this.primitives = []; + this.name = undefined; + this.primitives = []; } function Primitive() { - this.material = undefined; - this.indices = new ArrayStorage(ComponentDatatype.UNSIGNED_INT); - this.positions = new ArrayStorage(ComponentDatatype.FLOAT); - this.normals = new ArrayStorage(ComponentDatatype.FLOAT); - this.uvs = new ArrayStorage(ComponentDatatype.FLOAT); + this.material = undefined; + this.indices = new ArrayStorage(ComponentDatatype.UNSIGNED_INT); + this.positions = new ArrayStorage(ComponentDatatype.FLOAT); + this.normals = new ArrayStorage(ComponentDatatype.FLOAT); + this.uvs = new ArrayStorage(ComponentDatatype.FLOAT); } // OBJ regex patterns are modified from ThreeJS (https://github.com/mrdoob/three.js/blob/master/examples/js/loaders/OBJLoader.js) -const vertexPattern = /v( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)/; // v float float float -const normalPattern = /vn( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)/; // vn float float float -const uvPattern = /vt( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)/; // vt float float -const facePattern = /(-?\d+)\/?(-?\d*)\/?(-?\d*)/g; // for any face format "f v", "f v/v", "f v//v", "f v/v/v" +const vertexPattern = + /v( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)/; // v float float float +const normalPattern = + /vn( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)/; // vn float float float +const uvPattern = /vt( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)/; // vt float float +const facePattern = /(-?\d+)\/?(-?\d*)\/?(-?\d*)/g; // for any face format "f v", "f v/v", "f v//v", "f v/v/v" const scratchCartesian = new Cartesian3(); @@ -61,608 +63,701 @@ const scratchCartesian = new Cartesian3(); * @private */ function loadObj(objPath, options) { - const axisTransform = getAxisTransform(options.inputUpAxis, options.outputUpAxis); + const axisTransform = getAxisTransform( + options.inputUpAxis, + options.outputUpAxis + ); - // Global store of vertex attributes listed in the obj file - let globalPositions = new ArrayStorage(ComponentDatatype.FLOAT); - let globalNormals = new ArrayStorage(ComponentDatatype.FLOAT); - let globalUvs = new ArrayStorage(ComponentDatatype.FLOAT); + // Global store of vertex attributes listed in the obj file + let globalPositions = new ArrayStorage(ComponentDatatype.FLOAT); + let globalNormals = new ArrayStorage(ComponentDatatype.FLOAT); + let globalUvs = new ArrayStorage(ComponentDatatype.FLOAT); - // The current node, mesh, and primitive - let node; - let mesh; - let primitive; - let activeMaterial; + // The current node, mesh, and primitive + let node; + let mesh; + let primitive; + let activeMaterial; - // All nodes seen in the obj - const nodes = []; + // All nodes seen in the obj + const nodes = []; - // Used to build the indices. The vertex cache is unique to each primitive. - let vertexCache = {}; - const vertexCacheLimit = 1000000; - let vertexCacheCount = 0; - let vertexCount = 0; + // Used to build the indices. The vertex cache is unique to each primitive. + let vertexCache = {}; + const vertexCacheLimit = 1000000; + let vertexCacheCount = 0; + let vertexCount = 0; - // All mtl paths seen in the obj - let mtlPaths = []; + // All mtl paths seen in the obj + let mtlPaths = []; - // Buffers for face data that spans multiple lines - let lineBuffer = ''; + // Buffers for face data that spans multiple lines + let lineBuffer = ""; - // Used for parsing face data - const faceVertices = []; - const facePositions = []; - const faceUvs = []; - const faceNormals = []; + // Used for parsing face data + const faceVertices = []; + const facePositions = []; + const faceUvs = []; + const faceNormals = []; - function clearVertexCache() { - vertexCache = {}; - vertexCacheCount = 0; + function clearVertexCache() { + vertexCache = {}; + vertexCacheCount = 0; + } + + function getName(name) { + return name === "" ? undefined : name; + } + + function addNode(name) { + node = new Node(); + node.name = getName(name); + nodes.push(node); + addMesh(); + } + + function addMesh(name) { + mesh = new Mesh(); + mesh.name = getName(name); + node.meshes.push(mesh); + addPrimitive(); + } + + function addPrimitive() { + primitive = new Primitive(); + primitive.material = activeMaterial; + mesh.primitives.push(primitive); + + // Clear the vertex cache for each new primitive + clearVertexCache(); + vertexCount = 0; + } + + function reusePrimitive(callback) { + const primitives = mesh.primitives; + const primitivesLength = primitives.length; + for (let i = 0; i < primitivesLength; ++i) { + if (primitives[i].material === activeMaterial) { + if (!defined(callback) || callback(primitives[i])) { + primitive = primitives[i]; + clearVertexCache(); + vertexCount = primitive.positions.length / 3; + return; + } + } + } + addPrimitive(); + } + + function useMaterial(name) { + activeMaterial = getName(name); + reusePrimitive(); + } + + function faceAndPrimitiveMatch(uvs, normals, primitive) { + const faceHasUvs = defined(uvs[0]); + const faceHasNormals = defined(normals[0]); + const primitiveHasUvs = primitive.uvs.length > 0; + const primitiveHasNormals = primitive.normals.length > 0; + return ( + primitiveHasUvs === faceHasUvs && primitiveHasNormals === faceHasNormals + ); + } + + function checkPrimitive(uvs, normals) { + const firstFace = primitive.indices.length === 0; + if (!firstFace && !faceAndPrimitiveMatch(uvs, normals, primitive)) { + reusePrimitive(function (primitive) { + return faceAndPrimitiveMatch(uvs, normals, primitive); + }); + } + } + + function getIndexFromStart(index, attributeData, components) { + const i = parseInt(index); + if (i < 0) { + // Negative vertex indexes reference the vertices immediately above it + return attributeData.length / components + i; + } + return i - 1; + } + + function correctAttributeIndices( + attributeIndices, + attributeData, + components + ) { + const length = attributeIndices.length; + for (let i = 0; i < length; ++i) { + if (attributeIndices[i].length === 0) { + attributeIndices[i] = undefined; + } else { + attributeIndices[i] = getIndexFromStart( + attributeIndices[i], + attributeData, + components + ); + } + } + } + + function correctVertices(vertices, positions, uvs, normals) { + const length = vertices.length; + for (let i = 0; i < length; ++i) { + vertices[i] = + defaultValue(positions[i], "") + + "/" + + defaultValue(uvs[i], "") + + "/" + + defaultValue(normals[i], ""); + } + } + + function createVertex(p, u, n) { + // Positions + if (defined(p) && globalPositions.length > 0) { + if (p * 3 >= globalPositions.length) { + throw new RuntimeError(`Position index ${p} is out of bounds`); + } + const px = globalPositions.get(p * 3); + const py = globalPositions.get(p * 3 + 1); + const pz = globalPositions.get(p * 3 + 2); + primitive.positions.push(px); + primitive.positions.push(py); + primitive.positions.push(pz); } - function getName(name) { - return (name === '' ? undefined : name); + // Normals + if (defined(n) && globalNormals.length > 0) { + if (n * 3 >= globalNormals.length) { + throw new RuntimeError(`Normal index ${n} is out of bounds`); + } + const nx = globalNormals.get(n * 3); + const ny = globalNormals.get(n * 3 + 1); + const nz = globalNormals.get(n * 3 + 2); + primitive.normals.push(nx); + primitive.normals.push(ny); + primitive.normals.push(nz); } - function addNode(name) { - node = new Node(); - node.name = getName(name); - nodes.push(node); - addMesh(); + // UVs + if (defined(u) && globalUvs.length > 0) { + if (u * 2 >= globalUvs.length) { + throw new RuntimeError(`UV index ${u} is out of bounds`); + } + const ux = globalUvs.get(u * 2); + const uy = globalUvs.get(u * 2 + 1); + primitive.uvs.push(ux); + primitive.uvs.push(uy); } + } - function addMesh(name) { - mesh = new Mesh(); - mesh.name = getName(name); - node.meshes.push(mesh); - addPrimitive(); - } + function addVertex(v, p, u, n) { + let index = vertexCache[v]; + if (!defined(index)) { + index = vertexCount++; + vertexCache[v] = index; + createVertex(p, u, n); - function addPrimitive() { - primitive = new Primitive(); - primitive.material = activeMaterial; - mesh.primitives.push(primitive); - - // Clear the vertex cache for each new primitive + // Prevent the vertex cache from growing too large. As a result of clearing the cache there + // may be some duplicate vertices. + vertexCacheCount++; + if (vertexCacheCount > vertexCacheLimit) { clearVertexCache(); - vertexCount = 0; + } } + return index; + } - function reusePrimitive(callback) { - const primitives = mesh.primitives; - const primitivesLength = primitives.length; - for (let i = 0; i < primitivesLength; ++i) { - if (primitives[i].material === activeMaterial) { - if (!defined(callback) || callback(primitives[i])) { - primitive = primitives[i]; - clearVertexCache(); - vertexCount = primitive.positions.length / 3; - return; - } - } + function getPosition(index, result) { + const px = globalPositions.get(index * 3); + const py = globalPositions.get(index * 3 + 1); + const pz = globalPositions.get(index * 3 + 2); + return Cartesian3.fromElements(px, py, pz, result); + } + + function getNormal(index, result) { + const nx = globalNormals.get(index * 3); + const ny = globalNormals.get(index * 3 + 1); + const nz = globalNormals.get(index * 3 + 2); + return Cartesian3.fromElements(nx, ny, nz, result); + } + + const scratch1 = new Cartesian3(); + const scratch2 = new Cartesian3(); + const scratch3 = new Cartesian3(); + const scratch4 = new Cartesian3(); + const scratch5 = new Cartesian3(); + const scratchCenter = new Cartesian3(); + const scratchAxis1 = new Cartesian3(); + const scratchAxis2 = new Cartesian3(); + const scratchNormal = new Cartesian3(); + const scratchPositions = [ + new Cartesian3(), + new Cartesian3(), + new Cartesian3(), + new Cartesian3(), + ]; + const scratchVertexIndices = []; + const scratchPoints = []; + + function checkWindingCorrect( + positionIndex1, + positionIndex2, + positionIndex3, + normalIndex + ) { + if (!defined(normalIndex)) { + // If no face normal, we have to assume the winding is correct. + return true; + } + const normal = getNormal(normalIndex, scratchNormal); + const A = getPosition(positionIndex1, scratch1); + const B = getPosition(positionIndex2, scratch2); + const C = getPosition(positionIndex3, scratch3); + + const BA = Cartesian3.subtract(B, A, scratch4); + const CA = Cartesian3.subtract(C, A, scratch5); + const cross = Cartesian3.cross(BA, CA, scratch3); + + return Cartesian3.dot(normal, cross) >= 0; + } + + function addTriangle(index1, index2, index3, correctWinding) { + if (correctWinding) { + primitive.indices.push(index1); + primitive.indices.push(index2); + primitive.indices.push(index3); + } else { + primitive.indices.push(index1); + primitive.indices.push(index3); + primitive.indices.push(index2); + } + } + + function addFace( + vertices, + positions, + uvs, + normals, + triangleWindingOrderSanitization + ) { + correctAttributeIndices(positions, globalPositions, 3); + correctAttributeIndices(normals, globalNormals, 3); + correctAttributeIndices(uvs, globalUvs, 2); + correctVertices(vertices, positions, uvs, normals); + + checkPrimitive(uvs, faceNormals); + + if (vertices.length === 3) { + const isWindingCorrect = + !triangleWindingOrderSanitization || + checkWindingCorrect( + positions[0], + positions[1], + positions[2], + normals[0] + ); + const index1 = addVertex(vertices[0], positions[0], uvs[0], normals[0]); + const index2 = addVertex(vertices[1], positions[1], uvs[1], normals[1]); + const index3 = addVertex(vertices[2], positions[2], uvs[2], normals[2]); + addTriangle(index1, index2, index3, isWindingCorrect); + } else { + // Triangulate if the face is not a triangle + const points = scratchPoints; + const vertexIndices = scratchVertexIndices; + + points.length = 0; + vertexIndices.length = 0; + + for (let i = 0; i < vertices.length; ++i) { + const index = addVertex(vertices[i], positions[i], uvs[i], normals[i]); + vertexIndices.push(index); + if (i === scratchPositions.length) { + scratchPositions.push(new Cartesian3()); } - addPrimitive(); - } + points.push(getPosition(positions[i], scratchPositions[i])); + } - function useMaterial(name) { - activeMaterial = getName(name); - reusePrimitive(); - } + const validGeometry = + CoplanarPolygonGeometryLibrary.computeProjectTo2DArguments( + points, + scratchCenter, + scratchAxis1, + scratchAxis2 + ); + if (!validGeometry) { + return; + } + const projectPoints = + CoplanarPolygonGeometryLibrary.createProjectPointsTo2DFunction( + scratchCenter, + scratchAxis1, + scratchAxis2 + ); + const points2D = projectPoints(points); + const indices = PolygonPipeline.triangulate(points2D); + const isWindingCorrect = + PolygonPipeline.computeWindingOrder2D(points2D) !== + WindingOrder.CLOCKWISE; - function faceAndPrimitiveMatch(uvs, normals, primitive) { - const faceHasUvs = defined(uvs[0]); - const faceHasNormals = defined(normals[0]); - const primitiveHasUvs = primitive.uvs.length > 0; - const primitiveHasNormals = primitive.normals.length > 0; - return primitiveHasUvs === faceHasUvs && primitiveHasNormals === faceHasNormals; + for (let i = 0; i < indices.length - 2; i += 3) { + addTriangle( + vertexIndices[indices[i]], + vertexIndices[indices[i + 1]], + vertexIndices[indices[i + 2]], + isWindingCorrect + ); + } } + } - function checkPrimitive(uvs, normals) { - const firstFace = primitive.indices.length === 0; - if (!firstFace && !faceAndPrimitiveMatch(uvs, normals, primitive)) { - reusePrimitive(function(primitive) { - return faceAndPrimitiveMatch(uvs, normals, primitive); - }); + function parseLine(line) { + line = line.trim(); + let result; + + if (line.length === 0 || line.charAt(0) === "#") { + // Don't process empty lines or comments + } else if (/^o\s/i.test(line)) { + const objectName = line.substring(2).trim(); + addNode(objectName); + } else if (/^g\s/i.test(line)) { + const groupName = line.substring(2).trim(); + addMesh(groupName); + } else if (/^usemtl/i.test(line)) { + const materialName = line.substring(7).trim(); + useMaterial(materialName); + } else if (/^mtllib/i.test(line)) { + const mtllibLine = line.substring(7).trim(); + mtlPaths = mtlPaths.concat(getMtlPaths(mtllibLine)); + } else if ((result = vertexPattern.exec(line)) !== null) { + const position = scratchCartesian; + position.x = parseFloat(result[1]); + position.y = parseFloat(result[2]); + position.z = parseFloat(result[3]); + if (defined(axisTransform)) { + Matrix4.multiplyByPoint(axisTransform, position, position); + } + globalPositions.push(position.x); + globalPositions.push(position.y); + globalPositions.push(position.z); + } else if ((result = normalPattern.exec(line)) !== null) { + const normal = Cartesian3.fromElements( + parseFloat(result[1]), + parseFloat(result[2]), + parseFloat(result[3]), + scratchNormal + ); + if (Cartesian3.equals(normal, Cartesian3.ZERO)) { + Cartesian3.clone(Cartesian3.UNIT_Z, normal); + } else { + Cartesian3.normalize(normal, normal); + } + if (defined(axisTransform)) { + Matrix4.multiplyByPointAsVector(axisTransform, normal, normal); + } + globalNormals.push(normal.x); + globalNormals.push(normal.y); + globalNormals.push(normal.z); + } else if ((result = uvPattern.exec(line)) !== null) { + globalUvs.push(parseFloat(result[1])); + globalUvs.push(1.0 - parseFloat(result[2])); // Flip y so 0.0 is the bottom of the image + } else { + // face line or invalid line + // Because face lines can contain n vertices, we use a line buffer in case the face data spans multiple lines. + // If there's a line continuation don't create face yet + if (line.slice(-1) === "\\") { + lineBuffer += line.substring(0, line.length - 1); + return; + } + lineBuffer += line; + if (lineBuffer.substring(0, 2) === "f ") { + while ((result = facePattern.exec(lineBuffer)) !== null) { + faceVertices.push(result[0]); + facePositions.push(result[1]); + faceUvs.push(result[2]); + faceNormals.push(result[3]); } - } - - function getIndexFromStart(index, attributeData, components) { - const i = parseInt(index); - if (i < 0) { - // Negative vertex indexes reference the vertices immediately above it - return (attributeData.length / components + i); - } - return i - 1; - } - - function correctAttributeIndices(attributeIndices, attributeData, components) { - const length = attributeIndices.length; - for (let i = 0; i < length; ++i) { - if (attributeIndices[i].length === 0) { - attributeIndices[i] = undefined; - } else { - attributeIndices[i] = getIndexFromStart(attributeIndices[i], attributeData, components); - } - } - } - - function correctVertices(vertices, positions, uvs, normals) { - const length = vertices.length; - for (let i = 0; i < length; ++i) { - vertices[i] = defaultValue(positions[i], '') + '/' + defaultValue(uvs[i], '') + '/' + defaultValue(normals[i], ''); - } - } - - function createVertex(p, u, n) { - // Positions - if (defined(p) && (globalPositions.length > 0)) { - if (p * 3 >= globalPositions.length) { - throw new RuntimeError(`Position index ${p} is out of bounds`); - } - const px = globalPositions.get(p * 3); - const py = globalPositions.get(p * 3 + 1); - const pz = globalPositions.get(p * 3 + 2); - primitive.positions.push(px); - primitive.positions.push(py); - primitive.positions.push(pz); + if (faceVertices.length > 2) { + addFace( + faceVertices, + facePositions, + faceUvs, + faceNormals, + options.triangleWindingOrderSanitization + ); } - // Normals - if (defined(n) && (globalNormals.length > 0)) { - if (n * 3 >= globalNormals.length) { - throw new RuntimeError(`Normal index ${n} is out of bounds`); - } - const nx = globalNormals.get(n * 3); - const ny = globalNormals.get(n * 3 + 1); - const nz = globalNormals.get(n * 3 + 2); - primitive.normals.push(nx); - primitive.normals.push(ny); - primitive.normals.push(nz); - } - - // UVs - if (defined(u) && (globalUvs.length > 0)) { - if (u * 2 >= globalUvs.length) { - throw new RuntimeError(`UV index ${u} is out of bounds`); - } - const ux = globalUvs.get(u * 2); - const uy = globalUvs.get(u * 2 + 1); - primitive.uvs.push(ux); - primitive.uvs.push(uy); - } + faceVertices.length = 0; + facePositions.length = 0; + faceNormals.length = 0; + faceUvs.length = 0; + } + lineBuffer = ""; } + } - function addVertex(v, p, u, n) { - let index = vertexCache[v]; - if (!defined(index)) { - index = vertexCount++; - vertexCache[v] = index; - createVertex(p, u, n); + // Create a default node in case there are no o/g/usemtl lines in the obj + addNode(); - // Prevent the vertex cache from growing too large. As a result of clearing the cache there - // may be some duplicate vertices. - vertexCacheCount++; - if (vertexCacheCount > vertexCacheLimit) { - clearVertexCache(); - } - } - return index; - } + // Parse the obj file + return readLines(objPath, parseLine).then(function () { + // Unload resources + globalPositions = undefined; + globalNormals = undefined; + globalUvs = undefined; - function getPosition(index, result) { - const px = globalPositions.get(index * 3); - const py = globalPositions.get(index * 3 + 1); - const pz = globalPositions.get(index * 3 + 2); - return Cartesian3.fromElements(px, py, pz, result); - } - - function getNormal(index, result) { - const nx = globalNormals.get(index * 3); - const ny = globalNormals.get(index * 3 + 1); - const nz = globalNormals.get(index * 3 + 2); - return Cartesian3.fromElements(nx, ny, nz, result); - } - - const scratch1 = new Cartesian3(); - const scratch2 = new Cartesian3(); - const scratch3 = new Cartesian3(); - const scratch4 = new Cartesian3(); - const scratch5 = new Cartesian3(); - const scratchCenter = new Cartesian3(); - const scratchAxis1 = new Cartesian3(); - const scratchAxis2 = new Cartesian3(); - const scratchNormal = new Cartesian3(); - const scratchPositions = [new Cartesian3(), new Cartesian3(), new Cartesian3(), new Cartesian3()]; - const scratchVertexIndices = []; - const scratchPoints = []; - - function checkWindingCorrect(positionIndex1, positionIndex2, positionIndex3, normalIndex) { - if (!defined(normalIndex)) { - // If no face normal, we have to assume the winding is correct. - return true; - } - const normal = getNormal(normalIndex, scratchNormal); - const A = getPosition(positionIndex1, scratch1); - const B = getPosition(positionIndex2, scratch2); - const C = getPosition(positionIndex3, scratch3); - - const BA = Cartesian3.subtract(B, A, scratch4); - const CA = Cartesian3.subtract(C, A, scratch5); - const cross = Cartesian3.cross(BA, CA, scratch3); - - return (Cartesian3.dot(normal, cross) >= 0); - } - - function addTriangle(index1, index2, index3, correctWinding) { - if (correctWinding) { - primitive.indices.push(index1); - primitive.indices.push(index2); - primitive.indices.push(index3); - } else { - primitive.indices.push(index1); - primitive.indices.push(index3); - primitive.indices.push(index2); - } - } - - function addFace(vertices, positions, uvs, normals, triangleWindingOrderSanitization) { - correctAttributeIndices(positions, globalPositions, 3); - correctAttributeIndices(normals, globalNormals, 3); - correctAttributeIndices(uvs, globalUvs, 2); - correctVertices(vertices, positions, uvs, normals); - - checkPrimitive(uvs, faceNormals); - - if (vertices.length === 3) { - const isWindingCorrect = !triangleWindingOrderSanitization || checkWindingCorrect(positions[0], positions[1], positions[2], normals[0]); - const index1 = addVertex(vertices[0], positions[0], uvs[0], normals[0]); - const index2 = addVertex(vertices[1], positions[1], uvs[1], normals[1]); - const index3 = addVertex(vertices[2], positions[2], uvs[2], normals[2]); - addTriangle(index1, index2, index3, isWindingCorrect); - } else { // Triangulate if the face is not a triangle - const points = scratchPoints; - const vertexIndices = scratchVertexIndices; - - points.length = 0; - vertexIndices.length = 0; - - for (let i = 0; i < vertices.length; ++i) { - const index = addVertex(vertices[i], positions[i], uvs[i], normals[i]); - vertexIndices.push(index); - if (i === scratchPositions.length) { - scratchPositions.push(new Cartesian3()); - } - points.push(getPosition(positions[i], scratchPositions[i])); - } - - const validGeometry = CoplanarPolygonGeometryLibrary.computeProjectTo2DArguments(points, scratchCenter, scratchAxis1, scratchAxis2); - if (!validGeometry) { - return; - } - const projectPoints = CoplanarPolygonGeometryLibrary.createProjectPointsTo2DFunction(scratchCenter, scratchAxis1, scratchAxis2); - const points2D = projectPoints(points); - const indices = PolygonPipeline.triangulate(points2D); - const isWindingCorrect = PolygonPipeline.computeWindingOrder2D(points2D) !== WindingOrder.CLOCKWISE; - - for (let i = 0; i < indices.length - 2; i += 3) { - addTriangle(vertexIndices[indices[i]], vertexIndices[indices[i+1]], vertexIndices[indices[i+2]], isWindingCorrect); - } - } - } - - function parseLine(line) { - line = line.trim(); - let result; - - if ((line.length === 0) || (line.charAt(0) === '#')) { - // Don't process empty lines or comments - } else if (/^o\s/i.test(line)) { - const objectName = line.substring(2).trim(); - addNode(objectName); - } else if (/^g\s/i.test(line)) { - const groupName = line.substring(2).trim(); - addMesh(groupName); - } else if (/^usemtl/i.test(line)) { - const materialName = line.substring(7).trim(); - useMaterial(materialName); - } else if (/^mtllib/i.test(line)) { - const mtllibLine = line.substring(7).trim(); - mtlPaths = mtlPaths.concat(getMtlPaths(mtllibLine)); - } else if ((result = vertexPattern.exec(line)) !== null) { - const position = scratchCartesian; - position.x = parseFloat(result[1]); - position.y = parseFloat(result[2]); - position.z = parseFloat(result[3]); - if (defined(axisTransform)) { - Matrix4.multiplyByPoint(axisTransform, position, position); - } - globalPositions.push(position.x); - globalPositions.push(position.y); - globalPositions.push(position.z); - } else if ((result = normalPattern.exec(line) ) !== null) { - const normal = Cartesian3.fromElements(parseFloat(result[1]), parseFloat(result[2]), parseFloat(result[3]), scratchNormal); - if (Cartesian3.equals(normal, Cartesian3.ZERO)) { - Cartesian3.clone(Cartesian3.UNIT_Z, normal); - } else { - Cartesian3.normalize(normal, normal); - } - if (defined(axisTransform)) { - Matrix4.multiplyByPointAsVector(axisTransform, normal, normal); - } - globalNormals.push(normal.x); - globalNormals.push(normal.y); - globalNormals.push(normal.z); - } else if ((result = uvPattern.exec(line)) !== null) { - globalUvs.push(parseFloat(result[1])); - globalUvs.push(1.0 - parseFloat(result[2])); // Flip y so 0.0 is the bottom of the image - } else { // face line or invalid line - // Because face lines can contain n vertices, we use a line buffer in case the face data spans multiple lines. - // If there's a line continuation don't create face yet - if (line.slice(-1) === '\\') { - lineBuffer += line.substring(0, line.length-1); - return; - } - lineBuffer += line; - if (lineBuffer.substring(0, 2) === 'f ') { - while ((result = facePattern.exec(lineBuffer)) !== null) { - faceVertices.push(result[0]); - facePositions.push(result[1]); - faceUvs.push(result[2]); - faceNormals.push(result[3]); - } - if (faceVertices.length > 2) { - addFace(faceVertices, facePositions, faceUvs, faceNormals, options.triangleWindingOrderSanitization); - } - - faceVertices.length = 0; - facePositions.length = 0; - faceNormals.length = 0; - faceUvs.length = 0; - } - lineBuffer = ''; - } - } - - // Create a default node in case there are no o/g/usemtl lines in the obj - addNode(); - - // Parse the obj file - return readLines(objPath, parseLine) - .then(function() { - // Unload resources - globalPositions = undefined; - globalNormals = undefined; - globalUvs = undefined; - - // Load materials and textures - return finishLoading(nodes, mtlPaths, objPath, defined(activeMaterial), options); - }); + // Load materials and textures + return finishLoading( + nodes, + mtlPaths, + objPath, + defined(activeMaterial), + options + ); + }); } function getMtlPaths(mtllibLine) { - // Handle paths with spaces. E.g. mtllib my material file.mtl - const mtlPaths = []; - const splits = mtllibLine.split(' '); - const length = splits.length; - let startIndex = 0; - for (let i = 0; i < length; ++i) { - if (path.extname(splits[i]) !== '.mtl') { - continue; - } - const mtlPath = splits.slice(startIndex, i + 1).join(' '); - mtlPaths.push(mtlPath); - startIndex = i + 1; + // Handle paths with spaces. E.g. mtllib my material file.mtl + const mtlPaths = []; + const splits = mtllibLine.split(" "); + const length = splits.length; + let startIndex = 0; + for (let i = 0; i < length; ++i) { + if (path.extname(splits[i]) !== ".mtl") { + continue; } - return mtlPaths; + const mtlPath = splits.slice(startIndex, i + 1).join(" "); + mtlPaths.push(mtlPath); + startIndex = i + 1; + } + return mtlPaths; } function finishLoading(nodes, mtlPaths, objPath, usesMaterials, options) { - nodes = cleanNodes(nodes); - if (nodes.length === 0) { - throw new RuntimeError(objPath + ' does not have any geometry data'); + nodes = cleanNodes(nodes); + if (nodes.length === 0) { + throw new RuntimeError(objPath + " does not have any geometry data"); + } + const name = path.basename(objPath, path.extname(objPath)); + return loadMtls(mtlPaths, objPath, options).then(function (materials) { + if (materials.length > 0 && !usesMaterials) { + assignDefaultMaterial(nodes, materials, usesMaterials); } - const name = path.basename(objPath, path.extname(objPath)); - return loadMtls(mtlPaths, objPath, options) - .then(function(materials) { - if (materials.length > 0 && !usesMaterials) { - assignDefaultMaterial(nodes, materials, usesMaterials); - } - assignUnnamedMaterial(nodes, materials); - return { - nodes : nodes, - materials : materials, - name : name - }; - }); + assignUnnamedMaterial(nodes, materials); + return { + nodes: nodes, + materials: materials, + name: name, + }; + }); } function normalizeMtlPath(mtlPath, objDirectory) { - mtlPath = mtlPath.replace(/\\/g, '/'); - return path.normalize(path.resolve(objDirectory, mtlPath)); + mtlPath = mtlPath.replace(/\\/g, "/"); + return path.normalize(path.resolve(objDirectory, mtlPath)); } function loadMtls(mtlPaths, objPath, options) { - const objDirectory = path.dirname(objPath); - let materials = []; + const objDirectory = path.dirname(objPath); + let materials = []; - // Remove duplicates - mtlPaths = mtlPaths.filter(function(value, index, self) { - return self.indexOf(value) === index; - }); + // Remove duplicates + mtlPaths = mtlPaths.filter(function (value, index, self) { + return self.indexOf(value) === index; + }); - return Promise.map(mtlPaths, function(mtlPath) { - mtlPath = normalizeMtlPath(mtlPath, objDirectory); - const shallowPath = path.join(objDirectory, path.basename(mtlPath)); - if (options.secure && outsideDirectory(mtlPath, objDirectory)) { - // Try looking for the .mtl in the same directory as the obj - options.logger('The material file is outside of the obj directory and the secure flag is true. Attempting to read the material file from within the obj directory instead.'); - return loadMtl(shallowPath, options) - .then(function(materialsInMtl) { - materials = materials.concat(materialsInMtl); - }) - .catch(function(error) { - options.logger(error.message); - options.logger('Could not read material file at ' + shallowPath + '. Using default material instead.'); - }); - } + return Promise.map( + mtlPaths, + function (mtlPath) { + mtlPath = normalizeMtlPath(mtlPath, objDirectory); + const shallowPath = path.join(objDirectory, path.basename(mtlPath)); + if (options.secure && outsideDirectory(mtlPath, objDirectory)) { + // Try looking for the .mtl in the same directory as the obj + options.logger( + "The material file is outside of the obj directory and the secure flag is true. Attempting to read the material file from within the obj directory instead." + ); + return loadMtl(shallowPath, options) + .then(function (materialsInMtl) { + materials = materials.concat(materialsInMtl); + }) + .catch(function (error) { + options.logger(error.message); + options.logger( + "Could not read material file at " + + shallowPath + + ". Using default material instead." + ); + }); + } - return loadMtl(mtlPath, options) - .catch(function(error) { - // Try looking for the .mtl in the same directory as the obj - options.logger(error.message); - options.logger('Could not read material file at ' + mtlPath + '. Attempting to read the material file from within the obj directory instead.'); - return loadMtl(shallowPath, options); - }) - .then(function(materialsInMtl) { - materials = materials.concat(materialsInMtl); - }) - .catch(function(error) { - options.logger(error.message); - options.logger('Could not read material file at ' + shallowPath + '. Using default material instead.'); - }); - }, {concurrency : 10}) - .then(function() { - return materials; + return loadMtl(mtlPath, options) + .catch(function (error) { + // Try looking for the .mtl in the same directory as the obj + options.logger(error.message); + options.logger( + "Could not read material file at " + + mtlPath + + ". Attempting to read the material file from within the obj directory instead." + ); + return loadMtl(shallowPath, options); + }) + .then(function (materialsInMtl) { + materials = materials.concat(materialsInMtl); + }) + .catch(function (error) { + options.logger(error.message); + options.logger( + "Could not read material file at " + + shallowPath + + ". Using default material instead." + ); }); + }, + { concurrency: 10 } + ).then(function () { + return materials; + }); } function assignDefaultMaterial(nodes, materials) { - const defaultMaterial = materials[0].name; - const nodesLength = nodes.length; - for (let i = 0; i < nodesLength; ++i) { - const meshes = nodes[i].meshes; - const meshesLength = meshes.length; - for (let j = 0; j < meshesLength; ++j) { - const primitives = meshes[j].primitives; - const primitivesLength = primitives.length; - for (let k = 0; k < primitivesLength; ++k) { - const primitive = primitives[k]; - primitive.material = defaultValue(primitive.material, defaultMaterial); - } - } + const defaultMaterial = materials[0].name; + const nodesLength = nodes.length; + for (let i = 0; i < nodesLength; ++i) { + const meshes = nodes[i].meshes; + const meshesLength = meshes.length; + for (let j = 0; j < meshesLength; ++j) { + const primitives = meshes[j].primitives; + const primitivesLength = primitives.length; + for (let k = 0; k < primitivesLength; ++k) { + const primitive = primitives[k]; + primitive.material = defaultValue(primitive.material, defaultMaterial); + } } + } } function assignUnnamedMaterial(nodes, materials) { - // If there is a material that doesn't have a name, assign that - // material to any primitives whose material is undefined. - const unnamedMaterial = materials.find(function(material) { - return material.name.length === 0; - }); + // If there is a material that doesn't have a name, assign that + // material to any primitives whose material is undefined. + const unnamedMaterial = materials.find(function (material) { + return material.name.length === 0; + }); - if (!defined(unnamedMaterial)) { - return; - } + if (!defined(unnamedMaterial)) { + return; + } - const nodesLength = nodes.length; - for (let i = 0; i < nodesLength; ++i) { - const meshes = nodes[i].meshes; - const meshesLength = meshes.length; - for (let j = 0; j < meshesLength; ++j) { - const primitives = meshes[j].primitives; - const primitivesLength = primitives.length; - for (let k = 0; k < primitivesLength; ++k) { - const primitive = primitives[k]; - if (!defined(primitive.material)) { - primitive.material = unnamedMaterial.name; - } - } + const nodesLength = nodes.length; + for (let i = 0; i < nodesLength; ++i) { + const meshes = nodes[i].meshes; + const meshesLength = meshes.length; + for (let j = 0; j < meshesLength; ++j) { + const primitives = meshes[j].primitives; + const primitivesLength = primitives.length; + for (let k = 0; k < primitivesLength; ++k) { + const primitive = primitives[k]; + if (!defined(primitive.material)) { + primitive.material = unnamedMaterial.name; } + } } + } } function removeEmptyMeshes(meshes) { - return meshes.filter(function(mesh) { - // Remove empty primitives - mesh.primitives = mesh.primitives.filter(function(primitive) { - return primitive.indices.length > 0 && primitive.positions.length > 0; - }); - // Valid meshes must have at least one primitive - return (mesh.primitives.length > 0); + return meshes.filter(function (mesh) { + // Remove empty primitives + mesh.primitives = mesh.primitives.filter(function (primitive) { + return primitive.indices.length > 0 && primitive.positions.length > 0; }); + // Valid meshes must have at least one primitive + return mesh.primitives.length > 0; + }); } function meshesHaveNames(meshes) { - const meshesLength = meshes.length; - for (let i = 0; i < meshesLength; ++i) { - if (defined(meshes[i].name)) { - return true; - } + const meshesLength = meshes.length; + for (let i = 0; i < meshesLength; ++i) { + if (defined(meshes[i].name)) { + return true; } - return false; + } + return false; } function removeEmptyNodes(nodes) { - const final = []; - const nodesLength = nodes.length; - for (let i = 0; i < nodesLength; ++i) { - const node = nodes[i]; - const meshes = removeEmptyMeshes(node.meshes); - if (meshes.length === 0) { - continue; - } - node.meshes = meshes; - if (!defined(node.name) && meshesHaveNames(meshes)) { - // If the obj has groups (g) but not object groups (o) then convert meshes to nodes - const meshesLength = meshes.length; - for (let j = 0; j < meshesLength; ++j) { - const mesh = meshes[j]; - const convertedNode = new Node(); - convertedNode.name = mesh.name; - convertedNode.meshes = [mesh]; - final.push(convertedNode); - } - } else { - final.push(node); - } + const final = []; + const nodesLength = nodes.length; + for (let i = 0; i < nodesLength; ++i) { + const node = nodes[i]; + const meshes = removeEmptyMeshes(node.meshes); + if (meshes.length === 0) { + continue; } - return final; + node.meshes = meshes; + if (!defined(node.name) && meshesHaveNames(meshes)) { + // If the obj has groups (g) but not object groups (o) then convert meshes to nodes + const meshesLength = meshes.length; + for (let j = 0; j < meshesLength; ++j) { + const mesh = meshes[j]; + const convertedNode = new Node(); + convertedNode.name = mesh.name; + convertedNode.meshes = [mesh]; + final.push(convertedNode); + } + } else { + final.push(node); + } + } + return final; } function setDefaultNames(items, defaultName, usedNames) { - const itemsLength = items.length; - for (let i = 0; i < itemsLength; ++i) { - const item = items[i]; - let name = defaultValue(item.name, defaultName); - const occurrences = usedNames[name]; - if (defined(occurrences)) { - usedNames[name]++; - name = name + '_' + occurrences; - } else { - usedNames[name] = 1; - } - item.name = name; + const itemsLength = items.length; + for (let i = 0; i < itemsLength; ++i) { + const item = items[i]; + let name = defaultValue(item.name, defaultName); + const occurrences = usedNames[name]; + if (defined(occurrences)) { + usedNames[name]++; + name = name + "_" + occurrences; + } else { + usedNames[name] = 1; } + item.name = name; + } } function setDefaults(nodes) { - const usedNames = {}; - setDefaultNames(nodes, 'Node', usedNames); - const nodesLength = nodes.length; - for (let i = 0; i < nodesLength; ++i) { - const node = nodes[i]; - setDefaultNames(node.meshes, node.name + '-Mesh', usedNames); - } + const usedNames = {}; + setDefaultNames(nodes, "Node", usedNames); + const nodesLength = nodes.length; + for (let i = 0; i < nodesLength; ++i) { + const node = nodes[i]; + setDefaultNames(node.meshes, node.name + "-Mesh", usedNames); + } } function cleanNodes(nodes) { - nodes = removeEmptyNodes(nodes); - setDefaults(nodes); - return nodes; + nodes = removeEmptyNodes(nodes); + setDefaults(nodes); + return nodes; } function getAxisTransform(inputUpAxis, outputUpAxis) { - if (inputUpAxis === 'X' && outputUpAxis === 'Y') { - return Axis.X_UP_TO_Y_UP; - } else if (inputUpAxis === 'X' && outputUpAxis === 'Z') { - return Axis.X_UP_TO_Z_UP; - } else if (inputUpAxis === 'Y' && outputUpAxis === 'X') { - return Axis.Y_UP_TO_X_UP; - } else if (inputUpAxis === 'Y' && outputUpAxis === 'Z') { - return Axis.Y_UP_TO_Z_UP; - } else if (inputUpAxis === 'Z' && outputUpAxis === 'X') { - return Axis.Z_UP_TO_X_UP; - } else if (inputUpAxis === 'Z' && outputUpAxis === 'Y') { - return Axis.Z_UP_TO_Y_UP; - } + if (inputUpAxis === "X" && outputUpAxis === "Y") { + return Axis.X_UP_TO_Y_UP; + } else if (inputUpAxis === "X" && outputUpAxis === "Z") { + return Axis.X_UP_TO_Z_UP; + } else if (inputUpAxis === "Y" && outputUpAxis === "X") { + return Axis.Y_UP_TO_X_UP; + } else if (inputUpAxis === "Y" && outputUpAxis === "Z") { + return Axis.Y_UP_TO_Z_UP; + } else if (inputUpAxis === "Z" && outputUpAxis === "X") { + return Axis.Z_UP_TO_X_UP; + } else if (inputUpAxis === "Z" && outputUpAxis === "Y") { + return Axis.Z_UP_TO_Y_UP; + } } diff --git a/lib/loadTexture.js b/lib/loadTexture.js index 768e5ff..597d434 100644 --- a/lib/loadTexture.js +++ b/lib/loadTexture.js @@ -1,11 +1,11 @@ -'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'); +"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"); const defaultValue = Cesium.defaultValue; const defined = Cesium.defined; @@ -25,112 +25,109 @@ module.exports = loadTexture; * @private */ function loadTexture(texturePath, options) { - options = defaultValue(options, {}); - options.checkTransparency = defaultValue(options.checkTransparency, false); - options.decode = defaultValue(options.decode, false); - options.keepSource = defaultValue(options.keepSource, false); + options = defaultValue(options, {}); + options.checkTransparency = defaultValue(options.checkTransparency, false); + options.decode = defaultValue(options.decode, false); + options.keepSource = defaultValue(options.keepSource, false); - 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; + 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; - let decodePromise; - if (extension === '.png') { - decodePromise = decodePng(texture, options); - } else if (extension === '.jpg' || extension === '.jpeg') { - decodePromise = decodeJpeg(texture, options); - } + let decodePromise; + if (extension === ".png") { + decodePromise = decodePng(texture, options); + } else if (extension === ".jpg" || extension === ".jpeg") { + decodePromise = decodeJpeg(texture, options); + } - if (defined(decodePromise)) { - return decodePromise - .then(function() { - return texture; - }); - } + if (defined(decodePromise)) { + return decodePromise.then(function () { + return texture; + }); + } - return texture; - }); + return texture; + }); } function hasTransparency(pixels) { - const pixelsLength = pixels.length / 4; - for (let i = 0; i < pixelsLength; ++i) { - if (pixels[i * 4 + 3] < 255) { - return true; - } + const pixelsLength = pixels.length / 4; + for (let i = 0; i < pixelsLength; ++i) { + if (pixels[i * 4 + 3] < 255) { + return true; } - return false; + } + return false; } 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; - } + 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); - }); + 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) { - // Color type is encoded in the 25th bit of the png - const source = texture.source; - const colorType = source[25]; - const channels = getChannels(colorType); + // Color type is encoded in the 25th bit of the png + const source = texture.source; + const colorType = source[25]; + const channels = getChannels(colorType); - const checkTransparency = (channels === 4 && options.checkTransparency); - const decode = options.decode || checkTransparency; + const checkTransparency = channels === 4 && options.checkTransparency; + const decode = options.decode || checkTransparency; - 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) { - texture.source = undefined; // Unload resources - } - } - }); - } -} - -function decodeJpeg(texture, options) { - if (options.decode) { - const source = texture.source; - const decodedResults = jpeg.decode(source); + 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) { - texture.source = undefined; // Unload resources + texture.source = undefined; // Unload resources } - } + } + }); + } +} + +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 + } + } } diff --git a/lib/obj2gltf.js b/lib/obj2gltf.js index dd537e2..0916df9 100644 --- a/lib/obj2gltf.js +++ b/lib/obj2gltf.js @@ -1,10 +1,10 @@ -'use strict'; -const Cesium = require('cesium'); -const fsExtra = require('fs-extra'); -const path = require('path'); -const createGltf = require('./createGltf'); -const loadObj = require('./loadObj'); -const writeGltf = require('./writeGltf'); +"use strict"; +const Cesium = require("cesium"); +const fsExtra = require("fs-extra"); +const path = require("path"); +const createGltf = require("./createGltf"); +const loadObj = require("./loadObj"); +const writeGltf = require("./writeGltf"); const defaultValue = Cesium.defaultValue; const defined = Cesium.defined; @@ -43,152 +43,190 @@ module.exports = obj2gltf; * @return {Promise} A promise that resolves to the glTF JSON or glb buffer. */ function obj2gltf(objPath, options) { - const defaults = obj2gltf.defaults; - options = defaultValue(options, {}); - options.binary = defaultValue(options.binary, defaults.binary); - options.separate = defaultValue(options.separate, defaults.separate); - options.separateTextures = defaultValue(options.separateTextures, defaults.separateTextures) || options.separate; - options.checkTransparency = defaultValue(options.checkTransparency, defaults.checkTransparency); - options.secure = defaultValue(options.secure, defaults.secure); - options.packOcclusion = defaultValue(options.packOcclusion, defaults.packOcclusion); - options.metallicRoughness = defaultValue(options.metallicRoughness, defaults.metallicRoughness); - options.specularGlossiness = defaultValue(options.specularGlossiness, defaults.specularGlossiness); - options.unlit = defaultValue(options.unlit, defaults.unlit); - options.overridingTextures = defaultValue(options.overridingTextures, defaultValue.EMPTY_OBJECT); - options.logger = defaultValue(options.logger, getDefaultLogger()); - options.writer = defaultValue(options.writer, getDefaultWriter(options.outputDirectory)); - options.inputUpAxis = defaultValue(options.inputUpAxis, defaults.inputUpAxis); - options.outputUpAxis = defaultValue(options.outputUpAxis, defaults.outputUpAxis); - options.triangleWindingOrderSanitization = defaultValue(options.triangleWindingOrderSanitization, defaults.triangleWindingOrderSanitization); + const defaults = obj2gltf.defaults; + options = defaultValue(options, {}); + options.binary = defaultValue(options.binary, defaults.binary); + options.separate = defaultValue(options.separate, defaults.separate); + options.separateTextures = + defaultValue(options.separateTextures, defaults.separateTextures) || + options.separate; + options.checkTransparency = defaultValue( + options.checkTransparency, + defaults.checkTransparency + ); + options.secure = defaultValue(options.secure, defaults.secure); + options.packOcclusion = defaultValue( + options.packOcclusion, + defaults.packOcclusion + ); + options.metallicRoughness = defaultValue( + options.metallicRoughness, + defaults.metallicRoughness + ); + options.specularGlossiness = defaultValue( + options.specularGlossiness, + defaults.specularGlossiness + ); + options.unlit = defaultValue(options.unlit, defaults.unlit); + options.overridingTextures = defaultValue( + options.overridingTextures, + defaultValue.EMPTY_OBJECT + ); + options.logger = defaultValue(options.logger, getDefaultLogger()); + options.writer = defaultValue( + options.writer, + getDefaultWriter(options.outputDirectory) + ); + options.inputUpAxis = defaultValue(options.inputUpAxis, defaults.inputUpAxis); + options.outputUpAxis = defaultValue( + options.outputUpAxis, + defaults.outputUpAxis + ); + options.triangleWindingOrderSanitization = defaultValue( + options.triangleWindingOrderSanitization, + defaults.triangleWindingOrderSanitization + ); - if (!defined(objPath)) { - throw new DeveloperError('objPath is required'); - } + if (!defined(objPath)) { + throw new DeveloperError("objPath is required"); + } - if (options.separateTextures && !defined(options.writer)) { - throw new DeveloperError('Either options.writer or options.outputDirectory must be defined when writing separate resources.'); - } + if (options.separateTextures && !defined(options.writer)) { + throw new DeveloperError( + "Either options.writer or options.outputDirectory must be defined when writing separate resources." + ); + } - if (options.metallicRoughness + options.specularGlossiness + options.unlit > 1) { - throw new DeveloperError('Only one material type may be set from [metallicRoughness, specularGlossiness, unlit].'); - } + if ( + options.metallicRoughness + options.specularGlossiness + options.unlit > + 1 + ) { + throw new DeveloperError( + "Only one material type may be set from [metallicRoughness, specularGlossiness, unlit]." + ); + } - if (defined(options.overridingTextures.metallicRoughnessOcclusionTexture) && defined(options.overridingTextures.specularGlossinessTexture)) { - throw new DeveloperError('metallicRoughnessOcclusionTexture and specularGlossinessTexture cannot both be defined.'); - } + if ( + defined(options.overridingTextures.metallicRoughnessOcclusionTexture) && + defined(options.overridingTextures.specularGlossinessTexture) + ) { + throw new DeveloperError( + "metallicRoughnessOcclusionTexture and specularGlossinessTexture cannot both be defined." + ); + } - if (defined(options.overridingTextures.metallicRoughnessOcclusionTexture)) { - options.metallicRoughness = true; - options.specularGlossiness = false; - options.packOcclusion = true; - } + if (defined(options.overridingTextures.metallicRoughnessOcclusionTexture)) { + options.metallicRoughness = true; + options.specularGlossiness = false; + options.packOcclusion = true; + } - if (defined(options.overridingTextures.specularGlossinessTexture)) { - options.metallicRoughness = false; - options.specularGlossiness = true; - } + if (defined(options.overridingTextures.specularGlossinessTexture)) { + options.metallicRoughness = false; + options.specularGlossiness = true; + } - return loadObj(objPath, options) - .then(function(objData) { - return createGltf(objData, options); - }) - .then(function(gltf) { - return writeGltf(gltf, options); - }); + return loadObj(objPath, options) + .then(function (objData) { + return createGltf(objData, options); + }) + .then(function (gltf) { + return writeGltf(gltf, options); + }); } function getDefaultLogger() { - return function(message) { - console.log(message); - }; + return function (message) { + console.log(message); + }; } function getDefaultWriter(outputDirectory) { - if (defined(outputDirectory)) { - return function(file, data) { - const outputFile = path.join(outputDirectory, file); - return fsExtra.outputFile(outputFile, data); - }; - } + if (defined(outputDirectory)) { + return function (file, data) { + const outputFile = path.join(outputDirectory, file); + return fsExtra.outputFile(outputFile, data); + }; + } } /** * Default values that will be used when calling obj2gltf(options) unless specified in the options object. */ obj2gltf.defaults = { - /** - * Gets or sets whether the converter will return a glb. - * @type Boolean - * @default false - */ - binary : false, - /** - * Gets or sets whether to write out separate buffer and texture, - * shader files, and textures instead of embedding them in the glTF. - * @type Boolean - * @default false - */ - separate : false, - /** - * Gets or sets whether to write out separate textures only. - * @type Boolean - * @default false - */ - separateTextures : false, - /** - * Gets or sets whether the converter will do a more exhaustive check for texture transparency by looking at the alpha channel of each pixel. - * @type Boolean - * @default false - */ - checkTransparency : false, - /** - * Gets or sets whether the source model can reference paths outside of its directory. - * @type Boolean - * @default false - */ - secure : false, - /** - * Gets or sets whether to pack the occlusion texture in the red channel of the metallic-roughness texture. - * @type Boolean - * @default false - */ - packOcclusion : false, - /** - * Gets or sets whether rhe values in the .mtl file are already metallic-roughness PBR values and no conversion step should be applied. Metallic is stored in the Ks and map_Ks slots and roughness is stored in the Ns and map_Ns slots. - * @type Boolean - * @default false - */ - metallicRoughness : false, - /** - * Gets or sets whether the values in the .mtl file are already specular-glossiness PBR values and no conversion step should be applied. Specular is stored in the Ks and map_Ks slots and glossiness is stored in the Ns and map_Ns slots. The glTF will be saved with the KHR_materials_pbrSpecularGlossiness extension. - * @type Boolean - * @default false - */ - specularGlossiness : false, - /** - * Gets or sets whether the glTF will be saved with the KHR_materials_unlit extension. - * @type Boolean - * @default false - */ - unlit : false, - /** - * Gets or sets the up axis of the obj. - * @type String - * @default 'Y' - */ - inputUpAxis: 'Y', - /** - * Gets or sets the up axis of the converted glTF. - * @type String - * @default 'Y' - */ - outputUpAxis: 'Y', - /** - * Gets or sets whether triangle winding order sanitization will be applied. - * @type Boolean - * @default false - */ - windingOrderSanitization : false + /** + * Gets or sets whether the converter will return a glb. + * @type Boolean + * @default false + */ + binary: false, + /** + * Gets or sets whether to write out separate buffer and texture, + * shader files, and textures instead of embedding them in the glTF. + * @type Boolean + * @default false + */ + separate: false, + /** + * Gets or sets whether to write out separate textures only. + * @type Boolean + * @default false + */ + separateTextures: false, + /** + * Gets or sets whether the converter will do a more exhaustive check for texture transparency by looking at the alpha channel of each pixel. + * @type Boolean + * @default false + */ + checkTransparency: false, + /** + * Gets or sets whether the source model can reference paths outside of its directory. + * @type Boolean + * @default false + */ + secure: false, + /** + * Gets or sets whether to pack the occlusion texture in the red channel of the metallic-roughness texture. + * @type Boolean + * @default false + */ + packOcclusion: false, + /** + * Gets or sets whether rhe values in the .mtl file are already metallic-roughness PBR values and no conversion step should be applied. Metallic is stored in the Ks and map_Ks slots and roughness is stored in the Ns and map_Ns slots. + * @type Boolean + * @default false + */ + metallicRoughness: false, + /** + * Gets or sets whether the values in the .mtl file are already specular-glossiness PBR values and no conversion step should be applied. Specular is stored in the Ks and map_Ks slots and glossiness is stored in the Ns and map_Ns slots. The glTF will be saved with the KHR_materials_pbrSpecularGlossiness extension. + * @type Boolean + * @default false + */ + specularGlossiness: false, + /** + * Gets or sets whether the glTF will be saved with the KHR_materials_unlit extension. + * @type Boolean + * @default false + */ + unlit: false, + /** + * Gets or sets the up axis of the obj. + * @type String + * @default 'Y' + */ + inputUpAxis: "Y", + /** + * Gets or sets the up axis of the converted glTF. + * @type String + * @default 'Y' + */ + outputUpAxis: "Y", + /** + * Gets or sets whether triangle winding order sanitization will be applied. + * @type Boolean + * @default false + */ + windingOrderSanitization: false, }; /** diff --git a/lib/outsideDirectory.js b/lib/outsideDirectory.js index 27a284d..0d5bf0f 100644 --- a/lib/outsideDirectory.js +++ b/lib/outsideDirectory.js @@ -1,5 +1,5 @@ -'use strict'; -const path = require('path'); +"use strict"; +const path = require("path"); module.exports = outsideDirectory; @@ -13,5 +13,5 @@ module.exports = outsideDirectory; * @private */ function outsideDirectory(file, directory) { - return (path.relative(directory, file).indexOf('..') === 0); + return path.relative(directory, file).indexOf("..") === 0; } diff --git a/lib/readLines.js b/lib/readLines.js index a566fb5..0977b1c 100644 --- a/lib/readLines.js +++ b/lib/readLines.js @@ -1,7 +1,7 @@ -'use strict'; -const fsExtra = require('fs-extra'); -const Promise = require('bluebird'); -const readline = require('readline'); +"use strict"; +const fsExtra = require("fs-extra"); +const Promise = require("bluebird"); +const readline = require("readline"); module.exports = readLines; @@ -15,23 +15,23 @@ module.exports = readLines; * @private */ function readLines(path, callback) { - return new Promise(function(resolve, reject) { - const stream = fsExtra.createReadStream(path); - stream.on('error', reject); - stream.on('end', resolve); + return new Promise(function (resolve, reject) { + const stream = fsExtra.createReadStream(path); + stream.on("error", reject); + stream.on("end", resolve); - const lineReader = readline.createInterface({ - input : stream - }); - - const callbackWrapper = function(line) { - try { - callback(line); - } catch (error) { - reject(error); - } - }; - - lineReader.on('line', callbackWrapper); + const lineReader = readline.createInterface({ + input: stream, }); + + const callbackWrapper = function (line) { + try { + callback(line); + } catch (error) { + reject(error); + } + }; + + lineReader.on("line", callbackWrapper); + }); } diff --git a/lib/writeGltf.js b/lib/writeGltf.js index 000b7e4..903212f 100644 --- a/lib/writeGltf.js +++ b/lib/writeGltf.js @@ -1,10 +1,10 @@ -'use strict'; -const Cesium = require('cesium'); -const mime = require('mime'); -const PNG = require('pngjs').PNG; -const Promise = require('bluebird'); -const getBufferPadded = require('./getBufferPadded'); -const gltfToGlb = require('./gltfToGlb'); +"use strict"; +const Cesium = require("cesium"); +const mime = require("mime"); +const PNG = require("pngjs").PNG; +const Promise = require("bluebird"); +const getBufferPadded = require("./getBufferPadded"); +const gltfToGlb = require("./gltfToGlb"); const defined = Cesium.defined; const RuntimeError = Cesium.RuntimeError; @@ -21,170 +21,185 @@ module.exports = writeGltf; * @private */ function writeGltf(gltf, options) { - return encodeTextures(gltf) - .then(function() { - const binary = options.binary; - const separate = options.separate; - const separateTextures = options.separateTextures; + return encodeTextures(gltf).then(function () { + const binary = options.binary; + const separate = options.separate; + const separateTextures = options.separateTextures; - const promises = []; - if (separateTextures) { - promises.push(writeSeparateTextures(gltf, options)); - } else { - writeEmbeddedTextures(gltf); - } + const promises = []; + if (separateTextures) { + promises.push(writeSeparateTextures(gltf, options)); + } else { + writeEmbeddedTextures(gltf); + } - if (separate) { - promises.push(writeSeparateBuffers(gltf, options)); - } else if (!binary) { - writeEmbeddedBuffer(gltf); - } + if (separate) { + promises.push(writeSeparateBuffers(gltf, options)); + } else if (!binary) { + writeEmbeddedBuffer(gltf); + } - const binaryBuffer = gltf.buffers[0].extras._obj2gltf.source; + const binaryBuffer = gltf.buffers[0].extras._obj2gltf.source; - return Promise.all(promises) - .then(function() { - deleteExtras(gltf); - removeEmpty(gltf); - if (binary) { - return gltfToGlb(gltf, binaryBuffer); - } - return gltf; - }); - }); + return Promise.all(promises).then(function () { + deleteExtras(gltf); + removeEmpty(gltf); + if (binary) { + return gltfToGlb(gltf, binaryBuffer); + } + return gltf; + }); + }); } function encodePng(texture) { - // Constants defined by pngjs - const rgbColorType = 2; - const rgbaColorType = 6; + // Constants defined by pngjs + const rgbColorType = 2; + const rgbaColorType = 6; - const png = new PNG({ - width : texture.width, - height : texture.height, - colorType : texture.transparent ? rgbaColorType : rgbColorType, - inputColorType : rgbaColorType, - inputHasAlpha : true + const png = new PNG({ + width: texture.width, + height: texture.height, + colorType: texture.transparent ? rgbaColorType : rgbColorType, + inputColorType: rgbaColorType, + inputHasAlpha: true, + }); + + png.data = texture.pixels; + + return new Promise(function (resolve, reject) { + const chunks = []; + const stream = png.pack(); + stream.on("data", function (chunk) { + chunks.push(chunk); }); - - png.data = texture.pixels; - - return new Promise(function(resolve, reject) { - const chunks = []; - const stream = png.pack(); - stream.on('data', function(chunk) { - chunks.push(chunk); - }); - stream.on('end', function() { - resolve(Buffer.concat(chunks)); - }); - stream.on('error', reject); + stream.on("end", function () { + resolve(Buffer.concat(chunks)); }); + stream.on("error", reject); + }); } function encodeTexture(texture) { - if (!defined(texture.source) && defined(texture.pixels) && texture.extension === '.png') { - return encodePng(texture) - .then(function(encoded) { - texture.source = encoded; - }); - } + if ( + !defined(texture.source) && + defined(texture.pixels) && + texture.extension === ".png" + ) { + return encodePng(texture).then(function (encoded) { + texture.source = encoded; + }); + } } function encodeTextures(gltf) { - // Dynamically generated PBR textures need to be encoded to png prior to being saved - const encodePromises = []; - const images = gltf.images; - const length = images.length; - for (let i = 0; i < length; ++i) { - encodePromises.push(encodeTexture(images[i].extras._obj2gltf)); - } - return Promise.all(encodePromises); + // Dynamically generated PBR textures need to be encoded to png prior to being saved + const encodePromises = []; + const images = gltf.images; + const length = images.length; + for (let i = 0; i < length; ++i) { + encodePromises.push(encodeTexture(images[i].extras._obj2gltf)); + } + return Promise.all(encodePromises); } function deleteExtras(gltf) { - const buffers = gltf.buffers; - const buffersLength = buffers.length; - for (let i = 0; i < buffersLength; ++i) { - delete buffers[i].extras; - } + const buffers = gltf.buffers; + const buffersLength = buffers.length; + for (let i = 0; i < buffersLength; ++i) { + delete buffers[i].extras; + } - const images = gltf.images; - const imagesLength = images.length; - for (let i = 0; i < imagesLength; ++i) { - delete images[i].extras; - } + const images = gltf.images; + const imagesLength = images.length; + for (let i = 0; i < imagesLength; ++i) { + delete images[i].extras; + } } function removeEmpty(json) { - Object.keys(json).forEach(function(key) { - if (!defined(json[key]) || (Array.isArray(json[key]) && json[key].length === 0)) { - delete json[key]; // Delete values that are undefined or [] - } else if (typeof json[key] === 'object') { - removeEmpty(json[key]); - } - }); + Object.keys(json).forEach(function (key) { + if ( + !defined(json[key]) || + (Array.isArray(json[key]) && json[key].length === 0) + ) { + delete json[key]; // Delete values that are undefined or [] + } else if (typeof json[key] === "object") { + removeEmpty(json[key]); + } + }); } function writeSeparateBuffers(gltf, options) { - const buffers = gltf.buffers; - return Promise.map(buffers, function(buffer) { - const source = buffer.extras._obj2gltf.source; - const bufferUri = buffer.name + '.bin'; - buffer.uri = bufferUri; - return options.writer(bufferUri, source); - }, {concurrency : 10}); + const buffers = gltf.buffers; + return Promise.map( + buffers, + function (buffer) { + const source = buffer.extras._obj2gltf.source; + const bufferUri = buffer.name + ".bin"; + buffer.uri = bufferUri; + return options.writer(bufferUri, source); + }, + { concurrency: 10 } + ); } function writeSeparateTextures(gltf, options) { - const images = gltf.images; - return Promise.map(images, function(image) { - const texture = image.extras._obj2gltf; - const imageUri = image.name + texture.extension; - image.uri = imageUri; - return options.writer(imageUri, texture.source); - }, {concurrency : 10}); + const images = gltf.images; + return Promise.map( + images, + function (image) { + const texture = image.extras._obj2gltf; + const imageUri = image.name + texture.extension; + image.uri = imageUri; + return options.writer(imageUri, texture.source); + }, + { concurrency: 10 } + ); } function writeEmbeddedBuffer(gltf) { - const buffer = gltf.buffers[0]; - const source = buffer.extras._obj2gltf.source; + const buffer = gltf.buffers[0]; + const source = buffer.extras._obj2gltf.source; - // Buffers larger than ~192MB cannot be base64 encoded due to a NodeJS limitation. Source: https://github.com/nodejs/node/issues/4266 - if (source.length > 201326580) { - throw new RuntimeError('Buffer is too large to embed in the glTF. Use the --separate flag instead.'); - } + // Buffers larger than ~192MB cannot be base64 encoded due to a NodeJS limitation. Source: https://github.com/nodejs/node/issues/4266 + if (source.length > 201326580) { + throw new RuntimeError( + "Buffer is too large to embed in the glTF. Use the --separate flag instead." + ); + } - buffer.uri = 'data:application/octet-stream;base64,' + source.toString('base64'); + buffer.uri = + "data:application/octet-stream;base64," + source.toString("base64"); } function writeEmbeddedTextures(gltf) { - const buffer = gltf.buffers[0]; - const bufferExtras = buffer.extras._obj2gltf; - const bufferSource = bufferExtras.source; - const images = gltf.images; - const imagesLength = images.length; - const sources = [bufferSource]; - let byteOffset = bufferSource.length; + const buffer = gltf.buffers[0]; + const bufferExtras = buffer.extras._obj2gltf; + const bufferSource = bufferExtras.source; + const images = gltf.images; + const imagesLength = images.length; + const sources = [bufferSource]; + let byteOffset = bufferSource.length; - for (let i = 0; i < imagesLength; ++i) { - const image = images[i]; - const texture = image.extras._obj2gltf; - const textureSource = texture.source; - const textureByteLength = textureSource.length; + for (let i = 0; i < imagesLength; ++i) { + const image = images[i]; + const texture = image.extras._obj2gltf; + const textureSource = texture.source; + const textureByteLength = textureSource.length; - image.mimeType = mime.getType(texture.extension); - image.bufferView = gltf.bufferViews.length; - gltf.bufferViews.push({ - buffer : 0, - byteOffset : byteOffset, - byteLength : textureByteLength - }); - byteOffset += textureByteLength; - sources.push(textureSource); - } + image.mimeType = mime.getType(texture.extension); + image.bufferView = gltf.bufferViews.length; + gltf.bufferViews.push({ + buffer: 0, + byteOffset: byteOffset, + byteLength: textureByteLength, + }); + byteOffset += textureByteLength; + sources.push(textureSource); + } - const source = getBufferPadded(Buffer.concat(sources)); - bufferExtras.source = source; - buffer.byteLength = source.length; + const source = getBufferPadded(Buffer.concat(sources)); + bufferExtras.source = source; + buffer.byteLength = source.length; } diff --git a/specs/lib/createGltfSpec.js b/specs/lib/createGltfSpec.js index 831b1c8..00cdd91 100644 --- a/specs/lib/createGltfSpec.js +++ b/specs/lib/createGltfSpec.js @@ -1,288 +1,308 @@ -'use strict'; -const Cesium = require('cesium'); -const obj2gltf = require('../../lib/obj2gltf'); -const createGltf = require('../../lib/createGltf'); -const loadObj = require('../../lib/loadObj'); -const { getDefaultMaterial } = require('../../lib/loadMtl'); +"use strict"; +const Cesium = require("cesium"); +const obj2gltf = require("../../lib/obj2gltf"); +const createGltf = require("../../lib/createGltf"); +const loadObj = require("../../lib/loadObj"); +const { getDefaultMaterial } = require("../../lib/loadMtl"); const clone = Cesium.clone; const defined = Cesium.defined; const WebGLConstants = Cesium.WebGLConstants; -const boxObjPath = 'specs/data/box/box.obj'; -const groupObjPath = 'specs/data/box-objects-groups-materials/box-objects-groups-materials.obj'; -const complexObjPath = 'specs/data/box-complex-material/box-complex-material.obj'; -const noMaterialsObjPath = 'specs/data/box-no-materials/box-no-materials.obj'; -const mixedAttributesObjPath = 'specs/data/box-mixed-attributes-2/box-mixed-attributes-2.obj'; +const boxObjPath = "specs/data/box/box.obj"; +const groupObjPath = + "specs/data/box-objects-groups-materials/box-objects-groups-materials.obj"; +const complexObjPath = + "specs/data/box-complex-material/box-complex-material.obj"; +const noMaterialsObjPath = "specs/data/box-no-materials/box-no-materials.obj"; +const mixedAttributesObjPath = + "specs/data/box-mixed-attributes-2/box-mixed-attributes-2.obj"; let options; -describe('createGltf', () => { - let boxObjData; - let groupObjData; - let complexObjData; - let noMaterialsObjData; - let mixedAttributesObjData; +describe("createGltf", () => { + let boxObjData; + let groupObjData; + let complexObjData; + let noMaterialsObjData; + let mixedAttributesObjData; - beforeEach(async () => { - options = clone(obj2gltf.defaults); - options.overridingTextures = {}; - options.logger = () => {}; + beforeEach(async () => { + options = clone(obj2gltf.defaults); + options.overridingTextures = {}; + options.logger = () => {}; - boxObjData = await loadObj(boxObjPath, options); - groupObjData = await loadObj(groupObjPath, options); - complexObjData = await loadObj(complexObjPath, options); - noMaterialsObjData = await loadObj(noMaterialsObjPath, options); - mixedAttributesObjData = await loadObj(mixedAttributesObjPath, options); - }); + boxObjData = await loadObj(boxObjPath, options); + groupObjData = await loadObj(groupObjPath, options); + complexObjData = await loadObj(complexObjPath, options); + noMaterialsObjData = await loadObj(noMaterialsObjPath, options); + mixedAttributesObjData = await loadObj(mixedAttributesObjPath, options); + }); - it('simple gltf', () => { - const gltf = createGltf(boxObjData, options); + it("simple gltf", () => { + const gltf = createGltf(boxObjData, options); - expect(gltf.materials.length).toBe(1); - expect(gltf.scene).toBe(0); - expect(gltf.scenes[0].nodes[0]).toBe(0); - expect(gltf.nodes.length).toBe(1); - expect(gltf.meshes.length).toBe(1); + expect(gltf.materials.length).toBe(1); + expect(gltf.scene).toBe(0); + expect(gltf.scenes[0].nodes[0]).toBe(0); + expect(gltf.nodes.length).toBe(1); + expect(gltf.meshes.length).toBe(1); - const primitives = gltf.meshes[0].primitives; - const primitive = primitives[0]; - const attributes = primitive.attributes; - const positionAccessor = gltf.accessors[attributes.POSITION]; - const normalAccessor = gltf.accessors[attributes.NORMAL]; - const uvAccessor = gltf.accessors[attributes.TEXCOORD_0]; - const indexAccessor = gltf.accessors[primitive.indices]; + const primitives = gltf.meshes[0].primitives; + const primitive = primitives[0]; + const attributes = primitive.attributes; + const positionAccessor = gltf.accessors[attributes.POSITION]; + const normalAccessor = gltf.accessors[attributes.NORMAL]; + const uvAccessor = gltf.accessors[attributes.TEXCOORD_0]; + const indexAccessor = gltf.accessors[primitive.indices]; - expect(primitives.length).toBe(1); - expect(positionAccessor.count).toBe(24); - expect(normalAccessor.count).toBe(24); - expect(uvAccessor.count).toBe(24); - expect(indexAccessor.count).toBe(36); - }); + expect(primitives.length).toBe(1); + expect(positionAccessor.count).toBe(24); + expect(normalAccessor.count).toBe(24); + expect(uvAccessor.count).toBe(24); + expect(indexAccessor.count).toBe(36); + }); - it('does not combine buffers when that buffer would exceed the Node buffer size limit', () => { - spyOn(createGltf, '_getBufferMaxByteLength').and.returnValue(0); - const clonedOptions = clone(options, true); - clonedOptions.separate = true; + it("does not combine buffers when that buffer would exceed the Node buffer size limit", () => { + spyOn(createGltf, "_getBufferMaxByteLength").and.returnValue(0); + const clonedOptions = clone(options, true); + clonedOptions.separate = true; - const gltf = createGltf(boxObjData, clonedOptions); - expect(gltf.accessors.length).toBe(4); - expect(gltf.buffers.length).toBe(4); - expect(gltf.bufferViews.length).toBe(4); + const gltf = createGltf(boxObjData, clonedOptions); + expect(gltf.accessors.length).toBe(4); + expect(gltf.buffers.length).toBe(4); + expect(gltf.bufferViews.length).toBe(4); - const length = gltf.buffers.length; - for (let i = 0; i < length; ++i) { - const accessor = gltf.accessors[i]; - const bufferView = gltf.bufferViews[i]; - const buffer = gltf.buffers[i]; - expect(accessor.bufferView).toBe(i); - expect(accessor.byteOffset).toBe(0); - expect(bufferView.buffer).toBe(i); - expect(bufferView.byteOffset).toBe(0); - expect(bufferView.byteLength).toBe(buffer.byteLength); - } - }); - - it('multiple nodes, meshes, and primitives', () => { - const gltf = createGltf(groupObjData, options); - - expect(gltf.materials.length).toBe(3); - expect(gltf.scene).toBe(0); - expect(gltf.scenes[0].nodes[0]).toBe(0); - expect(gltf.nodes.length).toBe(4); - expect(gltf.nodes[0].mesh).toBeUndefined(); - expect(gltf.nodes[0].children.length).toBe(3); - expect(gltf.meshes.length).toBe(3); - - // Check for two primitives in each mesh - const length = gltf.meshes.length; - for (let i = 0; i < length; ++i) { - const mesh = gltf.meshes[i]; - expect(mesh.primitives.length).toBe(2); - } - }); - - it('multiple textures', () => { - const gltf = createGltf(complexObjData, options); - const material = gltf.materials[0]; - const pbr = material.pbrMetallicRoughness; - const textures = [pbr.metallicRoughnessTexture, pbr.baseColorTexture, material.emissiveTexture, material.normalTexture, material.occlusionTexture]; - expect(textures.map((texture) => { - return texture.index; - }).sort()).toEqual([0, 1, 2, 3, 4]); - expect(gltf.samplers[0]).toBeDefined(); - }); - - it('creates default material', () => { - const gltf = createGltf(noMaterialsObjData, options); - const material = gltf.materials[0]; - const pbr = material.pbrMetallicRoughness; - expect(material.name).toBe('default'); - expect(pbr.baseColorTexture).toBeUndefined(); - expect(pbr.metallicRoughnessTexture).toBeUndefined(); - expect(pbr.baseColorFactor).toEqual([0.5, 0.5, 0.5, 1.0]); - expect(pbr.metallicFactor).toBe(0.0); // No metallic - expect(pbr.roughnessFactor).toBe(1.0); // Fully rough - expect(material.emissiveTexture).toBeUndefined(); - expect(material.normalTexture).toBeUndefined(); - expect(material.ambientTexture).toBeUndefined(); - expect(material.emissiveFactor).toEqual([0.0, 0.0, 0.0]); - expect(material.alphaMode).toBe('OPAQUE'); - expect(material.doubleSided).toBe(false); - }); - - it('adds KHR_materials_pbrSpecularGlossiness extension when specularGlossiness is set', () => { - options.specularGlossiness = true; - const gltf = createGltf(noMaterialsObjData, options); - expect(gltf.extensionsUsed).toEqual(['KHR_materials_pbrSpecularGlossiness']); - expect(gltf.extensionsRequired).toEqual(['KHR_materials_pbrSpecularGlossiness']); - }); - - it('adds KHR_materials_unlit extension when unlit is set', () => { - options.unlit = true; - const gltf = createGltf(noMaterialsObjData, options); - expect(gltf.extensionsUsed).toEqual(['KHR_materials_unlit']); - expect(gltf.extensionsRequired).toEqual(['KHR_materials_unlit']); - }); - - it('runs without normals', () => { - boxObjData.nodes[0].meshes[0].primitives[0].normals.length = 0; - - const gltf = createGltf(boxObjData, options); - const attributes = gltf.meshes[0].primitives[0].attributes; - expect(attributes.POSITION).toBeDefined(); - expect(attributes.NORMAL).toBeUndefined(); - expect(attributes.TEXCOORD_0).toBeDefined(); - }); - - it('runs without uvs', () => { - boxObjData.nodes[0].meshes[0].primitives[0].uvs.length = 0; - - const gltf = createGltf(boxObjData, options); - const attributes = gltf.meshes[0].primitives[0].attributes; - expect(attributes.POSITION).toBeDefined(); - expect(attributes.NORMAL).toBeDefined(); - expect(attributes.TEXCOORD_0).toBeUndefined(); - }); - - it('runs without uvs and normals', () => { - boxObjData.nodes[0].meshes[0].primitives[0].normals.length = 0; - boxObjData.nodes[0].meshes[0].primitives[0].uvs.length = 0; - - const gltf = createGltf(boxObjData, options); - const attributes = gltf.meshes[0].primitives[0].attributes; - expect(attributes.POSITION).toBeDefined(); - expect(attributes.NORMAL).toBeUndefined(); - expect(attributes.TEXCOORD_0).toBeUndefined(); - }); - - it('splits incompatible materials', () => { - const gltf = createGltf(mixedAttributesObjData, options); - const materials = gltf.materials; - const meshes = gltf.meshes; - - const referenceMaterial = mixedAttributesObjData.materials[0]; - delete referenceMaterial.name; - referenceMaterial.pbrMetallicRoughness.baseColorTexture = { - index : 0 - }; - - const referenceMaterialNoTextures = clone(referenceMaterial, true); - referenceMaterialNoTextures.pbrMetallicRoughness.baseColorTexture = undefined; - - const defaultMaterial = getDefaultMaterial(options); - delete defaultMaterial.name; - - const materialNames = materials.map((material) => { - const name = material.name; - delete material.name; - return name; - }); - - // Expect three copies of each material for - // * positions/normals/uvs - // * positions/normals - // * positions/uvs - expect(materialNames).toEqual([ - 'default', - 'default-2', - 'default-3', - 'Material', - 'Material-2', - 'Material-3', - 'Missing', - 'Missing-2', - 'Missing-3' - ]); - - expect(materials.length).toBe(9); - expect(materials[0]).toEqual(defaultMaterial); - expect(materials[1]).toEqual(defaultMaterial); - expect(materials[2]).toEqual(defaultMaterial); - expect(materials[3]).toEqual(referenceMaterial); - expect(materials[4]).toEqual(referenceMaterial); - expect(materials[5]).toEqual(referenceMaterialNoTextures); - expect(materials[6]).toEqual(defaultMaterial); - expect(materials[7]).toEqual(defaultMaterial); - expect(materials[8]).toEqual(defaultMaterial); - - // Test that primitives without uvs reference materials without textures - const meshesLength = meshes.length; - for (let i = 0; i < meshesLength; ++i) { - const mesh = meshes[i]; - const primitives = mesh.primitives; - const primitivesLength = primitives.length; - for (let j = 0; j < primitivesLength; ++j) { - const primitive = primitives[j]; - const material = materials[primitive.material]; - if (!defined(primitive.attributes.TEXCOORD_0)) { - expect(material.pbrMetallicRoughness.baseColorTexture).toBeUndefined(); - } - } - } - }); - - function expandObjData(objData, duplicatesLength) { - const primitive = objData.nodes[0].meshes[0].primitives[0]; - const indices = primitive.indices; - const positions = primitive.positions; - const normals = primitive.normals; - const uvs = primitive.uvs; - - const indicesLength = indices.length; - const vertexCount = positions.length / 3; - - for (let i = 1; i < duplicatesLength; ++i) { - for (let j = 0; j < vertexCount; ++j) { - positions.push(0.0); - positions.push(0.0); - positions.push(0.0); - normals.push(0.0); - normals.push(0.0); - normals.push(0.0); - uvs.push(0.0); - uvs.push(0.0); - } - for (let k = 0; k < indicesLength; ++k) { - indices.push(indices.get(k) + vertexCount * i); - } - } + const length = gltf.buffers.length; + for (let i = 0; i < length; ++i) { + const accessor = gltf.accessors[i]; + const bufferView = gltf.bufferViews[i]; + const buffer = gltf.buffers[i]; + expect(accessor.bufferView).toBe(i); + expect(accessor.byteOffset).toBe(0); + expect(bufferView.buffer).toBe(i); + expect(bufferView.byteOffset).toBe(0); + expect(bufferView.byteLength).toBe(buffer.byteLength); } + }); - it('detects need to use uint32 indices', () => { - expandObjData(boxObjData, 2731); // Right above 65536 limit - let primitive = boxObjData.nodes[0].meshes[0].primitives[0]; - const indicesLength = primitive.indices.length; - const vertexCount = primitive.positions.length / 3; + it("multiple nodes, meshes, and primitives", () => { + const gltf = createGltf(groupObjData, options); - const gltf = createGltf(boxObjData, options); - primitive = gltf.meshes[0].primitives[0]; - const indicesAccessor = gltf.accessors[primitive.indices]; - expect(indicesAccessor.count).toBe(indicesLength); - expect(indicesAccessor.max[0]).toBe(vertexCount - 1); - expect(indicesAccessor.componentType).toBe(WebGLConstants.UNSIGNED_INT); + expect(gltf.materials.length).toBe(3); + expect(gltf.scene).toBe(0); + expect(gltf.scenes[0].nodes[0]).toBe(0); + expect(gltf.nodes.length).toBe(4); + expect(gltf.nodes[0].mesh).toBeUndefined(); + expect(gltf.nodes[0].children.length).toBe(3); + expect(gltf.meshes.length).toBe(3); - const positionAccessor = gltf.accessors[primitive.attributes.POSITION]; - expect(positionAccessor.count).toBe(vertexCount); + // Check for two primitives in each mesh + const length = gltf.meshes.length; + for (let i = 0; i < length; ++i) { + const mesh = gltf.meshes[i]; + expect(mesh.primitives.length).toBe(2); + } + }); + + it("multiple textures", () => { + const gltf = createGltf(complexObjData, options); + const material = gltf.materials[0]; + const pbr = material.pbrMetallicRoughness; + const textures = [ + pbr.metallicRoughnessTexture, + pbr.baseColorTexture, + material.emissiveTexture, + material.normalTexture, + material.occlusionTexture, + ]; + expect( + textures + .map((texture) => { + return texture.index; + }) + .sort() + ).toEqual([0, 1, 2, 3, 4]); + expect(gltf.samplers[0]).toBeDefined(); + }); + + it("creates default material", () => { + const gltf = createGltf(noMaterialsObjData, options); + const material = gltf.materials[0]; + const pbr = material.pbrMetallicRoughness; + expect(material.name).toBe("default"); + expect(pbr.baseColorTexture).toBeUndefined(); + expect(pbr.metallicRoughnessTexture).toBeUndefined(); + expect(pbr.baseColorFactor).toEqual([0.5, 0.5, 0.5, 1.0]); + expect(pbr.metallicFactor).toBe(0.0); // No metallic + expect(pbr.roughnessFactor).toBe(1.0); // Fully rough + expect(material.emissiveTexture).toBeUndefined(); + expect(material.normalTexture).toBeUndefined(); + expect(material.ambientTexture).toBeUndefined(); + expect(material.emissiveFactor).toEqual([0.0, 0.0, 0.0]); + expect(material.alphaMode).toBe("OPAQUE"); + expect(material.doubleSided).toBe(false); + }); + + it("adds KHR_materials_pbrSpecularGlossiness extension when specularGlossiness is set", () => { + options.specularGlossiness = true; + const gltf = createGltf(noMaterialsObjData, options); + expect(gltf.extensionsUsed).toEqual([ + "KHR_materials_pbrSpecularGlossiness", + ]); + expect(gltf.extensionsRequired).toEqual([ + "KHR_materials_pbrSpecularGlossiness", + ]); + }); + + it("adds KHR_materials_unlit extension when unlit is set", () => { + options.unlit = true; + const gltf = createGltf(noMaterialsObjData, options); + expect(gltf.extensionsUsed).toEqual(["KHR_materials_unlit"]); + expect(gltf.extensionsRequired).toEqual(["KHR_materials_unlit"]); + }); + + it("runs without normals", () => { + boxObjData.nodes[0].meshes[0].primitives[0].normals.length = 0; + + const gltf = createGltf(boxObjData, options); + const attributes = gltf.meshes[0].primitives[0].attributes; + expect(attributes.POSITION).toBeDefined(); + expect(attributes.NORMAL).toBeUndefined(); + expect(attributes.TEXCOORD_0).toBeDefined(); + }); + + it("runs without uvs", () => { + boxObjData.nodes[0].meshes[0].primitives[0].uvs.length = 0; + + const gltf = createGltf(boxObjData, options); + const attributes = gltf.meshes[0].primitives[0].attributes; + expect(attributes.POSITION).toBeDefined(); + expect(attributes.NORMAL).toBeDefined(); + expect(attributes.TEXCOORD_0).toBeUndefined(); + }); + + it("runs without uvs and normals", () => { + boxObjData.nodes[0].meshes[0].primitives[0].normals.length = 0; + boxObjData.nodes[0].meshes[0].primitives[0].uvs.length = 0; + + const gltf = createGltf(boxObjData, options); + const attributes = gltf.meshes[0].primitives[0].attributes; + expect(attributes.POSITION).toBeDefined(); + expect(attributes.NORMAL).toBeUndefined(); + expect(attributes.TEXCOORD_0).toBeUndefined(); + }); + + it("splits incompatible materials", () => { + const gltf = createGltf(mixedAttributesObjData, options); + const materials = gltf.materials; + const meshes = gltf.meshes; + + const referenceMaterial = mixedAttributesObjData.materials[0]; + delete referenceMaterial.name; + referenceMaterial.pbrMetallicRoughness.baseColorTexture = { + index: 0, + }; + + const referenceMaterialNoTextures = clone(referenceMaterial, true); + referenceMaterialNoTextures.pbrMetallicRoughness.baseColorTexture = + undefined; + + const defaultMaterial = getDefaultMaterial(options); + delete defaultMaterial.name; + + const materialNames = materials.map((material) => { + const name = material.name; + delete material.name; + return name; }); + + // Expect three copies of each material for + // * positions/normals/uvs + // * positions/normals + // * positions/uvs + expect(materialNames).toEqual([ + "default", + "default-2", + "default-3", + "Material", + "Material-2", + "Material-3", + "Missing", + "Missing-2", + "Missing-3", + ]); + + expect(materials.length).toBe(9); + expect(materials[0]).toEqual(defaultMaterial); + expect(materials[1]).toEqual(defaultMaterial); + expect(materials[2]).toEqual(defaultMaterial); + expect(materials[3]).toEqual(referenceMaterial); + expect(materials[4]).toEqual(referenceMaterial); + expect(materials[5]).toEqual(referenceMaterialNoTextures); + expect(materials[6]).toEqual(defaultMaterial); + expect(materials[7]).toEqual(defaultMaterial); + expect(materials[8]).toEqual(defaultMaterial); + + // Test that primitives without uvs reference materials without textures + const meshesLength = meshes.length; + for (let i = 0; i < meshesLength; ++i) { + const mesh = meshes[i]; + const primitives = mesh.primitives; + const primitivesLength = primitives.length; + for (let j = 0; j < primitivesLength; ++j) { + const primitive = primitives[j]; + const material = materials[primitive.material]; + if (!defined(primitive.attributes.TEXCOORD_0)) { + expect( + material.pbrMetallicRoughness.baseColorTexture + ).toBeUndefined(); + } + } + } + }); + + function expandObjData(objData, duplicatesLength) { + const primitive = objData.nodes[0].meshes[0].primitives[0]; + const indices = primitive.indices; + const positions = primitive.positions; + const normals = primitive.normals; + const uvs = primitive.uvs; + + const indicesLength = indices.length; + const vertexCount = positions.length / 3; + + for (let i = 1; i < duplicatesLength; ++i) { + for (let j = 0; j < vertexCount; ++j) { + positions.push(0.0); + positions.push(0.0); + positions.push(0.0); + normals.push(0.0); + normals.push(0.0); + normals.push(0.0); + uvs.push(0.0); + uvs.push(0.0); + } + for (let k = 0; k < indicesLength; ++k) { + indices.push(indices.get(k) + vertexCount * i); + } + } + } + + it("detects need to use uint32 indices", () => { + expandObjData(boxObjData, 2731); // Right above 65536 limit + let primitive = boxObjData.nodes[0].meshes[0].primitives[0]; + const indicesLength = primitive.indices.length; + const vertexCount = primitive.positions.length / 3; + + const gltf = createGltf(boxObjData, options); + primitive = gltf.meshes[0].primitives[0]; + const indicesAccessor = gltf.accessors[primitive.indices]; + expect(indicesAccessor.count).toBe(indicesLength); + expect(indicesAccessor.max[0]).toBe(vertexCount - 1); + expect(indicesAccessor.componentType).toBe(WebGLConstants.UNSIGNED_INT); + + const positionAccessor = gltf.accessors[primitive.attributes.POSITION]; + expect(positionAccessor.count).toBe(vertexCount); + }); }); diff --git a/specs/lib/loadMtlSpec.js b/specs/lib/loadMtlSpec.js index 9dffde0..4e845d8 100644 --- a/specs/lib/loadMtlSpec.js +++ b/specs/lib/loadMtlSpec.js @@ -1,31 +1,42 @@ -'use strict'; -const Cesium = require('cesium'); -const fsExtra = require('fs-extra'); -const loadMtl = require('../../lib/loadMtl'); -const loadTexture = require('../../lib/loadTexture'); -const obj2gltf = require('../../lib/obj2gltf'); +"use strict"; +const Cesium = require("cesium"); +const fsExtra = require("fs-extra"); +const loadMtl = require("../../lib/loadMtl"); +const loadTexture = require("../../lib/loadTexture"); +const obj2gltf = require("../../lib/obj2gltf"); const clone = Cesium.clone; -const coloredMaterialPath = 'specs/data/box/box.mtl'; -const texturedMaterialPath = 'specs/data/box-complex-material/box-complex-material.mtl'; -const texturedWithOptionsMaterialPath = 'specs/data/box-texture-options/box-texture-options.mtl'; -const multipleMaterialsPath = 'specs/data/box-multiple-materials/box-multiple-materials.mtl'; -const externalMaterialPath = 'specs/data/box-external-resources/box-external-resources.mtl'; -const resourcesInRootMaterialPath = 'specs/data/box-resources-in-root/box-resources-in-root.mtl'; -const externalInRootMaterialPath = 'specs/data/box-external-resources-in-root/box-external-resources-in-root.mtl'; -const transparentMaterialPath = 'specs/data/box-transparent/box-transparent.mtl'; -const sharedTexturesMaterialPath = 'specs/data/box-shared-textures/box-shared-textures.mtl'; -const sharedTexturesMaterial2Path = 'specs/data/box-shared-textures-2/box-shared-textures-2.mtl'; +const coloredMaterialPath = "specs/data/box/box.mtl"; +const texturedMaterialPath = + "specs/data/box-complex-material/box-complex-material.mtl"; +const texturedWithOptionsMaterialPath = + "specs/data/box-texture-options/box-texture-options.mtl"; +const multipleMaterialsPath = + "specs/data/box-multiple-materials/box-multiple-materials.mtl"; +const externalMaterialPath = + "specs/data/box-external-resources/box-external-resources.mtl"; +const resourcesInRootMaterialPath = + "specs/data/box-resources-in-root/box-resources-in-root.mtl"; +const externalInRootMaterialPath = + "specs/data/box-external-resources-in-root/box-external-resources-in-root.mtl"; +const transparentMaterialPath = + "specs/data/box-transparent/box-transparent.mtl"; +const sharedTexturesMaterialPath = + "specs/data/box-shared-textures/box-shared-textures.mtl"; +const sharedTexturesMaterial2Path = + "specs/data/box-shared-textures-2/box-shared-textures-2.mtl"; -const diffuseTexturePath = 'specs/data/box-textured/cesium.png'; -const transparentDiffuseTexturePath = 'specs/data/box-complex-material/diffuse.png'; -const alphaTexturePath = 'specs/data/box-complex-material-alpha/alpha.png'; -const ambientTexturePath = 'specs/data/box-complex-material/ambient.gif'; -const normalTexturePath = 'specs/data/box-complex-material/bump.png'; -const emissiveTexturePath = 'specs/data/box-complex-material/emission.jpg'; -const specularTexturePath = 'specs/data/box-complex-material/specular.jpeg'; -const specularShininessTexturePath = 'specs/data/box-complex-material/shininess.png'; +const diffuseTexturePath = "specs/data/box-textured/cesium.png"; +const transparentDiffuseTexturePath = + "specs/data/box-complex-material/diffuse.png"; +const alphaTexturePath = "specs/data/box-complex-material-alpha/alpha.png"; +const ambientTexturePath = "specs/data/box-complex-material/ambient.gif"; +const normalTexturePath = "specs/data/box-complex-material/bump.png"; +const emissiveTexturePath = "specs/data/box-complex-material/emission.jpg"; +const specularTexturePath = "specs/data/box-complex-material/specular.jpeg"; +const specularShininessTexturePath = + "specs/data/box-complex-material/shininess.png"; let diffuseTexture; let transparentDiffuseTexture; @@ -37,434 +48,487 @@ let specularTexture; let specularShininessTexture; const checkTransparencyOptions = { - checkTransparency : true + checkTransparency: true, }; const decodeOptions = { - decode : true + decode: true, }; let options; -describe('loadMtl', () => { - beforeAll(async () => { - diffuseTexture = await loadTexture(diffuseTexturePath, decodeOptions); - transparentDiffuseTexture = await loadTexture(transparentDiffuseTexturePath, checkTransparencyOptions); - alphaTexture = await loadTexture(alphaTexturePath, decodeOptions); - ambientTexture = await loadTexture(ambientTexturePath); - normalTexture = await loadTexture(normalTexturePath); - emissiveTexture = await loadTexture(emissiveTexturePath); - specularTexture = await loadTexture(specularTexturePath, decodeOptions); - specularShininessTexture = await loadTexture(specularShininessTexturePath, decodeOptions); +describe("loadMtl", () => { + beforeAll(async () => { + diffuseTexture = await loadTexture(diffuseTexturePath, decodeOptions); + transparentDiffuseTexture = await loadTexture( + transparentDiffuseTexturePath, + checkTransparencyOptions + ); + alphaTexture = await loadTexture(alphaTexturePath, decodeOptions); + ambientTexture = await loadTexture(ambientTexturePath); + normalTexture = await loadTexture(normalTexturePath); + emissiveTexture = await loadTexture(emissiveTexturePath); + specularTexture = await loadTexture(specularTexturePath, decodeOptions); + specularShininessTexture = await loadTexture( + specularShininessTexturePath, + decodeOptions + ); + }); + + beforeEach(() => { + options = clone(obj2gltf.defaults); + options.overridingTextures = {}; + options.logger = () => {}; + }); + + it("loads mtl", async () => { + options.metallicRoughness = true; + const materials = await loadMtl(coloredMaterialPath, options); + expect(materials.length).toBe(1); + const material = materials[0]; + const pbr = material.pbrMetallicRoughness; + expect(pbr.baseColorTexture).toBeUndefined(); + expect(pbr.metallicRoughnessTexture).toBeUndefined(); + expect(pbr.baseColorFactor).toEqual([0.64, 0.64, 0.64, 1.0]); + expect(pbr.metallicFactor).toBe(0.5); + expect(pbr.roughnessFactor).toBe(96.078431); + expect(material.name).toBe("Material"); + expect(material.emissiveTexture).toBeUndefined(); + expect(material.normalTexture).toBeUndefined(); + expect(material.ambientTexture).toBeUndefined(); + expect(material.emissiveFactor).toEqual([0.0, 0.0, 0.1]); + expect(material.alphaMode).toBe("OPAQUE"); + expect(material.doubleSided).toBe(false); + }); + + it("loads mtl with textures", async () => { + options.metallicRoughness = true; + const materials = await loadMtl(texturedMaterialPath, options); + expect(materials.length).toBe(1); + const material = materials[0]; + const pbr = material.pbrMetallicRoughness; + expect(pbr.baseColorTexture).toBeDefined(); + expect(pbr.metallicRoughnessTexture).toBeDefined(); + expect(pbr.baseColorFactor).toEqual([1.0, 1.0, 1.0, 0.9]); + expect(pbr.metallicFactor).toBe(1.0); + expect(pbr.roughnessFactor).toBe(1.0); + expect(material.name).toBe("Material"); + expect(material.emissiveTexture).toBeDefined(); + expect(material.normalTexture).toBeDefined(); + expect(material.occlusionTexture).toBeDefined(); + expect(material.emissiveFactor).toEqual([1.0, 1.0, 1.0]); + expect(material.alphaMode).toBe("BLEND"); + expect(material.doubleSided).toBe(true); + }); + + it("loads mtl with textures having options", async () => { + options.metallicRoughness = true; + const materials = await loadMtl(texturedWithOptionsMaterialPath, options); + expect(materials.length).toBe(1); + const material = materials[0]; + const pbr = material.pbrMetallicRoughness; + expect(pbr.baseColorTexture).toBeDefined(); + expect(pbr.metallicRoughnessTexture).toBeDefined(); + expect(pbr.baseColorFactor).toEqual([1.0, 1.0, 1.0, 0.9]); + expect(pbr.metallicFactor).toBe(1.0); + expect(pbr.roughnessFactor).toBe(1.0); + expect(material.name).toBe("Material"); + expect(material.emissiveTexture).toBeDefined(); + expect(material.normalTexture).toBeDefined(); + expect(material.occlusionTexture).toBeDefined(); + expect(material.emissiveFactor).toEqual([1.0, 1.0, 1.0]); + expect(material.alphaMode).toBe("BLEND"); + expect(material.doubleSided).toBe(true); + }); + + it("loads mtl with multiple materials", async () => { + options.metallicRoughness = true; + const materials = await loadMtl(multipleMaterialsPath, options); + expect(materials.length).toBe(3); + expect(materials[0].name).toBe("Blue"); + expect(materials[0].pbrMetallicRoughness.baseColorFactor).toEqual([ + 0.0, 0.0, 0.64, 1.0, + ]); + expect(materials[1].name).toBe("Green"); + expect(materials[1].pbrMetallicRoughness.baseColorFactor).toEqual([ + 0.0, 0.64, 0.0, 1.0, + ]); + expect(materials[2].name).toBe("Red"); + expect(materials[2].pbrMetallicRoughness.baseColorFactor).toEqual([ + 0.64, 0.0, 0.0, 1.0, + ]); + }); + + it("sets overriding textures", async () => { + spyOn(fsExtra, "readFile").and.callThrough(); + options.overridingTextures = { + metallicRoughnessOcclusionTexture: alphaTexturePath, + baseColorTexture: alphaTexturePath, + emissiveTexture: emissiveTexturePath, + }; + const materials = await loadMtl(texturedMaterialPath, options); + const material = materials[0]; + const pbr = material.pbrMetallicRoughness; + expect(pbr.baseColorTexture.name).toBe("alpha"); + expect(pbr.metallicRoughnessTexture.name).toBe("alpha"); + expect(material.emissiveTexture.name).toBe("emission"); + expect(material.normalTexture.name).toBe("bump"); + expect(fsExtra.readFile.calls.count()).toBe(3); + }); + + it("loads texture outside of the mtl directory", async () => { + const materials = await loadMtl(externalMaterialPath, options); + const material = materials[0]; + const baseColorTexture = material.pbrMetallicRoughness.baseColorTexture; + expect(baseColorTexture.source).toBeDefined(); + expect(baseColorTexture.name).toBe("cesium"); + }); + + it("does not load texture outside of the mtl directory when secure is true", async () => { + const spy = jasmine.createSpy("logger"); + options.logger = spy; + options.secure = true; + + const materials = await loadMtl(externalMaterialPath, options); + const material = materials[0]; + const baseColorTexture = material.pbrMetallicRoughness.baseColorTexture; + expect(baseColorTexture).toBeUndefined(); + expect( + spy.calls + .argsFor(0)[0] + .indexOf( + "Texture file is outside of the mtl directory and the secure flag is true. Attempting to read the texture file from within the obj directory instead" + ) >= 0 + ).toBe(true); + expect(spy.calls.argsFor(1)[0].indexOf("ENOENT") >= 0).toBe(true); + expect( + spy.calls.argsFor(2)[0].indexOf("Could not read texture file") >= 0 + ).toBe(true); + }); + + it("loads textures from root directory when the texture paths do not exist", async () => { + const materials = await loadMtl(resourcesInRootMaterialPath, options); + const material = materials[0]; + const baseColorTexture = material.pbrMetallicRoughness.baseColorTexture; + expect(baseColorTexture.source).toBeDefined(); + expect(baseColorTexture.name).toBe("cesium"); + }); + + it("loads textures from root directory when texture is outside of the mtl directory and secure is true", async () => { + options.secure = true; + + const materials = await loadMtl(externalInRootMaterialPath, options); + const material = materials[0]; + const baseColorTexture = material.pbrMetallicRoughness.baseColorTexture; + expect(baseColorTexture.source).toBeDefined(); + expect(baseColorTexture.name).toBe("cesium"); + }); + + it("alpha of 0.0 is treated as 1.0", async () => { + const materials = await loadMtl(transparentMaterialPath, options); + expect(materials.length).toBe(1); + const material = materials[0]; + const pbr = material.pbrMetallicRoughness; + expect(pbr.baseColorTexture).toBeUndefined(); + expect(pbr.metallicRoughnessTexture).toBeUndefined(); + expect(pbr.baseColorFactor[3]).toEqual(1.0); + expect(material.alphaMode).toBe("OPAQUE"); + expect(material.doubleSided).toBe(false); + }); + + it("ambient texture is ignored if it is the same as the diffuse texture", async () => { + const materials = await loadMtl(sharedTexturesMaterialPath, options); + expect(materials.length).toBe(1); + const material = materials[0]; + const pbr = material.pbrMetallicRoughness; + expect(pbr.baseColorTexture).toBeDefined(); + expect(pbr.occlusionTexture).toBeUndefined(); + }); + + it("texture referenced by specular is decoded", async () => { + const materials = await loadMtl(sharedTexturesMaterialPath, options); + expect(materials.length).toBe(1); + const material = materials[0]; + const pbr = material.pbrMetallicRoughness; + expect(pbr.baseColorTexture.pixels).toBeDefined(); + expect(pbr.baseColorTexture.source).toBeDefined(); + expect(pbr.metallicRoughnessTexture.pixels).toBeDefined(); + expect(pbr.metallicRoughnessTexture.source).toBeUndefined(); + }); + + it("texture referenced by diffuse and emissive is not decoded", async () => { + const materials = await loadMtl(sharedTexturesMaterial2Path, options); + expect(materials.length).toBe(1); + const material = materials[0]; + const pbr = material.pbrMetallicRoughness; + expect(pbr.baseColorTexture).toBe(material.emissiveTexture); + expect(pbr.baseColorTexture.pixels).toBeUndefined(); + expect(pbr.baseColorTexture.source).toBeDefined(); + }); + + describe("metallicRoughness", () => { + it("creates default material", () => { + const material = loadMtl._createMaterial(undefined, options); + const pbr = material.pbrMetallicRoughness; + expect(pbr.baseColorTexture).toBeUndefined(); + expect(pbr.metallicRoughnessTexture).toBeUndefined(); + expect(pbr.baseColorFactor).toEqual([0.5, 0.5, 0.5, 1.0]); + expect(pbr.metallicFactor).toBe(0.0); // No metallic + expect(pbr.roughnessFactor).toBe(1.0); // Fully rough + expect(material.emissiveTexture).toBeUndefined(); + expect(material.normalTexture).toBeUndefined(); + expect(material.ambientTexture).toBeUndefined(); + expect(material.emissiveFactor).toEqual([0.0, 0.0, 0.0]); + expect(material.alphaMode).toBe("OPAQUE"); + expect(material.doubleSided).toBe(false); }); - beforeEach(() => { - options = clone(obj2gltf.defaults); - options.overridingTextures = {}; - options.logger = () => {}; + it("creates material with textures", () => { + options.metallicRoughness = true; + + const material = loadMtl._createMaterial( + { + diffuseTexture: diffuseTexture, + ambientTexture: ambientTexture, + normalTexture: normalTexture, + emissiveTexture: emissiveTexture, + specularTexture: specularTexture, + specularShininessTexture: specularShininessTexture, + }, + options + ); + + const pbr = material.pbrMetallicRoughness; + expect(pbr.baseColorTexture).toBeDefined(); + expect(pbr.metallicRoughnessTexture).toBeDefined(); + expect(pbr.baseColorFactor).toEqual([1.0, 1.0, 1.0, 1.0]); + expect(pbr.metallicFactor).toBe(1.0); + expect(pbr.roughnessFactor).toBe(1.0); + expect(material.emissiveTexture).toBeDefined(); + expect(material.normalTexture).toBeDefined(); + expect(material.occlusionTexture).toBeDefined(); + expect(material.emissiveFactor).toEqual([1.0, 1.0, 1.0]); + expect(material.alphaMode).toBe("OPAQUE"); + expect(material.doubleSided).toBe(false); }); - it('loads mtl', async () => { - options.metallicRoughness = true; - const materials = await loadMtl(coloredMaterialPath, options); - expect(materials.length).toBe(1); - const material = materials[0]; - const pbr = material.pbrMetallicRoughness; - expect(pbr.baseColorTexture).toBeUndefined(); - expect(pbr.metallicRoughnessTexture).toBeUndefined(); - expect(pbr.baseColorFactor).toEqual([0.64, 0.64, 0.64, 1.0]); - expect(pbr.metallicFactor).toBe(0.5); - expect(pbr.roughnessFactor).toBe(96.078431); - expect(material.name).toBe('Material'); - expect(material.emissiveTexture).toBeUndefined(); - expect(material.normalTexture).toBeUndefined(); - expect(material.ambientTexture).toBeUndefined(); - expect(material.emissiveFactor).toEqual([0.0, 0.0, 0.1]); - expect(material.alphaMode).toBe('OPAQUE'); - expect(material.doubleSided).toBe(false); + it("packs occlusion in metallic roughness texture", () => { + options.metallicRoughness = true; + options.packOcclusion = true; + + const material = loadMtl._createMaterial( + { + ambientTexture: alphaTexture, + specularTexture: specularTexture, + specularShininessTexture: specularShininessTexture, + }, + options + ); + + const pbr = material.pbrMetallicRoughness; + expect(pbr.metallicRoughnessTexture).toBeDefined(); + expect(pbr.metallicRoughnessTexture).toBe(material.occlusionTexture); }); - it('loads mtl with textures', async () => { - options.metallicRoughness = true; - const materials = await loadMtl(texturedMaterialPath, options); - expect(materials.length).toBe(1); - const material = materials[0]; - const pbr = material.pbrMetallicRoughness; - expect(pbr.baseColorTexture).toBeDefined(); - expect(pbr.metallicRoughnessTexture).toBeDefined(); - expect(pbr.baseColorFactor).toEqual([1.0, 1.0, 1.0, 0.9]); - expect(pbr.metallicFactor).toBe(1.0); - expect(pbr.roughnessFactor).toBe(1.0); - expect(material.name).toBe('Material'); - expect(material.emissiveTexture).toBeDefined(); - expect(material.normalTexture).toBeDefined(); - expect(material.occlusionTexture).toBeDefined(); - expect(material.emissiveFactor).toEqual([1.0, 1.0, 1.0]); - expect(material.alphaMode).toBe('BLEND'); - expect(material.doubleSided).toBe(true); + it("does not create metallic roughness texture if decoded texture data is not available", () => { + options.metallicRoughness = true; + options.packOcclusion = true; + + const material = loadMtl._createMaterial( + { + ambientTexture: ambientTexture, // Is a .gif which can't be decoded + specularTexture: specularTexture, + specularShininessTexture: specularShininessTexture, + }, + options + ); + + const pbr = material.pbrMetallicRoughness; + expect(pbr.metallicRoughnessTexture).toBeUndefined(); + expect(material.occlusionTexture).toBeUndefined(); }); - it('loads mtl with textures having options', async () => { - options.metallicRoughness = true; - const materials = await loadMtl(texturedWithOptionsMaterialPath, options); - expect(materials.length).toBe(1); - const material = materials[0]; - const pbr = material.pbrMetallicRoughness; - expect(pbr.baseColorTexture).toBeDefined(); - expect(pbr.metallicRoughnessTexture).toBeDefined(); - expect(pbr.baseColorFactor).toEqual([1.0, 1.0, 1.0, 0.9]); - expect(pbr.metallicFactor).toBe(1.0); - expect(pbr.roughnessFactor).toBe(1.0); - expect(material.name).toBe('Material'); - expect(material.emissiveTexture).toBeDefined(); - expect(material.normalTexture).toBeDefined(); - expect(material.occlusionTexture).toBeDefined(); - expect(material.emissiveFactor).toEqual([1.0, 1.0, 1.0]); - expect(material.alphaMode).toBe('BLEND'); - expect(material.doubleSided).toBe(true); + it("sets material for transparent diffuse texture", () => { + options.metallicRoughness = true; + + const material = loadMtl._createMaterial( + { + diffuseTexture: transparentDiffuseTexture, + }, + options + ); + expect(material.alphaMode).toBe("BLEND"); + expect(material.doubleSided).toBe(true); }); - it('loads mtl with multiple materials', async () => { - options.metallicRoughness = true; - const materials = await loadMtl(multipleMaterialsPath, options); - expect(materials.length).toBe(3); - expect(materials[0].name).toBe('Blue'); - expect(materials[0].pbrMetallicRoughness.baseColorFactor).toEqual([0.0, 0.0, 0.64, 1.0]); - expect(materials[1].name).toBe('Green'); - expect(materials[1].pbrMetallicRoughness.baseColorFactor).toEqual([0.0, 0.64, 0.0, 1.0]); - expect(materials[2].name).toBe('Red'); - expect(materials[2].pbrMetallicRoughness.baseColorFactor).toEqual([0.64, 0.0, 0.0, 1.0]); + it("packs alpha texture in base color texture", () => { + options.metallicRoughness = true; + + const material = loadMtl._createMaterial( + { + diffuseTexture: diffuseTexture, + alphaTexture: alphaTexture, + }, + options + ); + + const pbr = material.pbrMetallicRoughness; + expect(pbr.baseColorTexture).toBeDefined(); + + let hasBlack = false; + let hasWhite = false; + const pixels = pbr.baseColorTexture.pixels; + const pixelsLength = pixels.length / 4; + for (let i = 0; i < pixelsLength; ++i) { + const alpha = pixels[i * 4 + 3]; + hasBlack = hasBlack || alpha === 0; + hasWhite = hasWhite || alpha === 255; + } + expect(hasBlack).toBe(true); + expect(hasWhite).toBe(true); + expect(pbr.baseColorFactor[3]).toEqual(1); + expect(material.alphaMode).toBe("BLEND"); + expect(material.doubleSided).toBe(true); }); - it('sets overriding textures', async () => { - spyOn(fsExtra, 'readFile').and.callThrough(); - options.overridingTextures = { - metallicRoughnessOcclusionTexture : alphaTexturePath, - baseColorTexture : alphaTexturePath, - emissiveTexture : emissiveTexturePath - }; - const materials = await loadMtl(texturedMaterialPath, options); - const material = materials[0]; - const pbr = material.pbrMetallicRoughness; - expect(pbr.baseColorTexture.name).toBe('alpha'); - expect(pbr.metallicRoughnessTexture.name).toBe('alpha'); - expect(material.emissiveTexture.name).toBe('emission'); - expect(material.normalTexture.name).toBe('bump'); - expect(fsExtra.readFile.calls.count()).toBe(3); + it("uses diffuse texture if diffuse and alpha are the same", () => { + options.metallicRoughness = true; + + const material = loadMtl._createMaterial( + { + diffuseTexture: diffuseTexture, + alphaTexture: diffuseTexture, + }, + options + ); + + const pbr = material.pbrMetallicRoughness; + expect(pbr.baseColorTexture).toBe(diffuseTexture); + expect(material.alphaMode).toBe("BLEND"); + expect(material.doubleSided).toBe(true); + }); + }); + + describe("specularGlossiness", () => { + it("creates default material", () => { + options.specularGlossiness = true; + const material = loadMtl._createMaterial(undefined, options); + const pbr = material.extensions.KHR_materials_pbrSpecularGlossiness; + expect(pbr.diffuseTexture).toBeUndefined(); + expect(pbr.specularGlossinessTexture).toBeUndefined(); + expect(pbr.diffuseFactor).toEqual([0.5, 0.5, 0.5, 1.0]); + expect(pbr.specularFactor).toEqual([0.0, 0.0, 0.0]); // No specular color + expect(pbr.glossinessFactor).toEqual(0.0); // Rough surface + expect(material.emissiveTexture).toBeUndefined(); + expect(material.normalTexture).toBeUndefined(); + expect(material.occlusionTexture).toBeUndefined(); + expect(material.emissiveFactor).toEqual([0.0, 0.0, 0.0]); + expect(material.alphaMode).toBe("OPAQUE"); + expect(material.doubleSided).toBe(false); }); - it('loads texture outside of the mtl directory', async () => { - const materials = await loadMtl(externalMaterialPath, options); - const material = materials[0]; - const baseColorTexture = material.pbrMetallicRoughness.baseColorTexture; - expect(baseColorTexture.source).toBeDefined(); - expect(baseColorTexture.name).toBe('cesium'); + it("creates material with textures", () => { + options.specularGlossiness = true; + + const material = loadMtl._createMaterial( + { + diffuseTexture: diffuseTexture, + ambientTexture: ambientTexture, + normalTexture: normalTexture, + emissiveTexture: emissiveTexture, + specularTexture: specularTexture, + specularShininessTexture: specularShininessTexture, + }, + options + ); + + const pbr = material.extensions.KHR_materials_pbrSpecularGlossiness; + expect(pbr.diffuseTexture).toBeDefined(); + expect(pbr.specularGlossinessTexture).toBeDefined(); + expect(pbr.diffuseFactor).toEqual([1.0, 1.0, 1.0, 1.0]); + expect(pbr.specularFactor).toEqual([1.0, 1.0, 1.0]); + expect(pbr.glossinessFactor).toEqual(1.0); + expect(material.emissiveTexture).toBeDefined(); + expect(material.normalTexture).toBeDefined(); + expect(material.occlusionTexture).toBeDefined(); + expect(material.emissiveFactor).toEqual([1.0, 1.0, 1.0]); + expect(material.alphaMode).toBe("OPAQUE"); + expect(material.doubleSided).toBe(false); }); - it('does not load texture outside of the mtl directory when secure is true', async () => { - const spy = jasmine.createSpy('logger'); - options.logger = spy; - options.secure = true; + it("does not create specular glossiness texture if decoded texture data is not available", () => { + options.specularGlossiness = true; - const materials = await loadMtl(externalMaterialPath, options); - const material = materials[0]; - const baseColorTexture = material.pbrMetallicRoughness.baseColorTexture; - expect(baseColorTexture).toBeUndefined(); - expect(spy.calls.argsFor(0)[0].indexOf('Texture file is outside of the mtl directory and the secure flag is true. Attempting to read the texture file from within the obj directory instead') >= 0).toBe(true); - expect(spy.calls.argsFor(1)[0].indexOf('ENOENT') >= 0).toBe(true); - expect(spy.calls.argsFor(2)[0].indexOf('Could not read texture file') >= 0).toBe(true); + const material = loadMtl._createMaterial( + { + specularTexture: ambientTexture, // Is a .gif which can't be decoded + specularShininessTexture: specularShininessTexture, + }, + options + ); + + const pbr = material.extensions.KHR_materials_pbrSpecularGlossiness; + expect(pbr.specularGlossinessTexture).toBeUndefined(); }); - it('loads textures from root directory when the texture paths do not exist', async () => { - const materials = await loadMtl(resourcesInRootMaterialPath, options); - const material = materials[0]; - const baseColorTexture = material.pbrMetallicRoughness.baseColorTexture; - expect(baseColorTexture.source).toBeDefined(); - expect(baseColorTexture.name).toBe('cesium'); + it("sets material for transparent diffuse texture", () => { + options.specularGlossiness = true; + + const material = loadMtl._createMaterial( + { + diffuseTexture: transparentDiffuseTexture, + }, + options + ); + + expect(material.alphaMode).toBe("BLEND"); + expect(material.doubleSided).toBe(true); }); - it('loads textures from root directory when texture is outside of the mtl directory and secure is true', async () => { - options.secure = true; + it("packs alpha texture in diffuse texture", () => { + options.specularGlossiness = true; - const materials = await loadMtl(externalInRootMaterialPath, options); - const material = materials[0]; - const baseColorTexture = material.pbrMetallicRoughness.baseColorTexture; - expect(baseColorTexture.source).toBeDefined(); - expect(baseColorTexture.name).toBe('cesium'); + const material = loadMtl._createMaterial( + { + diffuseTexture: diffuseTexture, + alphaTexture: alphaTexture, + }, + options + ); + + const pbr = material.extensions.KHR_materials_pbrSpecularGlossiness; + expect(pbr.diffuseTexture).toBeDefined(); + + let hasBlack = false; + let hasWhite = false; + const pixels = pbr.diffuseTexture.pixels; + const pixelsLength = pixels.length / 4; + for (let i = 0; i < pixelsLength; ++i) { + const alpha = pixels[i * 4 + 3]; + hasBlack = hasBlack || alpha === 0; + hasWhite = hasWhite || alpha === 255; + } + expect(hasBlack).toBe(true); + expect(hasWhite).toBe(true); + expect(pbr.diffuseFactor[3]).toEqual(1); + expect(material.alphaMode).toBe("BLEND"); + expect(material.doubleSided).toBe(true); }); - it('alpha of 0.0 is treated as 1.0', async () => { - const materials = await loadMtl(transparentMaterialPath, options); - expect(materials.length).toBe(1); - const material = materials[0]; - const pbr = material.pbrMetallicRoughness; - expect(pbr.baseColorTexture).toBeUndefined(); - expect(pbr.metallicRoughnessTexture).toBeUndefined(); - expect(pbr.baseColorFactor[3]).toEqual(1.0); - expect(material.alphaMode).toBe('OPAQUE'); - expect(material.doubleSided).toBe(false); - }); - - it('ambient texture is ignored if it is the same as the diffuse texture', async () => { - const materials = await loadMtl(sharedTexturesMaterialPath, options); - expect(materials.length).toBe(1); - const material = materials[0]; - const pbr = material.pbrMetallicRoughness; - expect(pbr.baseColorTexture).toBeDefined(); - expect(pbr.occlusionTexture).toBeUndefined(); - }); - - it('texture referenced by specular is decoded', async () => { - const materials = await loadMtl(sharedTexturesMaterialPath, options); - expect(materials.length).toBe(1); - const material = materials[0]; - const pbr = material.pbrMetallicRoughness; - expect(pbr.baseColorTexture.pixels).toBeDefined(); - expect(pbr.baseColorTexture.source).toBeDefined(); - expect(pbr.metallicRoughnessTexture.pixels).toBeDefined(); - expect(pbr.metallicRoughnessTexture.source).toBeUndefined(); - }); - - it('texture referenced by diffuse and emissive is not decoded', async () => { - const materials = await loadMtl(sharedTexturesMaterial2Path, options); - expect(materials.length).toBe(1); - const material = materials[0]; - const pbr = material.pbrMetallicRoughness; - expect(pbr.baseColorTexture).toBe(material.emissiveTexture); - expect(pbr.baseColorTexture.pixels).toBeUndefined(); - expect(pbr.baseColorTexture.source).toBeDefined(); - }); - - describe('metallicRoughness', () => { - it('creates default material', () => { - const material = loadMtl._createMaterial(undefined, options); - const pbr = material.pbrMetallicRoughness; - expect(pbr.baseColorTexture).toBeUndefined(); - expect(pbr.metallicRoughnessTexture).toBeUndefined(); - expect(pbr.baseColorFactor).toEqual([0.5, 0.5, 0.5, 1.0]); - expect(pbr.metallicFactor).toBe(0.0); // No metallic - expect(pbr.roughnessFactor).toBe(1.0); // Fully rough - expect(material.emissiveTexture).toBeUndefined(); - expect(material.normalTexture).toBeUndefined(); - expect(material.ambientTexture).toBeUndefined(); - expect(material.emissiveFactor).toEqual([0.0, 0.0, 0.0]); - expect(material.alphaMode).toBe('OPAQUE'); - expect(material.doubleSided).toBe(false); - }); - - it('creates material with textures', () => { - options.metallicRoughness = true; - - const material = loadMtl._createMaterial({ - diffuseTexture : diffuseTexture, - ambientTexture : ambientTexture, - normalTexture : normalTexture, - emissiveTexture : emissiveTexture, - specularTexture : specularTexture, - specularShininessTexture : specularShininessTexture - }, options); - - const pbr = material.pbrMetallicRoughness; - expect(pbr.baseColorTexture).toBeDefined(); - expect(pbr.metallicRoughnessTexture).toBeDefined(); - expect(pbr.baseColorFactor).toEqual([1.0, 1.0, 1.0, 1.0]); - expect(pbr.metallicFactor).toBe(1.0); - expect(pbr.roughnessFactor).toBe(1.0); - expect(material.emissiveTexture).toBeDefined(); - expect(material.normalTexture).toBeDefined(); - expect(material.occlusionTexture).toBeDefined(); - expect(material.emissiveFactor).toEqual([1.0, 1.0, 1.0]); - expect(material.alphaMode).toBe('OPAQUE'); - expect(material.doubleSided).toBe(false); - }); - - it('packs occlusion in metallic roughness texture', () => { - options.metallicRoughness = true; - options.packOcclusion = true; - - const material = loadMtl._createMaterial({ - ambientTexture : alphaTexture, - specularTexture : specularTexture, - specularShininessTexture : specularShininessTexture - }, options); - - const pbr = material.pbrMetallicRoughness; - expect(pbr.metallicRoughnessTexture).toBeDefined(); - expect(pbr.metallicRoughnessTexture).toBe(material.occlusionTexture); - }); - - it('does not create metallic roughness texture if decoded texture data is not available', () => { - options.metallicRoughness = true; - options.packOcclusion = true; - - const material = loadMtl._createMaterial({ - ambientTexture : ambientTexture, // Is a .gif which can't be decoded - specularTexture : specularTexture, - specularShininessTexture : specularShininessTexture - }, options); - - const pbr = material.pbrMetallicRoughness; - expect(pbr.metallicRoughnessTexture).toBeUndefined(); - expect(material.occlusionTexture).toBeUndefined(); - }); - - it('sets material for transparent diffuse texture', () => { - options.metallicRoughness = true; - - const material = loadMtl._createMaterial({ - diffuseTexture : transparentDiffuseTexture - }, options); - expect(material.alphaMode).toBe('BLEND'); - expect(material.doubleSided).toBe(true); - }); - - it('packs alpha texture in base color texture', () => { - options.metallicRoughness = true; - - const material = loadMtl._createMaterial({ - diffuseTexture : diffuseTexture, - alphaTexture : alphaTexture - }, options); - - const pbr = material.pbrMetallicRoughness; - expect(pbr.baseColorTexture).toBeDefined(); - - let hasBlack = false; - let hasWhite = false; - const pixels = pbr.baseColorTexture.pixels; - const pixelsLength = pixels.length / 4; - for (let i = 0; i < pixelsLength; ++i) { - const alpha = pixels[i * 4 + 3]; - hasBlack = hasBlack || (alpha === 0); - hasWhite = hasWhite || (alpha === 255); - } - expect(hasBlack).toBe(true); - expect(hasWhite).toBe(true); - expect(pbr.baseColorFactor[3]).toEqual(1); - expect(material.alphaMode).toBe('BLEND'); - expect(material.doubleSided).toBe(true); - }); - - it('uses diffuse texture if diffuse and alpha are the same', () => { - options.metallicRoughness = true; - - const material = loadMtl._createMaterial({ - diffuseTexture : diffuseTexture, - alphaTexture : diffuseTexture - }, options); - - const pbr = material.pbrMetallicRoughness; - expect(pbr.baseColorTexture).toBe(diffuseTexture); - expect(material.alphaMode).toBe('BLEND'); - expect(material.doubleSided).toBe(true); - }); - }); - - describe('specularGlossiness', () => { - it('creates default material', () => { - options.specularGlossiness = true; - const material = loadMtl._createMaterial(undefined, options); - const pbr = material.extensions.KHR_materials_pbrSpecularGlossiness; - expect(pbr.diffuseTexture).toBeUndefined(); - expect(pbr.specularGlossinessTexture).toBeUndefined(); - expect(pbr.diffuseFactor).toEqual([0.5, 0.5, 0.5, 1.0]); - expect(pbr.specularFactor).toEqual([0.0, 0.0, 0.0]); // No specular color - expect(pbr.glossinessFactor).toEqual(0.0); // Rough surface - expect(material.emissiveTexture).toBeUndefined(); - expect(material.normalTexture).toBeUndefined(); - expect(material.occlusionTexture).toBeUndefined(); - expect(material.emissiveFactor).toEqual([0.0, 0.0, 0.0]); - expect(material.alphaMode).toBe('OPAQUE'); - expect(material.doubleSided).toBe(false); - }); - - it('creates material with textures', () => { - options.specularGlossiness = true; - - const material = loadMtl._createMaterial({ - diffuseTexture : diffuseTexture, - ambientTexture : ambientTexture, - normalTexture : normalTexture, - emissiveTexture : emissiveTexture, - specularTexture : specularTexture, - specularShininessTexture : specularShininessTexture - }, options); - - const pbr = material.extensions.KHR_materials_pbrSpecularGlossiness; - expect(pbr.diffuseTexture).toBeDefined(); - expect(pbr.specularGlossinessTexture).toBeDefined(); - expect(pbr.diffuseFactor).toEqual([1.0, 1.0, 1.0, 1.0]); - expect(pbr.specularFactor).toEqual([1.0, 1.0, 1.0]); - expect(pbr.glossinessFactor).toEqual(1.0); - expect(material.emissiveTexture).toBeDefined(); - expect(material.normalTexture).toBeDefined(); - expect(material.occlusionTexture).toBeDefined(); - expect(material.emissiveFactor).toEqual([1.0, 1.0, 1.0]); - expect(material.alphaMode).toBe('OPAQUE'); - expect(material.doubleSided).toBe(false); - }); - - it('does not create specular glossiness texture if decoded texture data is not available', () => { - options.specularGlossiness = true; - - const material = loadMtl._createMaterial({ - specularTexture : ambientTexture, // Is a .gif which can't be decoded - specularShininessTexture : specularShininessTexture - }, options); - - const pbr = material.extensions.KHR_materials_pbrSpecularGlossiness; - expect(pbr.specularGlossinessTexture).toBeUndefined(); - }); - - it('sets material for transparent diffuse texture', () => { - options.specularGlossiness = true; - - const material = loadMtl._createMaterial({ - diffuseTexture : transparentDiffuseTexture - }, options); - - expect(material.alphaMode).toBe('BLEND'); - expect(material.doubleSided).toBe(true); - }); - - it('packs alpha texture in diffuse texture', () => { - options.specularGlossiness = true; - - const material = loadMtl._createMaterial({ - diffuseTexture : diffuseTexture, - alphaTexture : alphaTexture - }, options); - - const pbr = material.extensions.KHR_materials_pbrSpecularGlossiness; - expect(pbr.diffuseTexture).toBeDefined(); - - let hasBlack = false; - let hasWhite = false; - const pixels = pbr.diffuseTexture.pixels; - const pixelsLength = pixels.length / 4; - for (let i = 0; i < pixelsLength; ++i) { - const alpha = pixels[i * 4 + 3]; - hasBlack = hasBlack || (alpha === 0); - hasWhite = hasWhite || (alpha === 255); - } - expect(hasBlack).toBe(true); - expect(hasWhite).toBe(true); - expect(pbr.diffuseFactor[3]).toEqual(1); - expect(material.alphaMode).toBe('BLEND'); - expect(material.doubleSided).toBe(true); - }); - - it('uses diffuse texture if diffuse and alpha are the same', () => { - options.specularGlossiness = true; - - const material = loadMtl._createMaterial({ - diffuseTexture : diffuseTexture, - alphaTexture : diffuseTexture - }, options); - - const pbr = material.extensions.KHR_materials_pbrSpecularGlossiness; - expect(pbr.diffuseTexture).toEqual(diffuseTexture); - expect(material.alphaMode).toBe('BLEND'); - expect(material.doubleSided).toBe(true); - }); + it("uses diffuse texture if diffuse and alpha are the same", () => { + options.specularGlossiness = true; + + const material = loadMtl._createMaterial( + { + diffuseTexture: diffuseTexture, + alphaTexture: diffuseTexture, + }, + options + ); + + const pbr = material.extensions.KHR_materials_pbrSpecularGlossiness; + expect(pbr.diffuseTexture).toEqual(diffuseTexture); + expect(material.alphaMode).toBe("BLEND"); + expect(material.doubleSided).toBe(true); }); + }); }); diff --git a/specs/lib/loadObjSpec.js b/specs/lib/loadObjSpec.js index 0db5d32..11355e6 100644 --- a/specs/lib/loadObjSpec.js +++ b/specs/lib/loadObjSpec.js @@ -1,587 +1,699 @@ -'use strict'; -const Cesium = require('cesium'); -const path = require('path'); +"use strict"; +const Cesium = require("cesium"); +const path = require("path"); -const loadObj = require('../../lib/loadObj'); -const obj2gltf = require('../../lib/obj2gltf'); +const loadObj = require("../../lib/loadObj"); +const obj2gltf = require("../../lib/obj2gltf"); const Cartesian3 = Cesium.Cartesian3; const CesiumMath = Cesium.Math; const clone = Cesium.clone; const RuntimeError = Cesium.RuntimeError; -const objPath = 'specs/data/box/box.obj'; -const objRotatedUrl = 'specs/data/box-rotated/box-rotated.obj'; -const objNormalsPath = 'specs/data/box-normals/box-normals.obj'; -const objUvsPath = 'specs/data/box-uvs/box-uvs.obj'; -const objPositionsOnlyPath = 'specs/data/box-positions-only/box-positions-only.obj'; -const objNegativeIndicesPath = 'specs/data/box-negative-indices/box-negative-indices.obj'; -const objTrianglesPath = 'specs/data/box-triangles/box-triangles.obj'; -const objObjectsPath = 'specs/data/box-objects/box-objects.obj'; -const objGroupsPath = 'specs/data/box-groups/box-groups.obj'; -const objObjectsGroupsPath = 'specs/data/box-objects-groups/box-objects-groups.obj'; -const objObjectsGroupsMaterialsPath = 'specs/data/box-objects-groups-materials/box-objects-groups-materials.obj'; -const objObjectsGroupsMaterialsPath2 = 'specs/data/box-objects-groups-materials-2/box-objects-groups-materials-2.obj'; -const objUsemtlPath = 'specs/data/box-usemtl/box-usemtl.obj'; -const objNoMaterialsPath = 'specs/data/box-no-materials/box-no-materials.obj'; -const objMultipleMaterialsPath = 'specs/data/box-multiple-materials/box-multiple-materials.obj'; -const objUncleanedPath = 'specs/data/box-uncleaned/box-uncleaned.obj'; -const objMtllibPath = 'specs/data/box-mtllib/box-mtllib.obj'; -const objMtllibSpacesPath = 'specs/data/box-mtllib-spaces/box mtllib.obj'; -const objMissingMtllibPath = 'specs/data/box-missing-mtllib/box-missing-mtllib.obj'; -const objMissingUsemtlPath = 'specs/data/box-missing-usemtl/box-missing-usemtl.obj'; -const objUnnamedMaterialPath = 'specs/data/box-unnamed-material/box-unnamed-material.obj'; -const objExternalResourcesPath = 'specs/data/box-external-resources/box-external-resources.obj'; -const objResourcesInRootPath = 'specs/data/box-resources-in-root/box-resources-in-root.obj'; -const objExternalResourcesInRootPath = 'specs/data/box-external-resources-in-root/box-external-resources-in-root.obj'; -const objTexturedPath = 'specs/data/box-textured/box-textured.obj'; -const objMissingTexturePath = 'specs/data/box-missing-texture/box-missing-texture.obj'; -const objSubdirectoriesPath = 'specs/data/box-subdirectories/box-textured.obj'; -const objWindowsPaths = 'specs/data/box-windows-paths/box-windows-paths.obj'; -const objInvalidContentsPath = 'specs/data/box/box.mtl'; -const objConcavePath = 'specs/data/concave/concave.obj'; -const objUnnormalizedPath = 'specs/data/box-unnormalized/box-unnormalized.obj'; -const objMixedAttributesPath = 'specs/data/box-mixed-attributes/box-mixed-attributes.obj'; -const objMissingAttributesPath = 'specs/data/box-missing-attributes/box-missing-attributes.obj'; -const objIncompletePositionsPath = 'specs/data/box-incomplete-attributes/box-incomplete-positions.obj'; -const objIncompleteNormalsPath = 'specs/data/box-incomplete-attributes/box-incomplete-normals.obj'; -const objIncompleteUvsPath = 'specs/data/box-incomplete-attributes/box-incomplete-uvs.obj'; -const objIncorrectWindingOrderPath = 'specs/data/box-incorrect-winding-order/box-incorrect-winding-order.obj'; -const objInvalidPath = 'invalid.obj'; +const objPath = "specs/data/box/box.obj"; +const objRotatedUrl = "specs/data/box-rotated/box-rotated.obj"; +const objNormalsPath = "specs/data/box-normals/box-normals.obj"; +const objUvsPath = "specs/data/box-uvs/box-uvs.obj"; +const objPositionsOnlyPath = + "specs/data/box-positions-only/box-positions-only.obj"; +const objNegativeIndicesPath = + "specs/data/box-negative-indices/box-negative-indices.obj"; +const objTrianglesPath = "specs/data/box-triangles/box-triangles.obj"; +const objObjectsPath = "specs/data/box-objects/box-objects.obj"; +const objGroupsPath = "specs/data/box-groups/box-groups.obj"; +const objObjectsGroupsPath = + "specs/data/box-objects-groups/box-objects-groups.obj"; +const objObjectsGroupsMaterialsPath = + "specs/data/box-objects-groups-materials/box-objects-groups-materials.obj"; +const objObjectsGroupsMaterialsPath2 = + "specs/data/box-objects-groups-materials-2/box-objects-groups-materials-2.obj"; +const objUsemtlPath = "specs/data/box-usemtl/box-usemtl.obj"; +const objNoMaterialsPath = "specs/data/box-no-materials/box-no-materials.obj"; +const objMultipleMaterialsPath = + "specs/data/box-multiple-materials/box-multiple-materials.obj"; +const objUncleanedPath = "specs/data/box-uncleaned/box-uncleaned.obj"; +const objMtllibPath = "specs/data/box-mtllib/box-mtllib.obj"; +const objMtllibSpacesPath = "specs/data/box-mtllib-spaces/box mtllib.obj"; +const objMissingMtllibPath = + "specs/data/box-missing-mtllib/box-missing-mtllib.obj"; +const objMissingUsemtlPath = + "specs/data/box-missing-usemtl/box-missing-usemtl.obj"; +const objUnnamedMaterialPath = + "specs/data/box-unnamed-material/box-unnamed-material.obj"; +const objExternalResourcesPath = + "specs/data/box-external-resources/box-external-resources.obj"; +const objResourcesInRootPath = + "specs/data/box-resources-in-root/box-resources-in-root.obj"; +const objExternalResourcesInRootPath = + "specs/data/box-external-resources-in-root/box-external-resources-in-root.obj"; +const objTexturedPath = "specs/data/box-textured/box-textured.obj"; +const objMissingTexturePath = + "specs/data/box-missing-texture/box-missing-texture.obj"; +const objSubdirectoriesPath = "specs/data/box-subdirectories/box-textured.obj"; +const objWindowsPaths = "specs/data/box-windows-paths/box-windows-paths.obj"; +const objInvalidContentsPath = "specs/data/box/box.mtl"; +const objConcavePath = "specs/data/concave/concave.obj"; +const objUnnormalizedPath = "specs/data/box-unnormalized/box-unnormalized.obj"; +const objMixedAttributesPath = + "specs/data/box-mixed-attributes/box-mixed-attributes.obj"; +const objMissingAttributesPath = + "specs/data/box-missing-attributes/box-missing-attributes.obj"; +const objIncompletePositionsPath = + "specs/data/box-incomplete-attributes/box-incomplete-positions.obj"; +const objIncompleteNormalsPath = + "specs/data/box-incomplete-attributes/box-incomplete-normals.obj"; +const objIncompleteUvsPath = + "specs/data/box-incomplete-attributes/box-incomplete-uvs.obj"; +const objIncorrectWindingOrderPath = + "specs/data/box-incorrect-winding-order/box-incorrect-winding-order.obj"; +const objInvalidPath = "invalid.obj"; function getMeshes(data) { - let meshes = []; - const nodes = data.nodes; - const nodesLength = nodes.length; - for (let i = 0; i < nodesLength; ++i) { - meshes = meshes.concat(nodes[i].meshes); - } - return meshes; + let meshes = []; + const nodes = data.nodes; + const nodesLength = nodes.length; + for (let i = 0; i < nodesLength; ++i) { + meshes = meshes.concat(nodes[i].meshes); + } + return meshes; } function getPrimitives(data) { - let primitives = []; - const nodes = data.nodes; - const nodesLength = nodes.length; - for (let i = 0; i < nodesLength; ++i) { - const meshes = nodes[i].meshes; - const meshesLength = meshes.length; - for (let j = 0; j < meshesLength; ++j) { - primitives = primitives.concat(meshes[j].primitives); - } + let primitives = []; + const nodes = data.nodes; + const nodesLength = nodes.length; + for (let i = 0; i < nodesLength; ++i) { + const meshes = nodes[i].meshes; + const meshesLength = meshes.length; + for (let j = 0; j < meshesLength; ++j) { + primitives = primitives.concat(meshes[j].primitives); } - return primitives; + } + return primitives; } let options; -describe('loadObj', () => { - beforeEach(() => { - options = clone(obj2gltf.defaults); - options.overridingTextures = {}; - options.logger = () => {}; - }); +describe("loadObj", () => { + beforeEach(() => { + options = clone(obj2gltf.defaults); + options.overridingTextures = {}; + options.logger = () => {}; + }); - it('loads obj with positions, normals, and uvs', async () => { - const data = await loadObj(objPath, options); - const materials = data.materials; - const nodes = data.nodes; - const name = data.name; - const meshes = getMeshes(data); - const primitives = getPrimitives(data); + it("loads obj with positions, normals, and uvs", async () => { + const data = await loadObj(objPath, options); + const materials = data.materials; + const nodes = data.nodes; + const name = data.name; + const meshes = getMeshes(data); + const primitives = getPrimitives(data); - expect(name).toBe('box'); - expect(materials.length).toBe(1); - expect(nodes.length).toBe(1); - expect(meshes.length).toBe(1); - expect(primitives.length).toBe(1); + expect(name).toBe("box"); + expect(materials.length).toBe(1); + expect(nodes.length).toBe(1); + expect(meshes.length).toBe(1); + expect(primitives.length).toBe(1); - const node = nodes[0]; - const mesh = meshes[0]; - const primitive = primitives[0]; + const node = nodes[0]; + const mesh = meshes[0]; + const primitive = primitives[0]; - expect(node.name).toBe('Cube'); - expect(mesh.name).toBe('Cube-Mesh'); - expect(primitive.positions.length / 3).toBe(24); - expect(primitive.normals.length / 3).toBe(24); - expect(primitive.uvs.length / 2).toBe(24); - expect(primitive.indices.length).toBe(36); - expect(primitive.material).toBe('Material'); - }); + expect(node.name).toBe("Cube"); + expect(mesh.name).toBe("Cube-Mesh"); + expect(primitive.positions.length / 3).toBe(24); + expect(primitive.normals.length / 3).toBe(24); + expect(primitive.uvs.length / 2).toBe(24); + expect(primitive.indices.length).toBe(36); + expect(primitive.material).toBe("Material"); + }); - it('loads obj with normals', async () => { - const data = await loadObj(objNormalsPath, options); - const primitive = getPrimitives(data)[0]; - expect(primitive.positions.length / 3).toBe(24); - expect(primitive.normals.length / 3).toBe(24); - expect(primitive.uvs.length / 2).toBe(0); - }); + it("loads obj with normals", async () => { + const data = await loadObj(objNormalsPath, options); + const primitive = getPrimitives(data)[0]; + expect(primitive.positions.length / 3).toBe(24); + expect(primitive.normals.length / 3).toBe(24); + expect(primitive.uvs.length / 2).toBe(0); + }); - it('normalizes normals', async () => { - const data = await loadObj(objUnnormalizedPath, options); - const scratchNormal = new Cesium.Cartesian3(); - const primitive = getPrimitives(data)[0]; - const normals = primitive.normals; - const normalsLength = normals.length / 3; - for (let i = 0; i < normalsLength; ++i) { - const normalX = normals.get(i * 3); - const normalY = normals.get(i * 3 + 1); - const normalZ = normals.get(i * 3 + 2); - const normal = Cartesian3.fromElements(normalX, normalY, normalZ, scratchNormal); - expect(CesiumMath.equalsEpsilon(Cartesian3.magnitude(normal), 1.0, CesiumMath.EPSILON5)).toBe(true); - } - }); - - it('loads obj with uvs', async () => { - const data = await loadObj(objUvsPath, options); - const primitive = getPrimitives(data)[0]; - expect(primitive.positions.length / 3).toBe(20); - expect(primitive.normals.length / 3).toBe(0); - expect(primitive.uvs.length / 2).toBe(20); - }); - - it('loads obj with negative indices', async () => { - const results = [ - await loadObj(objPositionsOnlyPath, options), - await loadObj(objNegativeIndicesPath, options) - ]; - const positionsReference = getPrimitives(results[0])[0].positions.toFloatBuffer(); - const positions = getPrimitives(results[1])[0].positions.toFloatBuffer(); - expect(positions).toEqual(positionsReference); - }); - - it('loads obj with triangle faces', async () => { - const data = await loadObj(objTrianglesPath, options); - const primitive = getPrimitives(data)[0]; - expect(primitive.positions.length / 3).toBe(24); - expect(primitive.indices.length).toBe(36); - }); - - it('loads obj with objects', async () => { - const data = await loadObj(objObjectsPath, options); - const nodes = data.nodes; - expect(nodes.length).toBe(3); - expect(nodes[0].name).toBe('CubeBlue'); - expect(nodes[1].name).toBe('CubeGreen'); - expect(nodes[2].name).toBe('CubeRed'); - - const primitives = getPrimitives(data); - expect(primitives.length).toBe(3); - expect(primitives[0].material).toBe('Blue'); - expect(primitives[1].material).toBe('Green'); - expect(primitives[2].material).toBe('Red'); - }); - - it('loads obj with groups', async () => { - const data = await loadObj(objGroupsPath, options); - const nodes = data.nodes; - expect(nodes.length).toBe(3); - expect(nodes[0].name).toBe('CubeBlue'); - expect(nodes[1].name).toBe('CubeGreen'); - expect(nodes[2].name).toBe('CubeRed'); - - const primitives = getPrimitives(data); - expect(primitives.length).toBe(3); - expect(primitives[0].material).toBe('Blue'); - expect(primitives[1].material).toBe('Green'); - expect(primitives[2].material).toBe('Red'); - }); - - it('loads obj with objects and groups', async () => { - const data = await loadObj(objObjectsGroupsPath, options); - const nodes = data.nodes; - expect(nodes.length).toBe(3); - expect(nodes[0].name).toBe('CubeBlue'); - expect(nodes[1].name).toBe('CubeGreen'); - expect(nodes[2].name).toBe('CubeRed'); - - const meshes = getMeshes(data); - expect(meshes.length).toBe(3); - expect(meshes[0].name).toBe('CubeBlue_CubeBlue_Blue'); - expect(meshes[1].name).toBe('CubeGreen_CubeGreen_Green'); - expect(meshes[2].name).toBe('CubeRed_CubeRed_Red'); - - const primitives = getPrimitives(data); - expect(primitives.length).toBe(3); - expect(primitives[0].material).toBe('Blue'); - expect(primitives[1].material).toBe('Green'); - expect(primitives[2].material).toBe('Red'); - }); - - function loadsObjWithObjectsGroupsAndMaterials(data) { - const nodes = data.nodes; - expect(nodes.length).toBe(1); - expect(nodes[0].name).toBe('Cube'); - const meshes = getMeshes(data); - expect(meshes.length).toBe(3); - expect(meshes[0].name).toBe('Blue'); - expect(meshes[1].name).toBe('Green'); - expect(meshes[2].name).toBe('Red'); - const primitives = getPrimitives(data); - expect(primitives.length).toBe(6); - expect(primitives[0].material).toBe('Blue'); - expect(primitives[1].material).toBe('Green'); - expect(primitives[2].material).toBe('Green'); - expect(primitives[3].material).toBe('Red'); - expect(primitives[4].material).toBe('Red'); - expect(primitives[5].material).toBe('Blue'); + it("normalizes normals", async () => { + const data = await loadObj(objUnnormalizedPath, options); + const scratchNormal = new Cesium.Cartesian3(); + const primitive = getPrimitives(data)[0]; + const normals = primitive.normals; + const normalsLength = normals.length / 3; + for (let i = 0; i < normalsLength; ++i) { + const normalX = normals.get(i * 3); + const normalY = normals.get(i * 3 + 1); + const normalZ = normals.get(i * 3 + 2); + const normal = Cartesian3.fromElements( + normalX, + normalY, + normalZ, + scratchNormal + ); + expect( + CesiumMath.equalsEpsilon( + Cartesian3.magnitude(normal), + 1.0, + CesiumMath.EPSILON5 + ) + ).toBe(true); } + }); - it('loads obj with objects, groups, and materials', async () => { - const data = await loadObj(objObjectsGroupsMaterialsPath, options); - loadsObjWithObjectsGroupsAndMaterials(data); - }); + it("loads obj with uvs", async () => { + const data = await loadObj(objUvsPath, options); + const primitive = getPrimitives(data)[0]; + expect(primitive.positions.length / 3).toBe(20); + expect(primitive.normals.length / 3).toBe(0); + expect(primitive.uvs.length / 2).toBe(20); + }); - it('loads obj with objects, groups, and materials (2)', async () => { - // The usemtl lines are placed in an unordered fashion but - // should produce the same result as the previous test - const data = await loadObj(objObjectsGroupsMaterialsPath2, options); - loadsObjWithObjectsGroupsAndMaterials(data); - }); + it("loads obj with negative indices", async () => { + const results = [ + await loadObj(objPositionsOnlyPath, options), + await loadObj(objNegativeIndicesPath, options), + ]; + const positionsReference = getPrimitives( + results[0] + )[0].positions.toFloatBuffer(); + const positions = getPrimitives(results[1])[0].positions.toFloatBuffer(); + expect(positions).toEqual(positionsReference); + }); - it('loads obj with concave face containing 5 vertices', async () => { - const data = await loadObj(objConcavePath, options); - const primitive = getPrimitives(data)[0]; - expect(primitive.positions.length / 3).toBe(30); - expect(primitive.indices.length).toBe(48); - }); + it("loads obj with triangle faces", async () => { + const data = await loadObj(objTrianglesPath, options); + const primitive = getPrimitives(data)[0]; + expect(primitive.positions.length / 3).toBe(24); + expect(primitive.indices.length).toBe(36); + }); - it('loads obj with usemtl only', async () => { - const data = await loadObj(objUsemtlPath, options); - const nodes = data.nodes; - expect(nodes.length).toBe(1); - expect(nodes[0].name).toBe('Node'); // default name + it("loads obj with objects", async () => { + const data = await loadObj(objObjectsPath, options); + const nodes = data.nodes; + expect(nodes.length).toBe(3); + expect(nodes[0].name).toBe("CubeBlue"); + expect(nodes[1].name).toBe("CubeGreen"); + expect(nodes[2].name).toBe("CubeRed"); - const meshes = getMeshes(data); - expect(meshes.length).toBe(1); - expect(meshes[0].name).toBe('Node-Mesh'); + const primitives = getPrimitives(data); + expect(primitives.length).toBe(3); + expect(primitives[0].material).toBe("Blue"); + expect(primitives[1].material).toBe("Green"); + expect(primitives[2].material).toBe("Red"); + }); - const primitives = getPrimitives(data); - expect(primitives.length).toBe(3); - expect(primitives[0].material).toBe('Blue'); - expect(primitives[1].material).toBe('Green'); - expect(primitives[2].material).toBe('Red'); - }); + it("loads obj with groups", async () => { + const data = await loadObj(objGroupsPath, options); + const nodes = data.nodes; + expect(nodes.length).toBe(3); + expect(nodes[0].name).toBe("CubeBlue"); + expect(nodes[1].name).toBe("CubeGreen"); + expect(nodes[2].name).toBe("CubeRed"); - it('loads obj with no materials', async () => { - const data = await loadObj(objNoMaterialsPath, options); - const nodes = data.nodes; - expect(nodes.length).toBe(1); - expect(nodes[0].name).toBe('Node'); // default name + const primitives = getPrimitives(data); + expect(primitives.length).toBe(3); + expect(primitives[0].material).toBe("Blue"); + expect(primitives[1].material).toBe("Green"); + expect(primitives[2].material).toBe("Red"); + }); - const primitives = getPrimitives(data); - expect(primitives.length).toBe(1); - }); + it("loads obj with objects and groups", async () => { + const data = await loadObj(objObjectsGroupsPath, options); + const nodes = data.nodes; + expect(nodes.length).toBe(3); + expect(nodes[0].name).toBe("CubeBlue"); + expect(nodes[1].name).toBe("CubeGreen"); + expect(nodes[2].name).toBe("CubeRed"); - it('loads obj with multiple materials', async () => { - // The usemtl markers are interleaved, but should condense to just three primitives - const data = await loadObj(objMultipleMaterialsPath, options); - const nodes = data.nodes; - expect(nodes.length).toBe(1); + const meshes = getMeshes(data); + expect(meshes.length).toBe(3); + expect(meshes[0].name).toBe("CubeBlue_CubeBlue_Blue"); + expect(meshes[1].name).toBe("CubeGreen_CubeGreen_Green"); + expect(meshes[2].name).toBe("CubeRed_CubeRed_Red"); - const primitives = getPrimitives(data); - expect(primitives.length).toBe(3); + const primitives = getPrimitives(data); + expect(primitives.length).toBe(3); + expect(primitives[0].material).toBe("Blue"); + expect(primitives[1].material).toBe("Green"); + expect(primitives[2].material).toBe("Red"); + }); - expect(primitives[0].indices.length).toBe(12); - expect(primitives[1].indices.length).toBe(12); - expect(primitives[2].indices.length).toBe(12); - expect(primitives[0].material).toBe('Red'); - expect(primitives[1].material).toBe('Green'); - expect(primitives[2].material).toBe('Blue'); + function loadsObjWithObjectsGroupsAndMaterials(data) { + const nodes = data.nodes; + expect(nodes.length).toBe(1); + expect(nodes[0].name).toBe("Cube"); + const meshes = getMeshes(data); + expect(meshes.length).toBe(3); + expect(meshes[0].name).toBe("Blue"); + expect(meshes[1].name).toBe("Green"); + expect(meshes[2].name).toBe("Red"); + const primitives = getPrimitives(data); + expect(primitives.length).toBe(6); + expect(primitives[0].material).toBe("Blue"); + expect(primitives[1].material).toBe("Green"); + expect(primitives[2].material).toBe("Green"); + expect(primitives[3].material).toBe("Red"); + expect(primitives[4].material).toBe("Red"); + expect(primitives[5].material).toBe("Blue"); + } - for (let i = 0; i < 3; ++i) { - const indices = primitives[i].indices; - for (let j = 0; j < indices.length; ++j) { - expect(indices.get(j)).toBeLessThan(8); - } - } - }); + it("loads obj with objects, groups, and materials", async () => { + const data = await loadObj(objObjectsGroupsMaterialsPath, options); + loadsObjWithObjectsGroupsAndMaterials(data); + }); - it('loads obj uncleaned', async () => { - // Obj with extraneous o, g, and usemtl lines - // Also tests handling of o and g lines with the same names - const data = await loadObj(objUncleanedPath, options); - const nodes = data.nodes; - const meshes = getMeshes(data); - const primitives = getPrimitives(data); + it("loads obj with objects, groups, and materials (2)", async () => { + // The usemtl lines are placed in an unordered fashion but + // should produce the same result as the previous test + const data = await loadObj(objObjectsGroupsMaterialsPath2, options); + loadsObjWithObjectsGroupsAndMaterials(data); + }); - expect(nodes.length).toBe(1); - expect(meshes.length).toBe(1); - expect(primitives.length).toBe(1); + it("loads obj with concave face containing 5 vertices", async () => { + const data = await loadObj(objConcavePath, options); + const primitive = getPrimitives(data)[0]; + expect(primitive.positions.length / 3).toBe(30); + expect(primitive.indices.length).toBe(48); + }); - expect(nodes[0].name).toBe('Cube'); - expect(meshes[0].name).toBe('Cube_1'); - }); + it("loads obj with usemtl only", async () => { + const data = await loadObj(objUsemtlPath, options); + const nodes = data.nodes; + expect(nodes.length).toBe(1); + expect(nodes[0].name).toBe("Node"); // default name - it('loads obj with multiple mtllibs', async () => { - const data = await loadObj(objMtllibPath, options); - const materials = data.materials; - expect(materials.length).toBe(3); + const meshes = getMeshes(data); + expect(meshes.length).toBe(1); + expect(meshes[0].name).toBe("Node-Mesh"); - // .mtl files are loaded in an arbitrary order, so sort for testing purposes - materials.sort((a, b) => { - return a.name.localeCompare(b.name); - }); + const primitives = getPrimitives(data); + expect(primitives.length).toBe(3); + expect(primitives[0].material).toBe("Blue"); + expect(primitives[1].material).toBe("Green"); + expect(primitives[2].material).toBe("Red"); + }); - expect(materials[0].name).toBe('Blue'); - expect(materials[0].pbrMetallicRoughness.baseColorFactor).toEqual([0.0, 0.0, 0.64, 1.0]); - expect(materials[1].name).toBe('Green'); - expect(materials[1].pbrMetallicRoughness.baseColorFactor).toEqual([0.0, 0.64, 0.0, 1.0]); - expect(materials[2].name).toBe('Red'); - expect(materials[2].pbrMetallicRoughness.baseColorFactor).toEqual([0.64, 0.0, 0.0, 1.0]); - }); + it("loads obj with no materials", async () => { + const data = await loadObj(objNoMaterialsPath, options); + const nodes = data.nodes; + expect(nodes.length).toBe(1); + expect(nodes[0].name).toBe("Node"); // default name - it('loads obj with mtllib paths with spaces', async () => { - const data = await loadObj(objMtllibSpacesPath, options); - const materials = data.materials; - expect(materials.length).toBe(3); + const primitives = getPrimitives(data); + expect(primitives.length).toBe(1); + }); - // .mtl files are loaded in an arbitrary order, so sort for testing purposes - materials.sort((a, b) => { - return a.name.localeCompare(b.name); - }); + it("loads obj with multiple materials", async () => { + // The usemtl markers are interleaved, but should condense to just three primitives + const data = await loadObj(objMultipleMaterialsPath, options); + const nodes = data.nodes; + expect(nodes.length).toBe(1); - expect(materials[0].name).toBe('Blue'); - expect(materials[0].pbrMetallicRoughness.baseColorFactor).toEqual([0.0, 0.0, 0.64, 1.0]); - expect(materials[1].name).toBe('Green'); - expect(materials[1].pbrMetallicRoughness.baseColorFactor).toEqual([0.0, 0.64, 0.0, 1.0]); - expect(materials[2].name).toBe('Red'); - expect(materials[2].pbrMetallicRoughness.baseColorFactor).toEqual([0.64, 0.0, 0.0, 1.0]); - }); + const primitives = getPrimitives(data); + expect(primitives.length).toBe(3); - it('loads obj with missing mtllib', async () => { - const spy = jasmine.createSpy('logger'); - options.logger = spy; + expect(primitives[0].indices.length).toBe(12); + expect(primitives[1].indices.length).toBe(12); + expect(primitives[2].indices.length).toBe(12); + expect(primitives[0].material).toBe("Red"); + expect(primitives[1].material).toBe("Green"); + expect(primitives[2].material).toBe("Blue"); - const data = await loadObj(objMissingMtllibPath, options); - expect(data.materials.length).toBe(0); - expect(spy.calls.argsFor(0)[0].indexOf('ENOENT') >= 0).toBe(true); - expect(spy.calls.argsFor(0)[0].indexOf(path.resolve('/box.mtl')) >= 0).toBe(true); - expect(spy.calls.argsFor(1)[0].indexOf('Attempting to read the material file from within the obj directory instead.') >= 0).toBe(true); - expect(spy.calls.argsFor(2)[0].indexOf('ENOENT') >= 0).toBe(true); - expect(spy.calls.argsFor(3)[0].indexOf('Could not read material file') >= 0).toBe(true); - }); - - it('loads obj with missing usemtl', async () => { - const data = await loadObj(objMissingUsemtlPath, options); - expect(data.materials.length).toBe(1); - expect(data.nodes[0].meshes[0].primitives[0].material).toBe('Material'); - }); - - it('loads obj with unnamed material', async () => { - const data = await loadObj(objUnnamedMaterialPath, options); - expect(data.materials.length).toBe(1); - expect(data.nodes[0].meshes[0].primitives[0].material).toBe(''); - }); - - it('loads .mtl outside of the obj directory', async () => { - const data = await loadObj(objExternalResourcesPath, options); - const materials = data.materials; - expect(materials.length).toBe(2); - - // .mtl files are loaded in an arbitrary order, so find the "MaterialTextured" material - const materialTextured = materials[0].name === 'MaterialTextured' ? materials[0] : materials[1]; - const baseColorTexture = materialTextured.pbrMetallicRoughness.baseColorTexture; - expect(baseColorTexture.source).toBeDefined(); - expect(baseColorTexture.name).toEqual('cesium'); - }); - - it('does not load .mtl outside of the obj directory when secure is true', async () => { - const spy = jasmine.createSpy('logger'); - options.logger = spy; - options.secure = true; - - const data = await loadObj(objExternalResourcesPath, options); - expect(data.materials.length).toBe(1); // obj references 2 materials, one of which is outside the input directory - expect(spy.calls.argsFor(0)[0].indexOf('The material file is outside of the obj directory and the secure flag is true. Attempting to read the material file from within the obj directory instead.') >= 0).toBe(true); - expect(spy.calls.argsFor(1)[0].indexOf('ENOENT') >= 0).toBe(true); - expect(spy.calls.argsFor(2)[0].indexOf('Could not read material file') >= 0).toBe(true); - }); - - it('loads .mtl from root directory when the .mtl path does not exist', async () => { - const data = await loadObj(objResourcesInRootPath, options); - const baseColorTexture = data.materials[0].pbrMetallicRoughness.baseColorTexture; - expect(baseColorTexture.name).toBe('cesium'); - expect(baseColorTexture.source).toBeDefined(); - }); - - it('loads .mtl from root directory when the .mtl path is outside of the obj directory and secure is true', async () => { - options.secure = true; - - const data = await loadObj(objExternalResourcesInRootPath, options); - const materials = data.materials; - expect(materials.length).toBe(2); - - // .mtl files are loaded in an arbitrary order, so find the "MaterialTextured" material - const materialTextured = materials[0].name === 'MaterialTextured' ? materials[0] : materials[1]; - const baseColorTexture = materialTextured.pbrMetallicRoughness.baseColorTexture; - expect(baseColorTexture.source).toBeDefined(); - expect(baseColorTexture.name).toEqual('cesium'); - }); - - it('loads obj with texture', async () => { - const data = await loadObj(objTexturedPath, options); - const baseColorTexture = data.materials[0].pbrMetallicRoughness.baseColorTexture; - expect(baseColorTexture.name).toBe('cesium'); - expect(baseColorTexture.source).toBeDefined(); - }); - - it('loads obj with missing texture', async () => { - const spy = jasmine.createSpy('logger'); - options.logger = spy; - - const data = await loadObj(objMissingTexturePath, options); - const baseColorTexture = data.materials[0].pbrMetallicRoughness.baseColorTexture; - expect(baseColorTexture).toBeUndefined(); - expect(spy.calls.argsFor(0)[0].indexOf('ENOENT') >= 0).toBe(true); - expect(spy.calls.argsFor(0)[0].indexOf(path.resolve('/cesium.png')) >= 0).toBe(true); - expect(spy.calls.argsFor(1)[0].indexOf('Attempting to read the texture file from within the obj directory instead.') >= 0).toBe(true); - expect(spy.calls.argsFor(2)[0].indexOf('ENOENT') >= 0).toBe(true); - expect(spy.calls.argsFor(3)[0].indexOf('Could not read texture file') >= 0).toBe(true); - }); - - it('loads obj with subdirectories', async () => { - const data = await loadObj(objSubdirectoriesPath, options); - const baseColorTexture = data.materials[0].pbrMetallicRoughness.baseColorTexture; - expect(baseColorTexture.name).toBe('cesium'); - expect(baseColorTexture.source).toBeDefined(); - }); - - it('loads obj with windows paths', async () => { - const data = await loadObj(objWindowsPaths, options); - const baseColorTexture = data.materials[0].pbrMetallicRoughness.baseColorTexture; - expect(baseColorTexture.name).toBe('cesium'); - expect(baseColorTexture.source).toBeDefined(); - }); - - it('separates faces that don\'t use the same attributes as other faces in the primitive', async () => { - const data = await loadObj(objMixedAttributesPath, options); - const primitives = getPrimitives(data); - expect(primitives.length).toBe(4); - expect(primitives[0].indices.length).toBe(18); // 6 faces - expect(primitives[1].indices.length).toBe(6); // 2 faces - expect(primitives[2].indices.length).toBe(6); // 2 faces - expect(primitives[3].indices.length).toBe(6); // 2 faces - }); - - function getFirstPosition(data) { - const primitive = getPrimitives(data)[0]; - return new Cartesian3(primitive.positions.get(0), primitive.positions.get(1), primitive.positions.get(2)); + for (let i = 0; i < 3; ++i) { + const indices = primitives[i].indices; + for (let j = 0; j < indices.length; ++j) { + expect(indices.get(j)).toBeLessThan(8); + } } + }); - function getFirstNormal(data) { - const primitive = getPrimitives(data)[0]; - return new Cartesian3(primitive.normals.get(0), primitive.normals.get(1), primitive.normals.get(2)); + it("loads obj uncleaned", async () => { + // Obj with extraneous o, g, and usemtl lines + // Also tests handling of o and g lines with the same names + const data = await loadObj(objUncleanedPath, options); + const nodes = data.nodes; + const meshes = getMeshes(data); + const primitives = getPrimitives(data); + + expect(nodes.length).toBe(1); + expect(meshes.length).toBe(1); + expect(primitives.length).toBe(1); + + expect(nodes[0].name).toBe("Cube"); + expect(meshes[0].name).toBe("Cube_1"); + }); + + it("loads obj with multiple mtllibs", async () => { + const data = await loadObj(objMtllibPath, options); + const materials = data.materials; + expect(materials.length).toBe(3); + + // .mtl files are loaded in an arbitrary order, so sort for testing purposes + materials.sort((a, b) => { + return a.name.localeCompare(b.name); + }); + + expect(materials[0].name).toBe("Blue"); + expect(materials[0].pbrMetallicRoughness.baseColorFactor).toEqual([ + 0.0, 0.0, 0.64, 1.0, + ]); + expect(materials[1].name).toBe("Green"); + expect(materials[1].pbrMetallicRoughness.baseColorFactor).toEqual([ + 0.0, 0.64, 0.0, 1.0, + ]); + expect(materials[2].name).toBe("Red"); + expect(materials[2].pbrMetallicRoughness.baseColorFactor).toEqual([ + 0.64, 0.0, 0.0, 1.0, + ]); + }); + + it("loads obj with mtllib paths with spaces", async () => { + const data = await loadObj(objMtllibSpacesPath, options); + const materials = data.materials; + expect(materials.length).toBe(3); + + // .mtl files are loaded in an arbitrary order, so sort for testing purposes + materials.sort((a, b) => { + return a.name.localeCompare(b.name); + }); + + expect(materials[0].name).toBe("Blue"); + expect(materials[0].pbrMetallicRoughness.baseColorFactor).toEqual([ + 0.0, 0.0, 0.64, 1.0, + ]); + expect(materials[1].name).toBe("Green"); + expect(materials[1].pbrMetallicRoughness.baseColorFactor).toEqual([ + 0.0, 0.64, 0.0, 1.0, + ]); + expect(materials[2].name).toBe("Red"); + expect(materials[2].pbrMetallicRoughness.baseColorFactor).toEqual([ + 0.64, 0.0, 0.0, 1.0, + ]); + }); + + it("loads obj with missing mtllib", async () => { + const spy = jasmine.createSpy("logger"); + options.logger = spy; + + const data = await loadObj(objMissingMtllibPath, options); + expect(data.materials.length).toBe(0); + expect(spy.calls.argsFor(0)[0].indexOf("ENOENT") >= 0).toBe(true); + expect(spy.calls.argsFor(0)[0].indexOf(path.resolve("/box.mtl")) >= 0).toBe( + true + ); + expect( + spy.calls + .argsFor(1)[0] + .indexOf( + "Attempting to read the material file from within the obj directory instead." + ) >= 0 + ).toBe(true); + expect(spy.calls.argsFor(2)[0].indexOf("ENOENT") >= 0).toBe(true); + expect( + spy.calls.argsFor(3)[0].indexOf("Could not read material file") >= 0 + ).toBe(true); + }); + + it("loads obj with missing usemtl", async () => { + const data = await loadObj(objMissingUsemtlPath, options); + expect(data.materials.length).toBe(1); + expect(data.nodes[0].meshes[0].primitives[0].material).toBe("Material"); + }); + + it("loads obj with unnamed material", async () => { + const data = await loadObj(objUnnamedMaterialPath, options); + expect(data.materials.length).toBe(1); + expect(data.nodes[0].meshes[0].primitives[0].material).toBe(""); + }); + + it("loads .mtl outside of the obj directory", async () => { + const data = await loadObj(objExternalResourcesPath, options); + const materials = data.materials; + expect(materials.length).toBe(2); + + // .mtl files are loaded in an arbitrary order, so find the "MaterialTextured" material + const materialTextured = + materials[0].name === "MaterialTextured" ? materials[0] : materials[1]; + const baseColorTexture = + materialTextured.pbrMetallicRoughness.baseColorTexture; + expect(baseColorTexture.source).toBeDefined(); + expect(baseColorTexture.name).toEqual("cesium"); + }); + + it("does not load .mtl outside of the obj directory when secure is true", async () => { + const spy = jasmine.createSpy("logger"); + options.logger = spy; + options.secure = true; + + const data = await loadObj(objExternalResourcesPath, options); + expect(data.materials.length).toBe(1); // obj references 2 materials, one of which is outside the input directory + expect( + spy.calls + .argsFor(0)[0] + .indexOf( + "The material file is outside of the obj directory and the secure flag is true. Attempting to read the material file from within the obj directory instead." + ) >= 0 + ).toBe(true); + expect(spy.calls.argsFor(1)[0].indexOf("ENOENT") >= 0).toBe(true); + expect( + spy.calls.argsFor(2)[0].indexOf("Could not read material file") >= 0 + ).toBe(true); + }); + + it("loads .mtl from root directory when the .mtl path does not exist", async () => { + const data = await loadObj(objResourcesInRootPath, options); + const baseColorTexture = + data.materials[0].pbrMetallicRoughness.baseColorTexture; + expect(baseColorTexture.name).toBe("cesium"); + expect(baseColorTexture.source).toBeDefined(); + }); + + it("loads .mtl from root directory when the .mtl path is outside of the obj directory and secure is true", async () => { + options.secure = true; + + const data = await loadObj(objExternalResourcesInRootPath, options); + const materials = data.materials; + expect(materials.length).toBe(2); + + // .mtl files are loaded in an arbitrary order, so find the "MaterialTextured" material + const materialTextured = + materials[0].name === "MaterialTextured" ? materials[0] : materials[1]; + const baseColorTexture = + materialTextured.pbrMetallicRoughness.baseColorTexture; + expect(baseColorTexture.source).toBeDefined(); + expect(baseColorTexture.name).toEqual("cesium"); + }); + + it("loads obj with texture", async () => { + const data = await loadObj(objTexturedPath, options); + const baseColorTexture = + data.materials[0].pbrMetallicRoughness.baseColorTexture; + expect(baseColorTexture.name).toBe("cesium"); + expect(baseColorTexture.source).toBeDefined(); + }); + + it("loads obj with missing texture", async () => { + const spy = jasmine.createSpy("logger"); + options.logger = spy; + + const data = await loadObj(objMissingTexturePath, options); + const baseColorTexture = + data.materials[0].pbrMetallicRoughness.baseColorTexture; + expect(baseColorTexture).toBeUndefined(); + expect(spy.calls.argsFor(0)[0].indexOf("ENOENT") >= 0).toBe(true); + expect( + spy.calls.argsFor(0)[0].indexOf(path.resolve("/cesium.png")) >= 0 + ).toBe(true); + expect( + spy.calls + .argsFor(1)[0] + .indexOf( + "Attempting to read the texture file from within the obj directory instead." + ) >= 0 + ).toBe(true); + expect(spy.calls.argsFor(2)[0].indexOf("ENOENT") >= 0).toBe(true); + expect( + spy.calls.argsFor(3)[0].indexOf("Could not read texture file") >= 0 + ).toBe(true); + }); + + it("loads obj with subdirectories", async () => { + const data = await loadObj(objSubdirectoriesPath, options); + const baseColorTexture = + data.materials[0].pbrMetallicRoughness.baseColorTexture; + expect(baseColorTexture.name).toBe("cesium"); + expect(baseColorTexture.source).toBeDefined(); + }); + + it("loads obj with windows paths", async () => { + const data = await loadObj(objWindowsPaths, options); + const baseColorTexture = + data.materials[0].pbrMetallicRoughness.baseColorTexture; + expect(baseColorTexture.name).toBe("cesium"); + expect(baseColorTexture.source).toBeDefined(); + }); + + it("separates faces that don't use the same attributes as other faces in the primitive", async () => { + const data = await loadObj(objMixedAttributesPath, options); + const primitives = getPrimitives(data); + expect(primitives.length).toBe(4); + expect(primitives[0].indices.length).toBe(18); // 6 faces + expect(primitives[1].indices.length).toBe(6); // 2 faces + expect(primitives[2].indices.length).toBe(6); // 2 faces + expect(primitives[3].indices.length).toBe(6); // 2 faces + }); + + function getFirstPosition(data) { + const primitive = getPrimitives(data)[0]; + return new Cartesian3( + primitive.positions.get(0), + primitive.positions.get(1), + primitive.positions.get(2) + ); + } + + function getFirstNormal(data) { + const primitive = getPrimitives(data)[0]; + return new Cartesian3( + primitive.normals.get(0), + primitive.normals.get(1), + primitive.normals.get(2) + ); + } + + async function checkAxisConversion( + inputUpAxis, + outputUpAxis, + position, + normal + ) { + const sameAxis = inputUpAxis === outputUpAxis; + options.inputUpAxis = inputUpAxis; + options.outputUpAxis = outputUpAxis; + const data = await loadObj(objRotatedUrl, options); + const rotatedPosition = getFirstPosition(data); + const rotatedNormal = getFirstNormal(data); + if (sameAxis) { + expect(rotatedPosition).toEqual(position); + expect(rotatedNormal).toEqual(normal); + } else { + expect(rotatedPosition).not.toEqual(position); + expect(rotatedNormal).not.toEqual(normal); } + } - async function checkAxisConversion(inputUpAxis, outputUpAxis, position, normal) { - const sameAxis = (inputUpAxis === outputUpAxis); - options.inputUpAxis = inputUpAxis; - options.outputUpAxis = outputUpAxis; - const data = await loadObj(objRotatedUrl, options); - const rotatedPosition = getFirstPosition(data); - const rotatedNormal = getFirstNormal(data); - if (sameAxis) { - expect(rotatedPosition).toEqual(position); - expect(rotatedNormal).toEqual(normal); - } else { - expect(rotatedPosition).not.toEqual(position); - expect(rotatedNormal).not.toEqual(normal); - } + it("performs up axis conversion", async () => { + const data = await loadObj(objRotatedUrl, options); + const position = getFirstPosition(data); + const normal = getFirstNormal(data); + + const axes = ["X", "Y", "Z"]; + const axesLength = axes.length; + for (let i = 0; i < axesLength; ++i) { + for (let j = 0; j < axesLength; ++j) { + await checkAxisConversion(axes[i], axes[j], position, normal); + } } + }); - it('performs up axis conversion', async () => { - const data = await loadObj(objRotatedUrl, options); - const position = getFirstPosition(data); - const normal = getFirstNormal(data); + it("ignores missing normals and uvs", async () => { + const data = await loadObj(objMissingAttributesPath, options); + const primitive = getPrimitives(data)[0]; + expect(primitive.positions.length).toBeGreaterThan(0); + expect(primitive.normals.length).toBe(0); + expect(primitive.uvs.length).toBe(0); + }); - const axes = ['X', 'Y', 'Z']; - const axesLength = axes.length; - for (let i = 0; i < axesLength; ++i) { - for (let j = 0; j < axesLength; ++j) { - await checkAxisConversion(axes[i], axes[j], position, normal); - } - } - }); + async function loadAndGetIndices(objPath, options) { + const data = await loadObj(objPath, options); + const primitive = getPrimitives(data)[0]; + const indices = primitive.indices; + return new Uint16Array(indices.toUint16Buffer().buffer); + } - it('ignores missing normals and uvs', async () => { - const data = await loadObj(objMissingAttributesPath, options); - const primitive = getPrimitives(data)[0]; - expect(primitive.positions.length).toBeGreaterThan(0); - expect(primitive.normals.length).toBe(0); - expect(primitive.uvs.length).toBe(0); - }); + it("applies triangle winding order sanitization", async () => { + options.triangleWindingOrderSanitization = false; + const indicesIncorrect = await loadAndGetIndices( + objIncorrectWindingOrderPath, + options + ); - async function loadAndGetIndices(objPath, options) { - const data = await loadObj(objPath, options); - const primitive = getPrimitives(data)[0]; - const indices = primitive.indices; - return new Uint16Array(indices.toUint16Buffer().buffer); + options.triangleWindingOrderSanitization = true; + const indicesCorrect = await loadAndGetIndices( + objIncorrectWindingOrderPath, + options + ); + + expect(indicesIncorrect[0]).toBe(0); + expect(indicesIncorrect[2]).toBe(2); + expect(indicesIncorrect[1]).toBe(1); + + expect(indicesCorrect[0]).toBe(0); + expect(indicesCorrect[2]).toBe(1); + expect(indicesCorrect[1]).toBe(2); + }); + + it("throws when position index is out of bounds", async () => { + let thrownError; + try { + await loadObj(objIncompletePositionsPath, options); + } catch (e) { + thrownError = e; } + expect(thrownError).toEqual( + new RuntimeError("Position index 1 is out of bounds") + ); + }); - it('applies triangle winding order sanitization', async () => { - options.triangleWindingOrderSanitization = false; - const indicesIncorrect = await loadAndGetIndices(objIncorrectWindingOrderPath, options); + it("throws when normal index is out of bounds", async () => { + let thrownError; + try { + await loadObj(objIncompleteNormalsPath, options); + } catch (e) { + thrownError = e; + } + expect(thrownError).toEqual( + new RuntimeError("Normal index 1 is out of bounds") + ); + }); - options.triangleWindingOrderSanitization = true; - const indicesCorrect = await loadAndGetIndices(objIncorrectWindingOrderPath, options); + it("throws when uv index is out of bounds", async () => { + let thrownError; + try { + await loadObj(objIncompleteUvsPath, options); + } catch (e) { + thrownError = e; + } + expect(thrownError).toEqual( + new RuntimeError("UV index 1 is out of bounds") + ); + }); - expect(indicesIncorrect[0]).toBe(0); - expect(indicesIncorrect[2]).toBe(2); - expect(indicesIncorrect[1]).toBe(1); + it("throws when file has invalid contents", async () => { + let thrownError; + try { + await loadObj(objInvalidContentsPath, options); + } catch (e) { + thrownError = e; + } + expect(thrownError).toEqual( + new RuntimeError( + objInvalidContentsPath + " does not have any geometry data" + ) + ); + }); - expect(indicesCorrect[0]).toBe(0); - expect(indicesCorrect[2]).toBe(1); - expect(indicesCorrect[1]).toBe(2); - }); - - it('throws when position index is out of bounds', async () => { - let thrownError; - try { - await loadObj(objIncompletePositionsPath, options); - } catch (e) { - thrownError = e; - } - expect(thrownError).toEqual(new RuntimeError('Position index 1 is out of bounds')); - }); - - it('throws when normal index is out of bounds', async () => { - let thrownError; - try { - await loadObj(objIncompleteNormalsPath, options); - } catch (e) { - thrownError = e; - } - expect(thrownError).toEqual(new RuntimeError('Normal index 1 is out of bounds')); - }); - - it('throws when uv index is out of bounds', async () => { - let thrownError; - try { - await loadObj(objIncompleteUvsPath, options); - } catch (e) { - thrownError = e; - } - expect(thrownError).toEqual(new RuntimeError('UV index 1 is out of bounds')); - }); - - it('throws when file has invalid contents', async () => { - let thrownError; - try { - await loadObj(objInvalidContentsPath, options); - } catch (e) { - thrownError = e; - } - expect(thrownError).toEqual(new RuntimeError(objInvalidContentsPath + ' does not have any geometry data')); - }); - - it('throw when reading invalid file', async () => { - let thrownError; - try { - await loadObj(objInvalidPath, options); - } catch (e) { - thrownError = e; - } - expect(thrownError.message.startsWith('ENOENT: no such file or directory')).toBe(true); - }); + it("throw when reading invalid file", async () => { + let thrownError; + try { + await loadObj(objInvalidPath, options); + } catch (e) { + thrownError = e; + } + expect( + thrownError.message.startsWith("ENOENT: no such file or directory") + ).toBe(true); + }); }); diff --git a/specs/lib/loadTextureSpec.js b/specs/lib/loadTextureSpec.js index c1d0254..824a7f7 100644 --- a/specs/lib/loadTextureSpec.js +++ b/specs/lib/loadTextureSpec.js @@ -1,99 +1,99 @@ -'use strict'; -const loadTexture = require('../../lib/loadTexture'); +"use strict"; +const loadTexture = require("../../lib/loadTexture"); -const pngTexturePath = 'specs/data/box-complex-material/shininess.png'; -const jpgTexturePath = 'specs/data/box-complex-material/emission.jpg'; -const jpegTexturePath = 'specs/data/box-complex-material/specular.jpeg'; -const gifTexturePath = 'specs/data/box-complex-material/ambient.gif'; -const grayscaleTexturePath = 'specs/data/box-complex-material-alpha/alpha.png'; -const transparentTexturePath = 'specs/data/box-complex-material/diffuse.png'; +const pngTexturePath = "specs/data/box-complex-material/shininess.png"; +const jpgTexturePath = "specs/data/box-complex-material/emission.jpg"; +const jpegTexturePath = "specs/data/box-complex-material/specular.jpeg"; +const gifTexturePath = "specs/data/box-complex-material/ambient.gif"; +const grayscaleTexturePath = "specs/data/box-complex-material-alpha/alpha.png"; +const transparentTexturePath = "specs/data/box-complex-material/diffuse.png"; -describe('loadTexture', () => { - it('loads png texture', async () => { - const texture = await loadTexture(pngTexturePath); - expect(texture.transparent).toBe(false); - expect(texture.source).toBeDefined(); - expect(texture.name).toBe('shininess'); - expect(texture.extension).toBe('.png'); - expect(texture.path).toBe(pngTexturePath); - expect(texture.pixels).toBeUndefined(); - expect(texture.width).toBeUndefined(); - expect(texture.height).toBeUndefined(); - }); +describe("loadTexture", () => { + it("loads png texture", async () => { + const texture = await loadTexture(pngTexturePath); + expect(texture.transparent).toBe(false); + expect(texture.source).toBeDefined(); + expect(texture.name).toBe("shininess"); + expect(texture.extension).toBe(".png"); + expect(texture.path).toBe(pngTexturePath); + expect(texture.pixels).toBeUndefined(); + expect(texture.width).toBeUndefined(); + expect(texture.height).toBeUndefined(); + }); - it('loads jpg texture', async () => { - const texture = await loadTexture(jpgTexturePath); - expect(texture.transparent).toBe(false); - expect(texture.source).toBeDefined(); - expect(texture.name).toBe('emission'); - expect(texture.extension).toBe('.jpg'); - expect(texture.path).toBe(jpgTexturePath); - expect(texture.pixels).toBeUndefined(); - expect(texture.width).toBeUndefined(); - expect(texture.height).toBeUndefined(); - }); + it("loads jpg texture", async () => { + const texture = await loadTexture(jpgTexturePath); + expect(texture.transparent).toBe(false); + expect(texture.source).toBeDefined(); + expect(texture.name).toBe("emission"); + expect(texture.extension).toBe(".jpg"); + expect(texture.path).toBe(jpgTexturePath); + expect(texture.pixels).toBeUndefined(); + expect(texture.width).toBeUndefined(); + expect(texture.height).toBeUndefined(); + }); - it('loads jpeg texture', async () => { - const texture = await loadTexture(jpegTexturePath); - expect(texture.transparent).toBe(false); - expect(texture.source).toBeDefined(); - expect(texture.name).toBe('specular'); - expect(texture.extension).toBe('.jpeg'); - expect(texture.path).toBe(jpegTexturePath); - expect(texture.pixels).toBeUndefined(); - expect(texture.width).toBeUndefined(); - expect(texture.height).toBeUndefined(); - }); + it("loads jpeg texture", async () => { + const texture = await loadTexture(jpegTexturePath); + expect(texture.transparent).toBe(false); + expect(texture.source).toBeDefined(); + expect(texture.name).toBe("specular"); + expect(texture.extension).toBe(".jpeg"); + expect(texture.path).toBe(jpegTexturePath); + expect(texture.pixels).toBeUndefined(); + expect(texture.width).toBeUndefined(); + expect(texture.height).toBeUndefined(); + }); - it('loads gif texture', async () => { - const texture = await loadTexture(gifTexturePath); - expect(texture.transparent).toBe(false); - expect(texture.source).toBeDefined(); - expect(texture.name).toBe('ambient'); - expect(texture.extension).toBe('.gif'); - expect(texture.path).toBe(gifTexturePath); - expect(texture.pixels).toBeUndefined(); - expect(texture.width).toBeUndefined(); - expect(texture.height).toBeUndefined(); - }); + it("loads gif texture", async () => { + const texture = await loadTexture(gifTexturePath); + expect(texture.transparent).toBe(false); + expect(texture.source).toBeDefined(); + expect(texture.name).toBe("ambient"); + expect(texture.extension).toBe(".gif"); + expect(texture.path).toBe(gifTexturePath); + expect(texture.pixels).toBeUndefined(); + expect(texture.width).toBeUndefined(); + expect(texture.height).toBeUndefined(); + }); - it('loads grayscale texture', async () => { - const texture = await loadTexture(grayscaleTexturePath); - expect(texture.transparent).toBe(false); - expect(texture.source).toBeDefined(); - expect(texture.extension).toBe('.png'); - }); + it("loads grayscale texture", async () => { + const texture = await loadTexture(grayscaleTexturePath); + expect(texture.transparent).toBe(false); + expect(texture.source).toBeDefined(); + expect(texture.extension).toBe(".png"); + }); - it('loads texture with alpha channel', async () => { - const texture = await loadTexture(transparentTexturePath); - expect(texture.transparent).toBe(false); - }); + it("loads texture with alpha channel", async () => { + const texture = await loadTexture(transparentTexturePath); + expect(texture.transparent).toBe(false); + }); - it('loads texture with checkTransparency flag', async () => { - const options = { - checkTransparency : true - }; - const texture = await loadTexture(transparentTexturePath, options); - expect(texture.transparent).toBe(true); - }); + it("loads texture with checkTransparency flag", async () => { + const options = { + checkTransparency: true, + }; + const texture = await loadTexture(transparentTexturePath, options); + expect(texture.transparent).toBe(true); + }); - it('loads and decodes png', async () => { - const options = { - decode : true - }; - const texture = await loadTexture(pngTexturePath, options); - expect(texture.pixels).toBeDefined(); - expect(texture.width).toBe(211); - expect(texture.height).toBe(211); - }); + it("loads and decodes png", async () => { + const options = { + decode: true, + }; + const texture = await loadTexture(pngTexturePath, options); + expect(texture.pixels).toBeDefined(); + expect(texture.width).toBe(211); + expect(texture.height).toBe(211); + }); - it('loads and decodes jpeg', async () => { - const options = { - decode : true - }; - const texture = await loadTexture(jpegTexturePath, options); - expect(texture.pixels).toBeDefined(); - expect(texture.width).toBe(211); - expect(texture.height).toBe(211); - }); + it("loads and decodes jpeg", async () => { + const options = { + decode: true, + }; + const texture = await loadTexture(jpegTexturePath, options); + expect(texture.pixels).toBeDefined(); + expect(texture.width).toBe(211); + expect(texture.height).toBe(211); + }); }); diff --git a/specs/lib/obj2gltfSpec.js b/specs/lib/obj2gltfSpec.js index a30d57a..707d3d7 100644 --- a/specs/lib/obj2gltfSpec.js +++ b/specs/lib/obj2gltfSpec.js @@ -1,201 +1,215 @@ -'use strict'; -const { DeveloperError } = require('cesium'); -const fsExtra = require('fs-extra'); -const path = require('path'); -const Promise = require('bluebird'); -const createGltf = require('../../lib/createGltf'); -const obj2gltf = require('../../lib/obj2gltf'); +"use strict"; +const { DeveloperError } = require("cesium"); +const fsExtra = require("fs-extra"); +const path = require("path"); +const Promise = require("bluebird"); +const createGltf = require("../../lib/createGltf"); +const obj2gltf = require("../../lib/obj2gltf"); -const texturedObjPath = 'specs/data/box-textured/box-textured.obj'; -const complexObjPath = 'specs/data/box-complex-material/box-complex-material.obj'; -const missingMtllibObjPath = 'specs/data/box-missing-mtllib/box-missing-mtllib.obj'; +const texturedObjPath = "specs/data/box-textured/box-textured.obj"; +const complexObjPath = + "specs/data/box-complex-material/box-complex-material.obj"; +const missingMtllibObjPath = + "specs/data/box-missing-mtllib/box-missing-mtllib.obj"; -const outputDirectory = 'output'; +const outputDirectory = "output"; -const textureUrl = 'specs/data/box-textured/cesium.png'; +const textureUrl = "specs/data/box-textured/cesium.png"; -describe('obj2gltf', () => { - beforeEach(() => { - spyOn(fsExtra, 'outputFile').and.returnValue(Promise.resolve()); - }); +describe("obj2gltf", () => { + beforeEach(() => { + spyOn(fsExtra, "outputFile").and.returnValue(Promise.resolve()); + }); - it('converts obj to gltf', async () => { - const gltf = await obj2gltf(texturedObjPath); - expect(gltf).toBeDefined(); - expect(gltf.images.length).toBe(1); - }); + it("converts obj to gltf", async () => { + const gltf = await obj2gltf(texturedObjPath); + expect(gltf).toBeDefined(); + expect(gltf.images.length).toBe(1); + }); - it('converts obj to glb', async () => { - const options = { - binary : true - }; - const glb = await obj2gltf(texturedObjPath, options); - const magic = glb.toString('utf8', 0, 4); - expect(magic).toBe('glTF'); - }); + it("converts obj to glb", async () => { + const options = { + binary: true, + }; + const glb = await obj2gltf(texturedObjPath, options); + const magic = glb.toString("utf8", 0, 4); + expect(magic).toBe("glTF"); + }); - it('convert obj to gltf with separate resources', async () => { - const options = { - separate : true, - separateTextures : true, - outputDirectory : outputDirectory - }; - await obj2gltf(texturedObjPath, options); - expect(fsExtra.outputFile.calls.count()).toBe(2); // Saves out .png and .bin - }); + it("convert obj to gltf with separate resources", async () => { + const options = { + separate: true, + separateTextures: true, + outputDirectory: outputDirectory, + }; + await obj2gltf(texturedObjPath, options); + expect(fsExtra.outputFile.calls.count()).toBe(2); // Saves out .png and .bin + }); - it('convert obj to gltf with separate resources when buffer exceeds Node limit', async () => { - spyOn(createGltf, '_getBufferMaxByteLength').and.returnValue(0); - const options = { - separate : true, - separateTextures : true, - outputDirectory : outputDirectory - }; - await obj2gltf(texturedObjPath, options); - expect(fsExtra.outputFile.calls.count()).toBe(5); // Saves out .png and four .bin for positions, normals, uvs, and indices - }); + it("convert obj to gltf with separate resources when buffer exceeds Node limit", async () => { + spyOn(createGltf, "_getBufferMaxByteLength").and.returnValue(0); + const options = { + separate: true, + separateTextures: true, + outputDirectory: outputDirectory, + }; + await obj2gltf(texturedObjPath, options); + expect(fsExtra.outputFile.calls.count()).toBe(5); // Saves out .png and four .bin for positions, normals, uvs, and indices + }); - it('converts obj to glb with separate resources', async () => { - const options = { - separate : true, - separateTextures : true, - outputDirectory : outputDirectory, - binary : true - }; - await obj2gltf(texturedObjPath, options); - expect(fsExtra.outputFile.calls.count()).toBe(2); // Saves out .png and .bin - }); + it("converts obj to glb with separate resources", async () => { + const options = { + separate: true, + separateTextures: true, + outputDirectory: outputDirectory, + binary: true, + }; + await obj2gltf(texturedObjPath, options); + expect(fsExtra.outputFile.calls.count()).toBe(2); // Saves out .png and .bin + }); - it('converts obj with multiple textures', async () => { - const options = { - separateTextures : true, - outputDirectory : outputDirectory - }; - await obj2gltf(complexObjPath, options); - expect(fsExtra.outputFile.calls.count()).toBe(5); // baseColor, metallicRoughness, occlusion, emission, normal - }); + it("converts obj with multiple textures", async () => { + const options = { + separateTextures: true, + outputDirectory: outputDirectory, + }; + await obj2gltf(complexObjPath, options); + expect(fsExtra.outputFile.calls.count()).toBe(5); // baseColor, metallicRoughness, occlusion, emission, normal + }); - it('sets overriding textures (1)', async () => { - const options = { - overridingTextures : { - metallicRoughnessOcclusionTexture : textureUrl, - normalTexture : textureUrl, - baseColorTexture : textureUrl, - emissiveTexture : textureUrl, - alphaTexture : textureUrl - }, - separateTextures : true, - outputDirectory : outputDirectory - }; - await obj2gltf(complexObjPath, options); - const args = fsExtra.outputFile.calls.allArgs(); - const length = args.length; - for (let i = 0; i < length; ++i) { - expect(path.basename(args[i][0])).toBe(path.basename(textureUrl)); - } - }); + it("sets overriding textures (1)", async () => { + const options = { + overridingTextures: { + metallicRoughnessOcclusionTexture: textureUrl, + normalTexture: textureUrl, + baseColorTexture: textureUrl, + emissiveTexture: textureUrl, + alphaTexture: textureUrl, + }, + separateTextures: true, + outputDirectory: outputDirectory, + }; + await obj2gltf(complexObjPath, options); + const args = fsExtra.outputFile.calls.allArgs(); + const length = args.length; + for (let i = 0; i < length; ++i) { + expect(path.basename(args[i][0])).toBe(path.basename(textureUrl)); + } + }); - it('sets overriding textures (2)', async () => { - const options = { - overridingTextures : { - specularGlossinessTexture : textureUrl, - occlusionTexture : textureUrl, - normalTexture : textureUrl, - baseColorTexture : textureUrl, - emissiveTexture : textureUrl, - alphaTexture : textureUrl - }, - separateTextures : true, - outputDirectory : outputDirectory - }; - await obj2gltf(complexObjPath, options); - const args = fsExtra.outputFile.calls.allArgs(); - const length = args.length; - for (let i = 0; i < length; ++i) { - expect(path.basename(args[i][0])).toBe(path.basename(textureUrl)); - } - }); + it("sets overriding textures (2)", async () => { + const options = { + overridingTextures: { + specularGlossinessTexture: textureUrl, + occlusionTexture: textureUrl, + normalTexture: textureUrl, + baseColorTexture: textureUrl, + emissiveTexture: textureUrl, + alphaTexture: textureUrl, + }, + separateTextures: true, + outputDirectory: outputDirectory, + }; + await obj2gltf(complexObjPath, options); + const args = fsExtra.outputFile.calls.allArgs(); + const length = args.length; + for (let i = 0; i < length; ++i) { + expect(path.basename(args[i][0])).toBe(path.basename(textureUrl)); + } + }); - it('uses a custom logger', async () => { - let lastMessage; - const options = { - logger : (message) => { - lastMessage = message; - } - }; - await obj2gltf(missingMtllibObjPath, options); - expect(lastMessage.indexOf('Could not read material file') >= 0).toBe(true); - }); + it("uses a custom logger", async () => { + let lastMessage; + const options = { + logger: (message) => { + lastMessage = message; + }, + }; + await obj2gltf(missingMtllibObjPath, options); + expect(lastMessage.indexOf("Could not read material file") >= 0).toBe(true); + }); - it('uses a custom writer', async () => { - const filePaths = []; - const fileContents = []; - const options = { - separate : true, - writer : (relativePath, contents) => { - filePaths.push(relativePath); - fileContents.push(contents); - } - }; - await obj2gltf(texturedObjPath, options); - expect(filePaths).toEqual(['cesium.png', 'box-textured.bin']); - expect(fileContents[0]).toBeDefined(); - expect(fileContents[1]).toBeDefined(); - }); + it("uses a custom writer", async () => { + const filePaths = []; + const fileContents = []; + const options = { + separate: true, + writer: (relativePath, contents) => { + filePaths.push(relativePath); + fileContents.push(contents); + }, + }; + await obj2gltf(texturedObjPath, options); + expect(filePaths).toEqual(["cesium.png", "box-textured.bin"]); + expect(fileContents[0]).toBeDefined(); + expect(fileContents[1]).toBeDefined(); + }); - it('throws if objPath is undefined', () => { - let thrownError; - try { - obj2gltf(undefined); - } catch (e) { - thrownError = e; - } - expect(thrownError).toEqual(new DeveloperError('objPath is required')); - }); + it("throws if objPath is undefined", () => { + let thrownError; + try { + obj2gltf(undefined); + } catch (e) { + thrownError = e; + } + expect(thrownError).toEqual(new DeveloperError("objPath is required")); + }); - it('throws if both options.writer and options.outputDirectory are undefined when writing separate resources', () => { - const options = { - separateTextures : true - }; + it("throws if both options.writer and options.outputDirectory are undefined when writing separate resources", () => { + const options = { + separateTextures: true, + }; - let thrownError; - try { - obj2gltf(texturedObjPath, options); - } catch (e) { - thrownError = e; - } - expect(thrownError).toEqual(new DeveloperError('Either options.writer or options.outputDirectory must be defined when writing separate resources.')); - }); + let thrownError; + try { + obj2gltf(texturedObjPath, options); + } catch (e) { + thrownError = e; + } + expect(thrownError).toEqual( + new DeveloperError( + "Either options.writer or options.outputDirectory must be defined when writing separate resources." + ) + ); + }); - it('throws if more than one material type is set', () => { - const options = { - metallicRoughness : true, - specularGlossiness : true - }; + it("throws if more than one material type is set", () => { + const options = { + metallicRoughness: true, + specularGlossiness: true, + }; - let thrownError; - try { - obj2gltf(texturedObjPath, options); - } catch (e) { - thrownError = e; - } - expect(thrownError).toEqual(new DeveloperError('Only one material type may be set from [metallicRoughness, specularGlossiness, unlit].')); - }); + let thrownError; + try { + obj2gltf(texturedObjPath, options); + } catch (e) { + thrownError = e; + } + expect(thrownError).toEqual( + new DeveloperError( + "Only one material type may be set from [metallicRoughness, specularGlossiness, unlit]." + ) + ); + }); - it('throws if metallicRoughnessOcclusionTexture and specularGlossinessTexture are both defined', () => { - const options = { - overridingTextures : { - metallicRoughnessOcclusionTexture : textureUrl, - specularGlossinessTexture : textureUrl - } - }; + it("throws if metallicRoughnessOcclusionTexture and specularGlossinessTexture are both defined", () => { + const options = { + overridingTextures: { + metallicRoughnessOcclusionTexture: textureUrl, + specularGlossinessTexture: textureUrl, + }, + }; - let thrownError; - try { - obj2gltf(texturedObjPath, options); - } catch (e) { - thrownError = e; - } - expect(thrownError).toEqual(new DeveloperError('metallicRoughnessOcclusionTexture and specularGlossinessTexture cannot both be defined.')); - }); + let thrownError; + try { + obj2gltf(texturedObjPath, options); + } catch (e) { + thrownError = e; + } + expect(thrownError).toEqual( + new DeveloperError( + "metallicRoughnessOcclusionTexture and specularGlossinessTexture cannot both be defined." + ) + ); + }); });