Skip to content

Commit 5a747de

Browse files
committed
Guizmo working
1 parent 0e53bc4 commit 5a747de

File tree

6 files changed

+112
-15
lines changed

6 files changed

+112
-15
lines changed

src/Shader/LightPass.hlsl

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,73 @@ float3 ReconstructPosition(int2 pixelCoord, uint index)
9898
return worldPos.xyz;
9999
}
100100

101+
// Función para obtener la dirección del rayo en el espacio mundial
101102
float3 ViewDirectionFromUV(float2 uv)
102-
{
103-
float2 uvOffset = uv * 2.0f - 1.0f;
104-
uvOffset.y *= -1.0f;
103+
{
104+
float2 ndc = uv * 2.0f - 1.0f;
105+
106+
float3 rayView = normalize(mul(float4(-ndc.x, ndc.y, 1.0f, 0.0f), InverseProjection).xyz);
107+
108+
float3 rayWorld = normalize(mul(float4(rayView, 0.0f), InverseView).xyz);
109+
110+
return rayWorld;
111+
}
112+
113+
float GridMask(float3 worldPos, float scale)
114+
{
115+
// Proyectar las coordenadas del mundo sobre el plano XZ
116+
float2 gridUV = worldPos.xz * scale;
117+
118+
// Obtener las fracciones de la posición en la cuadrícula
119+
float2 grid = abs(frac(gridUV) - 0.5f);
120+
121+
// Calcular la distancia a la línea más cercana
122+
float lineDist = min(grid.x, grid.y);
123+
124+
// Suavizar las líneas usando fwidth para obtener bordes suaves
125+
float width = max(fwidth(lineDist), 1e-5); // evitar división por cero
105126

106-
float3 dir = float3(uvOffset.x, uvOffset.y, 1.0f);
107-
return normalize(dir);
127+
// Retornar la máscara suavizada para las líneas de la cuadrícula
128+
return 1.0f - smoothstep(0.0f, width * 0.5f, lineDist); // Ajuste para hacer las líneas más visibles
108129
}
109130

110131
PixelOutput PixelMain(VertexOutput input, uint index : SV_SampleIndex)
111132
{
133+
float depth = depthTex.Load(input.position.xy, index).r;
134+
float3 color = float3(0.f, 0.f, 0.f);
135+
//if (depth >= 1.f)
136+
//{
137+
// // Obtener las coordenadas UV del píxel
138+
// float2 uv = input.position.xy;
139+
140+
// // Calcular la dirección del rayo desde la cámara usando las UVs
141+
// float3 rayDirection = ViewDirectionFromUV(uv);
142+
143+
// // Elegir un plano para intersectar con el rayo (por ejemplo, el plano Z = 0)
144+
// float t = -cameraPos.z / rayDirection.z; // Intersección con el plano Z = 0
145+
146+
// // Obtener las coordenadas en el mundo para el punto de intersección
147+
// float3 worldPos = cameraPos + t * rayDirection;
148+
149+
// // Ajustar la escala de la cuadrícula (puedes modificar esta escala)
150+
// float scale = 10.0f; // Ajusta la escala de la cuadrícula
151+
152+
// // Obtener la máscara de la cuadrícula
153+
// float gridMaskValue = GridMask(worldPos, scale);
154+
155+
// // Asignar el color de la cuadrícula (blanco para las líneas)
156+
// color = float3(gridMaskValue, gridMaskValue, gridMaskValue);
157+
158+
// // Crear la salida del píxel para la cuadrícula
159+
// PixelOutput gridOut;
160+
// gridOut.screen = float4(color.x, color.y, color.z, 1.f);
161+
162+
// // Retornar el color de la cuadrícula
163+
// return gridOut;
164+
//}
165+
166+
167+
112168
float3 lightPos = float3(0.f, 1.f, -2.f);
113169
float3 lightColor = float3(100.f, 100.f, 100.f);
114170

@@ -149,7 +205,7 @@ PixelOutput PixelMain(VertexOutput input, uint index : SV_SampleIndex)
149205

150206
float3 ambient = 0.1f * albedoColor * ao;
151207

152-
float3 color = ambient + Lo;
208+
color = ambient + Lo;
153209

154210
/* Tonemap */
155211
color = color / (color + 1.0);

src/private/Core/Editor/Editor.cpp

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ Editor::Editor() {
88
this->m_inspectorObj = nullptr;
99
this->m_nWidth = 0;
1010
this->m_nHeight = 0;
11+
12+
this->m_bLocation = true;
13+
this->m_bRotation = false;
14+
this->m_bScale = false;
15+
this->m_guizmoOp = ImGuizmo::OPERATION::TRANSLATE;
1116
}
1217

1318
void Editor::Init(UINT nWidth, UINT nHeight) {
@@ -164,28 +169,55 @@ void Editor::Update() {
164169
ImGui::Begin("Inspector");
165170

166171
if (this->m_inspectorObj) {
172+
ImGui::Text(this->m_inspectorObj->m_name.c_str());
173+
ImGui::Text("Mode");
174+
if (ImGui::RadioButton("Location", this->m_bLocation)) {
175+
this->m_bLocation = true;
176+
this->m_bRotation = false;
177+
this->m_bScale = false;
178+
this->m_guizmoOp = ImGuizmo::OPERATION::TRANSLATE;
179+
}
180+
ImGui::SameLine();
181+
if (ImGui::RadioButton("Rotation", this->m_bRotation)) {
182+
this->m_bLocation = false;
183+
this->m_bRotation = true;
184+
this->m_bScale = false;
185+
this->m_guizmoOp = ImGuizmo::OPERATION::ROTATE;
186+
}
187+
ImGui::SameLine();
188+
if (ImGui::RadioButton("Scale", this->m_bScale)) {
189+
this->m_bLocation = false;
190+
this->m_bRotation = false;
191+
this->m_bScale = true;
192+
this->m_guizmoOp = ImGuizmo::OPERATION::SCALE;
193+
}
194+
167195
Transform* transform = &this->m_inspectorObj->transform;
168196

169-
XMMATRIX worldMatrixXM = XMMatrixScaling(transform->scale.x, transform->scale.y, transform->scale.z) *
170-
XMMatrixRotationX(XMConvertToRadians(transform->rotation.x)) *
171-
XMMatrixRotationY(XMConvertToRadians(transform->rotation.y)) *
172-
XMMatrixRotationZ(XMConvertToRadians(transform->rotation.z)) *
173-
XMMatrixTranslation(transform->location.x, transform->location.y, transform->location.z);
197+
XMMATRIX scaleMat = XMMatrixScaling(transform->scale.x, transform->scale.y, transform->scale.z);
198+
XMMATRIX rotMat = XMMatrixRotationRollPitchYaw(
199+
XMConvertToRadians(transform->rotation.x),
200+
XMConvertToRadians(transform->rotation.y),
201+
XMConvertToRadians(transform->rotation.z)
202+
);
203+
XMMATRIX transMat = XMMatrixTranslation(transform->location.x, transform->location.y, transform->location.z);
204+
205+
XMMATRIX worldMatrixXM = scaleMat * rotMat * transMat;
174206

175207
float worldMatrix[16];
176208
XMStoreFloat4x4(reinterpret_cast<XMFLOAT4X4*>(worldMatrix), worldMatrixXM);
177209

178-
ImGuizmo::Manipulate(viewMatrix, projectionMatrix, ImGuizmo::TRANSLATE, ImGuizmo::WORLD, worldMatrix);
210+
ImGuizmo::Manipulate(viewMatrix, projectionMatrix, this->m_guizmoOp, ImGuizmo::WORLD, worldMatrix);
179211

180212
if (ImGuizmo::IsUsing()) {
181213
float loc[3], rot[3], scale[3];
182214
ImGuizmo::DecomposeMatrixToComponents(worldMatrix, loc, rot, scale);
215+
183216
transform->location = Vector3{ loc[0], loc[1], loc[2] };
184-
transform->rotation = Vector3{ rot[0], rot[1], rot[2] };
217+
transform->rotation = Vector3{ rot[0], rot[1], rot[2]};
185218
transform->scale = Vector3{ scale[0], scale[1], scale[2] };
186219
}
187220

188-
ImGui::Text(this->m_inspectorObj->m_name.c_str());
189221

190222
ImGui::Text("Location");
191223
ImGui::PushItemWidth(100);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Mesh::Mesh(std::string name, Transform* parentTransform) : Component::Component(
2727
this->m_core->GetWindowSize(nWidth, nHeight);
2828

2929
this->m_wvp.World = (XMMatrixIdentity() * XMMatrixTranslation(this->m_transform->location.x, this->m_transform->location.y, this->m_transform->location.z));
30+
this->m_wvp.World *= (XMMatrixScaling(this->m_transform->scale.x, this->m_transform->scale.y, this->m_transform->scale.z));
3031
this->m_wvp.World *= (XMMatrixRotationX(XMConvertToRadians(this->m_transform->rotation.x)));
3132
this->m_wvp.World *= (XMMatrixRotationY(XMConvertToRadians(this->m_transform->rotation.y)));
3233
this->m_wvp.World *= (XMMatrixRotationZ(XMConvertToRadians(this->m_transform->rotation.z)));
@@ -140,6 +141,7 @@ void Mesh::UpdateConstantBuffer() {
140141
UINT nWVPSize = (sizeof(this->m_wvp) + 255) & ~255;
141142
this->m_wvp.World = (XMMatrixIdentity());
142143

144+
this->m_wvp.World *= (XMMatrixScaling(this->m_transform->scale.x, this->m_transform->scale.y, this->m_transform->scale.z));
143145
this->m_wvp.World *= (XMMatrixRotationX(XMConvertToRadians(this->m_transform->rotation.x)));
144146
this->m_wvp.World *= (XMMatrixRotationY(XMConvertToRadians(this->m_transform->rotation.y)));
145147
this->m_wvp.World *= (XMMatrixRotationZ(XMConvertToRadians(this->m_transform->rotation.z)));

src/private/Core/GameObject/GameObject.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ GameObject::GameObject(std::string name) {
44
this->m_name = name;
55
this->m_input = Input::GetInstance();
66
this->m_time = Time::GetInstance();
7+
this->transform = Transform();
8+
this->transform.scale = Vector3(1.f, 1.f, 1.f);
79
}
810

911
void GameObject::Init() {

src/private/Core/Scene/Scene.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void Scene::Init() {
1919
for (std::pair<std::string, GameObject*> object : this->m_gameObjects) {
2020
object.second->Init();
2121
}
22-
//m_go->transform.Rotate(Vector3(90.f, 0.f, 0.f));
22+
m_go->transform.Rotate(Vector3(90.f, 0.f, 0.f));
2323
}
2424

2525
void Scene::Update() {

src/public/Core/Editor/Editor.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class Editor {
1818
UINT m_nHeight;
1919

2020
WVP m_wvp;
21+
22+
bool m_bLocation;
23+
bool m_bRotation;
24+
bool m_bScale;
25+
ImGuizmo::OPERATION m_guizmoOp;
2126
public:
2227
Editor();
2328
void Init(UINT nWidth, UINT nHeight);

0 commit comments

Comments
 (0)