@@ -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,26 +525,35 @@ 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 glTFPbrMetallicRoughness glTF PBR Metallic Roughness interface
529
535
* @param hasUVs specifies if texture coordinates are present on the submesh to determine if textures should be applied
530
536
* @returns glTF PBR Metallic Roughness factors
531
537
*/
532
538
private async _convertMetalRoughFactorsToMetallicRoughnessAsync (
533
- babylonPBRMaterial : PBRBaseMaterial ,
539
+ baseColor : Color3 ,
540
+ metallic : Nullable < number > ,
541
+ roughness : Nullable < number > ,
542
+ albedoTexture : Nullable < BaseTexture > ,
543
+ metallicRoughnessTexture : Nullable < BaseTexture > ,
544
+ babylonPBRMaterial : PBRBaseMaterial | OpenPBRMaterial ,
534
545
glTFPbrMetallicRoughness : IMaterialPbrMetallicRoughness ,
535
546
hasUVs : boolean
536
547
) : Promise < IPBRMetallicRoughness > {
537
548
const promises : Promise < void > [ ] = [ ] ;
538
549
539
550
const metallicRoughness : IPBRMetallicRoughness = {
540
- baseColor : babylonPBRMaterial . _albedoColor ,
541
- metallic : babylonPBRMaterial . _metallic ,
542
- roughness : babylonPBRMaterial . _roughness ,
551
+ baseColor : baseColor ,
552
+ metallic : metallic ,
553
+ roughness : roughness ,
543
554
} ;
544
555
545
556
if ( hasUVs ) {
546
- const albedoTexture = babylonPBRMaterial . _albedoTexture ;
547
557
if ( albedoTexture ) {
548
558
promises . push (
549
559
this . exportTextureAsync ( albedoTexture ) . then ( ( glTFTexture ) => {
@@ -553,10 +563,9 @@ export class GLTFMaterialExporter {
553
563
} )
554
564
) ;
555
565
}
556
- const metallicTexture = babylonPBRMaterial . _metallicTexture ;
557
- if ( metallicTexture ) {
566
+ if ( metallicRoughnessTexture ) {
558
567
promises . push (
559
- this . exportTextureAsync ( metallicTexture ) . then ( ( glTFTexture ) => {
568
+ this . exportTextureAsync ( metallicRoughnessTexture ) . then ( ( glTFTexture ) => {
560
569
if ( glTFTexture ) {
561
570
glTFPbrMetallicRoughness . metallicRoughnessTexture = glTFTexture ;
562
571
}
@@ -741,7 +750,16 @@ export class GLTFMaterialExporter {
741
750
}
742
751
743
752
const metallicRoughness = useMetallicRoughness
744
- ? await this . _convertMetalRoughFactorsToMetallicRoughnessAsync ( babylonPBRMaterial , glTFPbrMetallicRoughness , hasUVs )
753
+ ? await this . _convertMetalRoughFactorsToMetallicRoughnessAsync (
754
+ babylonPBRMaterial . _albedoColor ,
755
+ babylonPBRMaterial . _metallic ,
756
+ babylonPBRMaterial . _roughness ,
757
+ babylonPBRMaterial . _albedoTexture ,
758
+ babylonPBRMaterial . _metallicTexture ,
759
+ babylonPBRMaterial ,
760
+ glTFPbrMetallicRoughness ,
761
+ hasUVs
762
+ )
745
763
: await this . _convertSpecGlossFactorsToMetallicRoughnessAsync ( babylonPBRMaterial , glTFPbrMetallicRoughness , hasUVs ) ;
746
764
747
765
await this . _setMetallicRoughnessPbrMaterialAsync ( metallicRoughness , babylonPBRMaterial , glTFMaterial , glTFPbrMetallicRoughness , hasUVs ) ;
@@ -754,7 +772,7 @@ export class GLTFMaterialExporter {
754
772
755
773
private async _setMetallicRoughnessPbrMaterialAsync (
756
774
metallicRoughness : IPBRMetallicRoughness ,
757
- babylonPBRMaterial : PBRBaseMaterial ,
775
+ babylonPBRMaterial : PBRBaseMaterial | OpenPBRMaterial ,
758
776
glTFMaterial : IMaterial ,
759
777
glTFPbrMetallicRoughness : IMaterialPbrMetallicRoughness ,
760
778
hasUVs : boolean
@@ -782,7 +800,7 @@ export class GLTFMaterialExporter {
782
800
if ( hasUVs ) {
783
801
const promises : Promise < void > [ ] = [ ] ;
784
802
785
- const bumpTexture = babylonPBRMaterial . _bumpTexture ;
803
+ const bumpTexture = babylonPBRMaterial instanceof PBRBaseMaterial ? babylonPBRMaterial . _bumpTexture : babylonPBRMaterial . geometryNormalTexture ;
786
804
if ( bumpTexture ) {
787
805
promises . push (
788
806
this . exportTextureAsync ( bumpTexture ) . then ( ( glTFTexture ) => {
@@ -796,7 +814,7 @@ export class GLTFMaterialExporter {
796
814
) ;
797
815
}
798
816
799
- const ambientTexture = babylonPBRMaterial . _ambientTexture ;
817
+ const ambientTexture = babylonPBRMaterial instanceof PBRBaseMaterial ? babylonPBRMaterial . _ambientTexture : babylonPBRMaterial . ambientOcclusionTexture ;
800
818
if ( ambientTexture ) {
801
819
promises . push (
802
820
this . exportTextureAsync ( ambientTexture ) . then ( ( glTFTexture ) => {
@@ -808,7 +826,8 @@ export class GLTFMaterialExporter {
808
826
} ;
809
827
810
828
glTFMaterial . occlusionTexture = occlusionTexture ;
811
- const ambientTextureStrength = babylonPBRMaterial . _ambientTextureStrength ;
829
+ const ambientTextureStrength =
830
+ babylonPBRMaterial instanceof PBRBaseMaterial ? babylonPBRMaterial . _ambientTextureStrength : babylonPBRMaterial . ambientOcclusionTexture . level ;
812
831
if ( ambientTextureStrength ) {
813
832
occlusionTexture . strength = ambientTextureStrength ;
814
833
}
@@ -817,7 +836,7 @@ export class GLTFMaterialExporter {
817
836
) ;
818
837
}
819
838
820
- const emissiveTexture = babylonPBRMaterial . _emissiveTexture ;
839
+ const emissiveTexture = babylonPBRMaterial instanceof PBRBaseMaterial ? babylonPBRMaterial . _emissiveTexture : babylonPBRMaterial . emissionColorTexture ;
821
840
if ( emissiveTexture ) {
822
841
promises . push (
823
842
this . exportTextureAsync ( emissiveTexture ) . then ( ( glTFTexture ) => {
@@ -834,14 +853,46 @@ export class GLTFMaterialExporter {
834
853
}
835
854
}
836
855
837
- const emissiveColor = babylonPBRMaterial . _emissiveColor ;
856
+ const emissiveColor = babylonPBRMaterial instanceof PBRBaseMaterial ? babylonPBRMaterial . _emissiveColor : babylonPBRMaterial . emissionColor ;
838
857
if ( ! emissiveColor . equalsWithEpsilon ( Black , Epsilon ) ) {
839
858
glTFMaterial . emissiveFactor = emissiveColor . asArray ( ) ;
840
859
}
841
860
842
861
glTFMaterial . pbrMetallicRoughness = glTFPbrMetallicRoughness ;
843
862
}
844
863
864
+ public async exportOpenPBRMaterialAsync ( babylonOpenPBRMaterial : OpenPBRMaterial , hasUVs : boolean ) : Promise < number > {
865
+ const glTFPbrMetallicRoughness : IMaterialPbrMetallicRoughness = { } ;
866
+
867
+ const glTFMaterial : IMaterial = {
868
+ name : babylonOpenPBRMaterial . name ,
869
+ } ;
870
+
871
+ const albedoColor = babylonOpenPBRMaterial . baseColor ;
872
+ const alpha = babylonOpenPBRMaterial . geometryOpacity ;
873
+ if ( albedoColor ) {
874
+ glTFPbrMetallicRoughness . baseColorFactor = [ albedoColor . r , albedoColor . g , albedoColor . b , alpha ] ;
875
+ }
876
+
877
+ const metallicRoughness = await this . _convertMetalRoughFactorsToMetallicRoughnessAsync (
878
+ babylonOpenPBRMaterial . baseColor ,
879
+ babylonOpenPBRMaterial . baseMetalness ,
880
+ babylonOpenPBRMaterial . specularRoughness ,
881
+ babylonOpenPBRMaterial . baseColorTexture ,
882
+ babylonOpenPBRMaterial . baseMetalRoughTexture ,
883
+ babylonOpenPBRMaterial ,
884
+ glTFPbrMetallicRoughness ,
885
+ hasUVs
886
+ ) ;
887
+
888
+ await this . _setMetallicRoughnessPbrMaterialAsync ( metallicRoughness , babylonOpenPBRMaterial , glTFMaterial , glTFPbrMetallicRoughness , hasUVs ) ;
889
+ await this . _finishMaterialAsync ( glTFMaterial , babylonOpenPBRMaterial ) ;
890
+
891
+ const materials = this . _exporter . _materials ;
892
+ materials . push ( glTFMaterial ) ;
893
+ return materials . length - 1 ;
894
+ }
895
+
845
896
public async exportTextureAsync ( babylonTexture : BaseTexture ) : Promise < Nullable < ITextureInfo > > {
846
897
let textureInfo = this . _textureMap . get ( babylonTexture ) ;
847
898
if ( textureInfo ) {
0 commit comments