Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add base color names option #637

Merged
merged 3 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
},
javagl marked this conversation as resolved.
Show resolved Hide resolved
})
.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
23 changes: 19 additions & 4 deletions lib/updateVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function updateVersion(gltf, options) {
}

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

Expand Down Expand Up @@ -1008,8 +1008,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 +1054,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();
});
});