Skip to content

Commit

Permalink
add user mofidified lighting for add pass (cocos#14191)
Browse files Browse the repository at this point in the history
prepare for skin 4s and cluster shading
  • Loading branch information
jk20012001 authored Feb 9, 2023
1 parent 0d75cf3 commit 59fa406
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// for user modified lighting result
struct LightingMiscData
{
// local light source attributes, like CCForwardLight
float lightType;
vec3 lightPos, lightDir;
vec4 lightColorAndIntensity;
vec4 lightSizeRangeAngle;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"ver": "1.0.7",
"importer": "effect-header",
"imported": true,
"uuid": "f5326068-1588-43b4-9f15-c190688fc7c0",
"files": [],
"subMetas": {},
"userData": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//#include <surfaces/data-structures/XXX>

// #define CC_SURFACES_LIGHTING_MODIFY_FINAL_RESULT
// void SurfacesLightingModifyFinalResult(inout LightingResult result, in LightingIntermediateData lightingData, in SurfacesMaterialData surfaceData)
// void SurfacesLightingModifyFinalResult(inout LightingResult result, in LightingIntermediateData lightingData, in SurfacesMaterialData surfaceData, in LightingMiscData miscData)
// {
// }

1 change: 1 addition & 0 deletions editor/assets/chunks/lighting-models/includes/common.chunk
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Lighting-model common includes
#include <lighting-models/data-structures/lighting-intermediate-data>
#include <lighting-models/data-structures/lighting-result>
#include <lighting-models/data-structures/lighting-misc-data>

Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
#if (CC_PIPELINE_TYPE == CC_PIPELINE_TYPE_FORWARD || CC_FORCE_FORWARD_SHADING)
#if CC_FORWARD_ADD && !CC_ENABLE_CLUSTERED_LIGHT_CULLING
void CCSurfacesLighting(inout LightingResult lightingResult, in SurfacesMaterialData surfaceData, in vec2 shadowBias)
void CCSurfacesLighting(inout LightingResult lightingResultAccumulated, in SurfacesMaterialData surfaceData, in vec2 shadowBias)
{
vec3 worldPos;
HIGHP_VALUE_FROM_STRUCT_DEFINED(worldPos, surfaceData.worldPos);

CCSurfacesInitializeLightingResult(lightingResult);
CCSurfacesInitializeLightingResult(lightingResultAccumulated);

LightingIntermediateData lightingData;
CCSurfacesInitializeLightingIntermediateData(lightingData, surfaceData);

LightingResult lightingResult;
CCSurfacesLightingInitializeColorWithLighting(lightingResult.diffuseColorWithLighting, lightingResult.specularColorWithLighting, surfaceData, lightingData);
lightingResultAccumulated.diffuseColorWithLighting = lightingResult.diffuseColorWithLighting;
lightingResultAccumulated.specularColorWithLighting = lightingResult.specularColorWithLighting;

int numLights = CC_PIPELINE_TYPE == CC_PIPELINE_TYPE_FORWARD ? LIGHTS_PER_PASS : int(cc_lightDir[0].w);
for (int i = 0; i < LIGHTS_PER_PASS; i++) {
Expand Down Expand Up @@ -53,35 +56,52 @@
}
#endif

lightingResult.shadow = shadow;

float distAtt = CCSurfacesLightingCalculateDistanceAttenuation(lightingData, cc_lightSizeRangeAngle[i]);

float angleAtt = 1.0;
if (cc_lightPos[i].w > 0.0) {
angleAtt = CCSurfacesLightingCalculateAngleAttenuation(lightingData, cc_lightSizeRangeAngle[i], -cc_lightDir[i].xyz);
}

float multiplier = distAtt * angleAtt * shadow;
float multiplier = distAtt * angleAtt;

lightingResult.directDiffuse += diffuseLighting * multiplier;
lightingResult.directSpecular += specularLighting * multiplier * fresnel;
lightingResult.directDiffuse = diffuseLighting * multiplier;
lightingResult.directSpecular = specularLighting * multiplier * fresnel;

#if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR
vec3 transmitSpecularLighting;
CCSurfacesLightingCalculateDirectTransmitSpecular(transmitSpecularLighting, lightingData, cc_lightColor[i]);
lightingResult.directTransmitSpecular += transmitSpecularLighting * multiplier * (1.0 - fresnel);
lightingResult.directTransmitSpecular = transmitSpecularLighting * multiplier * (1.0 - fresnel);
#endif

#if CC_SURFACES_LIGHTING_TRANSMIT_DIFFUSE
lightingResult.directTransmitDiffuse += CCSurfacesLightingCalculateDirectTransmitDiffuse(lightingResult, lightingData, cc_lightColor[i]);
lightingResult.directTransmitDiffuse = CCSurfacesLightingCalculateDirectTransmitDiffuse(lightingResult, lightingData, cc_lightColor[i]);
#endif

#if CC_SURFACES_LIGHTING_TRT
vec3 TRTLighting;
LightingIntermediateData lightingDataTRT;
CCSurfacesGetLightingIntermediateDataTRT(lightingDataTRT, lightingData, surfaceData);
CCSurfacesLightingCalculateDirectTRT(TRTLighting, lightingDataTRT, cc_lightColor[i]);
lightingResult.directTRT += TRTLighting;
lightingResult.directTRT = TRTLighting;
#endif


// user-defined lighting model
#ifdef CC_SURFACES_LIGHTING_MODIFY_FINAL_RESULT
LightingMiscData miscData;
miscData.lightType = cc_lightPos[i].w;
miscData.lightPos = cc_lightPos[i].xyz;
miscData.lightDir = cc_lightDir[i].xyz;
miscData.lightColorAndIntensity = cc_lightColor[i];
miscData.lightSizeRangeAngle = cc_lightSizeRangeAngle[i];
SurfacesLightingModifyFinalResult(lightingResult, lightingData, surfaceData, miscData);
#endif

// accumulate per-light results
CCSurfacesAccumulateLightingResult(lightingResultAccumulated, lightingResult);
}
}
#else
Expand Down Expand Up @@ -170,17 +190,23 @@
// apply screen-space shadow and ao
// lightingResult.shadow/ao *= XXX;



//#todo: cluster related lighting flow
#if CC_ENABLE_CLUSTERED_LIGHT_CULLING
//#include <legacy/shading-cluster-additive>
#endif

// user-defined lighting model
#ifdef CC_SURFACES_LIGHTING_MODIFY_FINAL_RESULT
SurfacesLightingModifyFinalResult(lightingResult, lightingData, surfaceData);
LightingMiscData miscData;
miscData.lightType = LIGHT_TYPE_DIRECTIONAL;
miscData.lightPos = vec3(0.0);
miscData.lightDir = cc_mainLitDir.xyz;
miscData.lightColorAndIntensity = cc_mainLitColor;
miscData.lightSizeRangeAngle = vec4(0.0, 0.0, 0.0, 0.0);
SurfacesLightingModifyFinalResult(lightingResult, lightingData, surfaceData, miscData);
#endif
}
#endif
#endif
#if CC_ENABLE_CLUSTERED_LIGHT_CULLING
// #todo: cluster related lighting flow, invoke after CCSurfacesLighting
// use same code as additive-pass
// void CCSurfacesLighting_Cluster(inout LightingResult lightingResult, in SurfacesMaterialData surfaceData, in vec2 shadowBias)
// {
// }
#endif // cluster lighting
#endif // base pass
#endif // forward shading
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,30 @@
void LightingCalculateDirect(out vec3 lightingDiffuse, out vec3 lightingSpecular, in LightingIntermediateData lightingData, in vec4 lightSourceColorAndIntensity)
{
vec3 irradiance = vec3(lightingData.NoLSat) * lightSourceColorAndIntensity.rgb * lightSourceColorAndIntensity.w;


#if CC_SURFACES_LIGHTING_CALCULATE_DIFFUSE
lightingDiffuse = irradiance * DiffuseCoefficient_EnergyConservation;
#else
lightingDiffuse = vec3(0.0);
#endif

#if CC_SURFACES_LIGHTING_CALCULATE_SPECULAR
float roughness = lightingData.specularParam;
#if CC_SURFACES_LIGHTING_ANISOTROPIC
float rT, rB;
GetAnisotropicRoughness(roughness, lightingData.anisotropyShape, rT, rB);
float calcSpec = D_GGXAniso(rT, rB, lightingData.NoHSat, lightingData.H, lightingData.T, lightingData.B);
#else
#if CC_SURFACES_USE_LEGACY_COMPATIBLE_LIGHTING
float calcSpec = (roughness * 0.25 + 0.25) * D_GGXMobile(roughness, lightingData.NoHSat);
#if CC_SURFACES_LIGHTING_ANISOTROPIC
float rT, rB;
GetAnisotropicRoughness(roughness, lightingData.anisotropyShape, rT, rB);
float calcSpec = D_GGXAniso(rT, rB, lightingData.NoHSat, lightingData.H, lightingData.T, lightingData.B);
#else
float calcSpec = D_GGX(roughness, lightingData.NoHSat);
#if CC_SURFACES_USE_LEGACY_COMPATIBLE_LIGHTING
float calcSpec = (roughness * 0.25 + 0.25) * D_GGXMobile(roughness, lightingData.NoHSat);
#else
float calcSpec = D_GGX(roughness, lightingData.NoHSat);
#endif
#endif
#endif
lightingSpecular = irradiance * calcSpec;
#else
lightingSpecular = vec3(0.0);
#endif
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,17 @@ void CCSurfacesLightingCalculateDirect(out vec3 lightingDiffuse, out vec3 lighti

void CCSurfacesLightingCalculateEnvironment(out vec3 lightingDiffuse, out vec3 lightingSpecular, in LightingIntermediateData lightingData, float lightIntensity)
{
#if CC_SURFACES_LIGHTING_CALCULATE_DIFFUSE
lightingDiffuse = CalculateEnvironmentDiffuse(lightingData, lightIntensity);
#else
lightingDiffuse = vec3(0.0);
#endif

#if CC_SURFACES_LIGHTING_CALCULATE_SPECULAR
lightingSpecular = CalculateEnvironmentSpecular(lightingData, lightIntensity);
#else
lightingSpecular = vec3(0.0);
#endif
}

#if CC_SURFACES_LIGHTING_USE_FRESNEL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,11 @@
#ifndef CC_SURFACES_ENABLE_DEBUG_VIEW
#define CC_SURFACES_ENABLE_DEBUG_VIEW 1
#endif

// for skin
#ifndef CC_SURFACES_LIGHTING_CALCULATE_DIFFUSE
#define CC_SURFACES_LIGHTING_CALCULATE_DIFFUSE 1
#endif
#ifndef CC_SURFACES_LIGHTING_CALCULATE_SPECULAR
#define CC_SURFACES_LIGHTING_CALCULATE_SPECULAR 1
#endif
17 changes: 17 additions & 0 deletions editor/assets/chunks/surfaces/module-functions/standard-fs.chunk
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,23 @@ void CCSurfacesInitializeLightingResult(inout LightingResult lightingResult)
#endif
}

// Accumulated lighting results for additive pass
void CCSurfacesAccumulateLightingResult(inout LightingResult lightingResultAccumulated, in LightingResult lightingResult)
{
lightingResultAccumulated.directDiffuse += lightingResult.directDiffuse * lightingResult.shadow;
lightingResultAccumulated.directSpecular += lightingResult.directSpecular * lightingResult.shadow;

#if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR
lightingResultAccumulated.directTransmitSpecular += lightingResult.directTransmitSpecular;
#endif
#if CC_SURFACES_LIGHTING_TRANSMIT_DIFFUSE
lightingResultAccumulated.directTransmitDiffuse += lightingResult.directTransmitDiffuse;
#endif
#if CC_SURFACES_LIGHTING_TRT
lightingResultAccumulated.directTRT += lightingResult.directTRT;
#endif
}



#if CC_PIPELINE_TYPE == CC_PIPELINE_TYPE_DEFERRED
Expand Down
7 changes: 7 additions & 0 deletions editor/assets/chunks/surfaces/module-functions/toon-fs.chunk
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ void CCSurfacesInitializeLightingResult(inout LightingResult lightingResult)
lightingResult.directDiffuse = lightingResult.directSpecular = vec3(0.0);
}

