skybox.effect 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. CCEffect %{
  2. techniques:
  3. - passes:
  4. - vert: sky-vs:vert
  5. frag: sky-fs:frag
  6. properties: &props
  7. environmentMap: { value: grey }
  8. priority: max - 10
  9. rasterizerState: &r1
  10. cullMode: none
  11. depthStencilState: &d1
  12. depthTest: true
  13. depthWrite: false
  14. - vert: sky-vs:vert
  15. frag: sky-fs:frag
  16. propertyIndex: 0
  17. priority: max - 10
  18. phase: deferred-forward
  19. rasterizerState: *r1
  20. depthStencilState: *d1
  21. }%
  22. CCProgram sky-vs %{
  23. precision highp float;
  24. #include <builtin/uniforms/cc-global>
  25. #include <legacy/decode>
  26. out mediump vec4 viewDir;
  27. vec4 vert () {
  28. CCDecode(viewDir);
  29. mat4 matViewRotOnly = mat4(mat3(cc_matView));
  30. vec4 pos = matViewRotOnly * viewDir;
  31. // orthographic projection adaptation
  32. if (cc_matProj[3].w > 0.0) {
  33. mat4 matProj = cc_matProj;
  34. // default ortho height 10 approximate to default FOV 45
  35. // stretch x by 2 to remedy perspective effect
  36. // w = sin(45/2), h = 2 * w, _11 = 2 / w, _22 = 2 / h
  37. matProj[0].x = 5.2;
  38. matProj[1].y = 2.6;
  39. matProj[2].zw = vec2(-1.0);
  40. matProj[3].zw = vec2(0.0);
  41. // position is modified inside branches to work around a vk driver bug seen in
  42. // low-end Qualcomm devices (Adreno 512, Oppo R11, cocos-creator/3d-tasks#9236)
  43. pos = matProj * pos;
  44. } else {
  45. pos = cc_matProj * pos;
  46. }
  47. pos.z = 0.99999 * pos.w;
  48. return pos;
  49. }
  50. }%
  51. CCProgram sky-fs %{
  52. precision mediump float;
  53. #include <builtin/uniforms/cc-global>
  54. #include <builtin/uniforms/cc-environment>
  55. #include <common/texture/texture-lod>
  56. #include <common/data/unpack>
  57. #include <common/color/gamma>
  58. #include <common/color/tone-mapping>
  59. #include <legacy/output-standard>
  60. #include <common/math/coordinates>
  61. in mediump vec4 viewDir;
  62. uniform samplerCube environmentMap;
  63. vec4 frag () {
  64. vec3 rotationDir = RotationVecFromAxisY(viewDir.xyz, cc_surfaceTransform.z, cc_surfaceTransform.w);
  65. #if USE_RGBE_CUBEMAP
  66. vec3 c = unpackRGBE(fragTextureLod(environmentMap, rotationDir.xyz, 0.0));
  67. #else
  68. vec3 c = SRGBToLinear(fragTextureLod(environmentMap, rotationDir.xyz, 0.0).rgb);
  69. #endif
  70. vec4 color = vec4(c * cc_ambientSky.w, 1.0);
  71. #if CC_USE_RGBE_OUTPUT
  72. color = packRGBE(color.rgb);
  73. #else
  74. color.rgb = HDRToLDR(color.rgb);
  75. color.rgb = LinearToSRGB(color.rgb);
  76. #endif
  77. //todo: change to CCFragOutput when sky render queue has been fixed with custom pipeline
  78. //return CCFragOutput(color);
  79. return color;
  80. }
  81. }%