Skip to content

Commit

Permalink
split normal from struct GeometricContext to fixed adreno gpu precisi…
Browse files Browse the repository at this point in the history
…on bug (oppenfuture#75)

Co-authored-by: WenjieLiuu <[email protected]>
  • Loading branch information
chubei-oppen and WenjieLiuu authored Mar 16, 2022
1 parent cd76cf8 commit 2399671
Show file tree
Hide file tree
Showing 15 changed files with 169 additions and 77 deletions.
22 changes: 8 additions & 14 deletions examples/jsm/csm/CSMShader.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,8 @@ const CSMShader = {
GeometricContext geometry;
geometry.position = - vViewPosition;
geometry.normal = normal;
geometry.viewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );
#ifdef CLEARCOAT
geometry.clearcoatNormal = clearcoatNormal;
#endif
vec3 splitGeoNormal = normal;
IncidentLight directLight;
Expand All @@ -35,7 +29,7 @@ IncidentLight directLight;
directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ i ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ i ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
#endif
RE_Direct( directLight, geometry, material, reflectedLight );
RE_Direct( directLight, splitGeoNormal, geometry, material, reflectedLight );
}
#pragma unroll_loop_end
Expand All @@ -61,7 +55,7 @@ IncidentLight directLight;
directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( spotShadowMap[ i ], spotLightShadow.shadowMapSize, spotLightShadow.shadowBias, spotLightShadow.shadowRadius, vSpotShadowCoord[ i ] ) : 1.0;
#endif
RE_Direct( directLight, geometry, material, reflectedLight );
RE_Direct( directLight, splitGeoNormal, geometry, material, reflectedLight );
}
#pragma unroll_loop_end
Expand Down Expand Up @@ -114,7 +108,7 @@ IncidentLight directLight;
}
ReflectedLight prevLight = reflectedLight;
RE_Direct( directLight, geometry, material, reflectedLight );
RE_Direct( directLight, splitGeoNormal, geometry, material, reflectedLight );
bool shouldBlend = UNROLLED_LOOP_INDEX != CSM_CASCADES - 1 || UNROLLED_LOOP_INDEX == CSM_CASCADES - 1 && linearDepth < cascadeCenter;
float blendRatio = shouldBlend ? ratio : 1.0;
Expand Down Expand Up @@ -143,7 +137,7 @@ IncidentLight directLight;
#endif
if(linearDepth >= CSM_cascades[UNROLLED_LOOP_INDEX].x && (linearDepth < CSM_cascades[UNROLLED_LOOP_INDEX].y || UNROLLED_LOOP_INDEX == CSM_CASCADES - 1)) RE_Direct( directLight, geometry, material, reflectedLight );
if(linearDepth >= CSM_cascades[UNROLLED_LOOP_INDEX].x && (linearDepth < CSM_cascades[UNROLLED_LOOP_INDEX].y || UNROLLED_LOOP_INDEX == CSM_CASCADES - 1)) RE_Direct( directLight, splitGeoNormal, geometry, material, reflectedLight );
}
#pragma unroll_loop_end
Expand Down Expand Up @@ -172,7 +166,7 @@ IncidentLight directLight;
directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;
#endif
RE_Direct( directLight, geometry, material, reflectedLight );
RE_Direct( directLight, splitGeoNormal, geometry, material, reflectedLight );
}
#pragma unroll_loop_end
Expand Down Expand Up @@ -200,14 +194,14 @@ IncidentLight directLight;
vec3 irradiance = getAmbientLightIrradiance( ambientLightColor );
irradiance += getLightProbeIrradiance( lightProbe, geometry.normal );
irradiance += getLightProbeIrradiance( lightProbe, splitGeoNormal );
#if ( NUM_HEMI_LIGHTS > 0 )
#pragma unroll_loop_start
for ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {
irradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry.normal );
irradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], splitGeoNormal );
}
#pragma unroll_loop_end
Expand Down
6 changes: 3 additions & 3 deletions examples/jsm/nodes/materials/nodes/StandardNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class StandardNode extends Node {
roughness: roughnessNode,
bias: new SpecularMIPLevelNode( roughnessNode ),
viewNormal: new ExpressionNode( 'normal', 'v3' ),
worldNormal: new ExpressionNode( 'inverseTransformDirection( geometry.normal, viewMatrix )', 'v3' ),
worldNormal: new ExpressionNode( 'inverseTransformDirection( splitGeoNormal, viewMatrix )', 'v3' ),
gamma: true
};

Expand All @@ -142,7 +142,7 @@ class StandardNode extends Node {
roughness: clearcoatRoughnessNode,
bias: new SpecularMIPLevelNode( clearcoatRoughnessNode ),
viewNormal: new ExpressionNode( 'clearcoatNormal', 'v3' ),
worldNormal: new ExpressionNode( 'inverseTransformDirection( geometry.clearcoatNormal, viewMatrix )', 'v3' ),
worldNormal: new ExpressionNode( 'inverseTransformDirection( splitGeoClearcoatNormal, viewMatrix )', 'v3' ),
gamma: true
};

