copy.glsl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 mediump float;
  9. precision mediump 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 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. precision mediump float;
  52. precision mediump int;
  53. #endif
  54. #if defined(USE_CUBEMAP) || defined(USE_PANORAMA)
  55. varying vec3 cube_interp;
  56. #else
  57. varying vec2 uv_interp;
  58. #endif
  59. /* clang-format on */
  60. #ifdef USE_ASYM_PANO
  61. uniform highp mat4 pano_transform;
  62. uniform highp vec4 asym_proj;
  63. #endif
  64. #ifdef USE_CUBEMAP
  65. uniform samplerCube source_cube; // texunit:0
  66. #else
  67. uniform sampler2D source; // texunit:0
  68. #endif
  69. varying vec2 uv2_interp;
  70. #ifdef USE_MULTIPLIER
  71. uniform float multiplier;
  72. #endif
  73. #ifdef USE_CUSTOM_ALPHA
  74. uniform float custom_alpha;
  75. #endif
  76. #if defined(USE_PANORAMA) || defined(USE_ASYM_PANO)
  77. uniform highp mat4 sky_transform;
  78. vec4 texturePanorama(sampler2D pano, vec3 normal) {
  79. vec2 st = vec2(
  80. atan(normal.x, normal.z),
  81. acos(normal.y));
  82. if (st.x < 0.0)
  83. st.x += M_PI * 2.0;
  84. st /= vec2(M_PI * 2.0, M_PI);
  85. return texture2D(pano, st);
  86. }
  87. #endif
  88. void main() {
  89. #ifdef USE_PANORAMA
  90. vec3 cube_normal = normalize(cube_interp);
  91. cube_normal.z = -cube_normal.z;
  92. cube_normal = mat3(sky_transform) * cube_normal;
  93. cube_normal.z = -cube_normal.z;
  94. vec4 color = texturePanorama(source, cube_normal);
  95. #elif defined(USE_ASYM_PANO)
  96. // 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.
  97. // 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.
  98. // The Matrix[2][0] (= asym_proj.x) and Matrix[2][1] (= asym_proj.z) values are what provide the right shift in the image.
  99. vec3 cube_normal;
  100. cube_normal.z = -1000000.0;
  101. cube_normal.x = (cube_normal.z * (-uv_interp.x - asym_proj.x)) / asym_proj.y;
  102. cube_normal.y = (cube_normal.z * (-uv_interp.y - asym_proj.z)) / asym_proj.a;
  103. cube_normal = mat3(sky_transform) * mat3(pano_transform) * cube_normal;
  104. cube_normal.z = -cube_normal.z;
  105. vec4 color = texturePanorama(source, normalize(cube_normal.xyz));
  106. #elif defined(USE_CUBEMAP)
  107. vec4 color = textureCube(source_cube, normalize(cube_interp));
  108. #else
  109. vec4 color = texture2D(source, uv_interp);
  110. #endif
  111. #ifdef USE_NO_ALPHA
  112. color.a = 1.0;
  113. #endif
  114. #ifdef USE_CUSTOM_ALPHA
  115. color.a = custom_alpha;
  116. #endif
  117. #ifdef USE_MULTIPLIER
  118. color.rgb *= multiplier;
  119. #endif
  120. gl_FragColor = color;
  121. }