Add custom TinyEmbree package with backface culling
This commit is contained in:
parent
8ae23eafb6
commit
392c7b24b5
|
@ -172,6 +172,8 @@ PublishScripts/
|
||||||
*.nuget.props
|
*.nuget.props
|
||||||
*.nuget.targets
|
*.nuget.targets
|
||||||
|
|
||||||
|
!**/LocalPackages/**
|
||||||
|
|
||||||
# Microsoft Azure Build Output
|
# Microsoft Azure Build Output
|
||||||
csx/
|
csx/
|
||||||
*.build.csdef
|
*.build.csdef
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
<PackageReference Include="Serilog" Version="4.2.0" />
|
<PackageReference Include="Serilog" Version="4.2.0" />
|
||||||
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
|
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
|
||||||
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
|
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
|
||||||
<PackageReference Include="TinyEmbree" Version="1.1.0" />
|
<PackageReference Include="TinyEmbree" Version="1.1.0.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
@ -490,8 +490,6 @@ public class LightMapper
|
||||||
// lightVisibleCells.Add(visibleSet);
|
// lightVisibleCells.Add(visibleSet);
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// Console.WriteLine($"17: [{string.Join(", ", pvs.GetVisible(17))}]");
|
|
||||||
//
|
|
||||||
// return lightVisibleCells;
|
// return lightVisibleCells;
|
||||||
// });
|
// });
|
||||||
|
|
||||||
|
@ -639,6 +637,21 @@ public class LightMapper
|
||||||
topLeft + xDir + yDir,
|
topLeft + xDir + yDir,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
// Log.Information("Poly plane: {X}x + {Y}y + {Z}z + {D} = 0", plane.Normal.X, plane.Normal.Y, plane.Normal.Z, plane.D);
|
||||||
|
var edgePlanes = new Plane[poly.VertexCount];
|
||||||
|
for (var i = 0; i < poly.VertexCount; i++)
|
||||||
|
{
|
||||||
|
var v0 = cell.Vertices[cell.Indices[cellIdxOffset + i]];
|
||||||
|
var v1 = cell.Vertices[cell.Indices[cellIdxOffset + (i + 1) % poly.VertexCount]];
|
||||||
|
|
||||||
|
var dir = Vector3.Normalize(v1 - v0);
|
||||||
|
var edgePlaneNormal = Vector3.Cross(dir, plane.Normal);
|
||||||
|
var edgePlaneDistance = -Vector3.Dot(edgePlaneNormal, v0);
|
||||||
|
var edgePlane = new Plane(edgePlaneNormal, edgePlaneDistance);
|
||||||
|
edgePlanes[i] = edgePlane;
|
||||||
|
// Log.Information("Edge plane: {X}x + {Y}y + {Z}z + {D} = 0", edgePlane.Normal.X, edgePlane.Normal.Y, edgePlane.Normal.Z, edgePlane.D);
|
||||||
|
}
|
||||||
|
|
||||||
// Used for clipping points to poly
|
// Used for clipping points to poly
|
||||||
var vs = new Vector3[poly.VertexCount];
|
var vs = new Vector3[poly.VertexCount];
|
||||||
for (var i = 0; i < poly.VertexCount; i++)
|
for (var i = 0; i < poly.VertexCount; i++)
|
||||||
|
@ -666,6 +679,10 @@ public class LightMapper
|
||||||
|
|
||||||
// TODO: Handle quad lit lights better. Right now we're computing two sets of points for every
|
// TODO: Handle quad lit lights better. Right now we're computing two sets of points for every
|
||||||
// luxel. Maybe it's better to only compute if we encounter a quadlit light?
|
// luxel. Maybe it's better to only compute if we encounter a quadlit light?
|
||||||
|
// var tracePoints = GetTracePoints(pos, offsets, renderPoly.Center, plane, edgePlanes);
|
||||||
|
// var quadTracePoints = settings.MultiSampling != SoftnessMode.Standard
|
||||||
|
// ? tracePoints
|
||||||
|
// : GetTracePoints(pos, quadOffsets, renderPoly.Center, plane, edgePlanes);
|
||||||
var tracePoints = GetTracePoints(pos, offsets, renderPoly.Center, planeMapper, v2ds);
|
var tracePoints = GetTracePoints(pos, offsets, renderPoly.Center, planeMapper, v2ds);
|
||||||
var quadTracePoints = settings.MultiSampling != SoftnessMode.Standard
|
var quadTracePoints = settings.MultiSampling != SoftnessMode.Standard
|
||||||
? tracePoints
|
? tracePoints
|
||||||
|
@ -817,6 +834,80 @@ public class LightMapper
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Vector3[] GetTracePoints(
|
||||||
|
Vector3 basePosition,
|
||||||
|
Vector3[] offsets,
|
||||||
|
Vector3 polyCenter,
|
||||||
|
Plane polyPlane,
|
||||||
|
Plane[] edgePlanes)
|
||||||
|
{
|
||||||
|
polyCenter += polyPlane.Normal * 0.25f;
|
||||||
|
|
||||||
|
var tracePoints = new Vector3[offsets.Length];
|
||||||
|
for (var i = 0; i < offsets.Length; i++)
|
||||||
|
{
|
||||||
|
var offset = offsets[i];
|
||||||
|
var pos = basePosition + offset;
|
||||||
|
|
||||||
|
// If the center can see the target lightmap point then we can just straight use it.
|
||||||
|
// Note that the target may actually be on another poly, or floating in space over a ledge.
|
||||||
|
if (!TraceOcclusion(_sceneNoObj, polyCenter, pos))
|
||||||
|
{
|
||||||
|
tracePoints[i] = pos;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we can't see our target point from the center of the poly
|
||||||
|
// then we need to clip the point to slightly inside the poly
|
||||||
|
// and retrace to avoid two problems:
|
||||||
|
// 1. Darkened spots from lightmap pixels whose center is outside
|
||||||
|
// the polygon but is partially contained in the polygon
|
||||||
|
// 2. Darkened spots from linear filtering of points outside the
|
||||||
|
// polygon which have missed
|
||||||
|
//
|
||||||
|
// TODO: This can cause seams. The ideal solution here is to check if it lies on any other poly, or maybe check if it's "within" any cells.
|
||||||
|
foreach (var plane in edgePlanes)
|
||||||
|
{
|
||||||
|
var distFromPlane = MathUtils.DistanceFromPlane(plane, pos);
|
||||||
|
if (distFromPlane >= -MathUtils.Epsilon)
|
||||||
|
{
|
||||||
|
// we're inside the plane :)
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var u = polyCenter - pos;
|
||||||
|
var w = pos - (plane.Normal * -plane.D);
|
||||||
|
|
||||||
|
var d = Vector3.Dot(plane.Normal, u);
|
||||||
|
var n = -Vector3.Dot(plane.Normal, w);
|
||||||
|
var t = n / d;
|
||||||
|
|
||||||
|
pos += u * (t + MathUtils.Epsilon);
|
||||||
|
}
|
||||||
|
|
||||||
|
// After clipping, we can still be in a weird spot. So to fully resolve it we do a cast
|
||||||
|
if (TraceOcclusion(_sceneNoObj, polyCenter + polyPlane.Normal * 0.25f, pos))
|
||||||
|
{
|
||||||
|
var origin = polyCenter + polyPlane.Normal * 0.25f;
|
||||||
|
var direction = pos - origin;
|
||||||
|
var hitResult = _sceneNoObj.Trace(new Ray
|
||||||
|
{
|
||||||
|
Origin = origin,
|
||||||
|
Direction = Vector3.Normalize(direction),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (hitResult)
|
||||||
|
{
|
||||||
|
pos = hitResult.Position;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tracePoints[i] = pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
return tracePoints;
|
||||||
|
}
|
||||||
|
|
||||||
private Vector3[] GetTracePoints(
|
private Vector3[] GetTracePoints(
|
||||||
Vector3 basePosition,
|
Vector3 basePosition,
|
||||||
Vector3[] offsets,
|
Vector3[] offsets,
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"version": 2,
|
||||||
|
"contentHash": "Uhc3bpe44l6oPT7XPAWg8MBJRZc4iZP8nZpaAM3+35tvwLxjspcuBIMzFatr124Zfm71HQxDf+K1o5Tic/FQYw==",
|
||||||
|
"source": null
|
||||||
|
}
|
Binary file not shown.
|
@ -0,0 +1 @@
|
||||||
|
Uhc3bpe44l6oPT7XPAWg8MBJRZc4iZP8nZpaAM3+35tvwLxjspcuBIMzFatr124Zfm71HQxDf+K1o5Tic/FQYw==
|
|
@ -0,0 +1,18 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
|
||||||
|
<metadata>
|
||||||
|
<id>TinyEmbree</id>
|
||||||
|
<version>1.1.0.1</version>
|
||||||
|
<title>TinyEmbree</title>
|
||||||
|
<authors>Pascal Grittmann</authors>
|
||||||
|
<license type="file">LICENSE</license>
|
||||||
|
<licenseUrl>https://aka.ms/deprecateLicenseUrl</licenseUrl>
|
||||||
|
<description>A very simple C# wrapper around the Embree ray tracing kernels.</description>
|
||||||
|
<copyright>(c) Pascal Grittmann</copyright>
|
||||||
|
<tags>ray tracing Embree</tags>
|
||||||
|
<repository type="git" url="https://github.com/pgrit/TinyEmbree" commit="66e46513777d3046b684f4d6fe796e9f3558b6b0" />
|
||||||
|
<dependencies>
|
||||||
|
<group targetFramework="net9.0" />
|
||||||
|
</dependencies>
|
||||||
|
</metadata>
|
||||||
|
</package>
|
|
@ -0,0 +1,5 @@
|
||||||
|
<configuration>
|
||||||
|
<packageSources>
|
||||||
|
<add key="KeepersCompound.Lightmapper Local" value="./LocalPackages" />
|
||||||
|
</packageSources>
|
||||||
|
</configuration>
|
Loading…
Reference in New Issue