ywwynm / Unity-EAC-shader

Equi-Angular Cubemap shader for Unity
6 stars 3 forks source link

Some flips needed on some faces #2

Open urlando opened 3 years ago

urlando commented 3 years ago

Hello, thanks for the shader, it saved me a lot of time.

I had some troubles (half the cubemap needed flip) when I downloaded in 2K (with 4K Video Downloader) that video: https://www.youtube.com/watch?v=YAABA-Ri0YI&t=219s

in unity 2019.3.a3 it's now perfect (still the line problem on some edges, but I'll work on it soon)

modified shader here (lines 60/62/65/73/74):

  1. Shader "Custom/EAC-sphere"
  2. {
  3. Properties
  4. {
  5. _MainTex ("Texture", 2D) = "white" {}
  6. }
  7. SubShader
  8. {
  9. // No culling or depth
  10. Cull Front ZWrite Off ZTest Always // Cull front will flip normals
  11. Pass
  12. {
  13. CGPROGRAM
  14. pragma vertex vert

  15. pragma fragment frag

  16. include "UnityCG.cginc"

  17. struct appdata
  18. {
  19. float4 vertex : POSITION;
  20. float3 normal : NORMAL;
  21. };
  22. struct v2f
  23. {
  24. float4 vertex : SV_POSITION;
  25. float3 coord : TEXCOORD0;
  26. };
  27. v2f vert (appdata v)
  28. {
  29. v2f o;
  30. o.vertex = UnityObjectToClipPos(v.vertex);
  31. o.coord = v.normal;
  32. return o;
  33. }
  34. sampler2D _MainTex;
  35. float4 frag (v2f i) : SV_Target {
  36. float pi = UNITY_PI;
  37. float3 xyz = normalize(i.coord);
  38. float x = xyz.x, y = xyz.y, z = xyz.z;
  39. float u = 0, v = 0;
  40. float scale; // sphere coordinates to cube coordinates according to similar-triangle
  41. if (abs(x) >= abs(y) && abs(x) >= abs(z)) {
  42. scale = 1.0 / abs(x); // let's assume that radius of sphere is 1, which means u is 6.0 and v is 4.0
  43. if (x >= 0) { // right
  44. u = 5.0 - 4.0 atan(z scale) / pi;
  45. v = 3.0 + 4.0 atan(y scale) / pi;
  46. } else { // left
  47. u = 1.0 + 4.0 atan(z scale) / pi;
  48. v = 3.0 + 4.0 atan(y scale) / pi;
  49. }
  50. } else if (abs(y) >= abs(x) && abs(y) >= abs(z)) {
  51. scale = 1.0 / abs(y);
  52. if (y < 0) { // top // < instead of >=
  53. u = 5.0 + 4.0 atan(z scale) / pi;
  54. v = 1.0 - 4.0 atan(x scale) / pi; // - instead of +
  55. } else { // down
  56. u = 1.0 - 4.0 atan(z scale) / pi;
  57. v = 1.0 - 4.0 atan(x scale) / pi; // - instead of +
  58. }
  59. } else if (abs(z) >= abs(x) && abs(z) >= abs(y)) {
  60. scale = 1.0 / abs(z);
  61. if (z >= 0) { // front
  62. u = 3.0 + 4.0 atan(x scale) / pi;
  63. v = 3.0 + 4.0 atan(y scale) / pi;
  64. } else { // end
  65. u = 3.0 - 4.0 atan(y scale) / pi; // - instead of +
  66. v = 1.0 - 4.0 atan(x scale) / pi; // - instead of +
  67. }
  68. }
  69. return tex2D(_MainTex, float2(u / 6.0, 1.0 - v / 4.0));
  70. }
  71. ENDCG
  72. }
  73. }
  74. }
Nolkeg commented 3 years ago

Thanks, this fix the issue on Andriod. But on Windows, the original shader worked fine.