From 94894c124660b53da6518ce0f43efc36d6990e60 Mon Sep 17 00:00:00 2001 From: "Mr.doob" Date: Wed, 9 Oct 2019 19:02:10 +0200 Subject: [PATCH] MeshPhysicalMaterial: Initialise sheen by default and only use when is not black. --- examples/webgl_materials_physical_sheen.html | 43 ++++++++------------ src/materials/Material.js | 4 +- src/materials/MeshPhysicalMaterial.d.ts | 2 +- src/materials/MeshPhysicalMaterial.js | 5 +-- src/renderers/WebGLRenderer.js | 2 +- src/renderers/webgl/WebGLPrograms.js | 2 +- 6 files changed, 25 insertions(+), 33 deletions(-) diff --git a/examples/webgl_materials_physical_sheen.html b/examples/webgl_materials_physical_sheen.html index 1ead0fcfb4..34693f2690 100644 --- a/examples/webgl_materials_physical_sheen.html +++ b/examples/webgl_materials_physical_sheen.html @@ -37,7 +37,6 @@ var params = { nodeMaterial: true, color: new THREE.Color( 255, 0, 127 ), - sheenBRDF: true, sheen: new THREE.Color( 10, 10, 10 ), // corresponds to .04 reflectance roughness: .9, exposure: 2, @@ -46,7 +45,7 @@ // model new FBXLoader().load( 'models/fbx/cloth.fbx', function ( loadedModel ) { - mesh = loadedModel.children[0]; + mesh = loadedModel.children[ 0 ]; init(); @@ -75,6 +74,7 @@ nodeMaterial.metalness = new Nodes.FloatNode( 0 ); nodeMaterial.roughness = new Nodes.FloatNode(); nodeMaterial.color = new Nodes.ColorNode( params.color.clone() ); + nodeMaterial.sheen = new Nodes.ColorNode( params.sheen.clone() ); // @@ -82,7 +82,7 @@ new THREE.SphereBufferGeometry( 1, 100, 100 ), material ); - scene.add(sphere); + scene.add( sphere ); camera.position.set( - 12, 7, 4 ); @@ -120,28 +120,27 @@ function onUpdate() { - mesh.material = sphere.material = params.nodeMaterial - ? nodeMaterial - : material; + mesh.material = sphere.material = params.nodeMaterial ? nodeMaterial : material; - material.sheen = params.sheenBRDF - ? new THREE.Color() - : null; + } + + function onSheenUpdate() { + + var newSheenEnabled = params.color.r > 0 || params.color.g > 0 || params.color.b > 0; + var oldSheenEnabled = material.sheen.r > 0 || material.sheen.g > 0 || material.sheen.b > 0; - material.needsUpdate = true; + if ( newSheenEnabled !== oldSheenEnabled ) { - nodeMaterial.sheen = params.sheenBRDF - ? new Nodes.ColorNode( material.sheen ) - : undefined; + material.needsUpdate = true; + nodeMaterial.needsCompile = true; - nodeMaterial.needsCompile = true; + } } gui.add( params, 'nodeMaterial' ).onChange( onUpdate ); gui.addColor( params, 'color' ); - gui.add( params, 'sheenBRDF' ).onChange( onUpdate ); - gui.addColor( params, 'sheen' ); + gui.addColor( params, 'sheen' ).onChange( onSheenUpdate ); gui.add( params, 'roughness', 0, 1 ); gui.add( params, 'exposure', 0, 3 ); gui.open(); @@ -172,26 +171,18 @@ function render() { - // - material.color.copy( params.color ).multiplyScalar( 1 / 255 ); + material.sheen.copy( params.sheen ).multiplyScalar( 1 / 255 ); material.roughness = params.roughness; // nodeMaterial.color.value.copy( material.color ); + nodeMaterial.sheen.value.copy( params.sheen ).multiplyScalar( 1 / 255 ); nodeMaterial.roughness.value = params.roughness; // - if ( params.sheenBRDF ) { - - material.sheen.copy( params.sheen ).multiplyScalar( 1 / 255 ); - - } - - // - renderer.toneMappingExposure = params.exposure; renderer.render( scene, camera ); diff --git a/src/materials/Material.js b/src/materials/Material.js index f8d5470d02..e3f2e18de0 100644 --- a/src/materials/Material.js +++ b/src/materials/Material.js @@ -169,12 +169,12 @@ Material.prototype = Object.assign( Object.create( EventDispatcher.prototype ), if ( this.roughness !== undefined ) data.roughness = this.roughness; if ( this.metalness !== undefined ) data.metalness = this.metalness; - if ( this.sheen && this.sheen.isColor ) data.sheen = this.sheen.getHex(); if ( this.emissive && this.emissive.isColor ) data.emissive = this.emissive.getHex(); if ( this.emissiveIntensity && this.emissiveIntensity !== 1 ) data.emissiveIntensity = this.emissiveIntensity; if ( this.specular && this.specular.isColor ) data.specular = this.specular.getHex(); if ( this.shininess !== undefined ) data.shininess = this.shininess; + if ( this.clearcoat !== undefined ) data.clearcoat = this.clearcoat; if ( this.clearcoatRoughness !== undefined ) data.clearcoatRoughness = this.clearcoatRoughness; @@ -185,6 +185,8 @@ Material.prototype = Object.assign( Object.create( EventDispatcher.prototype ), } + if ( this.sheen && this.sheen.isColor ) data.sheen = this.sheen.getHex(); + 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; diff --git a/src/materials/MeshPhysicalMaterial.d.ts b/src/materials/MeshPhysicalMaterial.d.ts index 50aa34cc86..dd3f22cf3e 100644 --- a/src/materials/MeshPhysicalMaterial.d.ts +++ b/src/materials/MeshPhysicalMaterial.d.ts @@ -27,7 +27,7 @@ export class MeshPhysicalMaterial extends MeshStandardMaterial { clearcoat: number; clearcoatRoughness: number; - sheen: Color | null; + sheen: Color; clearcoatNormalScale: Vector2; clearcoatNormalMap: Texture | null; diff --git a/src/materials/MeshPhysicalMaterial.js b/src/materials/MeshPhysicalMaterial.js index 09e1469f2a..c5ef3f3bc9 100644 --- a/src/materials/MeshPhysicalMaterial.js +++ b/src/materials/MeshPhysicalMaterial.js @@ -35,7 +35,7 @@ function MeshPhysicalMaterial( parameters ) { this.clearcoat = 0.0; this.clearcoatRoughness = 0.0; - this.sheen = null; // null will disable sheen bsdf + this.sheen = new Color( 0x000000 ); this.clearcoatNormalScale = new Vector2( 1, 1 ); this.clearcoatNormalMap = null; @@ -67,8 +67,7 @@ MeshPhysicalMaterial.prototype.copy = function ( source ) { this.clearcoat = source.clearcoat; this.clearcoatRoughness = source.clearcoatRoughness; - if ( source.sheen ) this.sheen = ( this.sheen || new Color() ).copy( source.sheen ); - else this.sheen = null; + this.sheen.copy( source.sheen ); this.clearcoatNormalMap = source.clearcoatNormalMap; this.clearcoatNormalScale.copy( source.clearcoatNormalScale ); diff --git a/src/renderers/WebGLRenderer.js b/src/renderers/WebGLRenderer.js index bb9451508f..e9e63a60c0 100644 --- a/src/renderers/WebGLRenderer.js +++ b/src/renderers/WebGLRenderer.js @@ -2385,7 +2385,7 @@ function WebGLRenderer( parameters ) { uniforms.clearcoat.value = material.clearcoat; uniforms.clearcoatRoughness.value = material.clearcoatRoughness; - if ( material.sheen ) uniforms.sheen.value.copy( material.sheen ); + uniforms.sheen.value.copy( material.sheen ); if ( material.clearcoatNormalMap ) { diff --git a/src/renderers/webgl/WebGLPrograms.js b/src/renderers/webgl/WebGLPrograms.js index 0eb4f04d37..c4626120fb 100644 --- a/src/renderers/webgl/WebGLPrograms.js +++ b/src/renderers/webgl/WebGLPrograms.js @@ -179,7 +179,7 @@ function WebGLPrograms( renderer, extensions, capabilities ) { gradientMap: !! material.gradientMap, - sheen: !! material.sheen, + sheen: material.sheen && ( material.sheen.r > 0 || material.sheen.g > 0 || material.sheen.b > 0 ), combine: material.combine, -- GitLab