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

MeshPhysicalMaterial: Add clearcoatMap and clearcoatRoughnessMap. #18769

Merged
merged 2 commits into from
Mar 3, 2020
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
2 changes: 2 additions & 0 deletions src/loaders/MaterialLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down
12 changes: 12 additions & 0 deletions src/materials/Material.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 7 additions & 3 deletions src/materials/MeshPhysicalMaterial.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
47 changes: 30 additions & 17 deletions src/materials/MeshPhysicalMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ import { Color } from '../math/Color.js';
* @author WestLangley / http://github.com/WestLangley
*
* parameters = {
* reflectivity: <float>
* clearcoat: <float>
* clearcoatRoughness: <float>
*
* sheen: <Color>
*
* clearcoat: <float>,
* clearcoatMap: new THREE.Texture( <Image> ),
* clearcoatRoughness: <float>,
* clearcoatRoughnessMap: new THREE.Texture( <Image> ),
* clearcoatNormalScale: <Vector2>,
* clearcoatNormalMap: new THREE.Texture( <Image> ),
*
* reflectivity: <float>,
*
* sheen: <Color>,
*
* transparency: <float>
* }
*/

Expand All @@ -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 );
Expand All @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion src/renderers/shaders/ShaderChunk.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/renderers/shaders/ShaderChunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down

This file was deleted.

21 changes: 21 additions & 0 deletions src/renderers/shaders/ShaderChunk/clearcoat_pars_fragment.glsl.js
Original file line number Diff line number Diff line change
@@ -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
`;
20 changes: 18 additions & 2 deletions src/renderers/shaders/ShaderChunk/lights_physical_fragment.glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions src/renderers/shaders/ShaderLib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
}
] ),

Expand Down
2 changes: 1 addition & 1 deletion src/renderers/shaders/ShaderLib/meshphysical_frag.glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ varying vec3 vViewPosition;
#include <shadowmap_pars_fragment>
#include <bumpmap_pars_fragment>
#include <normalmap_pars_fragment>
#include <clearcoat_normalmap_pars_fragment>
#include <clearcoat_pars_fragment>
#include <roughnessmap_pars_fragment>
#include <metalnessmap_pars_fragment>
#include <logdepthbuf_pars_fragment>
Expand Down
4 changes: 4 additions & 0 deletions src/renderers/webgl/WebGLProgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' : '',
Expand Down Expand Up @@ -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' : '',
Expand Down
6 changes: 4 additions & 2 deletions src/renderers/webgl/WebGLPrograms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down