@@ -359,6 +359,46 @@ THREE.GLTFExporter.prototype = {
359359
360360 }
361361
362+ /**
363+ * Applies a texture transform, if present, to the map definition. Requires
364+ * the KHR_texture_transform extension.
365+ */
366+ function applyTextureTransform ( mapDef , texture ) {
367+
368+ var didTransform = false
369+ var transformDef = { } ;
370+
371+ if ( texture . offset . x !== 0 || texture . offset . y !== 0 ) {
372+
373+ transformDef . offset = texture . offset . toArray ( ) ;
374+ didTransform = true ;
375+
376+ }
377+
378+ if ( texture . rotation !== 0 ) {
379+
380+ transformDef . rotation = texture . rotation ;
381+ didTransform = true ;
382+
383+ }
384+
385+ if ( texture . repeat . x !== 1 || texture . repeat . y !== 1 ) {
386+
387+ transformDef . scale = texture . repeat . toArray ( ) ;
388+ didTransform = true ;
389+
390+ }
391+
392+ if ( didTransform ) {
393+
394+ mapDef . extensions = mapDef . extensions || { } ;
395+ mapDef . extensions [ 'KHR_texture_transform' ] = transformDef ;
396+ extensionsUsed [ 'KHR_texture_transform' ] = true ;
397+
398+ }
399+
400+ }
401+
362402 /**
363403 * Process a buffer to append to the default one.
364404 * @param {ArrayBuffer } buffer
@@ -868,11 +908,9 @@ THREE.GLTFExporter.prototype = {
868908
869909 if ( material . metalnessMap === material . roughnessMap ) {
870910
871- gltfMaterial . pbrMetallicRoughness . metallicRoughnessTexture = {
872-
873- index : processTexture ( material . metalnessMap )
874-
875- } ;
911+ var metalRoughMapDef = { index : processTexture ( material . metalnessMap ) } ;
912+ applyTextureTransform ( metalRoughMapDef , material . metalnessMap ) ;
913+ gltfMaterial . pbrMetallicRoughness . metallicRoughnessTexture = metalRoughMapDef ;
876914
877915 } else {
878916
@@ -885,11 +923,9 @@ THREE.GLTFExporter.prototype = {
885923 // pbrMetallicRoughness.baseColorTexture
886924 if ( material . map ) {
887925
888- gltfMaterial . pbrMetallicRoughness . baseColorTexture = {
889-
890- index : processTexture ( material . map )
891-
892- } ;
926+ var baseColorMapDef = { index : processTexture ( material . map ) } ;
927+ applyTextureTransform ( baseColorMapDef , material . map ) ;
928+ gltfMaterial . pbrMetallicRoughness . baseColorTexture = baseColorMapDef ;
893929
894930 }
895931
@@ -911,11 +947,9 @@ THREE.GLTFExporter.prototype = {
911947 // emissiveTexture
912948 if ( material . emissiveMap ) {
913949
914- gltfMaterial . emissiveTexture = {
915-
916- index : processTexture ( material . emissiveMap )
917-
918- } ;
950+ var emissiveMapDef = { index : processTexture ( material . emissiveMap ) } ;
951+ applyTextureTransform ( emissiveMapDef , material . emissiveMap ) ;
952+ gltfMaterial . emissiveTexture = emissiveMapDef ;
919953
920954 }
921955
@@ -924,11 +958,7 @@ THREE.GLTFExporter.prototype = {
924958 // normalTexture
925959 if ( material . normalMap ) {
926960
927- gltfMaterial . normalTexture = {
928-
929- index : processTexture ( material . normalMap )
930-
931- } ;
961+ var normalMapDef = { index : processTexture ( material . normalMap ) } ;
932962
933963 if ( material . normalScale . x !== - 1 ) {
934964
@@ -938,27 +968,31 @@ THREE.GLTFExporter.prototype = {
938968
939969 }
940970
941- gltfMaterial . normalTexture . scale = material . normalScale . x ;
971+ normalMapDef . scale = material . normalScale . x ;
942972
943973 }
944974
975+ applyTextureTransform ( normalMapDef , material . normalMap ) ;
976+
977+ gltfMaterial . normalTexture = normalMapDef ;
978+
945979 }
946980
947981 // occlusionTexture
948982 if ( material . aoMap ) {
949983
950- gltfMaterial . occlusionTexture = {
951-
952- index : processTexture ( material . aoMap )
953-
954- } ;
984+ var occlusionMapDef = { index : processTexture ( material . aoMap ) } ;
955985
956986 if ( material . aoMapIntensity !== 1.0 ) {
957987
958- gltfMaterial . occlusionTexture . strength = material . aoMapIntensity ;
988+ occlusionMapDef . strength = material . aoMapIntensity ;
959989
960990 }
961991
992+ applyTextureTransform ( occlusionMapDef , material . aoMap ) ;
993+
994+ gltfMaterial . occlusionTexture = occlusionMapDef ;
995+
962996 }
963997
964998 // alphaMode
0 commit comments