deferred-lighting.effect 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd.
  2. CCEffect %{
  3. techniques:
  4. - passes:
  5. - vert: lighting-vs
  6. frag: lighting-fs
  7. pass: deferred-lighting
  8. rasterizerState:
  9. cullMode: none
  10. depthStencilState:
  11. depthTest: false
  12. depthWrite: false
  13. }%
  14. CCProgram lighting-vs %{
  15. precision highp float;
  16. #include <legacy/decode>
  17. #include <builtin/uniforms/cc-global>
  18. #include <common/common-define>
  19. out vec2 v_uv;
  20. void main () {
  21. vec4 position;
  22. CCDecode(position);
  23. CC_HANDLE_GET_CLIP_FLIP(position.xy);
  24. gl_Position = vec4(position.x, position.y, 1.0, 1.0);
  25. v_uv = a_texCoord;
  26. }
  27. }%
  28. CCProgram lighting-fs-tiled %{
  29. precision highp float;
  30. #include <builtin/uniforms/cc-global>
  31. #include <legacy/shading-standard-base>
  32. #include <legacy/shading-standard-additive>
  33. #if CC_ENABLE_CLUSTERED_LIGHT_CULLING == 1
  34. #include <legacy/shading-cluster-additive>
  35. #endif
  36. #include <legacy/output-standard>
  37. #include <legacy/fog-base>
  38. #include <common/math/octahedron-transform>
  39. #include <common/math/coordinates>
  40. #include <common/common-define>
  41. #pragma subpass
  42. #pragma subpassColor in albedoMap
  43. #pragma subpassColor in normalMap
  44. #pragma subpassColor in emissiveMap
  45. #pragma subpassDepth in depthStencil
  46. layout(location = 0) out vec4 fragColor;
  47. vec4 screen2WS(vec3 coord) {
  48. vec3 ndc = vec3(
  49. 2.0 * (coord.x - cc_viewPort.x) / cc_viewPort.z - 1.0,
  50. 2.0 * (coord.y - cc_viewPort.y) / cc_viewPort.w - 1.0,
  51. #if __VERSION__ >= 450
  52. coord.z);
  53. #else
  54. 2.0 * coord.z - 1.0);
  55. #endif
  56. CC_HANDLE_SAMPLE_NDC_FLIP_STATIC(ndc.y);
  57. return GetWorldPosFromNDCPosRH(ndc, cc_matProj, cc_matViewProjInv);
  58. }
  59. void main () {
  60. StandardSurface s;
  61. vec4 albedo = subpassLoad(albedoMap);
  62. vec4 normal = subpassLoad(normalMap);
  63. vec4 emissive = subpassLoad(emissiveMap);
  64. float depth = subpassLoad(depthStencil).x;
  65. s.albedo = albedo;
  66. vec3 position = screen2WS(vec3(gl_FragCoord.xy, depth)).xyz;
  67. s.position = position;
  68. s.roughness = normal.z;
  69. s.normal = oct_to_float32x3(normal.xy);
  70. s.specularIntensity = 0.5;
  71. s.metallic = normal.w;
  72. s.emissive = emissive.xyz;
  73. s.occlusion = emissive.w;
  74. #if CC_RECEIVE_SHADOW
  75. s.shadowBias = vec2(0, 0);
  76. #endif
  77. // fixme: default value is 0, and give black result
  78. float fogFactor;
  79. CC_TRANSFER_FOG_BASE(vec4(position, 1), fogFactor);
  80. vec4 shadowPos;
  81. CC_TRANSFER_SHADOW_BASE(vec4(position, 1), shadowPos);
  82. vec4 color = CCStandardShadingBase(s, shadowPos) +
  83. #if CC_ENABLE_CLUSTERED_LIGHT_CULLING == 1
  84. CCClusterShadingAdditive(s, shadowPos);
  85. #else
  86. CCStandardShadingAdditive(s, shadowPos);
  87. #endif
  88. CC_APPLY_FOG_BASE(color, fogFactor);
  89. color = CCFragOutput(color);
  90. #if CC_USE_DEBUG_VIEW == CC_SURFACES_DEBUG_VIEW_SINGLE
  91. color = vec4(albedoMap.rgb, 1.0);
  92. #endif
  93. fragColor = color;
  94. }
  95. }%
  96. CCProgram lighting-fs %{
  97. precision highp float;
  98. #include <builtin/uniforms/cc-global>
  99. #include <legacy/shading-standard-base>
  100. #include <legacy/shading-standard-additive>
  101. #if CC_ENABLE_CLUSTERED_LIGHT_CULLING == 1
  102. #include <legacy/shading-cluster-additive>
  103. #endif
  104. #include <legacy/output-standard>
  105. #include <legacy/fog-base>
  106. #include <common/math/octahedron-transform>
  107. #include <common/math/coordinates>
  108. #include <common/common-define>
  109. in vec2 v_uv;
  110. #pragma rate albedoMap pass
  111. layout(binding = 0) uniform sampler2D albedoMap;
  112. #pragma rate normalMap pass
  113. layout(binding = 1) uniform sampler2D normalMap;
  114. #pragma rate emissiveMap pass
  115. layout(binding = 2) uniform sampler2D emissiveMap;
  116. #pragma rate depthStencil pass
  117. layout(binding = 3) uniform sampler2D depthStencil;
  118. layout(location = 0) out vec4 fragColor;
  119. vec4 screen2WS(vec3 coord) {
  120. vec3 ndc = vec3(
  121. 2.0 * (coord.x - cc_viewPort.x) / cc_viewPort.z - 1.0,
  122. 2.0 * (coord.y - cc_viewPort.y) / cc_viewPort.w - 1.0,
  123. #if __VERSION__ >= 450
  124. coord.z);
  125. #else
  126. 2.0 * coord.z - 1.0);
  127. #endif
  128. CC_HANDLE_SAMPLE_NDC_FLIP_STATIC(ndc.y);
  129. return GetWorldPosFromNDCPosRH(ndc, cc_matProj, cc_matViewProjInv);
  130. }
  131. void main () {
  132. StandardSurface s;
  133. vec4 albedo = texture(albedoMap, v_uv);
  134. vec4 normal = texture(normalMap, v_uv);
  135. vec4 emissive = texture(emissiveMap, v_uv);
  136. float depth = texture(depthStencil, v_uv).x;
  137. s.albedo = albedo;
  138. vec3 position = screen2WS(vec3(gl_FragCoord.xy, depth)).xyz;
  139. s.position = position;
  140. s.roughness = normal.z;
  141. s.normal = oct_to_float32x3(normal.xy);
  142. s.specularIntensity = 0.5;
  143. s.metallic = normal.w;
  144. s.emissive = emissive.xyz;
  145. s.occlusion = emissive.w;
  146. #if CC_RECEIVE_SHADOW
  147. s.shadowBias = vec2(0, 0);
  148. #endif
  149. // fixme: default value is 0, and give black result
  150. float fogFactor;
  151. CC_TRANSFER_FOG_BASE(vec4(position, 1), fogFactor);
  152. vec4 shadowPos;
  153. CC_TRANSFER_SHADOW_BASE(vec4(position, 1), shadowPos);
  154. vec4 color = CCStandardShadingBase(s, shadowPos) +
  155. #if CC_ENABLE_CLUSTERED_LIGHT_CULLING == 1
  156. CCClusterShadingAdditive(s, shadowPos);
  157. #else
  158. CCStandardShadingAdditive(s, shadowPos);
  159. #endif
  160. CC_APPLY_FOG_BASE(color, fogFactor);
  161. color = CCFragOutput(color);
  162. #if CC_USE_DEBUG_VIEW == CC_SURFACES_DEBUG_VIEW_SINGLE
  163. color = vec4(albedoMap.rgb, 1.0);
  164. #endif
  165. fragColor = color;
  166. }
  167. }%