Skip to content

Commit 2c252eb

Browse files
Don McCurdydonmccurdy
authored andcommitted
GLTFLoader: Implement KHR_texture_transform.
1 parent fb46a77 commit 2c252eb

File tree

1 file changed

+77
-20
lines changed

1 file changed

+77
-20
lines changed

examples/js/loaders/GLTFLoader.js

Lines changed: 77 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,19 @@ THREE.GLTFLoader = ( function () {
137137

138138
if ( json.extensionsUsed.indexOf( EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ) >= 0 ) {
139139

140-
extensions[ EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ] = new GLTFMaterialsPbrSpecularGlossinessExtension();
140+
extensions[ EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ] = new GLTFMaterialsPbrSpecularGlossinessExtension( json );
141141

142142
}
143143

144144
if ( json.extensionsUsed.indexOf( EXTENSIONS.KHR_DRACO_MESH_COMPRESSION ) >= 0 ) {
145145

146-
extensions[ EXTENSIONS.KHR_DRACO_MESH_COMPRESSION ] = new GLTFDracoMeshCompressionExtension( this.dracoLoader );
146+
extensions[ EXTENSIONS.KHR_DRACO_MESH_COMPRESSION ] = new GLTFDracoMeshCompressionExtension( json, this.dracoLoader );
147+
148+
}
149+
150+
if ( json.extensionsUsed.indexOf( EXTENSIONS.KHR_TEXTURE_TRANSFORM ) >= 0 ) {
151+
152+
extensions[ EXTENSIONS.KHR_TEXTURE_TRANSFORM ] = new GLTFTextureTransformExtension( json );
147153

148154
}
149155

@@ -231,6 +237,7 @@ THREE.GLTFLoader = ( function () {
231237
KHR_LIGHTS: 'KHR_lights',
232238
KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS: 'KHR_materials_pbrSpecularGlossiness',
233239
KHR_MATERIALS_UNLIT: 'KHR_materials_unlit',
240+
KHR_TEXTURE_TRANSFORM: 'KHR_texture_transform',
234241
MSFT_TEXTURE_DDS: 'MSFT_texture_dds'
235242
};
236243

@@ -363,7 +370,7 @@ THREE.GLTFLoader = ( function () {
363370

364371
if ( metallicRoughness.baseColorTexture !== undefined ) {
365372

366-
pending.push( parser.assignTexture( materialParams, 'map', metallicRoughness.baseColorTexture.index ) );
373+
pending.push( parser.assignTexture( materialParams, 'map', metallicRoughness.baseColorTexture ) );
367374

368375
}
369376

@@ -446,7 +453,7 @@ THREE.GLTFLoader = ( function () {
446453
*
447454
* Specification: https://github.com/KhronosGroup/glTF/pull/874
448455
*/
449-
function GLTFDracoMeshCompressionExtension ( dracoLoader ) {
456+
function GLTFDracoMeshCompressionExtension ( json, dracoLoader ) {
450457

451458
if ( ! dracoLoader ) {
452459

@@ -486,6 +493,49 @@ THREE.GLTFLoader = ( function () {
486493

487494
};
488495

496+
/**
497+
* Texture Transform Extension
498+
*
499+
* Specification:
500+
*/
501+
function GLTFTextureTransformExtension ( json ) {
502+
503+
this.name = EXTENSIONS.KHR_TEXTURE_TRANSFORM;
504+
505+
}
506+
507+
GLTFTextureTransformExtension.prototype.extendTexture = function ( texture, mapDef ) {
508+
509+
var transform = mapDef.extensions !== undefined ? mapDef.extensions[ this.name ] : undefined;
510+
511+
if ( transform === undefined ) return texture;
512+
513+
texture = texture.clone();
514+
515+
if ( transform.offset !== undefined ) {
516+
517+
texture.offset.fromArray( transform.offset );
518+
519+
}
520+
521+
if ( transform.rotation !== undefined ) {
522+
523+
texture.rotation = -1 * transform.rotation;
524+
525+
}
526+
527+
if ( transform.scale !== undefined ) {
528+
529+
texture.repeat.fromArray( transform.scale );
530+
531+
}
532+
533+
texture.needsUpdate = true;
534+
535+
return texture;
536+
537+
}
538+
489539
/**
490540
* Specular-Glossiness Extension
491541
*
@@ -616,7 +666,7 @@ THREE.GLTFLoader = ( function () {
616666

617667
if ( pbrSpecularGlossiness.diffuseTexture !== undefined ) {
618668

619-
pending.push( parser.assignTexture( params, 'map', pbrSpecularGlossiness.diffuseTexture.index ) );
669+
pending.push( parser.assignTexture( params, 'map', pbrSpecularGlossiness.diffuseTexture ) );
620670

621671
}
622672

@@ -632,9 +682,9 @@ THREE.GLTFLoader = ( function () {
632682

633683
if ( pbrSpecularGlossiness.specularGlossinessTexture !== undefined ) {
634684

635-
var specGlossIndex = pbrSpecularGlossiness.specularGlossinessTexture.index;
636-
pending.push( parser.assignTexture( params, 'glossinessMap', specGlossIndex ) );
637-
pending.push( parser.assignTexture( params, 'specularMap', specGlossIndex ) );
685+
var specGlossMapDef = pbrSpecularGlossiness.specularGlossinessTexture;
686+
pending.push( parser.assignTexture( params, 'glossinessMap', specGlossMapDef ) );
687+
pending.push( parser.assignTexture( params, 'specularMap', specGlossMapDef ) );
638688

639689
}
640690

@@ -1938,15 +1988,23 @@ THREE.GLTFLoader = ( function () {
19381988
/**
19391989
* Asynchronously assigns a texture to the given material parameters.
19401990
* @param {Object} materialParams
1941-
* @param {string} textureName
1942-
* @param {number} textureIndex
1991+
* @param {string} mapName
1992+
* @param {Object} mapDef
19431993
* @return {Promise}
19441994
*/
1945-
GLTFParser.prototype.assignTexture = function ( materialParams, textureName, textureIndex ) {
1995+
GLTFParser.prototype.assignTexture = function ( materialParams, mapName, mapDef ) {
1996+
1997+
var parser = this;
19461998

1947-
return this.getDependency( 'texture', textureIndex ).then( function ( texture ) {
1999+
return this.getDependency( 'texture', mapDef.index ).then( function ( texture ) {
2000+
2001+
if ( parser.extensions[ EXTENSIONS.KHR_TEXTURE_TRANSFORM ] ) {
2002+
2003+
texture = parser.extensions[ EXTENSIONS.KHR_TEXTURE_TRANSFORM ].extendTexture( texture, mapDef );
2004+
2005+
}
19482006

1949-
materialParams[ textureName ] = texture;
2007+
materialParams[ mapName ] = texture;
19502008

19512009
} );
19522010

@@ -2005,7 +2063,7 @@ THREE.GLTFLoader = ( function () {
20052063

20062064
if ( metallicRoughness.baseColorTexture !== undefined ) {
20072065

2008-
pending.push( parser.assignTexture( materialParams, 'map', metallicRoughness.baseColorTexture.index ) );
2066+
pending.push( parser.assignTexture( materialParams, 'map', metallicRoughness.baseColorTexture ) );
20092067

20102068
}
20112069

@@ -2014,9 +2072,8 @@ THREE.GLTFLoader = ( function () {
20142072

20152073
if ( metallicRoughness.metallicRoughnessTexture !== undefined ) {
20162074

2017-
var textureIndex = metallicRoughness.metallicRoughnessTexture.index;
2018-
pending.push( parser.assignTexture( materialParams, 'metalnessMap', textureIndex ) );
2019-
pending.push( parser.assignTexture( materialParams, 'roughnessMap', textureIndex ) );
2075+
pending.push( parser.assignTexture( materialParams, 'metalnessMap', metallicRoughness.metallicRoughnessTexture ) );
2076+
pending.push( parser.assignTexture( materialParams, 'roughnessMap', metallicRoughness.metallicRoughnessTexture ) );
20202077

20212078
}
20222079

@@ -2048,7 +2105,7 @@ THREE.GLTFLoader = ( function () {
20482105

20492106
if ( materialDef.normalTexture !== undefined && materialType !== THREE.MeshBasicMaterial) {
20502107

2051-
pending.push( parser.assignTexture( materialParams, 'normalMap', materialDef.normalTexture.index ) );
2108+
pending.push( parser.assignTexture( materialParams, 'normalMap', materialDef.normalTexture ) );
20522109

20532110
materialParams.normalScale = new THREE.Vector2( 1, 1 );
20542111

@@ -2062,7 +2119,7 @@ THREE.GLTFLoader = ( function () {
20622119

20632120
if ( materialDef.occlusionTexture !== undefined && materialType !== THREE.MeshBasicMaterial) {
20642121

2065-
pending.push( parser.assignTexture( materialParams, 'aoMap', materialDef.occlusionTexture.index ) );
2122+
pending.push( parser.assignTexture( materialParams, 'aoMap', materialDef.occlusionTexture ) );
20662123

20672124
if ( materialDef.occlusionTexture.strength !== undefined ) {
20682125

@@ -2080,7 +2137,7 @@ THREE.GLTFLoader = ( function () {
20802137

20812138
if ( materialDef.emissiveTexture !== undefined && materialType !== THREE.MeshBasicMaterial) {
20822139

2083-
pending.push( parser.assignTexture( materialParams, 'emissiveMap', materialDef.emissiveTexture.index ) );
2140+
pending.push( parser.assignTexture( materialParams, 'emissiveMap', materialDef.emissiveTexture ) );
20842141

20852142
}
20862143

0 commit comments

Comments
 (0)