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