Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GLTFLoader+GLTFExporter: Initial support for 'EXT_materials_bump' #27036

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -491,6 +497,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 @@ -1201,6 +1208,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