copy.glsl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* clang-format off */
  2. [vertex]
  3. #ifdef USE_GLES_OVER_GL
  4. #define lowp
  5. #define mediump
  6. #define highp
  7. #else
  8. precision highp float;
  9. precision highp int;
  10. #endif
  11. attribute highp vec4 vertex_attrib; // attrib:0
  12. /* clang-format on */
  13. #if defined(USE_CUBEMAP) || defined(USE_PANORAMA)
  14. attribute vec3 cube_in; // attrib:4
  15. #else
  16. attribute vec2 uv_in; // attrib:4
  17. #endif
  18. attribute vec2 uv2_in; // attrib:5
  19. #if defined(USE_CUBEMAP) || defined(USE_PANORAMA)
  20. varying vec3 cube_interp;
  21. #else
  22. varying vec2 uv_interp;
  23. #endif
  24. varying vec2 uv2_interp;
  25. #ifdef USE_COPY_SECTION
  26. uniform highp vec4 copy_section;
  27. #endif
  28. void main() {
  29. #if defined(USE_CUBEMAP) || defined(USE_PANORAMA)
  30. cube_interp = cube_in;
  31. #elif defined(USE_ASYM_PANO)
  32. uv_interp = vertex_attrib.xy;
  33. #else
  34. uv_interp = uv_in;
  35. #endif
  36. uv2_interp = uv2_in;
  37. gl_Position = vertex_attrib;
  38. #ifdef USE_COPY_SECTION
  39. uv_interp = copy_section.xy + uv_interp * copy_section.zw;
  40. gl_Position.xy = (copy_section.xy + (gl_Position.xy * 0.5 + 0.5) * copy_section.zw) * 2.0 - 1.0;
  41. #endif
  42. }
  43. /* clang-format off */
  44. [fragment]
  45. #define M_PI 3.14159265359
  46. #ifdef USE_GLES_OVER_GL
  47. #define lowp
  48. #define mediump
  49. #define highp
  50. #else
  51. #if defined(USE_HIGHP_PRECISION)
  52. precision highp float;
  53. precision highp int;
  54. #else
  55. precision mediump float;
  56. precision mediump int;
  57. #endif
  58. #endif
  59. #if defined(USE_CUBEMAP) || defined(USE_PANORAMA)
  60. varying vec3 cube_interp;
  61. #else
  62. varying vec2 uv_interp;
  63. #endif
  64. /* clang-format on */
  65. #ifdef USE_ASYM_PANO
  66. uniform highp mat4 pano_transform;
  67. uniform highp vec4 asym_proj;
  68. #endif
  69. #ifdef USE_CUBEMAP
  70. uniform samplerCube source_cube; // texunit:0
  71. #else
  72. uniform sampler2D source; // texunit:0
  73. #endif
  74. varying vec2 uv2_interp;
  75. #ifdef USE_MULTIPLIER
  76. uniform float multiplier;
  77. #endif
  78. #ifdef USE_CUSTOM_ALPHA
  79. uniform float custom_alpha;
  80. #endif
  81. #if defined(USE_PANORAMA) || defined(USE_ASYM_PANO)
  82. uniform highp mat4 sky_transform;
  83. vec4 texturePanorama(sampler2D pano, vec3 normal) {
  84. vec2 st = vec2(
  85. atan(normal.x, normal.z),
  86. acos(normal.y));
  87. if (st.x < 0.0)
  88. st.x += M_PI * 2.0;
  89. st /= vec2(M_PI * 2.0, M_PI);
  90. return texture2D(pano, st);
  91. }
  92. #endif
  93. void main() {
  94. #ifdef USE_PANORAMA
  95. vec3 cube_normal = normalize(cube_interp);
  96. cube_normal.z = -cube_normal.z;
  97. cube_normal = mat3(sky_transform) * cube_normal;
  98. cube_normal.z = -cube_normal.z;
  99. vec4 color = texturePanorama(source, cube_normal);
  100. #elif defined(USE_ASYM_PANO)
  101. // When an asymmetrical projection matrix is used (applicable for stereoscopic rendering i.e. VR) we need to do this calculation per fragment to get a perspective correct result.
  102. // Note that we're ignoring the x-offset for IPD, with Z sufficiently in the distance it becomes neglectible, as a result we could probably just set cube_normal.z to -1.
  103. // The Matrix[2][0] (= asym_proj.x) and Matrix[2][1] (= asym_proj.z) values are what provide the right shift in the image.
  104. vec3 cube_normal;
  105. cube_normal.z = -1000000.0;
  106. cube_normal.x = (cube_normal.z * (-uv_interp.x - asym_proj.x)) / asym_proj.y;
  107. cube_normal.y = (cube_normal.z * (-uv_interp.y - asym_proj.z)) / asym_proj.a;
  108. cube_normal = mat3(sky_transform) * mat3(pano_transform) * cube_normal;
  109. cube_normal.z = -cube_normal.z;
  110. vec4 color = texturePanorama(source, normalize(cube_normal.xyz));
  111. #elif defined(USE_CUBEMAP)
  112. vec4 color = textureCube(source_cube, normalize(cube_interp));
  113. #else
  114. vec4 color = texture2D(source, uv_interp);
  115. #endif
  116. #ifdef USE_NO_ALPHA
  117. color.a = 1.0;
  118. #endif
  119. #ifdef USE_CUSTOM_ALPHA
  120. color.a = custom_alpha;
  121. #endif
  122. #ifdef USE_MULTIPLIER
  123. color.rgb *= multiplier;
  124. #endif
  125. gl_FragColor = color;
  126. }