diff --git a/.gitignore b/.gitignore index 076b14e..9740214 100644 --- a/.gitignore +++ b/.gitignore @@ -172,6 +172,8 @@ PublishScripts/ *.nuget.props *.nuget.targets +!**/LocalPackages/** + # Microsoft Azure Build Output csx/ *.build.csdef diff --git a/KeepersCompound.Lightmapper/KeepersCompound.Lightmapper.csproj b/KeepersCompound.Lightmapper/KeepersCompound.Lightmapper.csproj index 443a264..771cc75 100644 --- a/KeepersCompound.Lightmapper/KeepersCompound.Lightmapper.csproj +++ b/KeepersCompound.Lightmapper/KeepersCompound.Lightmapper.csproj @@ -16,7 +16,7 @@ - + diff --git a/KeepersCompound.Lightmapper/LightMapper.cs b/KeepersCompound.Lightmapper/LightMapper.cs index fae72eb..18aceff 100644 --- a/KeepersCompound.Lightmapper/LightMapper.cs +++ b/KeepersCompound.Lightmapper/LightMapper.cs @@ -489,9 +489,7 @@ public class LightMapper // var visibleSet = pvs.GetVisible(lightCellMap[i]); // lightVisibleCells.Add(visibleSet); // } - // - // Console.WriteLine($"17: [{string.Join(", ", pvs.GetVisible(17))}]"); - // + // // return lightVisibleCells; // }); @@ -536,7 +534,7 @@ public class LightMapper { continue; } - + // if (!lightVisibleCells[j].Contains(i)) // { // continue; @@ -638,6 +636,21 @@ public class LightMapper topLeft + 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 var vs = new Vector3[poly.VertexCount]; @@ -666,6 +679,10 @@ public class LightMapper // 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? + // 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 quadTracePoints = settings.MultiSampling != SoftnessMode.Standard ? 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( Vector3 basePosition, Vector3[] offsets, diff --git a/LocalPackages/tinyembree/1.1.0.1/.nupkg.metadata b/LocalPackages/tinyembree/1.1.0.1/.nupkg.metadata new file mode 100644 index 0000000..aeccd8a --- /dev/null +++ b/LocalPackages/tinyembree/1.1.0.1/.nupkg.metadata @@ -0,0 +1,5 @@ +{ + "version": 2, + "contentHash": "Uhc3bpe44l6oPT7XPAWg8MBJRZc4iZP8nZpaAM3+35tvwLxjspcuBIMzFatr124Zfm71HQxDf+K1o5Tic/FQYw==", + "source": null +} \ No newline at end of file diff --git a/LocalPackages/tinyembree/1.1.0.1/tinyembree.1.1.0.1.nupkg b/LocalPackages/tinyembree/1.1.0.1/tinyembree.1.1.0.1.nupkg new file mode 100644 index 0000000..b493800 Binary files /dev/null and b/LocalPackages/tinyembree/1.1.0.1/tinyembree.1.1.0.1.nupkg differ diff --git a/LocalPackages/tinyembree/1.1.0.1/tinyembree.1.1.0.1.nupkg.sha512 b/LocalPackages/tinyembree/1.1.0.1/tinyembree.1.1.0.1.nupkg.sha512 new file mode 100644 index 0000000..53526ad --- /dev/null +++ b/LocalPackages/tinyembree/1.1.0.1/tinyembree.1.1.0.1.nupkg.sha512 @@ -0,0 +1 @@ +Uhc3bpe44l6oPT7XPAWg8MBJRZc4iZP8nZpaAM3+35tvwLxjspcuBIMzFatr124Zfm71HQxDf+K1o5Tic/FQYw== \ No newline at end of file diff --git a/LocalPackages/tinyembree/1.1.0.1/tinyembree.nuspec b/LocalPackages/tinyembree/1.1.0.1/tinyembree.nuspec new file mode 100755 index 0000000..2bd116f --- /dev/null +++ b/LocalPackages/tinyembree/1.1.0.1/tinyembree.nuspec @@ -0,0 +1,18 @@ + + + + TinyEmbree + 1.1.0.1 + TinyEmbree + Pascal Grittmann + LICENSE + https://aka.ms/deprecateLicenseUrl + A very simple C# wrapper around the Embree ray tracing kernels. + (c) Pascal Grittmann + ray tracing Embree + + + + + + \ No newline at end of file diff --git a/NuGet.Config b/NuGet.Config new file mode 100644 index 0000000..4386c24 --- /dev/null +++ b/NuGet.Config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file