2024-10-28 20:03:20 +00:00
|
|
|
using System.Numerics;
|
|
|
|
using KeepersCompound.LGS;
|
|
|
|
using KeepersCompound.LGS.Database;
|
|
|
|
using KeepersCompound.LGS.Database.Chunks;
|
2025-01-11 13:16:31 +00:00
|
|
|
using Serilog;
|
2024-10-28 20:03:20 +00:00
|
|
|
using TinyEmbree;
|
|
|
|
|
|
|
|
namespace KeepersCompound.Lightmapper;
|
|
|
|
|
|
|
|
public class LightMapper
|
|
|
|
{
|
2024-12-09 14:35:53 +00:00
|
|
|
// The objcast element of sunlight is ignored, we just care if it's quadlit
|
|
|
|
private struct SunSettings
|
|
|
|
{
|
|
|
|
public bool Enabled;
|
|
|
|
public bool QuadLit;
|
|
|
|
public Vector3 Direction;
|
|
|
|
public Vector3 Color;
|
|
|
|
}
|
|
|
|
|
|
|
|
private struct Settings
|
2024-10-28 20:03:20 +00:00
|
|
|
{
|
2024-12-26 17:12:28 +00:00
|
|
|
public Vector3[] AmbientLight;
|
2024-10-29 18:01:50 +00:00
|
|
|
public bool Hdr;
|
|
|
|
public SoftnessMode MultiSampling;
|
|
|
|
public float MultiSamplingCenterWeight;
|
2024-11-04 18:39:10 +00:00
|
|
|
public bool LightmappedWater;
|
2024-12-09 14:35:53 +00:00
|
|
|
public SunSettings Sunlight;
|
2024-12-26 14:03:52 +00:00
|
|
|
public uint AnimLightCutoff;
|
2024-10-28 20:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private ResourcePathManager.CampaignResources _campaign;
|
|
|
|
private string _misPath;
|
|
|
|
private DbFile _mission;
|
|
|
|
private ObjectHierarchy _hierarchy;
|
|
|
|
private Raytracer _scene;
|
|
|
|
private List<Light> _lights;
|
2024-12-09 08:19:50 +00:00
|
|
|
private SurfaceType[] _triangleTypeMap;
|
2024-10-28 20:03:20 +00:00
|
|
|
|
|
|
|
public LightMapper(
|
|
|
|
string installPath,
|
|
|
|
string campaignName,
|
|
|
|
string missionName)
|
|
|
|
{
|
|
|
|
var pathManager = SetupPathManager(installPath);
|
|
|
|
_campaign = pathManager.GetCampaign(campaignName);
|
|
|
|
_misPath = _campaign.GetResourcePath(ResourceType.Mission, missionName);
|
|
|
|
_mission = Timing.TimeStage("Parse DB", () => new DbFile(_misPath));
|
|
|
|
_hierarchy = Timing.TimeStage("Build Hierarchy", BuildHierarchy);
|
|
|
|
_lights = [];
|
2024-12-09 08:19:50 +00:00
|
|
|
|
|
|
|
var mesh = Timing.TimeStage("Build Mesh", BuildMesh);
|
|
|
|
_triangleTypeMap = mesh.TriangleSurfaceMap;
|
|
|
|
_scene = Timing.TimeStage("Build RT Scene", () =>
|
|
|
|
{
|
|
|
|
var rt = new Raytracer();
|
|
|
|
rt.AddMesh(new TriangleMesh(mesh.Vertices, mesh.Indices));
|
|
|
|
rt.CommitScene();
|
|
|
|
return rt;
|
|
|
|
});
|
2024-10-28 20:03:20 +00:00
|
|
|
}
|
|
|
|
|
2024-10-29 18:01:50 +00:00
|
|
|
public void Light()
|
2024-10-28 20:03:20 +00:00
|
|
|
{
|
|
|
|
// TODO: Throw?
|
|
|
|
if (!_mission.TryGetChunk<RendParams>("RENDPARAMS", out var rendParams) ||
|
2024-10-29 18:01:50 +00:00
|
|
|
!_mission.TryGetChunk<LmParams>("LM_PARAM", out var lmParams) ||
|
2024-10-28 20:03:20 +00:00
|
|
|
!_mission.TryGetChunk<WorldRep>("WREXT", out var worldRep))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-12-09 14:35:53 +00:00
|
|
|
var sunlightSettings = new SunSettings()
|
|
|
|
{
|
|
|
|
Enabled = rendParams.useSunlight,
|
|
|
|
QuadLit = rendParams.sunlightMode is RendParams.SunlightMode.QuadUnshadowed or RendParams.SunlightMode.QuadObjcastShadows,
|
2024-12-09 15:23:47 +00:00
|
|
|
Direction = Vector3.Normalize(rendParams.sunlightDirection),
|
2024-12-09 14:35:53 +00:00
|
|
|
Color = Utils.HsbToRgb(rendParams.sunlightHue, rendParams.sunlightSaturation, rendParams.sunlightBrightness),
|
|
|
|
};
|
2024-12-26 17:12:28 +00:00
|
|
|
|
|
|
|
var ambientLight = rendParams.ambientLightZones.ToList();
|
|
|
|
ambientLight.Insert(0, rendParams.ambientLight);
|
|
|
|
for (var i = 0; i < ambientLight.Count; i++)
|
|
|
|
{
|
|
|
|
ambientLight[i] *= 255;
|
|
|
|
}
|
2024-12-09 14:35:53 +00:00
|
|
|
|
2024-11-04 18:39:10 +00:00
|
|
|
// TODO: lmParams LightmappedWater doesn't mean the game will actually *use* the lightmapped water hmm
|
2024-10-28 20:03:20 +00:00
|
|
|
var settings = new Settings
|
|
|
|
{
|
|
|
|
Hdr = worldRep.DataHeader.LightmapFormat == 2,
|
2024-12-26 17:12:28 +00:00
|
|
|
AmbientLight = [..ambientLight],
|
2024-10-29 18:01:50 +00:00
|
|
|
MultiSampling = lmParams.ShadowSoftness,
|
|
|
|
MultiSamplingCenterWeight = lmParams.CenterWeight,
|
2024-11-04 18:39:10 +00:00
|
|
|
LightmappedWater = lmParams.LightmappedWater,
|
2024-12-09 14:35:53 +00:00
|
|
|
Sunlight = sunlightSettings,
|
2024-12-26 14:03:52 +00:00
|
|
|
AnimLightCutoff = lmParams.AnimLightCutoff,
|
2024-10-28 20:03:20 +00:00
|
|
|
};
|
2024-11-04 18:39:10 +00:00
|
|
|
|
2024-10-28 20:03:20 +00:00
|
|
|
Timing.TimeStage("Gather Lights", BuildLightList);
|
2024-12-09 19:03:20 +00:00
|
|
|
Timing.TimeStage("Set Light Indices", () => SetCellLightIndices(settings));
|
2024-10-28 20:03:20 +00:00
|
|
|
Timing.TimeStage("Trace Scene", () => TraceScene(settings));
|
|
|
|
Timing.TimeStage("Update AnimLight Cell Mapping", SetAnimLightCellMaps);
|
2024-10-29 18:01:50 +00:00
|
|
|
|
2024-12-11 17:43:47 +00:00
|
|
|
// We always do object casting, so it's nice to let dromed know that :)
|
|
|
|
lmParams.ShadowType = LmParams.LightingMode.Objcast;
|
|
|
|
if (rendParams is { useSunlight: true, sunlightMode: RendParams.SunlightMode.SingleUnshadowed })
|
|
|
|
{
|
|
|
|
rendParams.sunlightMode = RendParams.SunlightMode.SingleObjcastShadows;
|
|
|
|
} else if (rendParams is { useSunlight: true, sunlightMode: RendParams.SunlightMode.QuadUnshadowed })
|
|
|
|
{
|
|
|
|
rendParams.sunlightMode = RendParams.SunlightMode.QuadObjcastShadows;
|
|
|
|
}
|
2024-10-28 20:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Save(string missionName)
|
|
|
|
{
|
|
|
|
var ext = Path.GetExtension(_misPath);
|
|
|
|
var dir = Path.GetDirectoryName(_misPath);
|
|
|
|
var savePath = Path.Join(dir, missionName + ext);
|
|
|
|
Timing.TimeStage("Save DB", () => _mission.Save(savePath));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static ResourcePathManager SetupPathManager(string installPath)
|
|
|
|
{
|
|
|
|
var tmpDir = Directory.CreateTempSubdirectory("KCLightmapper");
|
|
|
|
var resPathManager = new ResourcePathManager(tmpDir.FullName);
|
|
|
|
resPathManager.Init(installPath);
|
|
|
|
return resPathManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
private ObjectHierarchy BuildHierarchy()
|
|
|
|
{
|
|
|
|
if (!_mission.TryGetChunk<GamFile>("GAM_FILE", out var gamFile))
|
|
|
|
{
|
|
|
|
return new ObjectHierarchy(_mission);
|
|
|
|
}
|
|
|
|
|
|
|
|
var dir = Path.GetDirectoryName(_misPath);
|
|
|
|
var options = new EnumerationOptions { MatchCasing = MatchCasing.CaseInsensitive };
|
|
|
|
var name = gamFile.fileName;
|
|
|
|
var paths = Directory.GetFiles(dir!, name, options);
|
|
|
|
if (paths.Length > 0)
|
|
|
|
{
|
|
|
|
return new ObjectHierarchy(_mission, new DbFile(paths[0]));
|
|
|
|
}
|
|
|
|
return new ObjectHierarchy(_mission);
|
|
|
|
}
|
|
|
|
|
2024-12-09 08:19:50 +00:00
|
|
|
private Mesh BuildMesh()
|
2024-10-28 20:03:20 +00:00
|
|
|
{
|
2024-12-09 08:34:48 +00:00
|
|
|
var meshBuilder = new MeshBuilder();
|
|
|
|
|
2024-10-28 20:03:20 +00:00
|
|
|
// TODO: Should this throw?
|
2024-12-09 08:34:48 +00:00
|
|
|
// TODO: Only do object polys if objcast lighting?
|
2024-12-09 08:19:50 +00:00
|
|
|
if (!_mission.TryGetChunk<WorldRep>("WREXT", out var worldRep) ||
|
|
|
|
!_mission.TryGetChunk<BrList>("BRLIST", out var brList))
|
2024-10-28 20:03:20 +00:00
|
|
|
{
|
2024-12-09 08:34:48 +00:00
|
|
|
return meshBuilder.Build();
|
2024-12-07 18:28:39 +00:00
|
|
|
}
|
2024-12-09 08:19:50 +00:00
|
|
|
|
2024-12-09 08:34:48 +00:00
|
|
|
meshBuilder.AddWorldRepPolys(worldRep);
|
|
|
|
meshBuilder.AddObjectPolys(brList, _hierarchy, _campaign);
|
2024-12-09 08:19:50 +00:00
|
|
|
return meshBuilder.Build();
|
2024-10-28 20:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void BuildLightList()
|
|
|
|
{
|
|
|
|
_lights.Clear();
|
|
|
|
|
|
|
|
// Get the chunks we need
|
|
|
|
if (!_mission.TryGetChunk<WorldRep>("WREXT", out var worldRep) ||
|
|
|
|
!_mission.TryGetChunk<BrList>("BRLIST", out var brList))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
worldRep.LightingTable.Reset();
|
|
|
|
|
2024-12-09 20:49:00 +00:00
|
|
|
// TODO: Calculate the actual effective radius of infinite lights
|
|
|
|
// potentially do the same for all lights and lower their radius if necessary?
|
2024-10-28 20:03:20 +00:00
|
|
|
foreach (var brush in brList.Brushes)
|
|
|
|
{
|
|
|
|
switch (brush.media)
|
|
|
|
{
|
|
|
|
case BrList.Brush.Media.Light:
|
|
|
|
ProcessBrushLight(worldRep.LightingTable, brush);
|
|
|
|
break;
|
|
|
|
case BrList.Brush.Media.Object:
|
|
|
|
ProcessObjectLight(worldRep.LightingTable, brush);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-12-09 20:49:00 +00:00
|
|
|
|
2025-01-11 13:16:31 +00:00
|
|
|
var infinite = 0;
|
|
|
|
foreach (var light in _lights)
|
|
|
|
{
|
|
|
|
if (light.Radius != float.MaxValue)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (light.ObjId != -1)
|
|
|
|
{
|
|
|
|
Log.Warning("Infinite light from object {Id}", light.ObjId);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log.Warning("Infinite light from brush near {Position}", light.Position);
|
|
|
|
}
|
|
|
|
infinite++;
|
|
|
|
}
|
|
|
|
|
2024-12-09 20:49:00 +00:00
|
|
|
if (infinite > 0)
|
|
|
|
{
|
2025-01-11 13:16:31 +00:00
|
|
|
Log.Warning("Mission contains {Count} infinite lights", infinite);
|
2024-12-09 20:49:00 +00:00
|
|
|
}
|
2024-10-28 20:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Check if this works (brush is a record type)
|
|
|
|
private void ProcessBrushLight(WorldRep.LightTable lightTable, BrList.Brush brush)
|
|
|
|
{
|
|
|
|
// For some reason the light table index on brush lights is 1 indexed
|
|
|
|
brush.brushInfo = (uint)lightTable.LightCount + 1;
|
|
|
|
var sz = brush.size;
|
2024-12-09 18:32:16 +00:00
|
|
|
|
|
|
|
// Ignore 0 brightness lights
|
|
|
|
if (sz.X == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2024-12-23 18:01:33 +00:00
|
|
|
|
|
|
|
var brightness = Math.Min(sz.X, 255.0f);
|
2024-10-28 20:03:20 +00:00
|
|
|
var light = new Light
|
|
|
|
{
|
|
|
|
Position = brush.position,
|
2024-12-23 18:01:33 +00:00
|
|
|
Color = Utils.HsbToRgb(sz.Y, sz.Z, brightness),
|
|
|
|
Brightness = brightness,
|
2024-10-28 20:03:20 +00:00
|
|
|
Radius = float.MaxValue,
|
|
|
|
R2 = float.MaxValue,
|
|
|
|
LightTableIndex = lightTable.LightCount,
|
2024-12-26 21:39:29 +00:00
|
|
|
SpotlightInnerAngle = -1f,
|
2025-01-11 13:16:31 +00:00
|
|
|
ObjId = -1,
|
2024-10-28 20:03:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
_lights.Add(light);
|
|
|
|
lightTable.AddLight(light.ToLightData(32.0f));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ProcessObjectLight(WorldRep.LightTable lightTable, BrList.Brush brush)
|
|
|
|
{
|
|
|
|
// TODO: Handle PropSpotlightAndAmbient
|
|
|
|
var id = (int)brush.brushInfo;
|
2024-12-23 18:01:33 +00:00
|
|
|
var propScale = _hierarchy.GetProperty<PropVector>(id, "P$Scale");
|
2024-10-28 20:03:20 +00:00
|
|
|
var propAnimLight = _hierarchy.GetProperty<PropAnimLight>(id, "P$AnimLight", false);
|
|
|
|
var propLight = _hierarchy.GetProperty<PropLight>(id, "P$Light", false);
|
|
|
|
var propLightColor = _hierarchy.GetProperty<PropLightColor>(id, "P$LightColo");
|
|
|
|
var propSpotlight = _hierarchy.GetProperty<PropSpotlight>(id, "P$Spotlight");
|
2024-12-23 18:01:33 +00:00
|
|
|
var propSpotAmb = _hierarchy.GetProperty<PropSpotlightAndAmbient>(id, "P$SpotAmb");
|
2024-10-28 20:03:20 +00:00
|
|
|
var propModelName = _hierarchy.GetProperty<PropLabel>(id, "P$ModelName");
|
2024-12-23 18:01:33 +00:00
|
|
|
var propJointPos = _hierarchy.GetProperty<PropJointPos>(id, "P$JointPos");
|
2024-10-28 20:03:20 +00:00
|
|
|
|
|
|
|
propLightColor ??= new PropLightColor { Hue = 0, Saturation = 0 };
|
2024-12-23 18:01:33 +00:00
|
|
|
var joints = propJointPos?.Positions ?? [0, 0, 0, 0, 0, 0];
|
2024-12-09 20:22:49 +00:00
|
|
|
|
2024-12-23 18:01:33 +00:00
|
|
|
// Transform data
|
|
|
|
var translate = Matrix4x4.CreateTranslation(brush.position);
|
|
|
|
var rotate = Matrix4x4.Identity;
|
|
|
|
rotate *= Matrix4x4.CreateRotationX(float.DegreesToRadians(brush.angle.X));
|
|
|
|
rotate *= Matrix4x4.CreateRotationY(float.DegreesToRadians(brush.angle.Y));
|
|
|
|
rotate *= Matrix4x4.CreateRotationZ(float.DegreesToRadians(brush.angle.Z));
|
|
|
|
var scale = Matrix4x4.CreateScale(propScale?.value ?? Vector3.One);
|
2024-12-10 19:51:08 +00:00
|
|
|
|
2024-12-23 18:01:33 +00:00
|
|
|
var vhotLightPos = Vector3.Zero;
|
|
|
|
var vhotLightDir = -Vector3.UnitZ;
|
2024-10-28 20:03:20 +00:00
|
|
|
if (propModelName != null)
|
|
|
|
{
|
|
|
|
var resName = $"{propModelName.value.ToLower()}.bin";
|
|
|
|
var modelPath = _campaign.GetResourcePath(ResourceType.Object, resName);
|
|
|
|
if (modelPath != null)
|
|
|
|
{
|
|
|
|
var model = new ModelFile(modelPath);
|
2024-12-23 18:01:33 +00:00
|
|
|
model.ApplyJoints(joints);
|
|
|
|
|
2024-10-28 20:03:20 +00:00
|
|
|
if (model.TryGetVhot(ModelFile.VhotId.LightPosition, out var vhot))
|
|
|
|
{
|
2024-12-23 18:01:33 +00:00
|
|
|
vhotLightPos = vhot.Position - model.Header.Center;
|
2024-10-28 20:03:20 +00:00
|
|
|
}
|
|
|
|
if (model.TryGetVhot(ModelFile.VhotId.LightDirection, out vhot))
|
|
|
|
{
|
2024-12-26 13:23:07 +00:00
|
|
|
vhotLightDir = (vhot.Position - model.Header.Center) - vhotLightPos;
|
2024-10-28 20:03:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-12-23 18:01:33 +00:00
|
|
|
|
|
|
|
if (propAnimLight != null)
|
2024-10-28 20:03:20 +00:00
|
|
|
{
|
2024-12-23 18:01:33 +00:00
|
|
|
var lightIndex = lightTable.LightCount;
|
|
|
|
propAnimLight.LightTableLightIndex = (ushort)lightIndex;
|
|
|
|
|
2024-10-28 20:03:20 +00:00
|
|
|
var light = new Light
|
|
|
|
{
|
2024-12-23 18:01:33 +00:00
|
|
|
Position = propAnimLight.Offset,
|
|
|
|
Color = Utils.HsbToRgb(propLightColor.Hue, propLightColor.Saturation, propAnimLight.MaxBrightness),
|
2024-12-26 14:03:24 +00:00
|
|
|
Brightness = propAnimLight.MaxBrightness,
|
2024-12-23 18:01:33 +00:00
|
|
|
InnerRadius = propAnimLight.InnerRadius,
|
|
|
|
Radius = propAnimLight.Radius,
|
|
|
|
R2 = propAnimLight.Radius * propAnimLight.Radius,
|
|
|
|
QuadLit = propAnimLight.QuadLit,
|
|
|
|
ObjId = id,
|
|
|
|
LightTableIndex = propAnimLight.LightTableLightIndex,
|
2024-12-26 21:39:29 +00:00
|
|
|
Anim = true,
|
|
|
|
SpotlightInnerAngle = -1f,
|
2024-10-28 20:03:20 +00:00
|
|
|
};
|
2024-12-23 18:01:33 +00:00
|
|
|
|
|
|
|
if (propSpotlight != null)
|
2024-10-28 20:03:20 +00:00
|
|
|
{
|
2024-12-23 18:01:33 +00:00
|
|
|
light.Spotlight = true;
|
|
|
|
light.SpotlightInnerAngle = (float)Math.Cos(float.DegreesToRadians(propSpotlight.InnerAngle));
|
|
|
|
light.SpotlightOuterAngle = (float)Math.Cos(float.DegreesToRadians(propSpotlight.OuterAngle));
|
2024-10-28 20:03:20 +00:00
|
|
|
}
|
2024-12-23 18:01:33 +00:00
|
|
|
|
|
|
|
light.FixRadius();
|
|
|
|
light.ApplyTransforms(vhotLightPos, vhotLightDir, translate, rotate, scale);
|
2024-10-28 20:03:20 +00:00
|
|
|
|
|
|
|
_lights.Add(light);
|
|
|
|
lightTable.AddLight(light.ToLightData(32.0f));
|
|
|
|
}
|
|
|
|
|
2024-12-23 18:01:33 +00:00
|
|
|
if (propLight != null)
|
2024-10-28 20:03:20 +00:00
|
|
|
{
|
|
|
|
var light = new Light
|
|
|
|
{
|
2024-12-23 18:01:33 +00:00
|
|
|
Position = propLight.Offset,
|
|
|
|
Color = Utils.HsbToRgb(propLightColor.Hue, propLightColor.Saturation, propLight.Brightness),
|
|
|
|
Brightness = propLight.Brightness,
|
|
|
|
InnerRadius = propLight.InnerRadius,
|
|
|
|
Radius = propLight.Radius,
|
|
|
|
R2 = propLight.Radius * propLight.Radius,
|
|
|
|
QuadLit = propLight.QuadLit,
|
2024-10-28 20:03:20 +00:00
|
|
|
ObjId = id,
|
2024-12-23 18:01:33 +00:00
|
|
|
LightTableIndex = lightTable.LightCount,
|
2024-12-26 21:39:29 +00:00
|
|
|
SpotlightInnerAngle = -1f,
|
2024-10-28 20:03:20 +00:00
|
|
|
};
|
2024-12-23 18:01:33 +00:00
|
|
|
|
|
|
|
if (propSpotAmb != null)
|
2024-10-28 20:03:20 +00:00
|
|
|
{
|
2024-12-23 18:01:33 +00:00
|
|
|
var spot = new Light
|
|
|
|
{
|
|
|
|
Position = light.Position,
|
|
|
|
Color = Utils.HsbToRgb(propLightColor.Hue, propLightColor.Saturation, propSpotAmb.SpotBrightness),
|
2024-12-26 14:02:01 +00:00
|
|
|
Brightness = propSpotAmb.SpotBrightness,
|
2024-12-23 18:01:33 +00:00
|
|
|
InnerRadius = light.InnerRadius,
|
|
|
|
Radius = light.Radius,
|
|
|
|
R2 = light.R2,
|
|
|
|
QuadLit = light.QuadLit,
|
|
|
|
Spotlight = true,
|
|
|
|
SpotlightInnerAngle = (float)Math.Cos(float.DegreesToRadians(propSpotAmb.InnerAngle)),
|
|
|
|
SpotlightOuterAngle = (float)Math.Cos(float.DegreesToRadians(propSpotAmb.OuterAngle)),
|
|
|
|
ObjId = light.ObjId,
|
|
|
|
LightTableIndex = light.LightTableIndex,
|
|
|
|
};
|
2024-10-28 20:03:20 +00:00
|
|
|
|
2024-12-23 18:01:33 +00:00
|
|
|
light.LightTableIndex++; // Because we're inserting the spotlight part first
|
|
|
|
|
|
|
|
spot.FixRadius();
|
|
|
|
spot.ApplyTransforms(vhotLightPos, vhotLightDir, translate, rotate, scale);
|
|
|
|
|
|
|
|
_lights.Add(spot);
|
|
|
|
lightTable.AddLight(spot.ToLightData(32.0f));
|
|
|
|
}
|
|
|
|
else if (propSpotlight != null)
|
|
|
|
{
|
|
|
|
light.Spotlight = true;
|
|
|
|
light.SpotlightInnerAngle = (float)Math.Cos(float.DegreesToRadians(propSpotlight.InnerAngle));
|
|
|
|
light.SpotlightOuterAngle = (float)Math.Cos(float.DegreesToRadians(propSpotlight.OuterAngle));
|
|
|
|
}
|
|
|
|
|
|
|
|
light.FixRadius();
|
|
|
|
light.ApplyTransforms(vhotLightPos, vhotLightDir, translate, rotate, scale);
|
|
|
|
|
2024-10-28 20:03:20 +00:00
|
|
|
_lights.Add(light);
|
|
|
|
lightTable.AddLight(light.ToLightData(32.0f));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-09 19:03:20 +00:00
|
|
|
private void SetCellLightIndices(Settings settings)
|
2024-10-28 20:03:20 +00:00
|
|
|
{
|
2024-12-09 10:27:32 +00:00
|
|
|
// TODO: Doors aren't blocking lights. Need to do some cell traversal to remove light indices :(
|
|
|
|
|
2024-10-28 20:03:20 +00:00
|
|
|
if (!_mission.TryGetChunk<WorldRep>("WREXT", out var worldRep))
|
|
|
|
return;
|
|
|
|
|
2025-01-05 14:49:02 +00:00
|
|
|
// var lightVisibleCells = Timing.TimeStage("Light PVS", () =>
|
|
|
|
// {
|
|
|
|
// var cellCount = worldRep.Cells.Length;
|
|
|
|
// var aabbs = new MathUtils.Aabb[worldRep.Cells.Length];
|
|
|
|
// Parallel.For(0, cellCount, i => aabbs[i] = new MathUtils.Aabb(worldRep.Cells[i].Vertices));
|
|
|
|
//
|
|
|
|
// var lightCellMap = new int[_lights.Count];
|
|
|
|
// Parallel.For(0, _lights.Count, i =>
|
|
|
|
// {
|
|
|
|
// lightCellMap[i] = -1;
|
|
|
|
// var light = _lights[i];
|
|
|
|
// for (var j = 0; j < cellCount; j++)
|
|
|
|
// {
|
|
|
|
// if (!MathUtils.Intersects(aabbs[j], light.Position))
|
|
|
|
// {
|
|
|
|
// continue;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // Half-space contained
|
|
|
|
// var cell = worldRep.Cells[j];
|
|
|
|
// var contained = true;
|
|
|
|
// for (var k = 0; k < cell.PlaneCount; k++)
|
|
|
|
// {
|
|
|
|
// var plane = cell.Planes[k];
|
|
|
|
// if (MathUtils.DistanceFromPlane(plane, light.Position) < -MathUtils.Epsilon)
|
|
|
|
// {
|
|
|
|
// contained = false;
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// if (contained)
|
|
|
|
// {
|
|
|
|
// lightCellMap[i] = j;
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
//
|
|
|
|
// var lightVisibleCells = new List<int[]>(_lights.Count);
|
|
|
|
// var pvs = new PotentiallyVisibleSet(worldRep.Cells);
|
|
|
|
// for (var i = 0; i < _lights.Count; i++)
|
|
|
|
// {
|
|
|
|
// var cellIdx = lightCellMap[i];
|
|
|
|
// if (cellIdx == -1)
|
|
|
|
// {
|
|
|
|
// lightVisibleCells.Add([]);
|
|
|
|
// continue;
|
|
|
|
// }
|
|
|
|
// var visibleSet = pvs.GetVisible(lightCellMap[i]);
|
|
|
|
// lightVisibleCells.Add(visibleSet);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Console.WriteLine($"17: [{string.Join(", ", pvs.GetVisible(17))}]");
|
|
|
|
//
|
|
|
|
// return lightVisibleCells;
|
|
|
|
// });
|
2025-01-04 23:08:18 +00:00
|
|
|
|
2024-10-28 20:03:20 +00:00
|
|
|
// TODO: Move this functionality to the LGS library
|
|
|
|
// We set up light indices in separately from lighting because the actual
|
|
|
|
// lighting phase takes a lot of shortcuts that we don't want
|
2025-01-04 23:08:18 +00:00
|
|
|
// Parallel.ForEach(worldRep.Cells, cell =>
|
|
|
|
Parallel.For(0, worldRep.Cells.Length, i =>
|
2024-10-28 20:03:20 +00:00
|
|
|
{
|
2025-01-04 23:08:18 +00:00
|
|
|
var cell = worldRep.Cells[i];
|
2024-10-28 20:03:20 +00:00
|
|
|
cell.LightIndexCount = 0;
|
|
|
|
cell.LightIndices.Clear();
|
|
|
|
|
|
|
|
// The first element of the light indices array is used to store how many
|
|
|
|
// actual lights are in the list. Which is just LightIndexCount - 1...
|
|
|
|
// Odd choice I know
|
|
|
|
cell.LightIndexCount++;
|
|
|
|
cell.LightIndices.Add(0);
|
2024-12-09 14:48:16 +00:00
|
|
|
|
2024-12-09 19:03:20 +00:00
|
|
|
// If we have sunlight, then we just assume the sun has the potential to reach everything (ew)
|
|
|
|
// The sun enabled option doesn't actually seem to do anything at runtime, it's purely about if
|
|
|
|
// the cell has the sunlight idx on it.
|
|
|
|
if (settings.Sunlight.Enabled)
|
|
|
|
{
|
|
|
|
cell.LightIndexCount++;
|
|
|
|
cell.LightIndices.Add(0);
|
|
|
|
cell.LightIndices[0]++;
|
|
|
|
}
|
2024-10-28 20:03:20 +00:00
|
|
|
|
|
|
|
// The OG lightmapper uses the cell traversal to work out all the cells that
|
|
|
|
// are actually visited. We're a lot more coarse and just say if a cell is
|
|
|
|
// in range then we potentially affect the lighting in the cell and add it
|
2024-11-29 17:24:03 +00:00
|
|
|
// to the list.
|
|
|
|
// There's a soft length limit here of 96 due to the runtime object shadow
|
|
|
|
// cache, so we want this to be as minimal as possible. Additionally large
|
|
|
|
// lists actually cause performance issues!
|
|
|
|
var cellAabb = new MathUtils.Aabb(cell.Vertices);
|
2025-01-04 23:08:18 +00:00
|
|
|
for (var j = 0; j < _lights.Count; j++)
|
2024-10-28 20:03:20 +00:00
|
|
|
{
|
2025-01-04 23:08:18 +00:00
|
|
|
var light = _lights[j];
|
|
|
|
if (!MathUtils.Intersects(new MathUtils.Sphere(light.Position, light.Radius), cellAabb))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2025-01-05 14:49:02 +00:00
|
|
|
// if (!lightVisibleCells[j].Contains(i))
|
|
|
|
// {
|
|
|
|
// continue;
|
|
|
|
// }
|
2025-01-04 23:08:18 +00:00
|
|
|
|
|
|
|
cell.LightIndexCount++;
|
|
|
|
cell.LightIndices.Add((ushort)light.LightTableIndex);
|
|
|
|
cell.LightIndices[0]++;
|
2024-10-28 20:03:20 +00:00
|
|
|
}
|
2024-12-09 18:40:04 +00:00
|
|
|
|
|
|
|
if (cell.LightIndexCount > 97)
|
|
|
|
{
|
2025-01-11 13:16:31 +00:00
|
|
|
Log.Warning("Cell {Id} sees too many lights ({Count})", i, cell.LightIndices[0]);
|
2024-12-09 18:40:04 +00:00
|
|
|
}
|
2024-10-28 20:03:20 +00:00
|
|
|
});
|
2024-12-09 20:49:00 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
var overLit = 0;
|
|
|
|
var maxLights = 0;
|
|
|
|
foreach (var cell in worldRep.Cells)
|
|
|
|
{
|
|
|
|
if (cell.LightIndexCount > 97)
|
|
|
|
{
|
|
|
|
overLit++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cell.LightIndexCount > maxLights)
|
|
|
|
{
|
|
|
|
maxLights = cell.LightIndexCount - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (overLit > 0)
|
|
|
|
{
|
2025-01-11 13:16:31 +00:00
|
|
|
Log.Warning("{Count}/{CellCount} cells are overlit. Overlit cells can cause Object/Light Gem lighting issues.", overLit, worldRep.Cells.Length);
|
2024-12-09 20:49:00 +00:00
|
|
|
}
|
2025-01-04 23:08:18 +00:00
|
|
|
|
2025-01-11 13:16:31 +00:00
|
|
|
Log.Information("Max cell lights found ({Count}/96)", maxLights);
|
2024-12-09 20:49:00 +00:00
|
|
|
}
|
2024-10-28 20:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void TraceScene(Settings settings)
|
|
|
|
{
|
|
|
|
if (!_mission.TryGetChunk<WorldRep>("WREXT", out var worldRep))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Parallel.ForEach(worldRep.Cells, cell =>
|
|
|
|
{
|
|
|
|
// Reset cell AnimLight palette
|
|
|
|
cell.AnimLightCount = 0;
|
|
|
|
cell.AnimLights.Clear();
|
|
|
|
|
|
|
|
var numPolys = cell.PolyCount;
|
|
|
|
var numRenderPolys = cell.RenderPolyCount;
|
|
|
|
var numPortalPolys = cell.PortalPolyCount;
|
|
|
|
|
|
|
|
// There's nothing to render
|
|
|
|
// Portal polys can be render polys (e.g. water) but we're ignoring them for now
|
|
|
|
if (numRenderPolys == 0 || numPortalPolys >= numPolys)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-11-04 18:39:10 +00:00
|
|
|
var solidPolys = numPolys - numPortalPolys;
|
2024-10-28 20:03:20 +00:00
|
|
|
var cellIdxOffset = 0;
|
2024-11-04 18:39:10 +00:00
|
|
|
for (var polyIdx = 0; polyIdx < numRenderPolys; polyIdx++)
|
2024-10-28 20:03:20 +00:00
|
|
|
{
|
|
|
|
var poly = cell.Polys[polyIdx];
|
|
|
|
var plane = cell.Planes[poly.PlaneId];
|
|
|
|
var renderPoly = cell.RenderPolys[polyIdx];
|
|
|
|
var info = cell.LightList[polyIdx];
|
|
|
|
var lightmap = cell.Lightmaps[polyIdx];
|
|
|
|
|
|
|
|
info.AnimLightBitmask = 0;
|
2024-11-04 18:39:10 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2024-12-26 17:12:28 +00:00
|
|
|
|
|
|
|
var ambientLight = settings.AmbientLight[cell.ZoneInfo.GetAmbientLightZoneIndex()];
|
|
|
|
lightmap.Reset(ambientLight, settings.Hdr);
|
2024-10-28 20:03:20 +00:00
|
|
|
|
|
|
|
// Get world position of lightmap (0, 0) (+0.5 so we cast from the center of a pixel)
|
|
|
|
var topLeft = cell.Vertices[cell.Indices[cellIdxOffset]];
|
|
|
|
topLeft -= renderPoly.TextureVectors.Item1 * (renderPoly.TextureBases.Item1 - info.Bases.Item1 * 0.25f);
|
|
|
|
topLeft -= renderPoly.TextureVectors.Item2 * (renderPoly.TextureBases.Item2 - info.Bases.Item2 * 0.25f);
|
|
|
|
|
|
|
|
var xDir = 0.25f * lightmap.Width * renderPoly.TextureVectors.Item1;
|
|
|
|
var yDir = 0.25f * lightmap.Height * renderPoly.TextureVectors.Item2;
|
|
|
|
var aabb = new MathUtils.Aabb([
|
|
|
|
topLeft,
|
|
|
|
topLeft + xDir,
|
|
|
|
topLeft + yDir,
|
|
|
|
topLeft + xDir + yDir,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Used for clipping points to poly
|
|
|
|
var vs = new Vector3[poly.VertexCount];
|
|
|
|
for (var i = 0; i < poly.VertexCount; i++)
|
|
|
|
{
|
|
|
|
vs[i] = cell.Vertices[cell.Indices[cellIdxOffset + i]];
|
|
|
|
}
|
|
|
|
var planeMapper = new MathUtils.PlanePointMapper(plane.Normal, vs[0], vs[1]);
|
|
|
|
var v2ds = planeMapper.MapTo2d(vs);
|
2024-12-09 12:28:52 +00:00
|
|
|
|
|
|
|
var (texU, texV) = renderPoly.TextureVectors;
|
|
|
|
var (offsets, weights) =
|
|
|
|
GetTraceOffsetsAndWeights(settings.MultiSampling, texU, texV, settings.MultiSamplingCenterWeight);
|
|
|
|
var (quadOffsets, quadWeights) = settings.MultiSampling == SoftnessMode.HighFourPoint
|
|
|
|
? (offsets, weights)
|
|
|
|
: GetTraceOffsetsAndWeights(SoftnessMode.HighFourPoint, texU, texV, settings.MultiSamplingCenterWeight);
|
|
|
|
|
|
|
|
for (var y = 0; y < lightmap.Height; y++)
|
2024-10-28 20:03:20 +00:00
|
|
|
{
|
2024-12-09 12:28:52 +00:00
|
|
|
for (var x = 0; x < lightmap.Width; x++)
|
2024-10-28 20:03:20 +00:00
|
|
|
{
|
2024-12-09 12:28:52 +00:00
|
|
|
var pos = topLeft;
|
|
|
|
pos += x * 0.25f * renderPoly.TextureVectors.Item1;
|
|
|
|
pos += y * 0.25f * renderPoly.TextureVectors.Item2;
|
|
|
|
|
|
|
|
// 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, planeMapper, v2ds);
|
|
|
|
var quadTracePoints = settings.MultiSampling == SoftnessMode.HighFourPoint
|
|
|
|
? tracePoints
|
|
|
|
: GetTracePoints(pos, quadOffsets, renderPoly.Center, planeMapper, v2ds);
|
2024-12-09 14:35:53 +00:00
|
|
|
|
2024-12-09 15:23:47 +00:00
|
|
|
// This is almost perfect now. Any issues seem to be related to Dark not carrying HSB strength correctly
|
2024-12-09 14:35:53 +00:00
|
|
|
if (settings.Sunlight.Enabled) {
|
|
|
|
// Check if plane normal is facing towards the light
|
|
|
|
// If it's not then we're never going to be (directly) lit by this
|
|
|
|
// light.
|
|
|
|
var sunAngle = Vector3.Dot(-settings.Sunlight.Direction, plane.Normal);
|
|
|
|
if (sunAngle > 0)
|
|
|
|
{
|
|
|
|
var strength = 0f;
|
|
|
|
var targetPoints = settings.Sunlight.QuadLit ? quadTracePoints : tracePoints;
|
|
|
|
var targetWeights = settings.Sunlight.QuadLit ? quadWeights : weights;
|
|
|
|
for (var idx = 0; idx < targetPoints.Length; idx++)
|
|
|
|
{
|
|
|
|
var point = targetPoints[idx];
|
|
|
|
if (TraceSunRay(point, -settings.Sunlight.Direction))
|
|
|
|
{
|
|
|
|
// Sunlight is a simpler lighting algorithm than normal lights so we can just
|
|
|
|
// do it here
|
|
|
|
strength += targetWeights[idx] * sunAngle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strength != 0f)
|
|
|
|
{
|
|
|
|
lightmap.AddLight(0, x, y, settings.Sunlight.Color, strength, settings.Hdr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-05 14:49:02 +00:00
|
|
|
// foreach (var lightIdx in cell.LightIndices)
|
|
|
|
for (var i = 0; i < cell.LightIndexCount; i++)
|
2024-10-28 20:03:20 +00:00
|
|
|
{
|
2025-01-05 14:49:02 +00:00
|
|
|
var lightIdx = cell.LightIndices[i];
|
|
|
|
if (i == 0 || lightIdx == 0)
|
2025-01-04 23:30:32 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2025-01-05 14:49:02 +00:00
|
|
|
|
2025-01-04 23:30:32 +00:00
|
|
|
var light = _lights[lightIdx - 1];
|
2024-12-09 12:04:32 +00:00
|
|
|
|
2024-12-09 12:28:52 +00:00
|
|
|
// Check if plane normal is facing towards the light
|
|
|
|
// If it's not then we're never going to be (directly) lit by this
|
|
|
|
// light.
|
|
|
|
var centerDirection = renderPoly.Center - light.Position;
|
|
|
|
if (Vector3.Dot(plane.Normal, centerDirection) >= 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2024-12-09 12:04:32 +00:00
|
|
|
|
2024-12-09 12:28:52 +00:00
|
|
|
// If there aren't *any* points on the plane that are in range of the light
|
|
|
|
// then none of the lightmap points will be so we can discard.
|
|
|
|
// The more compact a map is the less effective this is
|
2025-01-05 14:49:02 +00:00
|
|
|
var planeDist = Math.Abs(MathUtils.DistanceFromPlane(plane, light.Position));
|
2024-12-09 12:28:52 +00:00
|
|
|
if (planeDist > light.Radius)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the poly of the lightmap doesn't intersect the light radius then
|
|
|
|
// none of the lightmap points will so we can discard.
|
|
|
|
if (!MathUtils.Intersects(new MathUtils.Sphere(light.Position, light.Radius), aabb))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-12-09 12:04:32 +00:00
|
|
|
var strength = 0f;
|
2024-12-09 12:28:52 +00:00
|
|
|
var targetPoints = light.QuadLit ? quadTracePoints : tracePoints;
|
|
|
|
var targetWeights = light.QuadLit ? quadWeights : weights;
|
|
|
|
for (var idx = 0; idx < targetPoints.Length; idx++)
|
2024-12-09 12:04:32 +00:00
|
|
|
{
|
2024-12-09 12:28:52 +00:00
|
|
|
var point = targetPoints[idx];
|
2024-12-09 12:04:32 +00:00
|
|
|
|
|
|
|
// If we're out of range there's no point casting a ray
|
|
|
|
// There's probably a better way to discard the entire lightmap
|
|
|
|
// if we're massively out of range
|
|
|
|
if ((point - light.Position).LengthSquared() > light.R2)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TraceRay(light.Position, point))
|
|
|
|
{
|
2024-12-26 14:04:47 +00:00
|
|
|
strength += targetWeights[idx] * light.StrengthAtPoint(point, plane, settings.AnimLightCutoff);
|
2024-12-09 12:04:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strength != 0f)
|
2024-10-28 20:03:20 +00:00
|
|
|
{
|
2025-01-04 23:30:32 +00:00
|
|
|
var layer = 0;
|
|
|
|
|
2024-10-28 20:03:20 +00:00
|
|
|
// If we're an anim light there's a lot of stuff we need to update
|
|
|
|
// Firstly we need to add the light to the cells anim light palette
|
|
|
|
// Secondly we need to set the appropriate bit of the lightmap's
|
|
|
|
// bitmask. Finally we need to check if the lightmap needs another layer
|
2025-01-04 23:08:18 +00:00
|
|
|
// TODO: Handle too many lights for a layer
|
2024-10-28 20:03:20 +00:00
|
|
|
if (light.Anim)
|
|
|
|
{
|
|
|
|
// TODO: Don't recalculate this for every point lol
|
|
|
|
var paletteIdx = cell.AnimLights.IndexOf((ushort)light.LightTableIndex);
|
|
|
|
if (paletteIdx == -1)
|
|
|
|
{
|
|
|
|
paletteIdx = cell.AnimLightCount;
|
|
|
|
cell.AnimLightCount++;
|
|
|
|
cell.AnimLights.Add((ushort)light.LightTableIndex);
|
|
|
|
}
|
|
|
|
info.AnimLightBitmask |= 1u << paletteIdx;
|
|
|
|
layer = paletteIdx + 1;
|
|
|
|
}
|
|
|
|
lightmap.AddLight(layer, x, y, light.Color, strength, settings.Hdr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cellIdxOffset += poly.VertexCount;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-12-09 12:04:32 +00:00
|
|
|
private static (Vector3[], float[]) GetTraceOffsetsAndWeights(
|
2024-10-29 18:01:50 +00:00
|
|
|
SoftnessMode mode,
|
|
|
|
Vector3 texU,
|
|
|
|
Vector3 texV,
|
2024-12-09 12:04:32 +00:00
|
|
|
float centerWeight)
|
2024-10-29 18:01:50 +00:00
|
|
|
{
|
2024-12-09 12:04:32 +00:00
|
|
|
var offsetScale = mode switch
|
2024-10-29 18:13:04 +00:00
|
|
|
{
|
2024-12-09 12:04:32 +00:00
|
|
|
SoftnessMode.HighFourPoint or SoftnessMode.HighFivePoint or SoftnessMode.HighNinePoint => 4f,
|
|
|
|
SoftnessMode.MediumFourPoint or SoftnessMode.MediumFivePoint or SoftnessMode.MediumNinePoint => 8f,
|
|
|
|
SoftnessMode.LowFourPoint => 16f,
|
|
|
|
_ => 1f,
|
|
|
|
};
|
|
|
|
|
|
|
|
var cw = centerWeight;
|
|
|
|
var w = 1f - cw;
|
|
|
|
texU /= offsetScale;
|
|
|
|
texV /= offsetScale;
|
2024-10-29 18:01:50 +00:00
|
|
|
|
2024-12-09 12:04:32 +00:00
|
|
|
return mode switch
|
|
|
|
{
|
|
|
|
SoftnessMode.LowFourPoint or SoftnessMode.MediumFourPoint or SoftnessMode.HighFourPoint => (
|
|
|
|
[-texU - texV, -texU - texV, -texU + texV, texU + texV],
|
|
|
|
[0.25f, 0.25f, 0.25f, 0.25f]),
|
|
|
|
SoftnessMode.MediumFivePoint or SoftnessMode.HighFivePoint => (
|
|
|
|
[Vector3.Zero, -texU - texV, texU - texV, -texU + texV, texU + texV],
|
|
|
|
[cw, w * 0.25f, w * 0.25f, w * 0.25f, w * 0.25f]),
|
|
|
|
SoftnessMode.MediumNinePoint or SoftnessMode.HighNinePoint => (
|
|
|
|
[Vector3.Zero, -texU - texV, texU - texV, -texU + texV, texU + texV, -texU, texU, -texV, texV],
|
|
|
|
[cw, w * 0.125f, w * 0.125f, w * 0.125f, w * 0.125f, w * 0.125f, w * 0.125f, w * 0.125f, w * 0.125f]),
|
|
|
|
_ => (
|
|
|
|
[Vector3.Zero],
|
|
|
|
[1f]),
|
2024-10-29 18:14:44 +00:00
|
|
|
};
|
2024-10-29 18:01:50 +00:00
|
|
|
}
|
2024-12-09 12:04:32 +00:00
|
|
|
|
|
|
|
private Vector3[] GetTracePoints(
|
|
|
|
Vector3 basePosition,
|
|
|
|
Vector3[] offsets,
|
2024-10-28 20:03:20 +00:00
|
|
|
Vector3 polyCenter,
|
|
|
|
MathUtils.PlanePointMapper planeMapper,
|
2024-12-09 12:04:32 +00:00
|
|
|
Vector2[] v2ds)
|
2024-10-28 20:03:20 +00:00
|
|
|
{
|
2024-12-09 12:04:32 +00:00
|
|
|
var tracePoints = new Vector3[offsets.Length];
|
|
|
|
for (var i = 0; i < offsets.Length; i++)
|
2024-10-28 20:03:20 +00:00
|
|
|
{
|
2024-12-09 12:04:32 +00:00
|
|
|
var offset = offsets[i];
|
|
|
|
var pos = basePosition + offset;
|
|
|
|
|
|
|
|
// Embree has robustness issues when hitting poly edges which
|
|
|
|
// results in false misses. To alleviate this we pre-push everything
|
|
|
|
// slightly towards the center of the poly.
|
|
|
|
var centerOffset = polyCenter - pos;
|
|
|
|
if (centerOffset.LengthSquared() > MathUtils.Epsilon)
|
|
|
|
{
|
|
|
|
pos += Vector3.Normalize(centerOffset) * MathUtils.Epsilon;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we can't see our target point from the center of the poly
|
|
|
|
// then it's outside the world. We need to clip the point to slightly
|
|
|
|
// inside the poly and retrace to avoid three 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
|
|
|
|
// 3. Darkened spots where centers are on the exact edge of a poly
|
|
|
|
// which can sometimes cause Embree to miss casts
|
|
|
|
var inPoly = TraceRay(polyCenter + planeMapper.Normal * 0.25f, pos);
|
|
|
|
if (!inPoly)
|
|
|
|
{
|
|
|
|
var p2d = planeMapper.MapTo2d(pos);
|
|
|
|
p2d = MathUtils.ClipPointToPoly2d(p2d, v2ds);
|
|
|
|
pos = planeMapper.MapTo3d(p2d);
|
|
|
|
}
|
2024-10-28 20:03:20 +00:00
|
|
|
|
2024-12-09 12:04:32 +00:00
|
|
|
tracePoints[i] = pos;
|
2024-10-28 20:03:20 +00:00
|
|
|
}
|
2024-12-09 12:04:32 +00:00
|
|
|
|
|
|
|
return tracePoints;
|
2024-10-28 20:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private bool TraceRay(Vector3 origin, Vector3 target)
|
|
|
|
{
|
2024-11-04 18:08:54 +00:00
|
|
|
var hitDistanceFromTarget = float.MinValue;
|
|
|
|
var hitSurfaceType = SurfaceType.Water;
|
|
|
|
while (hitDistanceFromTarget < -MathUtils.Epsilon && hitSurfaceType == SurfaceType.Water)
|
2024-10-28 20:03:20 +00:00
|
|
|
{
|
2024-11-04 18:08:54 +00:00
|
|
|
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;
|
2024-10-28 20:03:20 +00:00
|
|
|
}
|
|
|
|
|
2024-12-09 14:35:53 +00:00
|
|
|
// TODO: Can this be merged with the above?
|
|
|
|
private bool TraceSunRay(Vector3 origin, Vector3 direction)
|
|
|
|
{
|
|
|
|
// Avoid self intersection
|
|
|
|
origin += direction * MathUtils.Epsilon;
|
|
|
|
|
|
|
|
var hitSurfaceType = SurfaceType.Water;
|
|
|
|
while (hitSurfaceType == SurfaceType.Water)
|
|
|
|
{
|
|
|
|
var hitResult = _scene.Trace(new Ray
|
|
|
|
{
|
|
|
|
Origin = origin,
|
|
|
|
Direction = Vector3.Normalize(direction),
|
|
|
|
});
|
|
|
|
|
|
|
|
hitSurfaceType = _triangleTypeMap[(int)hitResult.PrimId];
|
|
|
|
origin = hitResult.Position += direction * MathUtils.Epsilon;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hitSurfaceType == SurfaceType.Sky;
|
|
|
|
}
|
|
|
|
|
2024-10-28 20:03:20 +00:00
|
|
|
private void SetAnimLightCellMaps()
|
|
|
|
{
|
|
|
|
if (!_mission.TryGetChunk<PropertyChunk<PropAnimLight>>("P$AnimLight", out var animLightChunk) ||
|
|
|
|
!_mission.TryGetChunk<WorldRep>("WREXT", out var worldRep))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we've set all the per-cell stuff we need to aggregate the cell mappings
|
|
|
|
// We can't do this in parallel which is why it's being done afterwards rather than
|
|
|
|
// as we go
|
|
|
|
var map = new Dictionary<ushort, List<WorldRep.LightTable.AnimCellMap>>();
|
|
|
|
for (ushort i = 0; i < worldRep.Cells.Length; i++)
|
|
|
|
{
|
|
|
|
var cell = worldRep.Cells[i];
|
|
|
|
for (ushort j = 0; j < cell.AnimLightCount; j++)
|
|
|
|
{
|
|
|
|
var animLightIdx = cell.AnimLights[j];
|
|
|
|
if (!map.TryGetValue(animLightIdx, out var value))
|
|
|
|
{
|
|
|
|
value = [];
|
|
|
|
map[animLightIdx] = value;
|
|
|
|
}
|
|
|
|
value.Add(new WorldRep.LightTable.AnimCellMap
|
|
|
|
{
|
|
|
|
CellIndex = i,
|
|
|
|
LightIndex = j,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var (lightIdx, animCellMaps) in map)
|
|
|
|
{
|
|
|
|
// We need to update the object property so it knows its mapping range
|
|
|
|
// TODO: Handle nulls
|
|
|
|
var light = _lights.Find(l => l.Anim && l.LightTableIndex == lightIdx);
|
|
|
|
var prop = animLightChunk.properties.Find(p => p.objectId == light.ObjId);
|
|
|
|
prop.LightTableLightIndex = lightIdx;
|
|
|
|
prop.LightTableMapIndex = (ushort)worldRep.LightingTable.AnimMapCount;
|
|
|
|
prop.CellsReached = (ushort)animCellMaps.Count;
|
|
|
|
|
|
|
|
worldRep.LightingTable.AnimCellMaps.AddRange(animCellMaps);
|
|
|
|
worldRep.LightingTable.AnimMapCount += animCellMaps.Count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|