// Accumulated lighting results for additive pass
void CCSurfacesAccumulateLightingResult(inout LightingResult lightingResultAccumulated, in LightingResult lightingResult)
{
lightingResultAccumulated.directDiffuse += lightingResult.directDiffuse * lightingResult.shadow;
lightingResultAccumulated.directSpecular += lightingResult.directSpecular * lightingResult.shadow;
}

// Lighting
#if CC_PIPELINE_TYPE == CC_PIPELINE_TYPE_DEFERRED
vec4 CCSurfacesDeferredOutput0(in SurfacesMaterialData surfaceData)
Expand Down
2 changes: 1 addition & 1 deletion editor/assets/effects/surfaces/standard.effect
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ CCProgram surface-fragment %{
// see <lighting-models/default-functions/XXXX> for more overrided functions, XXXX is lighting-model name
#include <lighting-models/includes/common>
#define CC_SURFACES_LIGHTING_MODIFY_FINAL_RESULT
void SurfacesLightingModifyFinalResult(inout LightingResult result, in LightingIntermediateData lightingData, in SurfacesMaterialData surfaceData)
void SurfacesLightingModifyFinalResult(inout LightingResult result, in LightingIntermediateData lightingData, in SurfacesMaterialData surfaceData, in LightingMiscData miscData)
{
}*/
}%
Expand Down
2 changes: 1 addition & 1 deletion editor/assets/effects/surfaces/water.effect
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ void SurfacesFragmentModifyTransmitParams(out vec4 transmitParams, out vec3 inSc
#include <surfaces/data-structures/standard>
#include <lighting-models/includes/common>
#define CC_SURFACES_LIGHTING_MODIFY_FINAL_RESULT
void SurfacesLightingModifyFinalResult(inout LightingResult result, in LightingIntermediateData lightingData, in SurfacesMaterialData surfaceData)
void SurfacesLightingModifyFinalResult(inout LightingResult result, in LightingIntermediateData lightingData, in SurfacesMaterialData surfaceData, in LightingMiscData miscData)
{
#if CC_SURFACES_LIGHTING_USE_FRESNEL
result.fresnel = saturate(result.fresnel * pbrParams.z);
Expand Down

0 comments on commit 59fa406

Please sign in to comment.