From 6dd2477b7a4d20756e709451703af8dbc89dd21b Mon Sep 17 00:00:00 2001 From: Don McCurdy Date: Thu, 27 Feb 2020 17:46:20 -0800 Subject: [PATCH 1/2] MeshPhysicalMaterial: Add clearcoatMap and clearcoatRoughnessMap. --- src/materials/MeshPhysicalMaterial.d.ts | 10 ++-- src/materials/MeshPhysicalMaterial.js | 47 ++++++++++++------- src/renderers/WebGLRenderer.js | 12 +++++ src/renderers/shaders/ShaderChunk.d.ts | 2 +- src/renderers/shaders/ShaderChunk.js | 4 +- .../clearcoat_normalmap_pars_fragment.glsl.js | 8 ---- .../clearcoat_pars_fragment.glsl.js | 21 +++++++++ .../lights_physical_fragment.glsl.js | 20 +++++++- src/renderers/shaders/ShaderLib.js | 6 ++- .../ShaderLib/meshphysical_frag.glsl.js | 2 +- src/renderers/webgl/WebGLProgram.js | 4 ++ src/renderers/webgl/WebGLPrograms.js | 6 ++- 12 files changed, 104 insertions(+), 38 deletions(-) delete mode 100644 src/renderers/shaders/ShaderChunk/clearcoat_normalmap_pars_fragment.glsl.js create mode 100644 src/renderers/shaders/ShaderChunk/clearcoat_pars_fragment.glsl.js diff --git a/src/materials/MeshPhysicalMaterial.d.ts b/src/materials/MeshPhysicalMaterial.d.ts index dc7cd708e91f7d..d32b8b25efed2e 100644 --- a/src/materials/MeshPhysicalMaterial.d.ts +++ b/src/materials/MeshPhysicalMaterial.d.ts @@ -22,13 +22,17 @@ export class MeshPhysicalMaterial extends MeshStandardMaterial { constructor( parameters: MeshPhysicalMaterialParameters ); - reflectivity: number; clearcoat: number; + clearcoatMap: Texture | null; clearcoatRoughness: number; + clearcoatRoughnessMap: Texture | null; + clearcoatNormalScale: Vector2; + clearcoatNormalMap: Texture | null; + + reflectivity: number; sheen: Color | null; - clearcoatNormalScale: Vector2; - clearcoatNormalMap: Texture | null; + transparency: number; } diff --git a/src/materials/MeshPhysicalMaterial.js b/src/materials/MeshPhysicalMaterial.js index 09e1469f2a49ca..bb787ac0042420 100644 --- a/src/materials/MeshPhysicalMaterial.js +++ b/src/materials/MeshPhysicalMaterial.js @@ -6,14 +6,18 @@ import { Color } from '../math/Color.js'; * @author WestLangley / http://github.com/WestLangley * * parameters = { - * reflectivity: - * clearcoat: - * clearcoatRoughness: - * - * sheen: - * + * clearcoat: , + * clearcoatMap: new THREE.Texture( ), + * clearcoatRoughness: , + * clearcoatRoughnessMap: new THREE.Texture( ), * clearcoatNormalScale: , * clearcoatNormalMap: new THREE.Texture( ), + * + * reflectivity: , + * + * sheen: , + * + * transparency: * } */ @@ -30,16 +34,17 @@ function MeshPhysicalMaterial( parameters ) { this.type = 'MeshPhysicalMaterial'; - this.reflectivity = 0.5; // maps to F0 = 0.04 - this.clearcoat = 0.0; + this.clearcoatMap = null; this.clearcoatRoughness = 0.0; - - this.sheen = null; // null will disable sheen bsdf - + this.clearcoatRoughnessMap = null; this.clearcoatNormalScale = new Vector2( 1, 1 ); this.clearcoatNormalMap = null; + this.reflectivity = 0.5; // maps to F0 = 0.04 + + this.sheen = null; // null will disable sheen bsdf + this.transparency = 0.0; this.setValues( parameters ); @@ -62,17 +67,25 @@ MeshPhysicalMaterial.prototype.copy = function ( source ) { }; - this.reflectivity = source.reflectivity; - this.clearcoat = source.clearcoat; + this.clearcoatMap = source.clearcoatMap; this.clearcoatRoughness = source.clearcoatRoughness; - - if ( source.sheen ) this.sheen = ( this.sheen || new Color() ).copy( source.sheen ); - else this.sheen = null; - + this.clearcoatRoughnessMap = source.clearcoatRoughnessMap; this.clearcoatNormalMap = source.clearcoatNormalMap; this.clearcoatNormalScale.copy( source.clearcoatNormalScale ); + this.reflectivity = source.reflectivity; + + if ( source.sheen ) { + + this.sheen = ( this.sheen || new Color() ).copy( source.sheen ); + + } else { + + this.sheen = null; + + } + this.transparency = source.transparency; return this; diff --git a/src/renderers/WebGLRenderer.js b/src/renderers/WebGLRenderer.js index dac1b74fe9e20e..78e3dab2d753b2 100644 --- a/src/renderers/WebGLRenderer.js +++ b/src/renderers/WebGLRenderer.js @@ -2480,6 +2480,18 @@ function WebGLRenderer( parameters ) { uniforms.clearcoatRoughness.value = material.clearcoatRoughness; if ( material.sheen ) uniforms.sheen.value.copy( material.sheen ); + if ( material.clearcoatMap ) { + + uniforms.clearcoatMap.value = material.clearcoatMap; + + } + + if ( material.clearcoatRoughnessMap ) { + + uniforms.clearcoatRoughnessMap.value = material.clearcoatRoughnessMap; + + } + if ( material.clearcoatNormalMap ) { uniforms.clearcoatNormalScale.value.copy( material.clearcoatNormalScale ); diff --git a/src/renderers/shaders/ShaderChunk.d.ts b/src/renderers/shaders/ShaderChunk.d.ts index 683c4060f30c45..cd08d18bfef08b 100644 --- a/src/renderers/shaders/ShaderChunk.d.ts +++ b/src/renderers/shaders/ShaderChunk.d.ts @@ -87,7 +87,7 @@ export let ShaderChunk: { normalmap_pars_fragment: string; clearcoat_normal_fragment_begin: string; clearcoat_normal_fragment_maps: string; - clearcoat_normalmap_pars_fragment: string; + clearcoat_pars_fragment: string; packing: string; points_frag: string; points_vert: string; diff --git a/src/renderers/shaders/ShaderChunk.js b/src/renderers/shaders/ShaderChunk.js index c9f881c6c0cf93..6f1fda200dfd45 100644 --- a/src/renderers/shaders/ShaderChunk.js +++ b/src/renderers/shaders/ShaderChunk.js @@ -66,7 +66,7 @@ import normal_fragment_maps from './ShaderChunk/normal_fragment_maps.glsl.js'; import normalmap_pars_fragment from './ShaderChunk/normalmap_pars_fragment.glsl.js'; import clearcoat_normal_fragment_begin from './ShaderChunk/clearcoat_normal_fragment_begin.glsl.js'; import clearcoat_normal_fragment_maps from './ShaderChunk/clearcoat_normal_fragment_maps.glsl.js'; -import clearcoat_normalmap_pars_fragment from './ShaderChunk/clearcoat_normalmap_pars_fragment.glsl.js'; +import clearcoat_pars_fragment from './ShaderChunk/clearcoat_pars_fragment.glsl.js'; import packing from './ShaderChunk/packing.glsl.js'; import premultiplied_alpha_fragment from './ShaderChunk/premultiplied_alpha_fragment.glsl.js'; import project_vertex from './ShaderChunk/project_vertex.glsl.js'; @@ -197,7 +197,7 @@ export var ShaderChunk = { normalmap_pars_fragment: normalmap_pars_fragment, clearcoat_normal_fragment_begin: clearcoat_normal_fragment_begin, clearcoat_normal_fragment_maps: clearcoat_normal_fragment_maps, - clearcoat_normalmap_pars_fragment: clearcoat_normalmap_pars_fragment, + clearcoat_pars_fragment: clearcoat_pars_fragment, packing: packing, premultiplied_alpha_fragment: premultiplied_alpha_fragment, project_vertex: project_vertex, diff --git a/src/renderers/shaders/ShaderChunk/clearcoat_normalmap_pars_fragment.glsl.js b/src/renderers/shaders/ShaderChunk/clearcoat_normalmap_pars_fragment.glsl.js deleted file mode 100644 index d4f32b0fb7e858..00000000000000 --- a/src/renderers/shaders/ShaderChunk/clearcoat_normalmap_pars_fragment.glsl.js +++ /dev/null @@ -1,8 +0,0 @@ -export default /* glsl */` -#ifdef USE_CLEARCOAT_NORMALMAP - - uniform sampler2D clearcoatNormalMap; - uniform vec2 clearcoatNormalScale; - -#endif -`; diff --git a/src/renderers/shaders/ShaderChunk/clearcoat_pars_fragment.glsl.js b/src/renderers/shaders/ShaderChunk/clearcoat_pars_fragment.glsl.js new file mode 100644 index 00000000000000..44f2157d388c39 --- /dev/null +++ b/src/renderers/shaders/ShaderChunk/clearcoat_pars_fragment.glsl.js @@ -0,0 +1,21 @@ +export default /* glsl */` + +#ifdef USE_CLEARCOATMAP + + uniform sampler2D clearcoatMap; + +#endif + +#ifdef USE_CLEARCOAT_ROUGHNESSMAP + + uniform sampler2D clearcoatRoughnessMap; + +#endif + +#ifdef USE_CLEARCOAT_NORMALMAP + + uniform sampler2D clearcoatNormalMap; + uniform vec2 clearcoatNormalScale; + +#endif +`; diff --git a/src/renderers/shaders/ShaderChunk/lights_physical_fragment.glsl.js b/src/renderers/shaders/ShaderChunk/lights_physical_fragment.glsl.js index fac7ecbc1a96d7..e2b3db82995767 100644 --- a/src/renderers/shaders/ShaderChunk/lights_physical_fragment.glsl.js +++ b/src/renderers/shaders/ShaderChunk/lights_physical_fragment.glsl.js @@ -21,12 +21,28 @@ material.specularRoughness = min( material.specularRoughness, 1.0 ); #ifdef CLEARCOAT - material.clearcoat = saturate( clearcoat ); // Burley clearcoat model - material.clearcoatRoughness = max( clearcoatRoughness, 0.0525 ); + material.clearcoat = clearcoat; + material.clearcoatRoughness = clearcoatRoughness; + + #ifdef USE_CLEARCOATMAP + + material.clearcoat *= texture2D( clearcoatMap, vUv ).x; + + #endif + + #ifdef USE_CLEARCOAT_ROUGHNESSMAP + + material.clearcoatRoughness *= texture2D( clearcoatRoughnessMap, vUv ).y; + + #endif + + material.clearcoat = saturate( material.clearcoat ); // Burley clearcoat model + material.clearcoatRoughness = max( material.clearcoatRoughness, 0.0525 ); material.clearcoatRoughness += geometryRoughness; material.clearcoatRoughness = min( material.clearcoatRoughness, 1.0 ); #endif + #ifdef USE_SHEEN material.sheenColor = sheen; diff --git a/src/renderers/shaders/ShaderLib.js b/src/renderers/shaders/ShaderLib.js index b53c25ba3239d9..13a26e906cd8e3 100644 --- a/src/renderers/shaders/ShaderLib.js +++ b/src/renderers/shaders/ShaderLib.js @@ -299,12 +299,14 @@ ShaderLib.physical = { uniforms: mergeUniforms( [ ShaderLib.standard.uniforms, { - transparency: { value: 0 }, clearcoat: { value: 0 }, + clearcoatMap: { value: null }, clearcoatRoughness: { value: 0 }, - sheen: { value: new Color( 0x000000 ) }, + clearcoatRoughnessMap: { value: null }, clearcoatNormalScale: { value: new Vector2( 1, 1 ) }, clearcoatNormalMap: { value: null }, + sheen: { value: new Color( 0x000000 ) }, + transparency: { value: 0 }, } ] ), diff --git a/src/renderers/shaders/ShaderLib/meshphysical_frag.glsl.js b/src/renderers/shaders/ShaderLib/meshphysical_frag.glsl.js index 9e7e8e7c0ce961..3863afe9d15ce4 100644 --- a/src/renderers/shaders/ShaderLib/meshphysical_frag.glsl.js +++ b/src/renderers/shaders/ShaderLib/meshphysical_frag.glsl.js @@ -66,7 +66,7 @@ varying vec3 vViewPosition; #include #include #include -#include +#include #include #include #include diff --git a/src/renderers/webgl/WebGLProgram.js b/src/renderers/webgl/WebGLProgram.js index eab285fa85b3c5..5a483fdcd2495c 100644 --- a/src/renderers/webgl/WebGLProgram.js +++ b/src/renderers/webgl/WebGLProgram.js @@ -458,6 +458,8 @@ function WebGLProgram( renderer, cacheKey, parameters ) { ( parameters.normalMap && parameters.objectSpaceNormalMap ) ? '#define OBJECTSPACE_NORMALMAP' : '', ( parameters.normalMap && parameters.tangentSpaceNormalMap ) ? '#define TANGENTSPACE_NORMALMAP' : '', + parameters.clearcoatMap ? '#define USE_CLEARCOATMAP' : '', + parameters.clearcoatRoughnessMap ? '#define USE_CLEARCOAT_ROUGHNESSMAP' : '', parameters.clearcoatNormalMap ? '#define USE_CLEARCOAT_NORMALMAP' : '', parameters.displacementMap && parameters.supportsVertexTextures ? '#define USE_DISPLACEMENTMAP' : '', parameters.specularMap ? '#define USE_SPECULARMAP' : '', @@ -584,6 +586,8 @@ function WebGLProgram( renderer, cacheKey, parameters ) { parameters.normalMap ? '#define USE_NORMALMAP' : '', ( parameters.normalMap && parameters.objectSpaceNormalMap ) ? '#define OBJECTSPACE_NORMALMAP' : '', ( parameters.normalMap && parameters.tangentSpaceNormalMap ) ? '#define TANGENTSPACE_NORMALMAP' : '', + parameters.clearcoatMap ? '#define USE_CLEARCOATMAP' : '', + parameters.clearcoatRoughnessMap ? '#define USE_CLEARCOAT_ROUGHNESSMAP' : '', parameters.clearcoatNormalMap ? '#define USE_CLEARCOAT_NORMALMAP' : '', parameters.specularMap ? '#define USE_SPECULARMAP' : '', parameters.roughnessMap ? '#define USE_ROUGHNESSMAP' : '', diff --git a/src/renderers/webgl/WebGLPrograms.js b/src/renderers/webgl/WebGLPrograms.js index 3535eed00dc3e4..b72713c5c69ab7 100644 --- a/src/renderers/webgl/WebGLPrograms.js +++ b/src/renderers/webgl/WebGLPrograms.js @@ -39,7 +39,7 @@ function WebGLPrograms( renderer, extensions, capabilities ) { var parameterNames = [ "precision", "isWebGL2", "supportsVertexTextures", "outputEncoding", "instancing", "numMultiviewViews", "map", "mapEncoding", "matcap", "matcapEncoding", "envMap", "envMapMode", "envMapEncoding", "envMapCubeUV", - "lightMap", "lightMapEncoding", "aoMap", "emissiveMap", "emissiveMapEncoding", "bumpMap", "normalMap", "objectSpaceNormalMap", "tangentSpaceNormalMap", "clearcoatNormalMap", "displacementMap", "specularMap", + "lightMap", "lightMapEncoding", "aoMap", "emissiveMap", "emissiveMapEncoding", "bumpMap", "normalMap", "objectSpaceNormalMap", "tangentSpaceNormalMap", "clearcoatMap", "clearcoatRoughnessMap", "clearcoatNormalMap", "displacementMap", "specularMap", "roughnessMap", "metalnessMap", "gradientMap", "alphaMap", "combine", "vertexColors", "vertexTangents", "vertexUvs", "uvsVertexOnly", "fog", "useFog", "fogExp2", "flatShading", "sizeAttenuation", "logarithmicDepthBuffer", "skinning", @@ -212,6 +212,8 @@ function WebGLPrograms( renderer, extensions, capabilities ) { normalMap: !! material.normalMap, objectSpaceNormalMap: material.normalMapType === ObjectSpaceNormalMap, tangentSpaceNormalMap: material.normalMapType === TangentSpaceNormalMap, + clearcoatMap: !! material.clearcoatMap, + clearcoatRoughnessMap: !! material.clearcoatRoughnessMap, clearcoatNormalMap: !! material.clearcoatNormalMap, displacementMap: !! material.displacementMap, roughnessMap: !! material.roughnessMap, @@ -227,7 +229,7 @@ function WebGLPrograms( renderer, extensions, capabilities ) { vertexTangents: ( material.normalMap && material.vertexTangents ), vertexColors: material.vertexColors, - vertexUvs: !! material.map || !! material.bumpMap || !! material.normalMap || !! material.specularMap || !! material.alphaMap || !! material.emissiveMap || !! material.roughnessMap || !! material.metalnessMap || !! material.clearcoatNormalMap || !! material.displacementMap, + vertexUvs: !! material.map || !! material.bumpMap || !! material.normalMap || !! material.specularMap || !! material.alphaMap || !! material.emissiveMap || !! material.roughnessMap || !! material.metalnessMap || !! material.clearcoatMap || !! material.clearcoatRoughnessMap || !! material.clearcoatNormalMap || !! material.displacementMap, uvsVertexOnly: ! ( !! material.map || !! material.bumpMap || !! material.normalMap || !! material.specularMap || !! material.alphaMap || !! material.emissiveMap || !! material.roughnessMap || !! material.metalnessMap || !! material.clearcoatNormalMap ) && !! material.displacementMap, fog: !! fog, From fd065ebfe1c287ae7af256f060446fa887e1a356 Mon Sep 17 00:00:00 2001 From: Don McCurdy Date: Fri, 28 Feb 2020 18:45:30 -0800 Subject: [PATCH 2/2] MeshPhysicalMaterial: Serialize clearcoatMap and clearcoatRoughnessMap. --- src/loaders/MaterialLoader.js | 2 ++ src/materials/Material.js | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/loaders/MaterialLoader.js b/src/loaders/MaterialLoader.js index 82ab72454d3999..f415875142494e 100644 --- a/src/loaders/MaterialLoader.js +++ b/src/loaders/MaterialLoader.js @@ -261,6 +261,8 @@ MaterialLoader.prototype = Object.assign( Object.create( Loader.prototype ), { if ( json.gradientMap !== undefined ) material.gradientMap = getTexture( json.gradientMap ); + if ( json.clearcoatMap !== undefined ) material.clearcoatMap = getTexture( json.clearcoatMap ); + if ( json.clearcoatRoughnessMap !== undefined ) material.clearcoatRoughnessMap = getTexture( json.clearcoatRoughnessMap ); if ( json.clearcoatNormalMap !== undefined ) material.clearcoatNormalMap = getTexture( json.clearcoatNormalMap ); if ( json.clearcoatNormalScale !== undefined ) material.clearcoatNormalScale = new Vector2().fromArray( json.clearcoatNormalScale ); diff --git a/src/materials/Material.js b/src/materials/Material.js index 440d470af2c8fa..e746f468d5a920 100644 --- a/src/materials/Material.js +++ b/src/materials/Material.js @@ -177,6 +177,18 @@ Material.prototype = Object.assign( Object.create( EventDispatcher.prototype ), if ( this.clearcoat !== undefined ) data.clearcoat = this.clearcoat; if ( this.clearcoatRoughness !== undefined ) data.clearcoatRoughness = this.clearcoatRoughness; + if ( this.clearcoatMap && this.clearcoatMap.isTexture ) { + + data.clearcoatMap = this.clearcoatMap.toJSON( meta ).uuid; + + } + + if ( this.clearcoatRoughnessMap && this.clearcoatRoughnessMap.isTexture ) { + + data.clearcoatRoughnessMap = this.clearcoatRoughnessMap.toJSON( meta ).uuid; + + } + if ( this.clearcoatNormalMap && this.clearcoatNormalMap.isTexture ) { data.clearcoatNormalMap = this.clearcoatNormalMap.toJSON( meta ).uuid;