Skip to content

Commit 9fed0d9

Browse files
committed
Skybox with manual cubemap (TEMPORAL)
1 parent b2ef7e2 commit 9fed0d9

File tree

7 files changed

+263
-11
lines changed

7 files changed

+263
-11
lines changed

src/Shader/LightPass.hlsl

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ cbuffer ScreenQuadBuffer : register(b0)
1010
struct VertexOutput
1111
{
1212
float4 position : SV_Position;
13+
float2 uv : TEEXCOORD0;
1314
};
1415

1516
VertexOutput 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);
2426
Texture2DMS<float4> depthTex : register(t2);
2527
Texture2DMS<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+
2738
struct 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+
90179
PixelOutput 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
}

src/Shader/SkyboxPass.hlsl

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
struct VertexOutput
22
{
33
float4 position : SV_Position;
4+
float3 texCoord : TEXCOORD0; // Coordenadas de textura para la skybox
45
};
56

67
cbuffer VP : register(b0)
@@ -9,10 +10,67 @@ cbuffer VP : register(b0)
910
matrix Projection;
1011
}
1112

13+
Texture2D frontTex : register(t0);
14+
Texture2D backTex : register(t1);
15+
Texture2D leftTex : register(t2);
16+
Texture2D rightTex : register(t3);
17+
Texture2D topTex : register(t4);
18+
Texture2D bottomTex : register(t5);
19+
20+
SamplerState skyboxSampler : register(s0);
21+
1222
VertexOutput VertexMain(float4 position : POSITION0)
1323
{
1424
VertexOutput output;
1525
output.position = mul(position, View);
1626
output.position = mul(output.position, Projection);
27+
28+
// Normalizamos la posición para usarla como coordenadas para la skybox.
29+
// La normalización en el espacio de la cámara es importante.
30+
output.texCoord = normalize(position.xyz);
31+
//output.texCoord = float3(1.f, 1.f, 1.f);
32+
1733
return output;
18-
}
34+
}
35+
36+
struct PixelOutput
37+
{
38+
float4 screen : SV_Target0;
39+
};
40+
41+
// En PixelMain
42+
PixelOutput PixelMain(VertexOutput input)
43+
{
44+
PixelOutput output;
45+
46+
// Las coordenadas de textura están en el espacio de la cámara, así que mapea correctamente.
47+
if (abs(input.texCoord.x) > abs(input.texCoord.y) && abs(input.texCoord.x) > abs(input.texCoord.z))
48+
{
49+
// Frente y atrás (eje X)
50+
if (input.texCoord.x > 0.0)
51+
output.screen = frontTex.Sample(skyboxSampler, float2(-input.texCoord.z, -input.texCoord.y));
52+
else
53+
output.screen = backTex.Sample(skyboxSampler, float2(input.texCoord.z, -input.texCoord.y));
54+
}
55+
else if (abs(input.texCoord.y) > abs(input.texCoord.z))
56+
{
57+
// Arriba y abajo (eje Y)
58+
if (input.texCoord.y > 0.0)
59+
output.screen = topTex.Sample(skyboxSampler, float2(input.texCoord.x, input.texCoord.z));
60+
else
61+
output.screen = bottomTex.Sample(skyboxSampler, float2(input.texCoord.x, -input.texCoord.z));
62+
}
63+
else
64+
{
65+
// Derecha e izquierda (eje Z)
66+
if (input.texCoord.z > 0.0)
67+
output.screen = rightTex.Sample(skyboxSampler, float2(input.texCoord.x, input.texCoord.y));
68+
else
69+
output.screen = leftTex.Sample(skyboxSampler, float2(-input.texCoord.x, input.texCoord.y));
70+
}
71+
72+
output.screen = float4(1.f, 0.f, 0.f, 1.f);
73+
74+
return output;
75+
}
76+

src/private/Core/Editor/Editor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void Editor::Init() {
1414

1515
ImFontConfig fontCfg = { };
1616
fontCfg.FontDataOwnedByAtlas = false;
17-
io.Fonts->AddFontFromMemoryTTF(Roboto, nRobotoSize, 16.f, &fontCfg);
17+
io.Fonts->AddFontFromMemoryTTF(Roboto, nRobotoSize, 20.f, &fontCfg);
1818

1919
/*
2020
https://github.com/ocornut/imgui/issues/707#issuecomment-2732535348

src/private/Core/GameObject/Component/Mesh.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,9 @@ void Mesh::Render() {
194194
this->m_list->SetGraphicsRootSignature(this->m_rootSig.Get());
195195

196196
Descriptor wvpDesc = this->m_cbv_srvHeap->GetDescriptor(this->m_nWvpIndex);
197+
Descriptor samplerDesc = dynamic_cast<D3D12*>(this->m_renderer)->m_samplerHeap->GetDescriptor(this->m_nSamplerIndex);
197198

198-
this->m_list->SetGraphicsRootDescriptorTable(0, this->m_samplerDescriptor.gpuHandle);
199+
this->m_list->SetGraphicsRootDescriptorTable(0, samplerDesc.gpuHandle);
199200
this->m_list->SetGraphicsRootDescriptorTable(3, wvpDesc.gpuHandle);
200201

201202
int i = 0;
@@ -360,7 +361,7 @@ void Mesh::InitSampler(D3D12* renderer) {
360361

361362
renderer->m_samplerHeap->Allocate(1);
362363
this->m_nSamplerIndex = renderer->m_samplerHeap->GetLastDescriptorIndex();
363-
this->m_samplerDescriptor = renderer->m_samplerHeap->GetDescriptor(this->m_nSamplerIndex);
364+
Descriptor samplerDescriptor = renderer->m_samplerHeap->GetDescriptor(this->m_nSamplerIndex);
364365

365366
D3D12_SAMPLER_DESC samplerDesc = { };
366367
samplerDesc.AddressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
@@ -371,7 +372,7 @@ void Mesh::InitSampler(D3D12* renderer) {
371372
samplerDesc.MaxLOD = D3D12_FLOAT32_MAX;
372373
samplerDesc.MaxAnisotropy = 1;
373374

374-
this->m_dev->CreateSampler(&samplerDesc, this->m_samplerDescriptor.cpuHandle);
375+
this->m_dev->CreateSampler(&samplerDesc, samplerDescriptor.cpuHandle);
375376
}
376377

377378
/*

src/private/Core/Renderer/D3D12.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,8 @@ void D3D12::Update() {
283283
this->ResourceBarrier(actualBuffer, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT);
284284

285285
ID3D12DescriptorHeap* sqDescriptorHeaps[] = {
286-
this->m_cbvSrvHeap->m_heap.Get()
286+
this->m_cbvSrvHeap->m_heap.Get(),
287+
this->m_samplerHeap->m_heap.Get()
287288
};
288289
this->m_list->SetDescriptorHeaps(_countof(sqDescriptorHeaps), sqDescriptorHeaps);
289290

0 commit comments

Comments
 (0)