@@ -10,12 +10,14 @@ cbuffer ScreenQuadBuffer : register(b0)
1010struct VertexOutput
1111{
1212 float4 position : SV_Position ;
13+ float2 uv : TEEXCOORD0;
1314};
1415
1516VertexOutput VertexMain (float4 position : POSITION0, float2 uv : TEXCOORD0 )
1617{
1718 VertexOutput output;
1819 output.position = position;
20+ output.uv = uv;
1921 return output;
2022}
2123
@@ -24,6 +26,15 @@ Texture2DMS<float4> normal : register(t1);
2426Texture2DMS <float4 > depthTex : register (t2);
2527Texture2DMS <float4 > orm : register (t3);
2628
29+ Texture2D frontTex : register (t4);
30+ Texture2D backTex : register (t5);
31+ Texture2D leftTex : register (t6);
32+ Texture2D rightTex : register (t7);
33+ Texture2D topTex : register (t8);
34+ Texture2D bottomTex : register (t9);
35+
36+ SamplerState skyboxSampler : register (s0);
37+
2738struct PixelOutput
2839{
2940 float4 screen : SV_Target0 ;
@@ -87,8 +98,97 @@ float3 ReconstructPosition(int2 pixelCoord, uint index)
8798 return worldPos.xyz;
8899}
89100
101+ float3 ViewDirectionFromUV (float2 uv)
102+ {
103+ // De UV [0,1] a NDC [-1,1]
104+ float2 ndc = uv * 2.0f - 1.0f ;
105+ // Aquí está el cambio clave - no invertimos Y en este punto
106+ // ndc.y *= -1.0f; // Esta línea estaba invirtiendo el eje Y
107+
108+ // Creamos un rayo en view space
109+ float3 rayView = normalize (mul (float4 (-ndc.x, ndc.y, 1.0f , 0.0f ), InverseProjection).xyz);
110+
111+ // Lo llevamos a world space
112+ float3 rayWorld = normalize (mul (float4 (rayView, 0.0f ), InverseView).xyz);
113+
114+ // Volvemos a poner el negativo - en realidad esto era correcto para cubemaps estándar
115+ return -rayWorld; // El negativo es necesario para el sistema de coordenadas estándar de cubemaps
116+ }
117+
118+
119+
120+ float3 SampleSkybox (float3 dir)
121+ {
122+ float3 absDir = abs (dir);
123+ float ma;
124+ float2 uv;
125+ float3 color;
126+
127+ if (absDir.x >= absDir.y && absDir.x >= absDir.z)
128+ {
129+ ma = absDir.x;
130+ if (dir.x > 0 )
131+ {
132+ // Right face (+X)
133+ uv = float2 (-dir.z, -dir.y) / ma * 0.5 + 0.5 ;
134+ color = rightTex.Sample (skyboxSampler, uv).rgb;
135+ }
136+ else
137+ {
138+ // Left face (-X)
139+ uv = float2 (dir.z, -dir.y) / ma * 0.5 + 0.5 ;
140+ color = leftTex.Sample (skyboxSampler, uv).rgb;
141+ }
142+ }
143+ else if (absDir.y >= absDir.x && absDir.y >= absDir.z)
144+ {
145+ ma = absDir.y;
146+ if (dir.y > 0 )
147+ {
148+ // Top face (+Y)
149+ uv = float2 (dir.x, dir.z) / ma * 0.5 + 0.5 ;
150+ color = topTex.Sample (skyboxSampler, uv).rgb;
151+ }
152+ else
153+ {
154+ // Bottom face (-Y)
155+ uv = float2 (dir.x, -dir.z) / ma * 0.5 + 0.5 ;
156+ color = bottomTex.Sample (skyboxSampler, uv).rgb;
157+ }
158+ }
159+ else
160+ {
161+ ma = absDir.z;
162+ if (dir.z > 0 )
163+ {
164+ // Front face (+Z)
165+ uv = float2 (dir.x, -dir.y) / ma * 0.5 + 0.5 ;
166+ color = frontTex.Sample (skyboxSampler, uv).rgb;
167+ }
168+ else
169+ {
170+ // Back face (-Z)
171+ uv = float2 (-dir.x, -dir.y) / ma * 0.5 + 0.5 ;
172+ color = backTex.Sample (skyboxSampler, uv).rgb;
173+ }
174+ }
175+
176+ return color;
177+ }
178+
90179PixelOutput PixelMain (VertexOutput input, uint index : SV_SampleIndex )
91180{
181+ float depth = depthTex.Load (input.position.xy, index).r;
182+ if (depth >= 1.0f - 1e-5 )
183+ {
184+ float3 dir = ViewDirectionFromUV (input.uv);
185+ float3 skyColor = SampleSkybox (dir);
186+
187+ PixelOutput outSky;
188+ outSky.screen = float4 (skyColor, 1.0f );
189+ return outSky;
190+ }
191+
92192 float3 lightPos = float3 (0.f , 1.f , 1.f );
93193 float3 lightColor = float3 (50.f , 50.f , 50.f );
94194
@@ -136,6 +236,6 @@ PixelOutput PixelMain(VertexOutput input, uint index : SV_SampleIndex)
136236 //color = pow(saturate(color), 1.0 / 2.2);
137237
138238 PixelOutput output;
139- output.screen = float4 (color, 1.f );
239+ output.screen = float4 (color.x, color.y, color.z , 1.f );
140240 return output;
141241}
0 commit comments