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

fix: Fix compatibility breaks at three r157 on MToon #1304

Merged
merged 6 commits into from
Oct 6, 2023
Merged
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
139 changes: 104 additions & 35 deletions packages/three-vrm-materials-mtoon/src/shaders/mtoon.frag
Original file line number Diff line number Diff line change
Expand Up @@ -170,34 +170,65 @@ vec3 getDiffuse(
return col;
}

void RE_Direct_MToon( const in IncidentLight directLight, const in GeometricContext geometry, const in MToonMaterial material, const in float shadow, inout ReflectedLight reflectedLight ) {
float dotNL = clamp( dot( geometry.normal, directLight.direction ), -1.0, 1.0 );
vec3 irradiance = directLight.color;
#if THREE_VRM_THREE_REVISION >= 157
void RE_Direct_MToon( const in IncidentLight directLight, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in MToonMaterial material, const in float shadow, inout ReflectedLight reflectedLight ) {
float dotNL = clamp( dot( geometryNormal, directLight.direction ), -1.0, 1.0 );
vec3 irradiance = directLight.color;

#if THREE_VRM_THREE_REVISION < 132
#ifndef PHYSICALLY_CORRECT_LIGHTS
irradiance *= PI;
#endif
#endif

#if THREE_VRM_THREE_REVISION < 132
#ifndef PHYSICALLY_CORRECT_LIGHTS
irradiance *= PI;
// directSpecular will be used for rim lighting, not an actual specular
reflectedLight.directSpecular += irradiance;

irradiance *= dotNL;

float shading = getShading( dotNL, shadow, material.shadingShift );

// toon shaded diffuse
reflectedLight.directDiffuse += getDiffuse( material, shading, directLight.color );
}

void RE_IndirectDiffuse_MToon( const in vec3 irradiance, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in MToonMaterial material, inout ReflectedLight reflectedLight ) {
// indirect diffuse will use diffuseColor, no shadeColor involved
reflectedLight.indirectDiffuse += irradiance * BRDF_Lambert( material.diffuseColor );

// directSpecular will be used for rim lighting, not an actual specular
reflectedLight.directSpecular += irradiance;
}
#else
void RE_Direct_MToon( const in IncidentLight directLight, const in GeometricContext geometry, const in MToonMaterial material, const in float shadow, inout ReflectedLight reflectedLight ) {
float dotNL = clamp( dot( geometry.normal, directLight.direction ), -1.0, 1.0 );
vec3 irradiance = directLight.color;

#if THREE_VRM_THREE_REVISION < 132
#ifndef PHYSICALLY_CORRECT_LIGHTS
irradiance *= PI;
#endif
#endif
#endif

// directSpecular will be used for rim lighting, not an actual specular
reflectedLight.directSpecular += irradiance;
// directSpecular will be used for rim lighting, not an actual specular
reflectedLight.directSpecular += irradiance;

irradiance *= dotNL;
irradiance *= dotNL;

float shading = getShading( dotNL, shadow, material.shadingShift );
float shading = getShading( dotNL, shadow, material.shadingShift );

// toon shaded diffuse
reflectedLight.directDiffuse += getDiffuse( material, shading, directLight.color );
}
// toon shaded diffuse
reflectedLight.directDiffuse += getDiffuse( material, shading, directLight.color );
}

void RE_IndirectDiffuse_MToon( const in vec3 irradiance, const in GeometricContext geometry, const in MToonMaterial material, inout ReflectedLight reflectedLight ) {
// indirect diffuse will use diffuseColor, no shadeColor involved
reflectedLight.indirectDiffuse += irradiance * BRDF_Lambert( material.diffuseColor );
void RE_IndirectDiffuse_MToon( const in vec3 irradiance, const in GeometricContext geometry, const in MToonMaterial material, inout ReflectedLight reflectedLight ) {
// indirect diffuse will use diffuseColor, no shadeColor involved
reflectedLight.indirectDiffuse += irradiance * BRDF_Lambert( material.diffuseColor );

// directSpecular will be used for rim lighting, not an actual specular
reflectedLight.directSpecular += irradiance;
}
// directSpecular will be used for rim lighting, not an actual specular
reflectedLight.directSpecular += irradiance;
}
#endif

