@@ -22,8 +22,9 @@ import { DumpTools } from "core/Misc/dumpTools";
22
22
23
23
import type { Material } from "core/Materials/material" ;
24
24
import type { StandardMaterial } from "core/Materials/standardMaterial" ;
25
- import type { PBRBaseMaterial } from "core/Materials/PBR/pbrBaseMaterial" ;
25
+ import { PBRBaseMaterial } from "core/Materials/PBR/pbrBaseMaterial" ;
26
26
import { SpecularPowerToRoughness } from "core/Helpers/materialConversionHelper" ;
27
+ import type { OpenPBRMaterial } from "core/Materials/PBR/openPbrMaterial" ;
27
28
28
29
const Epsilon = 1e-6 ;
29
30
const DielectricSpecular = new Color3 ( 0.04 , 0.04 , 0.04 ) ;
@@ -524,41 +525,49 @@ export class GLTFMaterialExporter {
524
525
525
526
/**
526
527
* Convert a PBRMaterial (Metallic/Roughness) to Metallic Roughness factors
528
+ * @param baseColor Base color of the material
529
+ * @param metallic Metallic factor of the material
530
+ * @param roughness Roughness factor of the material
531
+ * @param albedoTexture Albedo texture of the material
532
+ * @param metallicRoughnessTexture Metallic roughness texture of the material
527
533
* @param babylonPBRMaterial BJS PBR Metallic Roughness Material
528
534
* @param mimeType mime type to use for the textures
529
535
* @param glTFPbrMetallicRoughness glTF PBR Metallic Roughness interface
530
536
* @param hasUVs specifies if texture coordinates are present on the submesh to determine if textures should be applied
531
537
* @returns glTF PBR Metallic Roughness factors
532
538
*/
533
539
private async _convertMetalRoughFactorsToMetallicRoughnessAsync (
534
- babylonPBRMaterial : PBRBaseMaterial ,
540
+ baseColor : Color3 ,
541
+ metallic : Nullable < number > ,
542
+ roughness : Nullable < number > ,
543
+ albedoTexture : Nullable < BaseTexture > ,
544
+ metallicRoughnessTexture : Nullable < BaseTexture > ,
545
+ babylonPBRMaterial : PBRBaseMaterial | OpenPBRMaterial ,
535
546
mimeType : ImageMimeType ,
536
547
glTFPbrMetallicRoughness : IMaterialPbrMetallicRoughness ,
537
548
hasUVs : boolean
538
549
) : Promise < IPBRMetallicRoughness > {
539
550
const promises : Promise < void > [ ] = [ ] ;
540
551
541
552
const metallicRoughness : IPBRMetallicRoughness = {
542
- baseColor : babylonPBRMaterial . _albedoColor ,
543
- metallic : babylonPBRMaterial . _metallic ,
544
- roughness : babylonPBRMaterial . _roughness ,
553
+ baseColor : baseColor ,
554
+ metallic : metallic ,
555
+ roughness : roughness ,
545
556
} ;
546
557
547
558
if ( hasUVs ) {
548
- const albedoTexture = babylonPBRMaterial . _albedoTexture ;
549
559
if ( albedoTexture ) {
550
560
promises . push (
551
- this . exportTextureAsync ( babylonPBRMaterial . _albedoTexture ! , mimeType ) . then ( ( glTFTexture ) => {
561
+ this . exportTextureAsync ( albedoTexture , mimeType ) . then ( ( glTFTexture ) => {
552
562
if ( glTFTexture ) {
553
563
glTFPbrMetallicRoughness . baseColorTexture = glTFTexture ;
554
564
}
555
565
} )
556
566
) ;
557
567
}
558
- const metallicTexture = babylonPBRMaterial . _metallicTexture ;
559
- if ( metallicTexture ) {
568
+ if ( metallicRoughnessTexture ) {
560
569
promises . push (
561
- this . exportTextureAsync ( metallicTexture , mimeType ) . then ( ( glTFTexture ) => {
570
+ this . exportTextureAsync ( metallicRoughnessTexture , mimeType ) . then ( ( glTFTexture ) => {
562
571
if ( glTFTexture ) {
563
572
glTFPbrMetallicRoughness . metallicRoughnessTexture = glTFTexture ;
564
573
}
@@ -744,7 +753,17 @@ export class GLTFMaterialExporter {
744
753
}
745
754
746
755
const metallicRoughness = useMetallicRoughness
747
- ? await this . _convertMetalRoughFactorsToMetallicRoughnessAsync ( babylonPBRMaterial , mimeType , glTFPbrMetallicRoughness , hasUVs )
756
+ ? await this . _convertMetalRoughFactorsToMetallicRoughnessAsync (
757
+ babylonPBRMaterial . _albedoColor ,
758
+ babylonPBRMaterial . _metallic ,
759
+ babylonPBRMaterial . _roughness ,
760
+ babylonPBRMaterial . _albedoTexture ,
761
+ babylonPBRMaterial . _metallicTexture ,
762
+ babylonPBRMaterial ,
763
+ mimeType ,
764
+ glTFPbrMetallicRoughness ,
765
+ hasUVs
766
+ )
748
767
: await this . _convertSpecGlossFactorsToMetallicRoughnessAsync ( babylonPBRMaterial , mimeType , glTFPbrMetallicRoughness , hasUVs ) ;
749
768
750
769
await this . _setMetallicRoughnessPbrMaterialAsync ( metallicRoughness , babylonPBRMaterial , glTFMaterial , glTFPbrMetallicRoughness , mimeType , hasUVs ) ;
@@ -757,7 +776,7 @@ export class GLTFMaterialExporter {
757
776
758
777
private async _setMetallicRoughnessPbrMaterialAsync (
759
778
metallicRoughness : IPBRMetallicRoughness ,
760
- babylonPBRMaterial : PBRBaseMaterial ,
779
+ babylonPBRMaterial : PBRBaseMaterial | OpenPBRMaterial ,
761
780
glTFMaterial : IMaterial ,
762
781
glTFPbrMetallicRoughness : IMaterialPbrMetallicRoughness ,
763
782
mimeType : ImageMimeType ,
@@ -786,7 +805,7 @@ export class GLTFMaterialExporter {
786
805
if ( hasUVs ) {
787
806
const promises : Promise < void > [ ] = [ ] ;
788
807
789
- const bumpTexture = babylonPBRMaterial . _bumpTexture ;
808
+ const bumpTexture = babylonPBRMaterial instanceof PBRBaseMaterial ? babylonPBRMaterial . _bumpTexture : babylonPBRMaterial . geometryNormalTexture ;
790
809
if ( bumpTexture ) {
791
810
promises . push (
792
811
this . exportTextureAsync ( bumpTexture , mimeType ) . then ( ( glTFTexture ) => {
@@ -800,7 +819,7 @@ export class GLTFMaterialExporter {
800
819
) ;
801
820
}
802
821
803
- const ambientTexture = babylonPBRMaterial . _ambientTexture ;
822
+ const ambientTexture = babylonPBRMaterial instanceof PBRBaseMaterial ? babylonPBRMaterial . _ambientTexture : babylonPBRMaterial . ambientOcclusionTexture ;
804
823
if ( ambientTexture ) {
805
824
promises . push (
806
825
this . exportTextureAsync ( ambientTexture , mimeType ) . then ( ( glTFTexture ) => {
@@ -812,7 +831,8 @@ export class GLTFMaterialExporter {
812
831
} ;
813
832
814
833
glTFMaterial . occlusionTexture = occlusionTexture ;
815
- const ambientTextureStrength = babylonPBRMaterial . _ambientTextureStrength ;
834
+ const ambientTextureStrength =
835
+ babylonPBRMaterial instanceof PBRBaseMaterial ? babylonPBRMaterial . _ambientTextureStrength : babylonPBRMaterial . ambientOcclusionTexture . level ;
816
836
if ( ambientTextureStrength ) {
817
837
occlusionTexture . strength = ambientTextureStrength ;
818
838
}
@@ -821,7 +841,7 @@ export class GLTFMaterialExporter {
821
841
) ;
822
842
}
823
843
824
- const emissiveTexture = babylonPBRMaterial . _emissiveTexture ;
844
+ const emissiveTexture = babylonPBRMaterial instanceof PBRBaseMaterial ? babylonPBRMaterial . _emissiveTexture : babylonPBRMaterial . emissionColorTexture ;
825
845
if ( emissiveTexture ) {
826
846
promises . push (
827
847
this . exportTextureAsync ( emissiveTexture , mimeType ) . then ( ( glTFTexture ) => {
@@ -838,14 +858,47 @@ export class GLTFMaterialExporter {
838
858
}
839
859
}
840
860
841
- const emissiveColor = babylonPBRMaterial . _emissiveColor ;
861
+ const emissiveColor = babylonPBRMaterial instanceof PBRBaseMaterial ? babylonPBRMaterial . _emissiveColor : babylonPBRMaterial . emissionColor ;
842
862
if ( ! emissiveColor . equalsWithEpsilon ( Black , Epsilon ) ) {
843
863
glTFMaterial . emissiveFactor = emissiveColor . asArray ( ) ;
844
864
}
845
865
846
866
glTFMaterial . pbrMetallicRoughness = glTFPbrMetallicRoughness ;
847
867
}
848
868
869
+ public async exportOpenPBRMaterialAsync ( babylonOpenPBRMaterial : OpenPBRMaterial , mimeType : ImageMimeType , hasUVs : boolean ) : Promise < number > {
870
+ const glTFPbrMetallicRoughness : IMaterialPbrMetallicRoughness = { } ;
871
+
872
+ const glTFMaterial : IMaterial = {
873
+ name : babylonOpenPBRMaterial . name ,
874
+ } ;
875
+
876
+ const albedoColor = babylonOpenPBRMaterial . baseColor ;
877
+ const alpha = babylonOpenPBRMaterial . geometryOpacity ;
878
+ if ( albedoColor ) {
879
+ glTFPbrMetallicRoughness . baseColorFactor = [ albedoColor . r , albedoColor . g , albedoColor . b , alpha ] ;
880
+ }
881
+
882
+ const metallicRoughness = await this . _convertMetalRoughFactorsToMetallicRoughnessAsync (
883
+ babylonOpenPBRMaterial . baseColor ,
884
+ babylonOpenPBRMaterial . baseMetalness ,
885
+ babylonOpenPBRMaterial . specularRoughness ,
886
+ babylonOpenPBRMaterial . baseColorTexture ,
887
+ babylonOpenPBRMaterial . baseMetalRoughTexture ,
888
+ babylonOpenPBRMaterial ,
889
+ mimeType ,
890
+ glTFPbrMetallicRoughness ,
891
+ hasUVs
892
+ ) ;
893
+
894
+ await this . _setMetallicRoughnessPbrMaterialAsync ( metallicRoughness , babylonOpenPBRMaterial , glTFMaterial , glTFPbrMetallicRoughness , mimeType , hasUVs ) ;
895
+ await this . _finishMaterialAsync ( glTFMaterial , babylonOpenPBRMaterial , mimeType ) ;
896
+
897
+ const materials = this . _exporter . _materials ;
898
+ materials . push ( glTFMaterial ) ;
899
+ return materials . length - 1 ;
900
+ }
901
+
849
902
public async exportTextureAsync ( babylonTexture : BaseTexture , mimeType : ImageMimeType ) : Promise < Nullable < ITextureInfo > > {
850
903
const extensionPromise = this . _exporter . _extensionsPreExportTextureAsync ( "exporter" , babylonTexture as Texture , mimeType ) ;
851
904
if ( ! extensionPromise ) {
0 commit comments