Skip to content

Commit

Permalink
GLTFLoader+GLTFExporter: Initial support for 'EXT_materials_bump' (#2…
Browse files Browse the repository at this point in the history
…7036)

* initial support for EXT_materials_bump

* fix typo: EXR_ -> EXT_
  • Loading branch information
bhouston authored Nov 23, 2023
1 parent fe587fa commit b2741ea
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 1 deletion.
54 changes: 54 additions & 0 deletions examples/jsm/exporters/GLTFExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ class GLTFExporter {

} );

this.register( function ( writer ) {

return new GLTFMaterialsBumpExtension( writer );

} );

this.register( function ( writer ) {

return new GLTFMeshGpuInstancing( writer );
Expand Down Expand Up @@ -2971,6 +2977,54 @@ class GLTFMaterialsEmissiveStrengthExtension {

}


/**
* Materials bump Extension
*
* Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/EXT_materials_bump
*/
class GLTFMaterialsBumpExtension {

constructor( writer ) {

this.writer = writer;
this.name = 'EXT_materials_bump';

}

writeMaterial( material, materialDef ) {

if ( ! material.isMeshStandardMaterial || (
material.bumpScale === 1 &&
! material.bumpMap ) ) return;

const writer = this.writer;
const extensionsUsed = writer.extensionsUsed;

const extensionDef = {};

if ( material.bumpMap ) {

const bumpMapDef = {
index: writer.processTexture( material.bumpMap ),
texCoord: material.bumpMap.channel
};
writer.applyTextureTransform( bumpMapDef, material.bumpMap );
extensionDef.bumpTexture = bumpMapDef;

}

extensionDef.bumpFactor = material.bumpScale;

materialDef.extensions = materialDef.extensions || {};
materialDef.extensions[ this.name ] = extensionDef;

extensionsUsed[ this.name ] = true;

}

}

/**
* GPU Instancing Extension
*
Expand Down
62 changes: 62 additions & 0 deletions examples/jsm/loaders/GLTFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ class GLTFLoader extends Loader {

} );

this.register( function ( parser ) {

return new GLTFMaterialsBumpExtension( parser );

} );

this.register( function ( parser ) {

return new GLTFLightsExtension( parser );
Expand Down Expand Up @@ -497,6 +503,7 @@ const EXTENSIONS = {
KHR_TEXTURE_TRANSFORM: 'KHR_texture_transform',
KHR_MESH_QUANTIZATION: 'KHR_mesh_quantization',
KHR_MATERIALS_EMISSIVE_STRENGTH: 'KHR_materials_emissive_strength',
EXT_MATERIALS_BUMP: 'EXT_materials_bump',
EXT_TEXTURE_WEBP: 'EXT_texture_webp',
EXT_TEXTURE_AVIF: 'EXT_texture_avif',
EXT_MESHOPT_COMPRESSION: 'EXT_meshopt_compression',
Expand Down Expand Up @@ -1207,6 +1214,61 @@ class GLTFMaterialsSpecularExtension {

}


/**
* Materials bump Extension
*
* Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/EXT_materials_bump
*/
class GLTFMaterialsBumpExtension {

constructor( parser ) {

this.parser = parser;
this.name = EXTENSIONS.EXT_MATERIALS_BUMP;

}

getMaterialType( materialIndex ) {

const parser = this.parser;
const materialDef = parser.json.materials[ materialIndex ];

if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) return null;

return MeshPhysicalMaterial;

}

extendMaterialParams( materialIndex, materialParams ) {

const parser = this.parser;
const materialDef = parser.json.materials[ materialIndex ];

if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) {

return Promise.resolve();

}

const pending = [];

const extension = materialDef.extensions[ this.name ];

materialParams.bumpScale = extension.bumpFactor !== undefined ? extension.bumpFactor : 1.0;

if ( extension.bumpTexture !== undefined ) {

pending.push( parser.assignTexture( materialParams, 'bumpMap', extension.bumpTexture ) );

}

return Promise.all( pending );

}

}

/**
* Materials anisotropy Extension
*
Expand Down
3 changes: 2 additions & 1 deletion examples/misc_exporter_gltf.html
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,10 @@
color: 0xffff00,
metalness: 0.5,
roughness: 1.0,
flatShading: true
flatShading: true,
} );
material.map = gradientTexture;
material.bumpMap = mapGrid;
sphere = new THREE.Mesh( new THREE.SphereGeometry( 70, 10, 10 ), material );
sphere.position.set( 0, 0, 0 );
sphere.name = 'Sphere';
Expand Down

0 comments on commit b2741ea

Please sign in to comment.