Skip to content

Commit 543da09

Browse files
authored
Merge pull request #15507 from donmccurdy/feat-gltfexporter-khr_texture_transform
GLTFExporter: Implement KHR_texture_transform.
2 parents e5aa0f2 + b128d19 commit 543da09

File tree

2 files changed

+73
-27
lines changed

2 files changed

+73
-27
lines changed

docs/examples/exporters/GLTFExporter.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ <h1>[name]</h1>
2121
textures, skins, skeletons, morph targets, animations, lights, and/or cameras.
2222
</p>
2323

24+
<h2>Extensions</h2>
25+
26+
<p>
27+
GLTFExporter supports the following
28+
[link:https://github.com/KhronosGroup/glTF/tree/master/extensions/ glTF 2.0 extensions]:
29+
</p>
30+
31+
<ul>
32+
<li>KHR_materials_unlit</li>
33+
<li>KHR_texture_transform</li>
34+
</ul>
35+
2436
<h2>Example</h2>
2537

2638
<code>

examples/js/exporters/GLTFExporter.js

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)