feat: PBR point light — Unity-style attenuation, warm glow

- UBO expanded from 64 to 128 bytes: vp(64) + lightPos(16) + lightColor(16)
- Vertex shader: passes point light UBO data through
- Fragment shader: refactored calcLight() function, shared by directional + point
  - Point light: Unity-style attenuation pow(1 - dist/range, 2)
  - Directional light reduced intensity (0.4) to balance with point light
  - Point light: position (0,8,0), warm color (1,0.9,0.7), intensity 15, range 25
- Renderer: reads Light component from ECS, packs into UBO with Marshal.StructureToPtr
- Scene: MainLight entity with Light.Point at (0,8,0)
- 0 C# struct changes — pure UBO layout + shader upgrade
This commit is contained in:
emil28092005
2026-06-18 19:23:00 +03:00
parent 22748ed9ba
commit f718e6de49
7 changed files with 77 additions and 25 deletions
+22 -4
View File
@@ -77,6 +77,24 @@ internal sealed unsafe class VulkanRenderer : IRenderer, Engine.Graphics.IScreen
vp = Matrix4x4.CreateLookAt(new Vector3(0, 0, -6), Vector3.Zero, Vector3.UnitY) * proj;
}
// Collect point light from ECS (first point light found)
var lightPos = new Vector4(0, 5, 0, 0);
var lightColor = new Vector4(1, 1, 1, 0);
world.Each((Entity e, ref Light l) =>
{
if (l.IsPoint && lightPos.W == 0)
{
lightPos = new Vector4(l.Position, l.Intensity);
lightColor = new Vector4(l.Color, l.Range);
}
});
// Pack UBO: mat4 vp (64 bytes) + vec4 lightPos (16) + vec4 lightColor (16) = 96 bytes
var uboData = stackalloc byte[128];
Marshal.StructureToPtr(vp, (nint)uboData, false);
*(Vector4*)(uboData + 64) = lightPos;
*(Vector4*)(uboData + 80) = lightColor;
var drawCalls = new List<(VkBuffer vertexBuf, VkBuffer indexBuf, uint indexCount, Matrix4x4 model)>();
world.Each((Entity e, ref Transform t, ref Mesh m) =>
@@ -95,10 +113,10 @@ internal sealed unsafe class VulkanRenderer : IRenderer, Engine.Graphics.IScreen
drawCalls.Add((entry.vb.Buffer, entry.ib.Buffer, entry.indexCount, t.GetMatrix()));
});
Render(vp, drawCalls);
Render(vp, drawCalls, uboData);
}
private void Render(Matrix4x4 vp, List<(VkBuffer vertexBuf, VkBuffer indexBuf, uint indexCount, Matrix4x4 model)> drawCalls)
private void Render(Matrix4x4 vp, List<(VkBuffer vertexBuf, VkBuffer indexBuf, uint indexCount, Matrix4x4 model)> drawCalls, byte* uboData)
{
_frameResources.WaitFrame(_frameIndex);
@@ -110,7 +128,7 @@ internal sealed unsafe class VulkanRenderer : IRenderer, Engine.Graphics.IScreen
{
_swapchain.Recreate(_ctx.SurfaceExtent.Width == 0 ? 1280 : (int)_ctx.SurfaceExtent.Width,
_ctx.SurfaceExtent.Height == 0 ? 720 : (int)_ctx.SurfaceExtent.Height);
Render(vp, drawCalls);
Render(vp, drawCalls, uboData);
return;
}
@@ -119,7 +137,7 @@ internal sealed unsafe class VulkanRenderer : IRenderer, Engine.Graphics.IScreen
_totalTime += 0.016f;
_frameResources.UpdateUbo(_frameIndex, &vp, VulkanFrameResources.UboSize);
_frameResources.UpdateUbo(_frameIndex, uboData, VulkanFrameResources.UboSize);
var cmd = _frameResources.CommandBuffers[_frameIndex];
Vk.vkResetCommandBuffer(cmd, 0);