Skip to content

Commit

Permalink
GLTF KHR_materials_anisotropy support (#25580)
Browse files Browse the repository at this point in the history
* plumbing

* refactored tangents

* fix bump map

* fix bump map again

* added IBL anisotropy support

* added direct anisotropy support

* reshuffle chunks

* fix clearcoat

* fix dangling endif

* separate physical BRDF from blinnPhong

* prod CI

* move Schlick to common

* fixed plumbing

* fixed shader compilation

* fixed mappings

* fixed tangents

* changed IBL mapping

* fix typo

* fixed direct lighting

* linearized mappings

* added anisotropyDirection

* fixed angle direction

* added references

* fix anisotropy map

* Angle -> Rotation

* fixed tangent sign

* rename anisotropyMat

* fix 90 deg rotation

* update to spec

* updated alphaT mapping

* Revert "updated alphaT mapping"

This reverts commit cebace1.

* WebGLProgram: Added missing ANISOTROPYMAP_UV define.

* Reverted builds.

* WebGLPrograms: Fixed typo.

* update to spec

* updated golden

* update texture definition

* added example

* add build files

* added screenshot

---------

Co-authored-by: Mr.doob <[email protected]>
  • Loading branch information
elalish and mrdoob authored May 11, 2023
1 parent 4675440 commit 22c5f25
Show file tree
Hide file tree
Showing 31 changed files with 786 additions and 101 deletions.
137 changes: 110 additions & 27 deletions build/three.cjs

Large diffs are not rendered by default.

137 changes: 110 additions & 27 deletions build/three.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/three.min.js

Large diffs are not rendered by default.

137 changes: 110 additions & 27 deletions build/three.module.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/three.module.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions examples/files.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
"webgl_loader_gltf_sheen",
"webgl_loader_gltf_transmission",
"webgl_loader_gltf_variants",
"webgl_loader_gltf_anisotropy",
"webgl_loader_ifc",
"webgl_loader_imagebitmap",
"webgl_loader_kmz",
Expand Down
49 changes: 49 additions & 0 deletions examples/jsm/exporters/GLTFExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ class GLTFExporter {

} );

this.register( function ( writer ) {

return new GLTFMaterialsAnisotropyExtension( writer );

} );

this.register( function ( writer ) {

return new GLTFMaterialsEmissiveStrengthExtension( writer );
Expand Down Expand Up @@ -2856,6 +2862,49 @@ class GLTFMaterialsSheenExtension {

}

/**
* Anisotropy Materials Extension
*
* Specification: https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_materials_anisotropy
*/
class GLTFMaterialsAnisotropyExtension {

constructor( writer ) {

this.writer = writer;
this.name = 'KHR_materials_anisotropy';

}

writeMaterial( material, materialDef ) {

if ( ! material.isMeshPhysicalMaterial || material.anisotropy == 0.0 ) return;

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

const extensionDef = {};

if ( material.anisotropyMap ) {

const anisotropyMapDef = { index: writer.processTexture( material.anisotropyMap ) };
writer.applyTextureTransform( anisotropyMapDef, material.anisotropyMap );
extensionDef.anisotropyTexture = anisotropyMapDef;

}

extensionDef.anisotropyStrength = material.anisotropy;
extensionDef.anisotropyRotation = material.anisotropyRotation;

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

extensionsUsed[ this.name ] = true;

}

}

/**
* Materials Emissive Strength Extension
*
Expand Down
71 changes: 71 additions & 0 deletions examples/jsm/loaders/GLTFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ class GLTFLoader extends Loader {

} );

this.register( function ( parser ) {

return new GLTFMaterialsAnisotropyExtension( parser );

} );

this.register( function ( parser ) {

return new GLTFLightsExtension( parser );
Expand Down Expand Up @@ -472,6 +478,7 @@ const EXTENSIONS = {
KHR_MATERIALS_SPECULAR: 'KHR_materials_specular',
KHR_MATERIALS_TRANSMISSION: 'KHR_materials_transmission',
KHR_MATERIALS_IRIDESCENCE: 'KHR_materials_iridescence',
KHR_MATERIALS_ANISOTROPY: 'KHR_materials_anisotropy',
KHR_MATERIALS_UNLIT: 'KHR_materials_unlit',
KHR_MATERIALS_VOLUME: 'KHR_materials_volume',
KHR_TEXTURE_BASISU: 'KHR_texture_basisu',
Expand Down Expand Up @@ -1187,6 +1194,70 @@ class GLTFMaterialsSpecularExtension {

}

/**
* Materials anisotropy Extension
*
* Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_anisotropy
*/
class GLTFMaterialsAnisotropyExtension {

constructor( parser ) {

this.parser = parser;
this.name = EXTENSIONS.KHR_MATERIALS_ANISOTROPY;

}

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 ];

if ( extension.anisotropyStrength !== undefined ) {

materialParams.anisotropy = extension.anisotropyStrength;

}

if ( extension.anisotropyRotation !== undefined ) {

materialParams.anisotropyRotation = extension.anisotropyRotation;

}

if ( extension.anisotropyTexture !== undefined ) {

pending.push( parser.assignTexture( materialParams, 'anisotropyMap', extension.anisotropyTexture ) );

}

return Promise.all( pending );

}

}

