From 8ea7db4016bc56fa4683f4d82cf7999d2ee9ecd9 Mon Sep 17 00:00:00 2001 From: chubei-oppen Date: Fri, 29 Apr 2022 15:39:22 +0800 Subject: [PATCH] Add a r115 compatible version `perturbNormal2Arb` --- src/materials/MeshMatcapMaterial.js | 8 +++++++- src/materials/MeshStandardMaterial.js | 1 + .../normalmap_pars_fragment.glsl.js | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/materials/MeshMatcapMaterial.js b/src/materials/MeshMatcapMaterial.js index ea0992883b0248..9f11863f0b3c25 100644 --- a/src/materials/MeshMatcapMaterial.js +++ b/src/materials/MeshMatcapMaterial.js @@ -37,6 +37,12 @@ class MeshMatcapMaterial extends Material { this.defines = { 'MATCAP': '' }; + if ( Material.r115Compatible === true ) { + + this.defines[ 'PERTURB_NORMAL_R115_COMPATABILITY' ] = ''; + + } + this.type = 'MeshMatcapMaterial'; this.color = new Color( 0xffffff ); // diffuse @@ -69,7 +75,7 @@ class MeshMatcapMaterial extends Material { super.copy( source ); - this.defines = { 'MATCAP': '' }; + this.defines = source.defines; this.color.copy( source.color ); diff --git a/src/materials/MeshStandardMaterial.js b/src/materials/MeshStandardMaterial.js index bfd45ca92bd109..819abd6006c502 100644 --- a/src/materials/MeshStandardMaterial.js +++ b/src/materials/MeshStandardMaterial.js @@ -65,6 +65,7 @@ class MeshStandardMaterial extends Material { this.defines[ 'MULTISCATTERRING_R115_COMPATABILITY' ] = ''; this.defines[ 'ALPHA_R115_COMPATABILITY' ] = ''; + this.defines[ 'PERTURB_NORMAL_R115_COMPATABILITY' ] = ''; } diff --git a/src/renderers/shaders/ShaderChunk/normalmap_pars_fragment.glsl.js b/src/renderers/shaders/ShaderChunk/normalmap_pars_fragment.glsl.js index 006bdb7abee86c..8d427965dd5565 100644 --- a/src/renderers/shaders/ShaderChunk/normalmap_pars_fragment.glsl.js +++ b/src/renderers/shaders/ShaderChunk/normalmap_pars_fragment.glsl.js @@ -32,6 +32,22 @@ export default /* glsl */` vec2 st0 = dFdx( vUv.st ); vec2 st1 = dFdy( vUv.st ); + #ifdef PERTURB_NORMAL_R115_COMPATABILITY + + float scale = sign( st1.t * st0.s - st0.t * st1.s ); // we do not care about the magnitude + + vec3 S = normalize( ( q0 * st1.t - q1 * st0.t ) * scale ); + vec3 T = normalize( ( - q0 * st1.s + q1 * st0.s ) * scale ); + vec3 N = normalize( surf_norm ); + + mat3 tsn = mat3( S, T, N ); + + mapN.xy *= ( faceDirection * 2.0 - 1.0 ); + + return normalize( tsn * mapN ); + + #else + vec3 N = surf_norm; // normalized vec3 q1perp = cross( q1, N ); @@ -45,6 +61,8 @@ export default /* glsl */` return normalize( T * ( mapN.x * scale ) + B * ( mapN.y * scale ) + N * mapN.z ); + #endif + } #endif