#define RE_Direct RE_Direct_MToon
#define RE_IndirectDiffuse RE_IndirectDiffuse_MToon
Expand Down Expand Up @@ -475,7 +506,7 @@ void main() {

// non perturbed normal for clearcoat among others

vec3 geometryNormal = normal;
vec3 nonPerturbedNormal = normal;

#ifdef OUTLINE
normal *= -1.0;
Expand Down Expand Up @@ -580,16 +611,30 @@ void main() {
// Since we want to take shadows into account of shading instead of irradiance,
// we had to modify the codes that multiplies the results of shadowmap into color of direct lights.

GeometricContext geometry;
#if THREE_VRM_THREE_REVISION >= 157
vec3 geometryPosition = - vViewPosition;
vec3 geometryNormal = normal;
vec3 geometryViewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );

vec3 geometryClearcoatNormal;

#ifdef USE_CLEARCOAT

geometryClearcoatNormal = clearcoatNormal;

geometry.position = - vViewPosition;
geometry.normal = normal;
geometry.viewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );
#endif
#else
GeometricContext geometry;

#ifdef CLEARCOAT
geometry.position = - vViewPosition;
geometry.normal = normal;
geometry.viewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );

geometry.clearcoatNormal = clearcoatNormal;
#ifdef USE_CLEARCOAT

geometry.clearcoatNormal = clearcoatNormal;

#endif
#endif

IncidentLight directLight;
Expand All @@ -609,7 +654,9 @@ void main() {

pointLight = pointLights[ i ];

#if THREE_VRM_THREE_REVISION >= 132
#if THREE_VRM_THREE_REVISION >= 157
getPointLightInfo( pointLight, geometryPosition, directLight );
#elif THREE_VRM_THREE_REVISION >= 132
getPointLightInfo( pointLight, geometry, directLight );
#else
getPointDirectLightIrradiance( pointLight, geometry, directLight );
Expand All @@ -621,7 +668,11 @@ void main() {
shadow = 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, shadow, reflectedLight );
#if THREE_VRM_THREE_REVISION >= 157
RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, shadow, reflectedLight );
#else
RE_Direct( directLight, geometry, material, shadow, reflectedLight );
#endif

}
#pragma unroll_loop_end
Expand All @@ -640,7 +691,9 @@ void main() {

spotLight = spotLights[ i ];

#if THREE_VRM_THREE_REVISION >= 132
#if THREE_VRM_THREE_REVISION >= 157
getSpotLightInfo( spotLight, geometryPosition, directLight );
#elif THREE_VRM_THREE_REVISION >= 132
getSpotLightInfo( spotLight, geometry, directLight );
#else
getSpotDirectLightIrradiance( spotLight, geometry, directLight );
Expand All @@ -652,7 +705,11 @@ void main() {
shadow = all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( spotShadowMap[ i ], spotLightShadow.shadowMapSize, spotLightShadow.shadowBias, spotLightShadow.shadowRadius, vSpotShadowCoord[ i ] ) : 1.0;
#endif

RE_Direct( directLight, geometry, material, shadow, reflectedLight );
#if THREE_VRM_THREE_REVISION >= 157
RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, shadow, reflectedLight );
#else
RE_Direct( directLight, geometry, material, shadow, reflectedLight );
#endif

}
#pragma unroll_loop_end
Expand All @@ -671,7 +728,9 @@ void main() {

directionalLight = directionalLights[ i ];

#if THREE_VRM_THREE_REVISION >= 132
#if THREE_VRM_THREE_REVISION >= 157
getDirectionalLightInfo( directionalLight, directLight );
#elif THREE_VRM_THREE_REVISION >= 132
getDirectionalLightInfo( directionalLight, geometry, directLight );
#else
getDirectionalDirectLightIrradiance( directionalLight, geometry, directLight );
Expand All @@ -683,7 +742,11 @@ void main() {
shadow = all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;
#endif

RE_Direct( directLight, geometry, material, shadow, reflectedLight );
#if THREE_VRM_THREE_REVISION >= 157
RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, shadow, reflectedLight );
#else
RE_Direct( directLight, geometry, material, shadow, reflectedLight );
#endif

}
#pragma unroll_loop_end
Expand Down Expand Up @@ -711,7 +774,11 @@ void main() {

vec3 irradiance = getAmbientLightIrradiance( ambientLightColor );

#if THREE_VRM_THREE_REVISION >= 133
#if THREE_VRM_THREE_REVISION >= 157
#if defined( USE_LIGHT_PROBES )
irradiance += getLightProbeIrradiance( lightProbe, geometryNormal );
#endif
#elif THREE_VRM_THREE_REVISION >= 133
irradiance += getLightProbeIrradiance( lightProbe, geometry.normal );
#else
irradiance += getLightProbeIrradiance( lightProbe, geometry );
Expand All @@ -722,7 +789,9 @@ void main() {
#pragma unroll_loop_start
for ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {

#if THREE_VRM_THREE_REVISION >= 133
#if THREE_VRM_THREE_REVISION >= 157
irradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometryNormal );
#elif THREE_VRM_THREE_REVISION >= 133
irradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry.normal );
#else
irradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry );
Expand Down