Skip to content

Commit

Permalink
Merge pull request #637 from javagl/add-base-color-names-options
Browse files Browse the repository at this point in the history
Add base color names option
  • Loading branch information
lilleyse authored Apr 20, 2023
2 parents 901c94f + 00824c5 commit 575bd6b
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

### 4.0.3 - yyyy-mm-dd

- Add `baseColorTextureNames` and `baseColorFactorNames` to the options that can be given at the command line and passed to `processGltf` and `updateVersion`, to specify the uniform names in a `KHR_techniques_webgl` extension that indicate that a certain texture or color should be used as the base color texture or base color factor of a PBR material.

### 4.0.2 - 2023-02-10

- Update npm module dependencies.
Expand Down
44 changes: 23 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,27 +117,29 @@ processGltf(gltf, options).then(function (results) {

### Command-Line Flags

| Flag | Description | Required |
| ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------ | ---------------------- |
| `--help`, `-h` | Display help | No |
| `--input`, `-i` | Path to the glTF or glb file. | :white_check_mark: Yes |
| `--output`, `-o` | Output path of the glTF or glb file. Separate resources will be saved to the same directory. | No |
| `--binary`, `-b` | Convert the input glTF to glb. | No, default `false` |
| `--json`, `-j` | Convert the input glb to glTF. | No, default `false` |
| `--separate`, `-s` | Write separate buffers, shaders, and textures instead of embedding them in the glTF. | No, default `false` |
| `--separateTextures`, `-t` | Write out separate textures only. | No, default `false` |
| `--stats` | Print statistics to console for output glTF file. | No, default `false` |
| `--keepUnusedElements` | Keep unused materials, nodes and meshes. | No, default `false` |
| `--keepLegacyExtensions` | When false, materials with `KHR_techniques_webgl`, `KHR_blend`, or `KHR_materials_common` will be converted to PBR. | No, default `false` |
| `--draco.compressMeshes`, `-d` | Compress the meshes using Draco. Adds the `KHR_draco_mesh_compression` extension. | No, default `false` |
| `--draco.compressionLevel` | Draco compression level [0-10], most is 10, least is 0. A value of 0 will apply sequential encoding and preserve face order. | No, default `7` |
| `--draco.quantizePositionBits` | Quantization bits for position attribute when using Draco compression. | No, default `11` |
| `--draco.quantizeNormalBits` | Quantization bits for normal attribute when using Draco compression. | No, default `8` |
| `--draco.quantizeTexcoordBits` | Quantization bits for texture coordinate attribute when using Draco compression. | No, default `10` |
| `--draco.quantizeColorBits` | Quantization bits for color attribute when using Draco compression. | No, default `8` |
| `--draco.quantizeGenericBits` | Quantization bits for skinning attribute (joint indices and joint weights) and custom attributes when using Draco compression. | No, default `8` |
| `--draco.unifiedQuantization` | Quantize positions of all primitives using the same quantization grid. If not set, quantization is applied separately. | No, default `false` |
| `--draco.uncompressedFallback` | Adds uncompressed fallback versions of the compressed meshes. | No, default `false` |
| Flag | Description | Required |
| ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------- |
| `--help`, `-h` | Display help | No |
| `--input`, `-i` | Path to the glTF or glb file. | :white_check_mark: Yes |
| `--output`, `-o` | Output path of the glTF or glb file. Separate resources will be saved to the same directory. | No |
| `--binary`, `-b` | Convert the input glTF to glb. | No, default `false` |
| `--json`, `-j` | Convert the input glb to glTF. | No, default `false` |
| `--separate`, `-s` | Write separate buffers, shaders, and textures instead of embedding them in the glTF. | No, default `false` |
| `--separateTextures`, `-t` | Write out separate textures only. | No, default `false` |
| `--stats` | Print statistics to console for output glTF file. | No, default `false` |
| `--keepUnusedElements` | Keep unused materials, nodes and meshes. | No, default `false` |
| `--keepLegacyExtensions` | When false, materials with `KHR_techniques_webgl`, `KHR_blend`, or `KHR_materials_common` will be converted to PBR. | No, default `false` |
| `--draco.compressMeshes`, `-d` | Compress the meshes using Draco. Adds the `KHR_draco_mesh_compression` extension. | No, default `false` |
| `--draco.compressionLevel` | Draco compression level [0-10], most is 10, least is 0. A value of 0 will apply sequential encoding and preserve face order. | No, default `7` |
| `--draco.quantizePositionBits` | Quantization bits for position attribute when using Draco compression. | No, default `11` |
| `--draco.quantizeNormalBits` | Quantization bits for normal attribute when using Draco compression. | No, default `8` |
| `--draco.quantizeTexcoordBits` | Quantization bits for texture coordinate attribute when using Draco compression. | No, default `10` |
| `--draco.quantizeColorBits` | Quantization bits for color attribute when using Draco compression. | No, default `8` |
| `--draco.quantizeGenericBits` | Quantization bits for skinning attribute (joint indices and joint weights) and custom attributes when using Draco compression. | No, default `8` |
| `--draco.unifiedQuantization` | Quantize positions of all primitives using the same quantization grid. If not set, quantization is applied separately. | No, default `false` |
| `--draco.uncompressedFallback` | Adds uncompressed fallback versions of the compressed meshes. | No, default `false` |
| `--baseColorTextureNames` | Names of uniforms that should be considered to refer to base color textures <br /> when updating from the `KHR_techniques_webgl` extension to PBR materials. | No. (The defaults are not specified here) |
| `--baseColorFactorNames` | Names of uniforms that should be considered to refer to base color factors <br /> when updating from the `KHR_techniques_webgl` extension to PBR materials. | No. (The defaults are not specified here) |

## Build Instructions

Expand Down
12 changes: 12 additions & 0 deletions bin/gltf-pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ const argv = yargs
type: "boolean",
default: dracoDefaults.unifiedQuantization,
},
baseColorTextureNames: {
describe:
"Names of uniforms that should be considered to refer to base color textures when updating from the KHR_techniques_webgl extension to PBR materials.",
type: "array",
},
baseColorFactorNames: {
describe:
"Names of uniforms that should be considered to refer to base color factors when updating from the KHR_techniques_webgl extension to PBR materials.",
type: "array",
},
})
.parse(args);

