common-flow.chunk 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #if (CC_PIPELINE_TYPE == CC_PIPELINE_TYPE_FORWARD || CC_FORCE_FORWARD_SHADING)
  2. #if CC_FORWARD_ADD
  3. void CCSurfacesLighting(inout LightingResult lightingResultAccumulated, in SurfacesMaterialData surfaceData, in vec2 shadowBias)
  4. {
  5. vec3 worldPos;
  6. HIGHP_VALUE_FROM_STRUCT_DEFINED(worldPos, surfaceData.worldPos);
  7. CCSurfacesInitializeLightingResult(lightingResultAccumulated);
  8. LightingIntermediateData lightingData;
  9. CCSurfacesInitializeLightingIntermediateData(lightingData, surfaceData);
  10. LightingResult lightingResult;
  11. CCSurfacesLightingInitializeColorWithLighting(lightingResult.diffuseColorWithLighting, lightingResult.specularColorWithLighting, surfaceData, lightingData);
  12. lightingResultAccumulated.diffuseColorWithLighting = lightingResult.diffuseColorWithLighting;
  13. lightingResultAccumulated.specularColorWithLighting = lightingResult.specularColorWithLighting;
  14. #if CC_SURFACES_LIGHTING_2ND_LAYER_SPECULAR
  15. vec3 diff;
  16. SurfacesMaterialData surfaceData2ndSpecular;
  17. CCSurfacesGetSurfacesMaterialData2ndSpecular(surfaceData2ndSpecular, surfaceData);
  18. CCSurfacesLightingInitializeColorWithLighting(diff, lightingResult.specularColorWithLighting2ndSpecular, surfaceData2ndSpecular, lightingData);
  19. lightingResultAccumulated.specularColorWithLighting2ndSpecular = lightingResult.specularColorWithLighting2ndSpecular;
  20. #endif
  21. int numLights = CC_PIPELINE_TYPE == CC_PIPELINE_TYPE_FORWARD ? LIGHTS_PER_PASS : int(cc_lightDir[0].w);
  22. for (int i = 0; i < LIGHTS_PER_PASS; i++) {
  23. if (i >= numLights) break;
  24. vec3 lightDirWithLength = IS_RANGED_DIRECTIONAL_LIGHT(cc_lightPos[i].w) ? -normalize(cc_lightDir[i].xyz) : cc_lightPos[i].xyz - worldPos;
  25. CCSurfacesLightingCalculateIntermediateData_PerLight(lightingData, surfaceData, lightDirWithLength);
  26. CCSurfacesLightingCalculateColorWithLighting(lightingResult.diffuseColorWithLighting, lightingResult.specularColorWithLighting, surfaceData, lightingData);
  27. vec3 diffuseLighting, specularLighting;
  28. CCSurfacesLightingCalculateDirect(diffuseLighting, specularLighting, lightingData, cc_lightColor[i]);
  29. #if CC_SURFACES_LIGHTING_USE_FRESNEL
  30. vec3 fresnel = CCSurfaceLightingCalculateExtraFresnel(lightingData);
  31. #else
  32. vec3 fresnel = vec3(1.0);
  33. #endif
  34. lightingResult.fresnel = fresnel;
  35. float shadow = 1.0;
  36. #if CC_RECEIVE_SHADOW && CC_SHADOW_TYPE == CC_SHADOW_MAP
  37. if (cc_lightPos[i].w > 0.0 && cc_lightSizeRangeAngle[i].w > 0.0) {
  38. vec4 shadowPos = vec4(0.0), shadowPosWithDepthBias = vec4(0.0);
  39. if (CCSurfacesLightingEnableShadow(lightingData.NoL)) {
  40. shadowPos = cc_matLightViewProj * vec4(worldPos, 1.0);
  41. shadow = CCSpotShadowFactorBase(shadowPosWithDepthBias, shadowPos, worldPos, shadowBias);
  42. }
  43. #if CC_SURFACES_LIGHTING_TRANSMIT_DIFFUSE && CC_SURFACES_LIGHTING_USE_SHADOWMAP_TRANSMIT
  44. vec3 shadowNDCPos;
  45. bool isExceedShadowMap = !GetShadowNDCPos(shadowNDCPos, shadowPosWithDepthBias);
  46. lightingData.shadowPosAndDepth.xy = shadowNDCPos.xy;
  47. // notice: view space z value is negative
  48. lightingData.shadowPosAndDepth.z = isExceedShadowMap ? 0.0 : GetViewSpaceDepthFromNDCDepth_Perspective(shadowNDCPos.z, shadowPosWithDepthBias.w, cc_shadowInvProjDepthInfo.x, cc_shadowInvProjDepthInfo.y);
  49. lightingData.shadowPosAndDepth.w = isExceedShadowMap ? lightingData.shadowPosAndDepth.w : GetViewSpaceDepthFromNDCDepth_Perspective(SampleShadowMapSoft(shadowNDCPos, cc_spotShadowMap, cc_shadowWHPBInfo.xy), shadowPosWithDepthBias.w, cc_shadowInvProjDepthInfo.x, cc_shadowInvProjDepthInfo.y);
  50. #endif
  51. }
  52. #endif
  53. #if CC_USE_DEBUG_VIEW == CC_SURFACES_DEBUG_VIEW_COMPOSITE_AND_MISC
  54. if (!IS_DEBUG_VIEW_COMPOSITE_ENABLE_SHADOW)
  55. shadow = 1.0;
  56. #endif
  57. lightingResult.shadow = shadow;
  58. // attenuations
  59. float distAtt = 1.0;
  60. if(IS_RANGED_DIRECTIONAL_LIGHT(cc_lightPos[i].w)) {
  61. distAtt = GetOutOfRange(worldPos, cc_lightPos[i].xyz, cc_lightDir[i].xyz, cc_lightSizeRangeAngle[i].xyz, cc_lightBoundingSizeVS[i].xyz);
  62. } else {
  63. distAtt = CCSurfacesLightingCalculateDistanceAttenuation(lightingData, cc_lightSizeRangeAngle[i], cc_lightPos[i].w);
  64. }
  65. float angleAtt = 1.0;
  66. if (IS_SPOT_LIGHT(cc_lightPos[i].w)) {
  67. angleAtt = CCSurfacesLightingCalculateAngleAttenuation(lightingData, cc_lightSizeRangeAngle[i], -cc_lightDir[i].xyz);
  68. }
  69. float multiplier = distAtt * angleAtt;
  70. // output
  71. lightingData.angleAttenuation = angleAtt;
  72. lightingData.distAttenuation = distAtt;
  73. lightingResult.directDiffuse = diffuseLighting * multiplier;
  74. lightingResult.directSpecular = specularLighting * multiplier;
  75. CCSurfaceLightingCalculateDirectFresnel(lightingResult.directGF, lightingData, lightingResult.specularColorWithLighting, vec3(1.0));
  76. vec4 attenuatedLightColorAndIntensity = vec4(cc_lightColor[i].xyz, cc_lightColor[i].w * multiplier);
  77. #if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR
  78. vec3 transmitSpecularLighting;
  79. CCSurfacesLightingCalculateDirectTransmitSpecular(transmitSpecularLighting, lightingData, attenuatedLightColorAndIntensity);
  80. lightingResult.directTransmitSpecular = transmitSpecularLighting * multiplier * (1.0 - fresnel);
  81. #endif
  82. #if CC_SURFACES_LIGHTING_TRANSMIT_DIFFUSE
  83. LightingIntermediateData lightingDataTD;
  84. CCSurfacesGetLightingIntermediateDataTransmitDiffuse(lightingDataTD, lightingData, surfaceData);
  85. CCSurfacesLightingCalculateIntermediateData_PerLight(lightingDataTD, surfaceData, lightDirWithLength);
  86. CCSurfacesLightingCalculateDirectTransmitDiffuse(lightingResult.directTransmitDiffuse, lightingDataTD, attenuatedLightColorAndIntensity, lightingResult.shadow);
  87. #endif
  88. #if CC_SURFACES_LIGHTING_2ND_LAYER_SPECULAR
  89. LightingIntermediateData lightingData2ndSpecular;
  90. CCSurfacesInitializeLightingIntermediateData(lightingData2ndSpecular, surfaceData2ndSpecular);
  91. CCSurfacesLightingCalculateIntermediateData_PerLight(lightingData2ndSpecular, surfaceData, lightDirWithLength);
  92. #if !CC_SURFACES_LIGHTING_SHEEN
  93. CCSurfacesLightingCalculateDirect2ndSpecular(lightingResult.direct2ndSpecular, lightingData2ndSpecular, attenuatedLightColorAndIntensity, surfaceData2ndSpecular.intensity2ndSpecular, lightingResult.directSpecular);
  94. CCSurfaceLightingCalculateDirectFresnel(lightingResult.directGF2ndSpecular, lightingData2ndSpecular, lightingResult.specularColorWithLighting2ndSpecular, vec3(1.0));
  95. #else
  96. CCSurfacesLightingCalculateDirectSheen(lightingResult.direct2ndSpecular, lightingResult.directGF2ndSpecular, lightingData2ndSpecular, attenuatedLightColorAndIntensity, surfaceData2ndSpecular.intensity2ndSpecular);
  97. #endif
  98. lightingResult.direct2ndSpecular *= multiplier;
  99. #endif
  100. #if CC_SURFACES_LIGHTING_TT
  101. CCSurfacesLightingCalculateDirectTT(lightingResult, lightingData, attenuatedLightColorAndIntensity);
  102. #endif
  103. #if CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND
  104. lightingResult.specularColorWithLighting2ndSpecular *= surfaceData.color2ndSpecular; //temporary coloration
  105. CCSurfacesLightingCalculateDirectMultiLayerBlending(lightingResult, lightingData2ndSpecular);
  106. #endif
  107. // user-defined lighting model
  108. #ifdef CC_SURFACES_LIGHTING_MODIFY_FINAL_RESULT
  109. LightingMiscData miscData;
  110. miscData.lightType = cc_lightPos[i].w;
  111. miscData.lightPos = cc_lightPos[i].xyz;
  112. miscData.lightDir = cc_lightDir[i].xyz;
  113. miscData.lightColorAndIntensity = cc_lightColor[i];
  114. miscData.lightSizeRangeAngle = cc_lightSizeRangeAngle[i];
  115. SurfacesLightingModifyFinalResult(lightingResult, lightingData, surfaceData, miscData);
  116. #endif
  117. // accumulate per-light results
  118. CCSurfacesAccumulateLightingResult(lightingResultAccumulated, lightingResult);
  119. }
  120. }
  121. #else
  122. void CCSurfacesLighting(inout LightingResult lightingResult, in SurfacesMaterialData surfaceData, in vec2 shadowBias)
  123. {
  124. vec3 worldPos;
  125. HIGHP_VALUE_FROM_STRUCT_DEFINED(worldPos, surfaceData.worldPos);
  126. LightingIntermediateData lightingData;
  127. CCSurfacesInitializeLightingIntermediateData(lightingData, surfaceData);
  128. CCSurfacesInitializeLightingResult(lightingResult, surfaceData);
  129. CCSurfacesLightingInitializeColorWithLighting(lightingResult.diffuseColorWithLighting, lightingResult.specularColorWithLighting, surfaceData, lightingData);
  130. CCSurfacesLightingCalculateIntermediateData_PerLight(lightingData, surfaceData, -cc_mainLitDir.xyz);
  131. // CCSurfacesLightingCalculateShadow:
  132. lightingResult.shadow = 1.0;
  133. #if CC_RECEIVE_SHADOW && CC_SHADOW_TYPE == CC_SHADOW_MAP
  134. if (cc_mainLitDir.w > 0.0) {
  135. vec4 shadowPos = vec4(0.0), shadowPosWithDepthBias = vec4(0.0);
  136. vec4 shadowProjDepthInfo = vec4(0.0);
  137. vec3 shadowNDCPos;
  138. bool isExceedShadowMap = true;
  139. if (CCSurfacesLightingEnableShadow(lightingData.NoL)) {
  140. #if CC_DIR_LIGHT_SHADOW_TYPE == CC_DIR_LIGHT_SHADOW_CASCADED
  141. lightingResult.shadow = CCCSMFactorBase(worldPos, lightingData.N, shadowBias);
  142. #if CC_SURFACES_LIGHTING_TRANSMIT_DIFFUSE && CC_SURFACES_LIGHTING_USE_SHADOWMAP_TRANSMIT
  143. vec4 shadowProjInfo;
  144. vec3 shadowViewDir0, shadowViewDir1, shadowViewDir2;
  145. isExceedShadowMap = 0 > CCGetCSMLevel(shadowPosWithDepthBias, shadowProjDepthInfo, shadowProjInfo, shadowViewDir0, shadowViewDir1, shadowViewDir2, worldPos);
  146. GetShadowNDCPos(shadowNDCPos, shadowPosWithDepthBias);
  147. #endif
  148. #endif
  149. #if CC_DIR_LIGHT_SHADOW_TYPE == CC_DIR_LIGHT_SHADOW_UNIFORM
  150. shadowPos = cc_matLightViewProj * vec4(worldPos, 1.0);
  151. lightingResult.shadow = CCShadowFactorBase(shadowPosWithDepthBias, shadowPos, lightingData.N, shadowBias);
  152. shadowProjDepthInfo = cc_shadowProjDepthInfo;
  153. isExceedShadowMap = !GetShadowNDCPos(shadowNDCPos, shadowPosWithDepthBias);
  154. #endif
  155. }
  156. #if CC_SURFACES_LIGHTING_TRANSMIT_DIFFUSE && CC_SURFACES_LIGHTING_USE_SHADOWMAP_TRANSMIT
  157. lightingData.shadowPosAndDepth.xy = shadowNDCPos.xy;
  158. // notice: view space z value is negative
  159. lightingData.shadowPosAndDepth.z = isExceedShadowMap ? 0.0 : GetViewSpaceDepthFromNDCDepth_Orthgraphic(shadowNDCPos.z, shadowProjDepthInfo.x, shadowProjDepthInfo.y);
  160. lightingData.shadowPosAndDepth.w = isExceedShadowMap ? lightingData.shadowPosAndDepth.w : GetViewSpaceDepthFromNDCDepth_Orthgraphic(SampleShadowMapSoft(shadowNDCPos, cc_shadowMap, cc_shadowWHPBInfo.xy), shadowProjDepthInfo.x, shadowProjDepthInfo.y);
  161. #endif
  162. }
  163. #endif
  164. // CCSurfacesLightingCalculateBaked:
  165. lightingResult.lightmapColor = vec3(0.0);
  166. #if CC_SURFACES_USE_LIGHT_MAP && !CC_FORWARD_ADD
  167. float lightmapShadow, lightmapAO;
  168. GetLightMapColor(lightingResult.lightmapColor, lightmapShadow, lightmapAO, cc_lightingMap, FSInput_lightMapUV.xy, FSInput_lightMapUV.z, surfaceData.worldNormal);
  169. #if CC_SURFACES_USE_LIGHT_MAP == LIGHT_MAP_TYPE_INDIRECT_OCCLUSION
  170. lightingResult.shadow *= lightmapShadow;
  171. #endif
  172. // env lighting hasn't baked with static / stationary type, so ao need to be output all the time
  173. lightingResult.ao *= lightmapAO;
  174. #endif
  175. // apply screen-space shadow and ao
  176. // lightingResult.shadow/ao *= XXX;
  177. #if CC_USE_DEBUG_VIEW == CC_SURFACES_DEBUG_VIEW_COMPOSITE_AND_MISC
  178. if (!IS_DEBUG_VIEW_COMPOSITE_ENABLE_SHADOW)
  179. lightingResult.shadow = 1.0;
  180. if (!IS_DEBUG_VIEW_COMPOSITE_ENABLE_AO)
  181. lightingResult.ao = 1.0;
  182. #endif
  183. // apply SSR, local probe & global probe
  184. vec3 unused;
  185. CCSurfacesLightingCalculateEnvironment(lightingResult.environmentDiffuse, lightingResult.environmentSpecular, lightingData, cc_ambientSky.w);
  186. CCSurfaceLightingCalculateEnvironmentFresnel(lightingResult.environmentGF, unused, lightingData, lightingResult.specularColorWithLighting);
  187. // dynamic & stationary dir light
  188. lightingResult.directDiffuse = lightingResult.directSpecular = vec3(0.0);
  189. #if !CC_DISABLE_DIRECTIONAL_LIGHT && !CC_FORWARD_ADD
  190. CCSurfacesLightingCalculateColorWithLighting(lightingResult.diffuseColorWithLighting, lightingResult.specularColorWithLighting, surfaceData, lightingData);
  191. CCSurfacesLightingCalculateDirect(lightingResult.directDiffuse, lightingResult.directSpecular, lightingData, cc_mainLitColor);
  192. CCSurfaceLightingCalculateDirectFresnel(lightingResult.directGF, lightingData, lightingResult.specularColorWithLighting, lightingResult.environmentGF);
  193. #endif
  194. #if CC_SURFACES_LIGHTING_USE_FRESNEL
  195. lightingResult.fresnel = CCSurfaceLightingCalculateExtraFresnel(lightingData);
  196. #else
  197. lightingResult.fresnel = vec3(1.0);
  198. #endif
  199. #if CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR
  200. CCSurfacesLightingCalculateDirectTransmitSpecular(lightingResult.directTransmitSpecular, lightingData, cc_mainLitColor);
  201. CCSurfacesLightingCalculateEnvironmentTransmitSpecular(lightingResult.environmentTransmitSpecular, lightingData, cc_ambientSky.w);
  202. #endif
  203. #if CC_SURFACES_LIGHTING_TRANSMIT_DIFFUSE
  204. LightingIntermediateData lightingDataTD;
  205. CCSurfacesGetLightingIntermediateDataTransmitDiffuse(lightingDataTD, lightingData, surfaceData);
  206. CCSurfacesLightingCalculateIntermediateData_PerLight(lightingDataTD, surfaceData, -cc_mainLitDir.xyz);
  207. CCSurfacesLightingCalculateDirectTransmitDiffuse(lightingResult.directTransmitDiffuse, lightingDataTD, cc_mainLitColor, lightingResult.shadow);
  208. CCSurfacesLightingCalculateEnvironmentTransmitDiffuse(lightingResult.environmentTransmitDiffuse, lightingDataTD, cc_ambientSky.w, lightingResult.ao, -cc_mainLitDir.xyz);
  209. #endif
  210. #if CC_SURFACES_LIGHTING_2ND_LAYER_SPECULAR
  211. SurfacesMaterialData surfaceData2ndSpecular;
  212. CCSurfacesGetSurfacesMaterialData2ndSpecular(surfaceData2ndSpecular, surfaceData);
  213. LightingIntermediateData lightingData2ndSpecular;
  214. CCSurfacesInitializeLightingIntermediateData(lightingData2ndSpecular, surfaceData2ndSpecular);
  215. CCSurfacesLightingCalculateIntermediateData_PerLight(lightingData2ndSpecular, surfaceData2ndSpecular, -cc_mainLitDir.xyz);
  216. vec3 diff;
  217. CCSurfacesLightingInitializeColorWithLighting(diff, lightingResult.specularColorWithLighting2ndSpecular, surfaceData2ndSpecular, lightingData2ndSpecular);
  218. #if !CC_SURFACES_LIGHTING_SHEEN
  219. CCSurfacesLightingCalculateEnvironment2ndSpecular(lightingResult.environment2ndSpecular, lightingData2ndSpecular, cc_ambientSky.w, surfaceData2ndSpecular.intensity2ndSpecular, lightingResult.environmentSpecular);
  220. CCSurfaceLightingCalculateEnvironmentFresnel(lightingResult.environmentGF2ndSpecular, lightingResult.environmentSubLayerF, lightingData2ndSpecular, lightingResult.specularColorWithLighting2ndSpecular);
  221. CCSurfacesLightingCalculateDirect2ndSpecular(lightingResult.direct2ndSpecular, lightingData2ndSpecular, cc_mainLitColor, surfaceData2ndSpecular.intensity2ndSpecular, lightingResult.directSpecular);
  222. CCSurfaceLightingCalculateDirectFresnel(lightingResult.directGF2ndSpecular, lightingData2ndSpecular, lightingResult.specularColorWithLighting2ndSpecular, lightingResult.environmentGF2ndSpecular);
  223. #else
  224. CCSurfacesLightingCalculateDirectSheen(lightingResult.direct2ndSpecular, lightingResult.directGF2ndSpecular, lightingData2ndSpecular, cc_mainLitColor, surfaceData2ndSpecular.intensity2ndSpecular);
  225. CCSurfacesLightingCalculateEnvironmentSheen(lightingResult.environment2ndSpecular, lightingResult.environmentGF2ndSpecular, lightingData2ndSpecular, cc_ambientSky.w, surfaceData2ndSpecular.intensity2ndSpecular);
  226. #endif
  227. #endif
  228. #if CC_SURFACES_LIGHTING_TT
  229. CCSurfacesLightingCalculateDirectTT(lightingResult, lightingData, cc_mainLitColor);
  230. #endif
  231. #if CC_SURFACES_LIGHTING_USE_MULTIPLE_LAYER_BLEND
  232. lightingResult.specularColorWithLighting2ndSpecular *= surfaceData.color2ndSpecular; //temporary coloration
  233. CCSurfacesLightingCalculateDirectMultiLayerBlending(lightingResult, lightingData2ndSpecular);
  234. CCSurfacesLightingCalculateEnvironmentMultiLayerBlending(lightingResult, lightingData2ndSpecular);
  235. #endif
  236. // user-defined lighting model
  237. #ifdef CC_SURFACES_LIGHTING_MODIFY_FINAL_RESULT
  238. LightingMiscData miscData;
  239. miscData.lightType = LIGHT_TYPE_DIRECTIONAL;
  240. miscData.lightPos = vec3(0.0);
  241. miscData.lightDir = cc_mainLitDir.xyz;
  242. miscData.lightColorAndIntensity = cc_mainLitColor;
  243. miscData.lightSizeRangeAngle = vec4(0.0, 0.0, 0.0, 0.0);
  244. SurfacesLightingModifyFinalResult(lightingResult, lightingData, surfaceData, miscData);
  245. #endif
  246. }
  247. #if CC_ENABLE_CLUSTERED_LIGHT_CULLING
  248. // #todo: cluster related lighting flow, invoke after CCSurfacesLighting
  249. // use same code as additive-pass
  250. // void CCSurfacesLighting_Cluster(inout LightingResult lightingResult, in SurfacesMaterialData surfaceData, in vec2 shadowBias)
  251. // {
  252. // }
  253. #endif // cluster lighting
  254. #endif // base pass
  255. #endif // forward shading