Files
Cortex_Engine/tests/Engine.Tests/MeshMathAndProceduralTests.cs
T
emil28092005 c82ff48119 fix: rlgl depth FBO shadows, correct normals, linear gamma, per-channel Fresnel
- Shadow mapping via rlgl: custom depth FBO (2048x2048, 24-bit depth texture)
  instead of color-attachment approach. Proper depth-only render pass.
- Shadow map bound via MaterialMapIndex.Emission (texture unit 1), sampled
  in shadow receiver shader with PCF 3x3 soft shadows.
- Fixed inverted normals: Cross(ac, ab) for CW winding in OBJ files.
- Linear workflow: pow(albedo, 2.2) before lighting, pow(result, 1/2.2) after.
- Per-channel Fresnel: vec3 F0 + (1-F0)*pow(1-HdotV,5) instead of F0.x.
- Single CollectLights call after BeginMode3D.
- Shadow pass after BeginDrawing (inside frame).
- Backface culling disabled globally for mixed-winding meshes.
- Point light support: Light.Point/Directional factory methods, attenuation,
  ImGui inspector with type combo, position/range for point lights.
- Floor rendered for both light types (shadow receiver or main shader).
- 66/66 tests passing.
2026-06-17 16:13:46 +03:00

121 lines
3.1 KiB
C#

using System.Numerics;
using Engine.Core;
using Engine.Graphics;
namespace Engine.Tests;
public class MeshMathTests
{
[Fact]
public void Computes_Normal_For_CCW_Triangle()
{
var n = MeshMath.ComputeFaceNormal(
new Vector3(0, 0, 0),
new Vector3(1, 0, 0),
new Vector3(0, 1, 0));
// CW winding (typical OBJ) → normal points -Z
Assert.Equal(0f, n.X, 0.001f);
Assert.Equal(0f, n.Y, 0.001f);
Assert.Equal(-1f, n.Z, 0.001f);
}
[Fact]
public void Normal_Is_Unit_Length()
{
var n = MeshMath.ComputeFaceNormal(
new Vector3(0, 0, 0),
new Vector3(3, 0, 0),
new Vector3(0, 4, 0));
Assert.Equal(1f, n.Length(), 0.001f);
}
[Fact]
public void Degenerate_Triangle_Falls_Back_To_UnitY()
{
var n = MeshMath.ComputeFaceNormal(
new Vector3(0, 0, 0),
new Vector3(1, 0, 0),
new Vector3(2, 0, 0));
Assert.Equal(Vector3.UnitY, n);
}
}
public class ProceduralMeshTests
{
[Fact]
public void Sphere_Has_Correct_Vertex_Count()
{
var mesh = ProceduralMesh.CreateSphere(1f, 16, 8, Vector3.One);
Assert.Equal((8 + 1) * (16 + 1), mesh.Vertices.Length);
}
[Fact]
public void Sphere_Has_Correct_Index_Count()
{
var mesh = ProceduralMesh.CreateSphere(1f, 16, 8, Vector3.One);
Assert.Equal(8 * 16 * 6, mesh.Indices.Length);
}
[Fact]
public void Sphere_Vertices_Lie_On_Surface()
{
const float radius = 2.5f;
var mesh = ProceduralMesh.CreateSphere(radius, 8, 4, Vector3.One);
foreach (var v in mesh.Vertices)
Assert.Equal(radius, v.Position.Length(), 0.001f);
}
[Fact]
public void Sphere_Normals_Are_Unit_Length()
{
var mesh = ProceduralMesh.CreateSphere(1f, 8, 4, Vector3.One);
foreach (var v in mesh.Vertices)
Assert.Equal(1f, v.Normal.Length(), 0.001f);
}
[Fact]
public void Sphere_Top_Pole_At_Positive_Y()
{
var mesh = ProceduralMesh.CreateSphere(1f, 8, 4, Vector3.One);
Assert.Equal(1f, mesh.Vertices[0].Position.Y, 0.001f);
Assert.Equal(0f, mesh.Vertices[0].Position.X, 0.001f);
Assert.Equal(0f, mesh.Vertices[0].Position.Z, 0.001f);
}
[Fact]
public void Grid_Has_Correct_Vertex_Count()
{
var mesh = ProceduralMesh.CreateGrid(5, 1f, Vector3.One);
var expectedLines = 2 * 5 + 1;
Assert.Equal(expectedLines * 4 * 2, mesh.Vertices.Length);
}
[Fact]
public void Grid_All_Normals_Point_Up()
{
var mesh = ProceduralMesh.CreateGrid(3, 1f, Vector3.One);
foreach (var v in mesh.Vertices)
Assert.Equal(Vector3.UnitY, v.Normal);
}
[Fact]
public void Grid_Extent_Matches_Lines_And_Spacing()
{
var mesh = ProceduralMesh.CreateGrid(10, 2f, Vector3.One);
var maxPos = 10f * 2f;
Assert.True(mesh.Vertices.Any(v => v.Position.X <= -maxPos));
Assert.True(mesh.Vertices.Any(v => v.Position.X >= maxPos));
}
}