Expand Down Expand Up @@ -417,7 +417,7 @@ class StandardNode extends Node {
output.push(
ao.code,
'reflectedLight.indirectDiffuse *= ' + ao.result + ';',
'float dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );',
'float dotNV = saturate( dot( splitGeoNormal, geometry.viewDir ) );',
'reflectedLight.indirectSpecular *= computeSpecularOcclusion( dotNV, ' + ao.result + ', material.roughness );'
);

Expand Down
15 changes: 12 additions & 3 deletions examples/jsm/shaders/MMDToonShader.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,22 @@ struct BlinnPhongMaterial {
};
void RE_Direct_BlinnPhong( const in IncidentLight directLight, const in GeometricContext geometry, const in BlinnPhongMaterial material, inout ReflectedLight reflectedLight ) {
void RE_Direct_BlinnPhong(
const in IncidentLight directLight,
const in vec3 normal,
#ifdef USE_CLEARCOAT
const in vec3 ccNormal,
#endif
const in GeometricContext geometry,
const in BlinnPhongMaterial material,
inout ReflectedLight reflectedLight
) {
vec3 irradiance = getGradientIrradiance( geometry.normal, directLight.direction ) * directLight.color;
vec3 irradiance = getGradientIrradiance( normal, directLight.direction ) * directLight.color;
reflectedLight.directDiffuse += irradiance * BRDF_Lambert( material.diffuseColor );
reflectedLight.directSpecular += irradiance * BRDF_BlinnPhong( directLight.direction, geometry.viewDir, geometry.normal, material.specularColor, material.specularShininess ) * material.specularStrength;
reflectedLight.directSpecular += irradiance * BRDF_BlinnPhong( directLight.direction, geometry.viewDir, normal, material.specularColor, material.specularShininess ) * material.specularStrength;
}
Expand Down
38 changes: 28 additions & 10 deletions examples/jsm/shaders/SubsurfaceScatteringShader.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ const SubsurfaceScatteringShader = {
'uniform float thicknessAttenuation;',
'uniform vec3 thicknessColor;',

'void RE_Direct_Scattering(const in IncidentLight directLight, const in vec2 uv, const in GeometricContext geometry, inout ReflectedLight reflectedLight) {',
'void RE_Direct_Scattering(const in IncidentLight directLight, const in vec2 uv, const in vec3 normal, const in GeometricContext geometry, inout ReflectedLight reflectedLight) {',
' vec3 thickness = thicknessColor * texture2D(thicknessMap, uv).r;',
' vec3 scatteringHalf = normalize(directLight.direction + (geometry.normal * thicknessDistortion));',
' vec3 scatteringHalf = normalize(directLight.direction + (normal * thicknessDistortion));',
' float scatteringDot = pow(saturate(dot(geometry.viewDir, -scatteringHalf)), thicknessPower) * thicknessScale;',
' vec3 scatteringIllu = (scatteringDot + thicknessAmbient) * thickness;',
' reflectedLight.directDiffuse += scatteringIllu * thicknessAttenuation * directLight.color;',
Expand All @@ -69,14 +69,32 @@ const SubsurfaceScatteringShader = {

replaceAll(
ShaderChunk[ 'lights_fragment_begin' ],
'RE_Direct( directLight, geometry, material, reflectedLight );',
[
'RE_Direct( directLight, geometry, material, reflectedLight );',

'#if defined( SUBSURFACE ) && defined( USE_UV )',
' RE_Direct_Scattering(directLight, vUv, geometry, reflectedLight);',
'#endif',
].join( '\n' )
`RE_Direct(
directLight,
splitGeoNormal,
#ifdef USE_CLEARCOAT
splitGeoClearcoatNormal,
#endif
geometry,
material,
reflectedLight
);`,
`
RE_Direct(
directLight,
splitGeoNormal,
#ifdef USE_CLEARCOAT
splitGeoClearcoatNormal,
#endif
geometry,
material,
reflectedLight
);
#if defined( SUBSURFACE ) && defined( USE_UV )
RE_Direct_Scattering(directLight, vUv, splitGeoNormal, geometry, reflectedLight);
#endif
`
),

),
Expand Down
2 changes: 1 addition & 1 deletion examples/webgl_tiled_forward.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
' pointlight.color = lightColor.rgb;',
' pointlight.decay = lightColor.a;',
' getPointLightInfo( pointlight, geometry, directLight );',
' RE_Direct( directLight, geometry, material, reflectedLight );',
' RE_Direct( directLight, splitGeoNormal, geometry, material, reflectedLight );',
' }',
'}',
'#endif'
Expand Down
2 changes: 1 addition & 1 deletion src/renderers/shaders/ShaderChunk/aomap_fragment.glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default /* glsl */`
#if defined( USE_ENVMAP ) && defined( STANDARD )
float dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );
float dotNV = saturate( dot( splitGeoNormal, geometry.viewDir ) );
reflectedLight.indirectSpecular *= computeSpecularOcclusion( dotNV, ambientOcclusion, material.roughness );
Expand Down
4 changes: 0 additions & 4 deletions src/renderers/shaders/ShaderChunk/common.glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ struct ReflectedLight {
struct GeometricContext {
vec3 position;
vec3 normal;
vec3 viewDir;
#ifdef USE_CLEARCOAT
vec3 clearcoatNormal;
#endif
};
vec3 transformDirection( in vec3 dir, in mat4 matrix ) {
Expand Down
43 changes: 35 additions & 8 deletions src/renderers/shaders/ShaderChunk/lights_fragment_begin.glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ export default /* glsl */`
GeometricContext geometry;
geometry.position = - vViewPosition;
geometry.normal = normal;
geometry.viewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );
vec3 splitGeoNormal = normal;
#ifdef USE_CLEARCOAT
geometry.clearcoatNormal = clearcoatNormal;
vec3 splitGeoClearcoatNormal = clearcoatNormal;
#endif
Expand All @@ -47,7 +47,16 @@ IncidentLight directLight;
directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ i ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ i ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
#endif
RE_Direct( directLight, geometry, material, reflectedLight );
RE_Direct(
directLight,
splitGeoNormal,
#ifdef USE_CLEARCOAT
splitGeoClearcoatNormal,
#endif
geometry,
material,
reflectedLight
);
}
#pragma unroll_loop_end
Expand All @@ -73,7 +82,16 @@ IncidentLight directLight;
directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( spotShadowMap[ i ], spotLightShadow.shadowMapSize, spotLightShadow.shadowBias, spotLightShadow.shadowRadius, vSpotShadowCoord[ i ] ) : 1.0;
#endif
RE_Direct( directLight, geometry, material, reflectedLight );
RE_Direct(
directLight,
splitGeoNormal,
#ifdef USE_CLEARCOAT
splitGeoClearcoatNormal,
#endif
geometry,
material,
reflectedLight
);
}
#pragma unroll_loop_end
Expand All @@ -99,7 +117,16 @@ IncidentLight directLight;
directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;
#endif
RE_Direct( directLight, geometry, material, reflectedLight );
RE_Direct(
directLight,
splitGeoNormal,
#ifdef USE_CLEARCOAT
splitGeoClearcoatNormal,
#endif
geometry,
material,
reflectedLight
);
}
#pragma unroll_loop_end
Expand All @@ -114,7 +141,7 @@ IncidentLight directLight;
for ( int i = 0; i < NUM_RECT_AREA_LIGHTS; i ++ ) {
rectAreaLight = rectAreaLights[ i ];
RE_Direct_RectArea( rectAreaLight, geometry, material, reflectedLight );
RE_Direct_RectArea( rectAreaLight, splitGeoNormal, geometry, material, reflectedLight );
}
#pragma unroll_loop_end
Expand All @@ -127,14 +154,14 @@ IncidentLight directLight;
vec3 irradiance = getAmbientLightIrradiance( ambientLightColor );
irradiance += getLightProbeIrradiance( lightProbe, geometry.normal );
irradiance += getLightProbeIrradiance( lightProbe, splitGeoNormal );
#if ( NUM_HEMI_LIGHTS > 0 )
#pragma unroll_loop_start
for ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {
irradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry.normal );
irradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], splitGeoNormal );
}
#pragma unroll_loop_end
Expand Down
13 changes: 12 additions & 1 deletion src/renderers/shaders/ShaderChunk/lights_fragment_end.glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,18 @@ export default /* glsl */`
#if defined( RE_IndirectSpecular )
RE_IndirectSpecular( radiance, iblIrradiance, clearcoatRadiance, geometry, material, reflectedLight );
RE_IndirectSpecular(
radiance,
iblIrradiance,
clearcoatRadiance,
splitGeoNormal,
#ifdef USE_CLEARCOAT
splitGeoClearcoatNormal,
#endif
geometry,
material,
reflectedLight
);
#endif
`;
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ export default /* glsl */`
#if defined( USE_ENVMAP ) && defined( STANDARD ) && defined( ENVMAP_TYPE_CUBE_UV )
iblIrradiance += getIBLIrradiance( geometry.normal );
iblIrradiance += getIBLIrradiance( splitGeoNormal );
#endif
#endif
#if defined( USE_ENVMAP ) && defined( RE_IndirectSpecular )
radiance += getIBLRadiance( geometry.viewDir, geometry.normal, material.roughness );
radiance += getIBLRadiance( geometry.viewDir, splitGeoNormal, material.roughness );
#ifdef USE_CLEARCOAT
clearcoatRadiance += getIBLRadiance( geometry.viewDir, geometry.clearcoatNormal, material.clearcoatRoughness );
clearcoatRadiance += getIBLRadiance( geometry.viewDir, splitGeoClearcoatNormal, material.clearcoatRoughness );
#endif
Expand Down
Loading

0 comments on commit 2399671

Please sign in to comment.