Skip to content

Commit 8a305f3

Browse files
Don McCurdydonmccurdy
authored andcommitted
GLTFLoader: Implement KHR_texture_transform.
1 parent e5c5f99 commit 8a305f3

File tree

1 file changed

+71
-20
lines changed

1 file changed

+71
-20
lines changed

examples/js/loaders/GLTFLoader.js

Lines changed: 71 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,15 @@ THREE.GLTFLoader = ( function () {
139139
break;
140140

141141
case EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS:
142-
extensions[ extensionName ] = new GLTFMaterialsPbrSpecularGlossinessExtension();
142+
extensions[ extensionName ] = new GLTFMaterialsPbrSpecularGlossinessExtension( json );
143143
break;
144144

145145
case EXTENSIONS.KHR_DRACO_MESH_COMPRESSION:
146146
extensions[ extensionName ] = new GLTFDracoMeshCompressionExtension( json, this.dracoLoader );
147147
break;
148148

149-
case EXTENSIONS.MSFT_TEXTURE_DDS:
150-
extensions[ EXTENSIONS.MSFT_TEXTURE_DDS ] = new GLTFTextureDDSExtension();
149+
case EXTENSIONS.KHR_TEXTURE_TRANSFORM:
150+
extensions[ EXTENSIONS.KHR_TEXTURE_TRANSFORM ] = new GLTFTextureTransformExtension( json );
151151
break;
152152

153153
default:
@@ -240,6 +240,7 @@ THREE.GLTFLoader = ( function () {
240240
KHR_LIGHTS: 'KHR_lights',
241241
KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS: 'KHR_materials_pbrSpecularGlossiness',
242242
KHR_MATERIALS_UNLIT: 'KHR_materials_unlit',
243+
KHR_TEXTURE_TRANSFORM: 'KHR_texture_transform',
243244
MSFT_TEXTURE_DDS: 'MSFT_texture_dds'
244245
};
245246

@@ -372,7 +373,7 @@ THREE.GLTFLoader = ( function () {
372373

373374
if ( metallicRoughness.baseColorTexture !== undefined ) {
374375

375-
pending.push( parser.assignTexture( materialParams, 'map', metallicRoughness.baseColorTexture.index ) );
376+
pending.push( parser.assignTexture( materialParams, 'map', metallicRoughness.baseColorTexture ) );
376377

377378
}
378379

@@ -526,6 +527,49 @@ THREE.GLTFLoader = ( function () {
526527

527528
};
528529

530+
/**
531+
* Texture Transform Extension
532+
*
533+
* Specification:
534+
*/
535+
function GLTFTextureTransformExtension ( json ) {
536+
537+
this.name = EXTENSIONS.KHR_TEXTURE_TRANSFORM;
538+
539+
}
540+
541+
GLTFTextureTransformExtension.prototype.extendTexture = function ( texture, mapDef ) {
542+
543+
var transform = mapDef.extensions !== undefined ? mapDef.extensions[ this.name ] : undefined;
544+
545+
if ( transform === undefined ) return texture;
546+
547+
texture = texture.clone();
548+
549+
if ( transform.offset !== undefined ) {
550+
551+
texture.offset.fromArray( transform.offset );
552+
553+
}
554+
555+
if ( transform.rotation !== undefined ) {
556+
557+
texture.rotation = -1 * transform.rotation;
558+
559+
}
560+
561+
if ( transform.scale !== undefined ) {
562+
563+
texture.repeat.fromArray( transform.scale );
564+
565+
}
566+
567+
texture.needsUpdate = true;
568+
569+
return texture;
570+
571+
}
572+
529573
/**
530574
* Specular-Glossiness Extension
531575
*
@@ -655,7 +699,7 @@ THREE.GLTFLoader = ( function () {
655699

656700
if ( pbrSpecularGlossiness.diffuseTexture !== undefined ) {
657701

658-
pending.push( parser.assignTexture( params, 'map', pbrSpecularGlossiness.diffuseTexture.index ) );
702+
pending.push( parser.assignTexture( params, 'map', pbrSpecularGlossiness.diffuseTexture ) );
659703

660704
}
661705

@@ -671,9 +715,9 @@ THREE.GLTFLoader = ( function () {
671715

672716
if ( pbrSpecularGlossiness.specularGlossinessTexture !== undefined ) {
673717

674-
var specGlossIndex = pbrSpecularGlossiness.specularGlossinessTexture.index;
675-
pending.push( parser.assignTexture( params, 'glossinessMap', specGlossIndex ) );
676-
pending.push( parser.assignTexture( params, 'specularMap', specGlossIndex ) );
718+
var specGlossMapDef = pbrSpecularGlossiness.specularGlossinessTexture;
719+
pending.push( parser.assignTexture( params, 'glossinessMap', specGlossMapDef ) );
720+
pending.push( parser.assignTexture( params, 'specularMap', specGlossMapDef ) );
677721

678722
}
679723

@@ -2047,15 +2091,23 @@ THREE.GLTFLoader = ( function () {
20472091
/**
20482092
* Asynchronously assigns a texture to the given material parameters.
20492093
* @param {Object} materialParams
2050-
* @param {string} textureName
2051-
* @param {number} textureIndex
2094+
* @param {string} mapName
2095+
* @param {Object} mapDef
20522096
* @return {Promise}
20532097
*/
2054-
GLTFParser.prototype.assignTexture = function ( materialParams, textureName, textureIndex ) {
2098+
GLTFParser.prototype.assignTexture = function ( materialParams, mapName, mapDef ) {
2099+
2100+
var parser = this;
20552101

2056-
return this.getDependency( 'texture', textureIndex ).then( function ( texture ) {
2102+
return this.getDependency( 'texture', mapDef.index ).then( function ( texture ) {
2103+
2104+
if ( parser.extensions[ EXTENSIONS.KHR_TEXTURE_TRANSFORM ] ) {
2105+
2106+
texture = parser.extensions[ EXTENSIONS.KHR_TEXTURE_TRANSFORM ].extendTexture( texture, mapDef );
2107+
2108+
}
20572109

2058-
materialParams[ textureName ] = texture;
2110+
materialParams[ mapName ] = texture;
20592111

20602112
} );
20612113

@@ -2114,7 +2166,7 @@ THREE.GLTFLoader = ( function () {
21142166

21152167
if ( metallicRoughness.baseColorTexture !== undefined ) {
21162168

2117-
pending.push( parser.assignTexture( materialParams, 'map', metallicRoughness.baseColorTexture.index ) );
2169+
pending.push( parser.assignTexture( materialParams, 'map', metallicRoughness.baseColorTexture ) );
21182170

21192171
}
21202172

@@ -2123,9 +2175,8 @@ THREE.GLTFLoader = ( function () {
21232175

21242176
if ( metallicRoughness.metallicRoughnessTexture !== undefined ) {
21252177

2126-
var textureIndex = metallicRoughness.metallicRoughnessTexture.index;
2127-
pending.push( parser.assignTexture( materialParams, 'metalnessMap', textureIndex ) );
2128-
pending.push( parser.assignTexture( materialParams, 'roughnessMap', textureIndex ) );
2178+
pending.push( parser.assignTexture( materialParams, 'metalnessMap', metallicRoughness.metallicRoughnessTexture ) );
2179+
pending.push( parser.assignTexture( materialParams, 'roughnessMap', metallicRoughness.metallicRoughnessTexture ) );
21292180

21302181
}
21312182

@@ -2157,7 +2208,7 @@ THREE.GLTFLoader = ( function () {
21572208

21582209
if ( materialDef.normalTexture !== undefined && materialType !== THREE.MeshBasicMaterial ) {
21592210

2160-
pending.push( parser.assignTexture( materialParams, 'normalMap', materialDef.normalTexture.index ) );
2211+
pending.push( parser.assignTexture( materialParams, 'normalMap', materialDef.normalTexture ) );
21612212

21622213
materialParams.normalScale = new THREE.Vector2( 1, 1 );
21632214

@@ -2171,7 +2222,7 @@ THREE.GLTFLoader = ( function () {
21712222

21722223
if ( materialDef.occlusionTexture !== undefined && materialType !== THREE.MeshBasicMaterial ) {
21732224

2174-
pending.push( parser.assignTexture( materialParams, 'aoMap', materialDef.occlusionTexture.index ) );
2225+
pending.push( parser.assignTexture( materialParams, 'aoMap', materialDef.occlusionTexture ) );
21752226

21762227
if ( materialDef.occlusionTexture.strength !== undefined ) {
21772228

@@ -2189,7 +2240,7 @@ THREE.GLTFLoader = ( function () {
21892240

21902241
if ( materialDef.emissiveTexture !== undefined && materialType !== THREE.MeshBasicMaterial ) {
21912242

2192-
pending.push( parser.assignTexture( materialParams, 'emissiveMap', materialDef.emissiveTexture.index ) );
2243+
pending.push( parser.assignTexture( materialParams, 'emissiveMap', materialDef.emissiveTexture ) );
21932244

21942245
}
21952246

0 commit comments

Comments
 (0)