1
1
import type { Nullable } from "core/types" ;
2
- import { PBRMaterial } from "core/Materials/PBR/pbrMaterial" ;
2
+ import type { PBRMaterial } from "core/Materials/PBR/pbrMaterial" ;
3
+ import type { OpenPBRMaterial } from "core/Materials/PBR/openPbrMaterial" ;
3
4
import type { Material } from "core/Materials/material" ;
5
+ import type { BaseTexture } from "core/Materials/Textures/baseTexture" ;
4
6
5
7
import type { IMaterial , ITextureInfo } from "../glTFLoaderInterfaces" ;
6
8
import type { IGLTFLoaderExtension } from "../glTFLoaderExtension" ;
@@ -21,6 +23,8 @@ declare module "../../glTFFileLoader" {
21
23
}
22
24
}
23
25
26
+ let PBRMaterialClass : typeof PBRMaterial | typeof OpenPBRMaterial ;
27
+
24
28
/**
25
29
* [Specification](https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_clearcoat/README.md)
26
30
* [Playground Sample](https://www.babylonjs-playground.com/frame.html#7F7PN6#8)
@@ -61,54 +65,66 @@ export class KHR_materials_clearcoat implements IGLTFLoaderExtension {
61
65
* @internal
62
66
*/
63
67
// eslint-disable-next-line no-restricted-syntax
64
- public loadMaterialPropertiesAsync ( context : string , material : IMaterial , babylonMaterial : Material ) : Nullable < Promise < void > > {
68
+ public loadMaterialPropertiesAsync ( context : string , material : IMaterial , babylonMaterial : Material , useOpenPBR : boolean = false ) : Nullable < Promise < void > > {
65
69
return GLTFLoader . LoadExtensionAsync < IKHRMaterialsClearcoat > ( context , material , this . name , async ( extensionContext , extension ) => {
70
+ if ( useOpenPBR ) {
71
+ const mod = await import ( "core/Materials/PBR/openPbrMaterial" ) ;
72
+ PBRMaterialClass = mod . OpenPBRMaterial ;
73
+ } else {
74
+ const mod = await import ( "core/Materials/PBR/pbrMaterial" ) ;
75
+ PBRMaterialClass = mod . PBRMaterial ;
76
+ }
77
+ if ( ! ( babylonMaterial instanceof PBRMaterialClass ) ) {
78
+ throw new Error ( `${ context } : Material type not supported` ) ;
79
+ }
66
80
const promises = new Array < Promise < any > > ( ) ;
67
81
promises . push ( this . _loader . loadMaterialPropertiesAsync ( context , material , babylonMaterial ) ) ;
68
- promises . push ( this . _loadClearCoatPropertiesAsync ( extensionContext , extension , babylonMaterial ) ) ;
82
+ promises . push ( this . _loadClearCoatPropertiesAsync ( extensionContext , extension , babylonMaterial , useOpenPBR ) ) ;
69
83
await Promise . all ( promises ) ;
70
84
} ) ;
71
85
}
72
86
73
87
// eslint-disable-next-line @typescript-eslint/promise-function-async, no-restricted-syntax
74
- private _loadClearCoatPropertiesAsync ( context : string , properties : IKHRMaterialsClearcoat , babylonMaterial : Material ) : Promise < void > {
75
- if ( ! ( babylonMaterial instanceof PBRMaterial ) ) {
88
+ private _loadClearCoatPropertiesAsync ( context : string , properties : IKHRMaterialsClearcoat , babylonMaterial : Material , useOpenPBR : boolean = false ) : Promise < void > {
89
+ if ( ! ( babylonMaterial instanceof PBRMaterialClass ) ) {
76
90
throw new Error ( `${ context } : Material type not supported` ) ;
77
91
}
78
92
79
93
const promises = new Array < Promise < any > > ( ) ;
80
-
81
- babylonMaterial . clearCoat . isEnabled = true ;
82
- babylonMaterial . clearCoat . useRoughnessFromMainTexture = false ;
83
- babylonMaterial . clearCoat . remapF0OnInterfaceChange = false ;
94
+ let coatRoughness = 0 ;
95
+ let coatWeight = 0 ;
96
+ let coatWeightTexture : Nullable < BaseTexture > = null ;
97
+ let coatRoughnessTexture : Nullable < BaseTexture > = null ;
98
+ let coatNormalTexture : Nullable < BaseTexture > = null ;
99
+ let coatNormalTextureScale = 1 ;
84
100
85
101
if ( properties . clearcoatFactor != undefined ) {
86
- babylonMaterial . clearCoat . intensity = properties . clearcoatFactor ;
102
+ coatWeight = properties . clearcoatFactor ;
87
103
} else {
88
- babylonMaterial . clearCoat . intensity = 0 ;
104
+ coatWeight = 0 ;
89
105
}
90
106
91
107
if ( properties . clearcoatTexture ) {
92
108
promises . push (
93
109
this . _loader . loadTextureInfoAsync ( `${ context } /clearcoatTexture` , properties . clearcoatTexture , ( texture ) => {
94
110
texture . name = `${ babylonMaterial . name } (ClearCoat)` ;
95
- babylonMaterial . clearCoat . texture = texture ;
111
+ coatWeightTexture = texture ;
96
112
} )
97
113
) ;
98
114
}
99
115
100
116
if ( properties . clearcoatRoughnessFactor != undefined ) {
101
- babylonMaterial . clearCoat . roughness = properties . clearcoatRoughnessFactor ;
117
+ coatRoughness = properties . clearcoatRoughnessFactor ;
102
118
} else {
103
- babylonMaterial . clearCoat . roughness = 0 ;
119
+ coatRoughness = 0 ;
104
120
}
105
121
106
122
if ( properties . clearcoatRoughnessTexture ) {
107
123
( properties . clearcoatRoughnessTexture as ITextureInfo ) . nonColorData = true ;
108
124
promises . push (
109
125
this . _loader . loadTextureInfoAsync ( `${ context } /clearcoatRoughnessTexture` , properties . clearcoatRoughnessTexture , ( texture ) => {
110
126
texture . name = `${ babylonMaterial . name } (ClearCoat Roughness)` ;
111
- babylonMaterial . clearCoat . textureRoughness = texture ;
127
+ coatRoughnessTexture = texture ;
112
128
} )
113
129
) ;
114
130
}
@@ -118,19 +134,37 @@ export class KHR_materials_clearcoat implements IGLTFLoaderExtension {
118
134
promises . push (
119
135
this . _loader . loadTextureInfoAsync ( `${ context } /clearcoatNormalTexture` , properties . clearcoatNormalTexture , ( texture ) => {
120
136
texture . name = `${ babylonMaterial . name } (ClearCoat Normal)` ;
121
- babylonMaterial . clearCoat . bumpTexture = texture ;
137
+ coatNormalTexture = texture ;
122
138
} )
123
139
) ;
124
140
125
141
babylonMaterial . invertNormalMapX = ! babylonMaterial . getScene ( ) . useRightHandedSystem ;
126
142
babylonMaterial . invertNormalMapY = babylonMaterial . getScene ( ) . useRightHandedSystem ;
127
143
if ( properties . clearcoatNormalTexture . scale != undefined ) {
128
- babylonMaterial . clearCoat . bumpTexture ! . level = properties . clearcoatNormalTexture . scale ;
144
+ coatNormalTextureScale = properties . clearcoatNormalTexture . scale ;
129
145
}
130
146
}
131
147
132
148
// eslint-disable-next-line github/no-then
133
- return Promise . all ( promises ) . then ( ( ) => { } ) ;
149
+ return Promise . all ( promises ) . then ( ( ) => {
150
+ if ( useOpenPBR ) {
151
+ // eslint-disable-next-line github/no-then
152
+ return ;
153
+ }
154
+ const material = babylonMaterial as PBRMaterial ;
155
+ material . clearCoat . isEnabled = true ;
156
+ material . clearCoat . useRoughnessFromMainTexture = false ;
157
+ material . clearCoat . remapF0OnInterfaceChange = false ;
158
+ material . clearCoat . intensity = coatWeight ;
159
+ material . clearCoat . texture = coatWeightTexture ;
160
+ material . clearCoat . roughness = coatRoughness ;
161
+ material . clearCoat . textureRoughness = coatRoughnessTexture ;
162
+
163
+ material . clearCoat . bumpTexture = coatNormalTexture ;
164
+ if ( coatNormalTexture ) {
165
+ material . clearCoat . bumpTexture ! . level = coatNormalTextureScale ;
166
+ }
167
+ } ) ;
134
168
}
135
169
}
136
170
0 commit comments