-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Export KHR extensions: IOR, Transmission and Volume (#12389)
* initial support for KHR_maetrials_specular * update factors test to keep 0 significativ * update with prettier * link to specification * collapse,unecessary test block * Add support for KHR_materials ior * add ref to KHR_materials_ior * finalize extensions * IKHRLightsPunctual_LightType name change * Revert "IKHRLightsPunctual_LightType name change" This reverts commit 0b87226. * Update KHR_lights_punctual.ts * formatting * update test Co-authored-by: Gary Hsu <[email protected]> * add test if refraction enabled Co-authored-by: Gary Hsu <[email protected]>
- Loading branch information
1 parent
20b8ab5
commit 145041a
Showing
10 changed files
with
321 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
packages/dev/serializers/src/glTF/2.0/Extensions/KHR_materials_ior.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type { IMaterial, IKHRMaterialsIor } from "babylonjs-gltf2interface"; | ||
import type { IGLTFExporterExtensionV2 } from "../glTFExporterExtension"; | ||
import { _Exporter } from "../glTFExporter"; | ||
import type { Material } from "core/Materials/material"; | ||
import { PBRMaterial } from "core/Materials/PBR/pbrMaterial"; | ||
|
||
const NAME = "KHR_materials_ior"; | ||
|
||
/** | ||
* [Specification](https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_ior/README.md) | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
export class KHR_materials_ior implements IGLTFExporterExtensionV2 { | ||
/** Name of this extension */ | ||
public readonly name = NAME; | ||
|
||
/** Defines whether this extension is enabled */ | ||
public enabled = true; | ||
|
||
/** Defines whether this extension is required */ | ||
public required = false; | ||
|
||
private _wasUsed = false; | ||
|
||
constructor() {} | ||
|
||
public dispose() {} | ||
|
||
/** @hidden */ | ||
public get wasUsed() { | ||
return this._wasUsed; | ||
} | ||
|
||
private _isExtensionEnabled(mat: PBRMaterial): boolean { | ||
// This extension must not be used on a material that also uses KHR_materials_unlit | ||
if (mat.unlit) { | ||
return false; | ||
} | ||
return mat.indexOfRefraction != undefined && mat.indexOfRefraction != 1.5; // 1.5 is normative default value. | ||
} | ||
|
||
public postExportMaterialAsync?(context: string, node: IMaterial, babylonMaterial: Material): Promise<IMaterial> { | ||
return new Promise((resolve) => { | ||
if (babylonMaterial instanceof PBRMaterial && this._isExtensionEnabled(babylonMaterial)) { | ||
this._wasUsed = true; | ||
|
||
const iorInfo: IKHRMaterialsIor = { | ||
ior: babylonMaterial.indexOfRefraction, | ||
}; | ||
node.extensions = node.extensions || {}; | ||
node.extensions[NAME] = iorInfo; | ||
} | ||
resolve(node); | ||
}); | ||
} | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
_Exporter.RegisterExtension(NAME, (exporter) => new KHR_materials_ior()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
packages/dev/serializers/src/glTF/2.0/Extensions/KHR_materials_transmission.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import type { IMaterial, IKHRMaterialsTransmission } from "babylonjs-gltf2interface"; | ||
import type { IGLTFExporterExtensionV2 } from "../glTFExporterExtension"; | ||
import { _Exporter } from "../glTFExporter"; | ||
import type { Material } from "core/Materials/material"; | ||
import { PBRMaterial } from "core/Materials/PBR/pbrMaterial"; | ||
import type { BaseTexture } from "core/Materials/Textures/baseTexture"; | ||
|
||
const NAME = "KHR_materials_transmission"; | ||
|
||
/** | ||
* [Specification](https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_transmission/README.md) | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
export class KHR_materials_transmission implements IGLTFExporterExtensionV2 { | ||
/** Name of this extension */ | ||
public readonly name = NAME; | ||
|
||
/** Defines whether this extension is enabled */ | ||
public enabled = true; | ||
|
||
/** Defines whether this extension is required */ | ||
public required = false; | ||
|
||
private _exporter: _Exporter; | ||
|
||
private _wasUsed = false; | ||
|
||
constructor(exporter: _Exporter) { | ||
this._exporter = exporter; | ||
} | ||
|
||
public dispose() {} | ||
|
||
/** @hidden */ | ||
public get wasUsed() { | ||
return this._wasUsed; | ||
} | ||
|
||
public postExportMaterialAdditionalTextures?(context: string, node: IMaterial, babylonMaterial: Material): BaseTexture[] { | ||
const additionalTextures: BaseTexture[] = []; | ||
|
||
if (babylonMaterial instanceof PBRMaterial) { | ||
if (this._isExtensionEnabled(babylonMaterial)) { | ||
if (babylonMaterial.subSurface.thicknessTexture) { | ||
additionalTextures.push(babylonMaterial.subSurface.thicknessTexture); | ||
} | ||
return additionalTextures; | ||
} | ||
} | ||
|
||
return additionalTextures; | ||
} | ||
|
||
private _isExtensionEnabled(mat: PBRMaterial): boolean { | ||
// This extension must not be used on a material that also uses KHR_materials_unlit | ||
if (mat.unlit) { | ||
return false; | ||
} | ||
const subs = mat.subSurface; | ||
return (subs.isRefractionEnabled && subs.refractionIntensity != undefined && subs.refractionIntensity != 0) || this._hasTexturesExtension(mat); | ||
} | ||
|
||
private _hasTexturesExtension(mat: PBRMaterial): boolean { | ||
return mat.subSurface.refractionIntensityTexture != null; | ||
} | ||
|
||
public postExportMaterialAsync?(context: string, node: IMaterial, babylonMaterial: Material): Promise<IMaterial> { | ||
return new Promise((resolve) => { | ||
if (babylonMaterial instanceof PBRMaterial && this._isExtensionEnabled(babylonMaterial)) { | ||
this._wasUsed = true; | ||
|
||
const subs = babylonMaterial.subSurface; | ||
const transmissionFactor = subs.refractionIntensity === 0 ? undefined : subs.refractionIntensity; | ||
|
||
const transmissionTexture = this._exporter._glTFMaterialExporter._getTextureInfo(subs.refractionIntensityTexture) ?? undefined; | ||
|
||
const volumeInfo: IKHRMaterialsTransmission = { | ||
transmissionFactor: transmissionFactor, | ||
transmissionTexture: transmissionTexture, | ||
hasTextures: () => { | ||
return this._hasTexturesExtension(babylonMaterial); | ||
}, | ||
}; | ||
node.extensions = node.extensions || {}; | ||
node.extensions[NAME] = volumeInfo; | ||
} | ||
resolve(node); | ||
}); | ||
} | ||
} | ||
|
||
_Exporter.RegisterExtension(NAME, (exporter) => new KHR_materials_transmission(exporter)); |
105 changes: 105 additions & 0 deletions
105
packages/dev/serializers/src/glTF/2.0/Extensions/KHR_materials_volume.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import type { IMaterial, IKHRMaterialsVolume } from "babylonjs-gltf2interface"; | ||
import type { IGLTFExporterExtensionV2 } from "../glTFExporterExtension"; | ||
import { _Exporter } from "../glTFExporter"; | ||
import type { Material } from "core/Materials/material"; | ||
import { PBRMaterial } from "core/Materials/PBR/pbrMaterial"; | ||
import type { BaseTexture } from "core/Materials/Textures/baseTexture"; | ||
import { Color3 } from "core/Maths/math.color"; | ||
|
||
const NAME = "KHR_materials_volume"; | ||
|
||
/** | ||
* [Specification](https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_volume/README.md) | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
export class KHR_materials_volume implements IGLTFExporterExtensionV2 { | ||
/** Name of this extension */ | ||
public readonly name = NAME; | ||
|
||
/** Defines whether this extension is enabled */ | ||
public enabled = true; | ||
|
||
/** Defines whether this extension is required */ | ||
public required = false; | ||
|
||
private _exporter: _Exporter; | ||
|
||
private _wasUsed = false; | ||
|
||
constructor(exporter: _Exporter) { | ||
this._exporter = exporter; | ||
} | ||
|
||
public dispose() {} | ||
|
||
/** @hidden */ | ||
public get wasUsed() { | ||
return this._wasUsed; | ||
} | ||
|
||
public postExportMaterialAdditionalTextures?(context: string, node: IMaterial, babylonMaterial: Material): BaseTexture[] { | ||
const additionalTextures: BaseTexture[] = []; | ||
|
||
if (babylonMaterial instanceof PBRMaterial) { | ||
if (this._isExtensionEnabled(babylonMaterial)) { | ||
if (babylonMaterial.subSurface.thicknessTexture) { | ||
additionalTextures.push(babylonMaterial.subSurface.thicknessTexture); | ||
} | ||
return additionalTextures; | ||
} | ||
} | ||
|
||
return additionalTextures; | ||
} | ||
|
||
private _isExtensionEnabled(mat: PBRMaterial): boolean { | ||
// This extension must not be used on a material that also uses KHR_materials_unlit | ||
if (mat.unlit) { | ||
return false; | ||
} | ||
const subs = mat.subSurface; | ||
// this extension requires either the KHR_materials_transmission or KHR_materials_translucency extensions. | ||
if (!subs.isRefractionEnabled && !subs.isTranslucencyEnabled) { | ||
return false; | ||
} | ||
return ( | ||
(subs.maximumThickness != undefined && subs.maximumThickness != 0) || | ||
(subs.tintColorAtDistance != undefined && subs.tintColorAtDistance != Number.POSITIVE_INFINITY) || | ||
(subs.tintColor != undefined && subs.tintColor != Color3.White()) || | ||
this._hasTexturesExtension(mat) | ||
); | ||
} | ||
|
||
private _hasTexturesExtension(mat: PBRMaterial): boolean { | ||
return mat.subSurface.thicknessTexture != null; | ||
} | ||
|
||
public postExportMaterialAsync?(context: string, node: IMaterial, babylonMaterial: Material): Promise<IMaterial> { | ||
return new Promise((resolve) => { | ||
if (babylonMaterial instanceof PBRMaterial && this._isExtensionEnabled(babylonMaterial)) { | ||
this._wasUsed = true; | ||
|
||
const subs = babylonMaterial.subSurface; | ||
const thicknessFactor = subs.maximumThickness == 0 ? undefined : subs.maximumThickness; | ||
const thicknessTexture = this._exporter._glTFMaterialExporter._getTextureInfo(subs.thicknessTexture) ?? undefined; | ||
const attenuationDistance = subs.tintColorAtDistance == Number.POSITIVE_INFINITY ? undefined : subs.tintColorAtDistance; | ||
const attenuationColor = subs.tintColor.equalsFloats(1.0, 1.0, 1.0) ? undefined : subs.tintColor.asArray(); | ||
|
||
const volumeInfo: IKHRMaterialsVolume = { | ||
thicknessFactor: thicknessFactor, | ||
thicknessTexture: thicknessTexture, | ||
attenuationDistance: attenuationDistance, | ||
attenuationColor: attenuationColor, | ||
hasTextures: () => { | ||
return this._hasTexturesExtension(babylonMaterial); | ||
}, | ||
}; | ||
node.extensions = node.extensions || {}; | ||
node.extensions[NAME] = volumeInfo; | ||
} | ||
resolve(node); | ||
}); | ||
} | ||
} | ||
|
||
_Exporter.RegisterExtension(NAME, (exporter) => new KHR_materials_volume(exporter)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.