Compare commits
No commits in common. "b3a71e68276ba66d25cab9ac7bdfcb72923cd1b4" and "fe100bb938a51cdbcd1b8b8155103c3483b99695" have entirely different histories.
b3a71e6827
...
fe100bb938
|
@ -8,20 +8,12 @@ namespace KeepersCompound.Lightmapper;
|
|||
|
||||
public class LightMapper
|
||||
{
|
||||
private enum SurfaceType
|
||||
{
|
||||
Solid,
|
||||
Sky,
|
||||
Water,
|
||||
}
|
||||
|
||||
private class Settings
|
||||
{
|
||||
public Vector3 AmbientLight;
|
||||
public bool Hdr;
|
||||
public SoftnessMode MultiSampling;
|
||||
public float MultiSamplingCenterWeight;
|
||||
public bool LightmappedWater;
|
||||
}
|
||||
|
||||
private ResourcePathManager.CampaignResources _campaign;
|
||||
|
@ -30,7 +22,6 @@ public class LightMapper
|
|||
private ObjectHierarchy _hierarchy;
|
||||
private Raytracer _scene;
|
||||
private List<Light> _lights;
|
||||
private List<SurfaceType> _triangleTypeMap;
|
||||
|
||||
public LightMapper(
|
||||
string installPath,
|
||||
|
@ -42,7 +33,6 @@ public class LightMapper
|
|||
_misPath = _campaign.GetResourcePath(ResourceType.Mission, missionName);
|
||||
_mission = Timing.TimeStage("Parse DB", () => new DbFile(_misPath));
|
||||
_hierarchy = Timing.TimeStage("Build Hierarchy", BuildHierarchy);
|
||||
_triangleTypeMap = [];
|
||||
_scene = Timing.TimeStage("Build Scene", BuildRaytracingScene);
|
||||
_lights = [];
|
||||
}
|
||||
|
@ -57,16 +47,14 @@ public class LightMapper
|
|||
return;
|
||||
}
|
||||
|
||||
// TODO: lmParams LightmappedWater doesn't mean the game will actually *use* the lightmapped water hmm
|
||||
var settings = new Settings
|
||||
{
|
||||
Hdr = worldRep.DataHeader.LightmapFormat == 2,
|
||||
AmbientLight = rendParams.ambientLight * 255,
|
||||
MultiSampling = lmParams.ShadowSoftness,
|
||||
MultiSamplingCenterWeight = lmParams.CenterWeight,
|
||||
LightmappedWater = lmParams.LightmappedWater,
|
||||
};
|
||||
|
||||
|
||||
Timing.TimeStage("Gather Lights", BuildLightList);
|
||||
Timing.TimeStage("Set Light Indices", SetCellLightIndices);
|
||||
Timing.TimeStage("Trace Scene", () => TraceScene(settings));
|
||||
|
@ -131,9 +119,9 @@ public class LightMapper
|
|||
continue;
|
||||
}
|
||||
|
||||
var solidPolys = numPolys - numPortalPolys;
|
||||
var maxPolyIdx = Math.Min(numRenderPolys, numPolys - numPortalPolys);
|
||||
var cellIdxOffset = 0;
|
||||
for (var polyIdx = 0; polyIdx < numRenderPolys; polyIdx++)
|
||||
for (var polyIdx = 0; polyIdx < maxPolyIdx; polyIdx++)
|
||||
{
|
||||
var poly = cell.Polys[polyIdx];
|
||||
var meshIndexOffset = vertices.Count;
|
||||
|
@ -144,25 +132,12 @@ public class LightMapper
|
|||
vertices.Add(vertex);
|
||||
}
|
||||
|
||||
// We need to know what type of surface this poly is so we can map Embree primitive IDs to surface
|
||||
// types
|
||||
var renderPoly = cell.RenderPolys[polyIdx];
|
||||
var primType = SurfaceType.Solid;
|
||||
if (renderPoly.TextureId == 249)
|
||||
{
|
||||
primType = SurfaceType.Sky;
|
||||
} else if (polyIdx >= solidPolys)
|
||||
{
|
||||
primType = SurfaceType.Water;
|
||||
}
|
||||
|
||||
// Cell polygons are n-sided, but fortunately they're convex so we can just do a fan triangulation
|
||||
for (var j = 1; j < numPolyVertices - 1; j++)
|
||||
{
|
||||
indices.Add(meshIndexOffset);
|
||||
indices.Add(meshIndexOffset + j);
|
||||
indices.Add(meshIndexOffset + j + 1);
|
||||
_triangleTypeMap.Add(primType);
|
||||
}
|
||||
|
||||
cellIdxOffset += cell.Polys[polyIdx].VertexCount;
|
||||
|
@ -393,9 +368,9 @@ public class LightMapper
|
|||
return;
|
||||
}
|
||||
|
||||
var solidPolys = numPolys - numPortalPolys;
|
||||
var maxPolyIdx = Math.Min(numRenderPolys, numPolys - numPortalPolys);
|
||||
var cellIdxOffset = 0;
|
||||
for (var polyIdx = 0; polyIdx < numRenderPolys; polyIdx++)
|
||||
for (var polyIdx = 0; polyIdx < maxPolyIdx; polyIdx++)
|
||||
{
|
||||
var poly = cell.Polys[polyIdx];
|
||||
var plane = cell.Planes[poly.PlaneId];
|
||||
|
@ -404,14 +379,6 @@ public class LightMapper
|
|||
var lightmap = cell.Lightmaps[polyIdx];
|
||||
|
||||
info.AnimLightBitmask = 0;
|
||||
|
||||
// We have to reset the lightmaps for water, but we don't want to do anything else
|
||||
var waterPoly = polyIdx >= solidPolys;
|
||||
if (!settings.LightmappedWater && waterPoly)
|
||||
{
|
||||
lightmap.Reset(Vector3.One * 255f, settings.Hdr);
|
||||
continue;
|
||||
}
|
||||
lightmap.Reset(settings.AmbientLight, settings.Hdr);
|
||||
|
||||
// Get world position of lightmap (0, 0) (+0.5 so we cast from the center of a pixel)
|
||||
|
@ -631,23 +598,13 @@ public class LightMapper
|
|||
|
||||
private bool TraceRay(Vector3 origin, Vector3 target)
|
||||
{
|
||||
var hitDistanceFromTarget = float.MinValue;
|
||||
var hitSurfaceType = SurfaceType.Water;
|
||||
while (hitDistanceFromTarget < -MathUtils.Epsilon && hitSurfaceType == SurfaceType.Water)
|
||||
var direction = target - origin;
|
||||
var hitResult = _scene.Trace(new Ray
|
||||
{
|
||||
var direction = target - origin;
|
||||
var hitResult = _scene.Trace(new Ray
|
||||
{
|
||||
Origin = origin,
|
||||
Direction = Vector3.Normalize(direction),
|
||||
});
|
||||
|
||||
hitDistanceFromTarget = hitResult.Distance - direction.Length();
|
||||
hitSurfaceType = _triangleTypeMap[(int)hitResult.PrimId];
|
||||
origin = hitResult.Position += direction * MathUtils.Epsilon;
|
||||
}
|
||||
|
||||
return Math.Abs(hitDistanceFromTarget) < MathUtils.Epsilon;
|
||||
Origin = origin,
|
||||
Direction = Vector3.Normalize(direction),
|
||||
});
|
||||
return hitResult && Math.Abs(hitResult.Distance - direction.Length()) < MathUtils.Epsilon;
|
||||
}
|
||||
|
||||
private void SetAnimLightCellMaps()
|
||||
|
|
Loading…
Reference in New Issue