Skip to content

Commit 751d898

Browse files
authored
Add KHR_materials_unlit export support (playcanvas#8270)
* Add KHR_materials_unlit export support * Refactor adding extensions
1 parent 40b1982 commit 751d898

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

src/extras/exporters/gltf-exporter.js

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -382,13 +382,27 @@ class GltfExporter extends CoreExporter {
382382
}
383383
}
384384

385+
addExtension(json, output, name, data = {}) {
386+
output.extensions = output.extensions || {};
387+
output.extensions[name] = data;
388+
389+
json.extensionsUsed = json.extensionsUsed ?? [];
390+
if (!json.extensionsUsed.includes(name)) {
391+
json.extensionsUsed.push(name);
392+
}
393+
}
394+
385395
writeStandardMaterial(resources, mat, output, json) {
386396

387397
const { diffuse, emissive, opacity, metalness, gloss, glossInvert } = mat;
388398
const pbr = output.pbrMetallicRoughness;
389399

390-
if (!diffuse.equals(Color.WHITE) || opacity !== 1) {
391-
const { r, g, b } = diffuse.clone().linear();
400+
// For unlit materials, the parser copies baseColor to emissive and sets diffuse to white.
401+
// So we need to use emissive as the source for baseColorFactor.
402+
const baseColor = mat.useLighting ? diffuse : emissive;
403+
404+
if (!baseColor.equals(Color.WHITE) || opacity !== 1) {
405+
const { r, g, b } = baseColor.clone().linear();
392406
pbr.baseColorFactor = [r, g, b, opacity];
393407
}
394408

@@ -401,24 +415,20 @@ class GltfExporter extends CoreExporter {
401415
pbr.roughnessFactor = roughness;
402416
}
403417

404-
this.attachTexture(resources, mat, pbr, 'baseColorTexture', 'diffuseMap', json);
418+
// For unlit, use emissiveMap as baseColorTexture source (parser copies diffuseMap to emissiveMap)
419+
this.attachTexture(resources, mat, pbr, 'baseColorTexture', mat.useLighting ? 'diffuseMap' : 'emissiveMap', json);
405420
this.attachTexture(resources, mat, pbr, 'metallicRoughnessTexture', 'metalnessMap', json);
406421

407-
if (!emissive.equals(Color.BLACK)) {
422+
// Skip emissive for unlit materials (emissive holds the baseColor)
423+
if (mat.useLighting && !emissive.equals(Color.BLACK)) {
408424
const { r, g, b } = emissive.clone().linear();
409425
output.emissiveFactor = [r, g, b];
410426
}
411427

412-
if (mat.emissiveIntensity !== 1) {
413-
output.extensions = output.extensions || {};
414-
output.extensions.KHR_materials_emissive_strength = {
428+
if (mat.useLighting && mat.emissiveIntensity !== 1) {
429+
this.addExtension(json, output, 'KHR_materials_emissive_strength', {
415430
emissiveStrength: mat.emissiveIntensity
416-
};
417-
418-
json.extensionsUsed = json.extensionsUsed ?? [];
419-
if (!json.extensionsUsed.includes('KHR_materials_emissive_strength')) {
420-
json.extensionsUsed.push('KHR_materials_emissive_strength');
421-
}
431+
});
422432
}
423433

424434
if (mat.useMetalnessSpecularColor) {
@@ -437,15 +447,13 @@ class GltfExporter extends CoreExporter {
437447
this.attachTexture(resources, mat, specularExt, 'specularTexture', 'specularityFactorMap', json);
438448

439449
if (Object.keys(specularExt).length > 0) {
440-
output.extensions = output.extensions || {};
441-
output.extensions.KHR_materials_specular = specularExt;
442-
443-
json.extensionsUsed = json.extensionsUsed ?? [];
444-
if (!json.extensionsUsed.includes('KHR_materials_specular')) {
445-
json.extensionsUsed.push('KHR_materials_specular');
446-
}
450+
this.addExtension(json, output, 'KHR_materials_specular', specularExt);
447451
}
448452
}
453+
454+
if (!mat.useLighting) {
455+
this.addExtension(json, output, 'KHR_materials_unlit');
456+
}
449457
}
450458

451459
writeMaterials(resources, json) {

0 commit comments

Comments
 (0)