/**
* BasisU Texture Extension
*
Expand Down
Binary file added examples/models/gltf/AnisotropyBarnLamp.glb
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/screenshots/webgl_loader_gltf_lights.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
112 changes: 112 additions & 0 deletions examples/webgl_loader_gltf_anisotropy.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - GLTFloader + Anisotropy</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
</head>

<body>
<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - GLTFLoader + <a href="https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_anisotropy" target="_blank" rel="noopener">KHR_materials_anisotropy</a><br />
Anisotropy Barn Lamp from <a href="https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0/AnisotropyBarnLamp" target="_blank" rel="noopener">glTF-Sample-Models</a><br />
<a href="https://hdrihaven.com/hdri/?h=venice_sunset" target="_blank" rel="noopener">Venice Sunset</a> by <a href="https://hdrihaven.com/" target="_blank" rel="noopener">HDRI Haven</a>
</div>

<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>

<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/"
}
}
</script>

<script type="module">

import * as THREE from 'three';

import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';

let renderer, scene, camera, controls;

init().catch( function ( err ) {

console.error( err );

} );

async function init() {

renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.toneMapping = THREE.ACESFilmicToneMapping;
document.body.appendChild( renderer.domElement );

scene = new THREE.Scene();

camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.25, 20 );
camera.position.set( 0.35, 0.05, 0.35 );

controls = new OrbitControls( camera, renderer.domElement );
controls.addEventListener( 'change', render );
controls.target.set( 0, -0.05, 0.1 );
controls.update();

const rgbeLoader = new RGBELoader()
.setPath( 'textures/equirectangular/' );

const gltfLoader = new GLTFLoader().setPath( 'models/gltf/' );

const [ texture, gltf ] = await Promise.all( [
rgbeLoader.loadAsync( 'venice_sunset_1k.hdr' ),
gltfLoader.loadAsync( 'AnisotropyBarnLamp.glb' ),
] );

// environment

texture.mapping = THREE.EquirectangularReflectionMapping;

scene.background = texture;
scene.environment = texture;

// model

scene.add( gltf.scene );

render();

window.addEventListener( 'resize', onWindowResize );

}

function onWindowResize() {

camera.aspect = window.innerWidth / window.innerHeight;

camera.updateProjectionMatrix();

renderer.setSize( window.innerWidth, window.innerHeight );

render();

}

function render() {

renderer.render( scene, camera );

}

</script>

</body>
</html>
3 changes: 3 additions & 0 deletions manual/resources/threejs-material-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ const materials = [
'transmissionMap',
'attenuationDistance',
'attenuationColor',
'anisotropy',
'anisotropyRotation',
'anisotropyMap',
'specularIntensity',
'specularIntensityMap',
'specularColor',
Expand Down
4 changes: 4 additions & 0 deletions src/loaders/MaterialLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ class MaterialLoader extends Loader {
if ( json.thickness !== undefined ) material.thickness = json.thickness;
if ( json.attenuationDistance !== undefined ) material.attenuationDistance = json.attenuationDistance;
if ( json.attenuationColor !== undefined && material.attenuationColor !== undefined ) material.attenuationColor.setHex( json.attenuationColor );
if ( json.anisotropy !== undefined ) material.anisotropy = json.anisotropy;
if ( json.anisotropyRotation !== undefined ) material.anisotropyRotation = json.anisotropyRotation;
if ( json.fog !== undefined ) material.fog = json.fog;
if ( json.flatShading !== undefined ) material.flatShading = json.flatShading;
if ( json.blending !== undefined ) material.blending = json.blending;
Expand Down Expand Up @@ -313,6 +315,8 @@ class MaterialLoader extends Loader {
if ( json.transmissionMap !== undefined ) material.transmissionMap = getTexture( json.transmissionMap );
if ( json.thicknessMap !== undefined ) material.thicknessMap = getTexture( json.thicknessMap );

if ( json.anisotropyMap !== undefined ) material.anisotropyMap = getTexture( json.anisotropyMap );

if ( json.sheenColorMap !== undefined ) material.sheenColorMap = getTexture( json.sheenColorMap );
if ( json.sheenRoughnessMap !== undefined ) material.sheenRoughnessMap = getTexture( json.sheenRoughnessMap );

Expand Down
9 changes: 9 additions & 0 deletions src/materials/Material.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,15 @@ class Material extends EventDispatcher {

}

if ( this.anisotropy !== undefined ) data.anisotropy = this.anisotropy;
if ( this.anisotropyRotation !== undefined ) data.anisotropyRotation = this.anisotropyRotation;

if ( this.anisotropyMap && this.anisotropyMap.isTexture ) {

data.anisotropyMap = this.anisotropyMap.toJSON( meta ).uuid;

}

if ( this.map && this.map.isTexture ) data.map = this.map.toJSON( meta ).uuid;
if ( this.matcap && this.matcap.isTexture ) data.matcap = this.matcap.toJSON( meta ).uuid;
if ( this.alphaMap && this.alphaMap.isTexture ) data.alphaMap = this.alphaMap.toJSON( meta ).uuid;
Expand Down
Loading

0 comments on commit 22c5f25

Please sign in to comment.