@@ -232,119 +232,147 @@ private void ExportMaterial(BabylonMaterial babylonMaterial, GLTF gltf)
232232 if ( babylonTexture != null )
233233 {
234234 //Check if the texture already exist
235- var _key = SetStandText ( babylonStandardMaterial ) ;
236-
237- bool isAlphaInTexture = ( isTextureOk ( babylonStandardMaterial . diffuseTexture ) && babylonStandardMaterial . diffuseTexture . hasAlpha ) ||
238- isTextureOk ( babylonStandardMaterial . opacityTexture ) ;
239-
240- Bitmap baseColorBitmap = null ;
241- Bitmap metallicRoughnessBitmap = null ;
242-
243- GLTFTextureInfo textureInfoBC = new GLTFTextureInfo ( ) ;
244- GLTFTextureInfo textureInfoMR = new GLTFTextureInfo ( ) ;
245-
246- if ( exportParameters . writeTextures )
235+ var _key = SetStandText ( babylonStandardMaterial ) ;
236+
237+ if ( GetStandTextInfo ( _key ) != null )
238+ {
239+ var _pairBCMR = GetStandTextInfo ( _key ) ;
240+ gltfPbrMetallicRoughness . baseColorTexture = _pairBCMR . baseColor ;
241+ gltfPbrMetallicRoughness . metallicRoughnessTexture = _pairBCMR . metallicRoughness ;
242+ }
243+ else
247244 {
248- // Diffuse
249- Bitmap diffuseBitmap = null ;
250- if ( babylonStandardMaterial . diffuseTexture != null )
251- {
252- diffuseBitmap = TextureUtilities . LoadTexture ( babylonStandardMaterial . diffuseTexture . originalPath , logger ) ;
253- }
254245
255- // Specular
256- Bitmap specularBitmap = null ;
257- if ( babylonStandardMaterial . specularTexture != null )
258- {
259- if ( babylonStandardMaterial . specularTexture . bitmap != null )
260- {
261- // Specular color map has been computed by the exporter
262- specularBitmap = babylonStandardMaterial . specularTexture . bitmap ;
263- }
264- else
265- {
266- // Specular color map is straight input
267- specularBitmap = TextureUtilities . LoadTexture ( babylonStandardMaterial . specularTexture . originalPath , logger ) ;
268- }
269- }
246+ bool isAlphaInTexture = ( isTextureOk ( babylonStandardMaterial . diffuseTexture ) && babylonStandardMaterial . diffuseTexture . hasAlpha ) ||
247+ isTextureOk ( babylonStandardMaterial . opacityTexture ) ;
270248
271- // Opacity / Alpha / Transparency
272- Bitmap opacityBitmap = null ;
273- if ( ( babylonStandardMaterial . diffuseTexture == null || babylonStandardMaterial . diffuseTexture . hasAlpha == false ) && babylonStandardMaterial . opacityTexture != null )
274- {
275- opacityBitmap = TextureUtilities . LoadTexture ( babylonStandardMaterial . opacityTexture . originalPath , logger ) ;
276- }
249+ Bitmap baseColorBitmap = null ;
250+ Bitmap metallicRoughnessBitmap = null ;
277251
278- if ( diffuseBitmap != null || specularBitmap != null || opacityBitmap != null )
252+ GLTFTextureInfo textureInfoBC = new GLTFTextureInfo ( ) ;
253+ GLTFTextureInfo textureInfoMR = new GLTFTextureInfo ( ) ;
254+
255+ if ( exportParameters . writeTextures )
279256 {
280- // Retreive dimensions
281- int width = 0 ;
282- int height = 0 ;
283- var haveSameDimensions = TextureUtilities . GetMinimalBitmapDimensions ( out width , out height , diffuseBitmap , specularBitmap , opacityBitmap ) ;
284- if ( ! haveSameDimensions )
257+ // Diffuse
258+ Bitmap diffuseBitmap = null ;
259+ if ( babylonStandardMaterial . diffuseTexture != null )
260+ {
261+ diffuseBitmap = TextureUtilities . LoadTexture ( babylonStandardMaterial . diffuseTexture . originalPath , logger ) ;
262+ }
263+
264+ // Specular
265+ Bitmap specularBitmap = null ;
266+ if ( babylonStandardMaterial . specularTexture != null )
267+ {
268+ if ( babylonStandardMaterial . specularTexture . bitmap != null )
269+ {
270+ // Specular color map has been computed by the exporter
271+ specularBitmap = babylonStandardMaterial . specularTexture . bitmap ;
272+ }
273+ else
274+ {
275+ // Specular color map is straight input
276+ specularBitmap = TextureUtilities . LoadTexture ( babylonStandardMaterial . specularTexture . originalPath , logger ) ;
277+ }
278+ }
279+
280+ // Opacity / Alpha / Transparency
281+ Bitmap opacityBitmap = null ;
282+ if ( ( babylonStandardMaterial . diffuseTexture == null || babylonStandardMaterial . diffuseTexture . hasAlpha == false ) && babylonStandardMaterial . opacityTexture != null )
285283 {
286- logger . RaiseError ( "Diffuse, specular and opacity maps should have same dimensions" , 2 ) ;
284+ opacityBitmap = TextureUtilities . LoadTexture ( babylonStandardMaterial . opacityTexture . originalPath , logger ) ;
287285 }
288286
289- // Create baseColor+alpha and metallic+roughness maps
290- baseColorBitmap = new Bitmap ( width , height ) ;
291- metallicRoughnessBitmap = new Bitmap ( width , height ) ;
292- for ( int x = 0 ; x < width ; x ++ )
287+ if ( diffuseBitmap != null || specularBitmap != null || opacityBitmap != null )
293288 {
294- for ( int y = 0 ; y < height ; y ++ )
289+ // Retreive dimensions
290+ int width = 0 ;
291+ int height = 0 ;
292+ var haveSameDimensions = TextureUtilities . GetMinimalBitmapDimensions ( out width , out height , diffuseBitmap , specularBitmap , opacityBitmap ) ;
293+ if ( ! haveSameDimensions )
294+ {
295+ logger . RaiseError ( "Diffuse, specular and opacity maps should have same dimensions" , 2 ) ;
296+ }
297+
298+ // Create baseColor+alpha and metallic+roughness maps
299+ baseColorBitmap = new Bitmap ( width , height ) ;
300+ metallicRoughnessBitmap = new Bitmap ( width , height ) ;
301+ for ( int x = 0 ; x < width ; x ++ )
295302 {
296- SpecularGlossiness specularGlossinessTexture = new SpecularGlossiness
303+ for ( int y = 0 ; y < height ; y ++ )
297304 {
298- diffuse = diffuseBitmap != null ? new BabylonColor3 ( diffuseBitmap . GetPixel ( x , y ) ) :
299- _specularGlossiness . diffuse ,
300- opacity = diffuseBitmap != null && babylonStandardMaterial . diffuseTexture . hasAlpha ? diffuseBitmap . GetPixel ( x , y ) . A / 255.0f :
301- opacityBitmap != null && babylonStandardMaterial . opacityTexture . getAlphaFromRGB ? opacityBitmap . GetPixel ( x , y ) . R / 255.0f :
302- opacityBitmap != null && babylonStandardMaterial . opacityTexture . getAlphaFromRGB == false ? opacityBitmap . GetPixel ( x , y ) . A / 255.0f :
303- _specularGlossiness . opacity ,
304- specular = specularBitmap != null ? new BabylonColor3 ( specularBitmap . GetPixel ( x , y ) ) :
305- _specularGlossiness . specular ,
306- glossiness = babylonStandardMaterial . useGlossinessFromSpecularMapAlpha && specularBitmap != null ? specularBitmap . GetPixel ( x , y ) . A / 255.0f :
307- _specularGlossiness . glossiness
308- } ;
305+ SpecularGlossiness specularGlossinessTexture = new SpecularGlossiness
306+ {
307+ diffuse = diffuseBitmap != null ? new BabylonColor3 ( diffuseBitmap . GetPixel ( x , y ) ) :
308+ _specularGlossiness . diffuse ,
309+ opacity = diffuseBitmap != null && babylonStandardMaterial . diffuseTexture . hasAlpha ? diffuseBitmap . GetPixel ( x , y ) . A / 255.0f :
310+ opacityBitmap != null && babylonStandardMaterial . opacityTexture . getAlphaFromRGB ? opacityBitmap . GetPixel ( x , y ) . R / 255.0f :
311+ opacityBitmap != null && babylonStandardMaterial . opacityTexture . getAlphaFromRGB == false ? opacityBitmap . GetPixel ( x , y ) . A / 255.0f :
312+ _specularGlossiness . opacity ,
313+ specular = specularBitmap != null ? new BabylonColor3 ( specularBitmap . GetPixel ( x , y ) ) :
314+ _specularGlossiness . specular ,
315+ glossiness = babylonStandardMaterial . useGlossinessFromSpecularMapAlpha && specularBitmap != null ? specularBitmap . GetPixel ( x , y ) . A / 255.0f :
316+ _specularGlossiness . glossiness
317+ } ;
309318
310- var displayPrints = x == width / 2 && y == height / 2 ;
311- MetallicRoughness metallicRoughnessTexture = ConvertToMetallicRoughness ( specularGlossinessTexture , displayPrints ) ;
319+ var displayPrints = x == width / 2 && y == height / 2 ;
320+ MetallicRoughness metallicRoughnessTexture = ConvertToMetallicRoughness ( specularGlossinessTexture , displayPrints ) ;
312321
313- Color colorBase = Color . FromArgb (
314- ( int ) ( metallicRoughnessTexture . opacity * 255 ) ,
315- ( int ) ( metallicRoughnessTexture . baseColor . r * 255 ) ,
316- ( int ) ( metallicRoughnessTexture . baseColor . g * 255 ) ,
317- ( int ) ( metallicRoughnessTexture . baseColor . b * 255 )
318- ) ;
319- baseColorBitmap . SetPixel ( x , y , colorBase ) ;
322+ Color colorBase = Color . FromArgb (
323+ ( int ) ( metallicRoughnessTexture . opacity * 255 ) ,
324+ ( int ) ( metallicRoughnessTexture . baseColor . r * 255 ) ,
325+ ( int ) ( metallicRoughnessTexture . baseColor . g * 255 ) ,
326+ ( int ) ( metallicRoughnessTexture . baseColor . b * 255 )
327+ ) ;
328+ baseColorBitmap . SetPixel ( x , y , colorBase ) ;
320329
321- // The metalness values are sampled from the B channel.
322- // The roughness values are sampled from the G channel.
323- // These values are linear. If other channels are present (R or A), they are ignored for metallic-roughness calculations.
324- Color colorMetallicRoughness = Color . FromArgb (
325- 0 ,
326- ( int ) ( metallicRoughnessTexture . roughness * 255 ) ,
327- ( int ) ( metallicRoughnessTexture . metallic * 255 )
328- ) ;
329- metallicRoughnessBitmap . SetPixel ( x , y , colorMetallicRoughness ) ;
330- }
331- }
332- }
333- }
334-
335- //export textures
336- textureInfoBC = ExportBitmapTexture ( gltf , babylonTexture , baseColorBitmap , babylonTexture . name ) ;
337- gltfPbrMetallicRoughness . baseColorTexture = textureInfoBC ;
338-
339- // If no specular map is defined, the metallic and roughness values are be driven by the global parameters
340- if ( babylonStandardMaterial . specularTexture != null )
341- {
342- textureInfoMR = ExportBitmapTexture ( gltf , babylonTexture , metallicRoughnessBitmap , babylonMaterial . name + "_metallicRoughness" + ".jpg" ) ;
343- gltfPbrMetallicRoughness . metallicRoughnessTexture = textureInfoMR ;
330+ // The metalness values are sampled from the B channel.
331+ // The roughness values are sampled from the G channel.
332+ // These values are linear. If other channels are present (R or A), they are ignored for metallic-roughness calculations.
333+ Color colorMetallicRoughness = Color . FromArgb (
334+ 0 ,
335+ ( int ) ( metallicRoughnessTexture . roughness * 255 ) ,
336+ ( int ) ( metallicRoughnessTexture . metallic * 255 )
337+ ) ;
338+ metallicRoughnessBitmap . SetPixel ( x , y , colorMetallicRoughness ) ;
339+ }
340+ }
341+ }
342+ }
343+
344+ //export textures
345+ if ( baseColorBitmap != null || babylonTexture . bitmap != null )
346+ {
347+ var baseColorFileName = babylonMaterial . name + "_baseColor" + ( isAlphaInTexture ? ".png" : ".jpg" ) ;
348+ baseColorFileName = baseColorFileName . Replace ( ":" , "_" ) ;
349+ textureInfoBC = ExportBitmapTexture ( gltf , babylonTexture , baseColorBitmap , baseColorFileName ) ;
350+ gltfPbrMetallicRoughness . baseColorTexture = textureInfoBC ;
351+ }
352+
353+ if ( isTextureOk ( babylonStandardMaterial . specularTexture ) )
354+ {
355+ var metallicRoughnessFileName = babylonMaterial . name + "_metallicRoughness" + ".jpg" ;
356+ metallicRoughnessFileName = metallicRoughnessFileName . Replace ( ":" , "_" ) ;
357+ textureInfoMR = ExportBitmapTexture ( gltf , babylonTexture , metallicRoughnessBitmap , metallicRoughnessFileName ) ;
358+ gltfPbrMetallicRoughness . metallicRoughnessTexture = textureInfoMR ;
359+ }
360+
361+ //register the texture
362+ AddStandText ( _key , textureInfoBC , textureInfoMR ) ;
363+ }
364+
365+ // Constraints
366+ if ( gltfPbrMetallicRoughness . baseColorTexture != null )
367+ {
368+ gltfPbrMetallicRoughness . baseColorFactor = new [ ] { 1.0f , 1.0f , 1.0f , 1.0f } ;
369+ }
370+
371+ if ( gltfPbrMetallicRoughness . metallicRoughnessTexture != null )
372+ {
373+ gltfPbrMetallicRoughness . metallicFactor = 1.0f ;
374+ gltfPbrMetallicRoughness . roughnessFactor = 1.0f ;
344375 }
345-
346- //register the texture
347- AddStandText ( _key , textureInfoBC , textureInfoMR ) ;
348376 }
349377 }
350378 else if ( babylonMaterial . GetType ( ) == typeof ( BabylonPBRMetallicRoughnessMaterial ) )
0 commit comments