Expand Down Expand Up @@ -192,6 +202,8 @@ const options = {
keepLegacyExtensions: argv.keepLegacyExtensions,
name: outputName,
dracoOptions: dracoOptions,
baseColorTextureNames: argv.baseColorTextureNames,
baseColorFactorNames: argv.baseColorFactorNames,
};

const inputIsBinary = inputExtension === ".glb";
Expand Down
2 changes: 2 additions & 0 deletions lib/processGltf.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ module.exports = processGltf;
* @param {Object} [options.dracoOptions] Options to pass to the compressDracoMeshes stage. If undefined, stage is not run.
* @param {Stage[]} [options.customStages] Custom stages to run on the glTF asset.
* @param {Logger} [options.logger] A callback function for handling logged messages. Defaults to console.log.
* @param {String[]} [options.baseColorTextureNames] Names of uniforms that indicate base color textures.
* @param {String[]} [options.baseColorFactorNames] Names of uniforms that indicate base color factors.
*
* @returns {Promise} A promise that resolves to an object containing the glTF and a dictionary containing separate resources.
*/
Expand Down
25 changes: 21 additions & 4 deletions lib/updateVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const updateFunctions = {
* @param {Object} gltf A javascript object containing a glTF asset.
* @param {Object} [options] Options for updating the glTF.
* @param {String} [options.targetVersion] The glTF will be upgraded until it hits the specified version.
* @param {String[]} [options.baseColorTextureNames] Names of uniforms that indicate base color textures.
* @param {String[]} [options.baseColorFactorNames] Names of uniforms that indicate base color factors.
* @returns {Object} The updated glTF asset.
*
* @private
Expand Down Expand Up @@ -78,7 +80,7 @@ function updateVersion(gltf, options) {
}

if (!options.keepLegacyExtensions) {
convertTechniquesToPbr(gltf);
convertTechniquesToPbr(gltf, options);
convertMaterialsCommonToPbr(gltf);
}

Expand Down Expand Up @@ -1008,8 +1010,13 @@ function glTF10to20(gltf) {
// It's not possible to upgrade glTF 1.0 shaders to 2.0 PBR materials in a generic way,
// but we can look for certain uniform names that are commonly found in glTF 1.0 assets
// and create PBR materials out of those.
const baseColorTextureNames = ["u_tex", "u_diffuse", "u_emission"];
const baseColorFactorNames = ["u_diffuse"];
const defaultBaseColorTextureNames = [
"u_tex",
"u_diffuse",
"u_emission",
"u_diffuse_tex",
];
const defaultBaseColorFactorNames = ["u_diffuse", "u_diffuse_mat"];

function initializePbrMaterial(material) {
material.pbrMetallicRoughness = defined(material.pbrMetallicRoughness)
Expand Down Expand Up @@ -1049,7 +1056,17 @@ function srgbToLinear(srgb) {
return linear;
}

function convertTechniquesToPbr(gltf) {
function convertTechniquesToPbr(gltf, options) {
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
const baseColorTextureNames = defaultValue(
options.baseColorTextureNames,
defaultBaseColorTextureNames
);
const baseColorFactorNames = defaultValue(
options.baseColorFactorNames,
defaultBaseColorFactorNames
);

// Future work: convert other values like emissive, specular, etc. Only handling diffuse right now.
ForEach.material(gltf, function (material) {
ForEach.materialValue(material, function (value, name) {
Expand Down
39 changes: 39 additions & 0 deletions specs/lib/updateVersionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -926,4 +926,43 @@ describe("updateVersion", () => {
expect(gltf.extensionsUsed).toBeUndefined();
expect(material.extensions).toBeUndefined();
});

it("creates a PBR material from KHR_techniques_webgl with a custom diffuse texture name", async () => {
const gltf = fsExtra.readJsonSync(gltf2TechniquesTextured);
await readResources(gltf);

const options = {
baseColorTextureNames: ["u_diffuse"],
};
updateVersion(gltf, options);

expect(gltf.materials.length).toBe(1);

const material = gltf.materials[0];
expect(material.pbrMetallicRoughness.roughnessFactor).toBe(1.0);
expect(material.pbrMetallicRoughness.metallicFactor).toBe(0.0);
expect(material.pbrMetallicRoughness.baseColorTexture.index).toBe(0);

expect(gltf.extensionsRequired).toBeUndefined();
expect(gltf.extensionsUsed).toBeUndefined();
});

it("does not create a PBR material from KHR_techniques_webgl when the diffuse texture name is unknown", async () => {
const gltf = fsExtra.readJsonSync(gltf2TechniquesTextured);
await readResources(gltf);

const options = {
baseColorTextureNames: ["NOT_u_diffuse"],
};
updateVersion(gltf, options);

expect(gltf.materials.length).toBe(1);

const material = gltf.materials[0];
expect(material.pbrMetallicRoughness).toBeUndefined();
expect(material.extensions).toBeUndefined();

expect(gltf.extensionsRequired).toBeUndefined();
expect(gltf.extensionsUsed).toBeUndefined();
});
});

0 comments on commit 575bd6b

Please sign in to comment.