Compare commits
No commits in common. "c64343f57444c219c79f17d56764df048d02e978" and "b263bdd77e088220c0e2a8ca2fb08195e8595a9e" have entirely different histories.
c64343f574
...
b263bdd77e
|
@ -1,83 +0,0 @@
|
|||
using System.Numerics;
|
||||
using KeepersCompound.LGS.Database.Chunks;
|
||||
|
||||
namespace KeepersCompound.Lightmapper;
|
||||
|
||||
public class Light
|
||||
{
|
||||
public Vector3 Position;
|
||||
public Vector3 Color;
|
||||
public float InnerRadius;
|
||||
public float Radius;
|
||||
public float R2;
|
||||
|
||||
public bool Spotlight;
|
||||
public Vector3 SpotlightDir;
|
||||
public float SpotlightInnerAngle;
|
||||
public float SpotlightOuterAngle;
|
||||
|
||||
public int ObjId;
|
||||
public int LightTableIndex;
|
||||
public bool Anim;
|
||||
|
||||
public WorldRep.LightTable.LightData ToLightData(float lightScale)
|
||||
{
|
||||
return new WorldRep.LightTable.LightData
|
||||
{
|
||||
Location = Position,
|
||||
Direction = SpotlightDir,
|
||||
Color = Color / lightScale,
|
||||
InnerAngle = SpotlightInnerAngle,
|
||||
OuterAngle = SpotlightOuterAngle,
|
||||
Radius = Radius == float.MaxValue ? 0 : Radius,
|
||||
};
|
||||
}
|
||||
|
||||
public float StrengthAtPoint(Vector3 point, Plane plane)
|
||||
{
|
||||
// Calculate light strength at a given point. As far as I can tell
|
||||
// this is exact to Dark (I'm a genius??). It's just an inverse distance
|
||||
// falloff with diffuse angle, except we have to scale the length.
|
||||
var dir = Position - point;
|
||||
var angle = Vector3.Dot(Vector3.Normalize(dir), plane.Normal);
|
||||
var len = dir.Length();
|
||||
var slen = len / 4.0f;
|
||||
var strength = (angle + 1.0f) / slen;
|
||||
|
||||
// Inner radius starts a linear falloff to 0 at the radius
|
||||
if (InnerRadius != 0 && len > InnerRadius)
|
||||
{
|
||||
strength *= (Radius - len) / (Radius - InnerRadius);
|
||||
}
|
||||
|
||||
// This is basically the same as how inner radius works. It just applies
|
||||
// a linear falloff to 0 between the inner angle and outer angle.
|
||||
if (Spotlight)
|
||||
{
|
||||
var spotAngle = Vector3.Dot(-Vector3.Normalize(dir), SpotlightDir);
|
||||
var inner = SpotlightInnerAngle;
|
||||
var outer = SpotlightOuterAngle;
|
||||
|
||||
// In an improperly configured spotlight inner and outer angles might be the
|
||||
// same. So to avoid division by zero (and some clamping) we explicitly handle
|
||||
// some cases
|
||||
float spotlightMultiplier;
|
||||
if (spotAngle >= inner)
|
||||
{
|
||||
spotlightMultiplier = 1.0f;
|
||||
}
|
||||
else if (spotAngle <= outer)
|
||||
{
|
||||
spotlightMultiplier = 0.0f;
|
||||
}
|
||||
else
|
||||
{
|
||||
spotlightMultiplier = (spotAngle - outer) / (inner - outer);
|
||||
}
|
||||
|
||||
strength *= spotlightMultiplier;
|
||||
}
|
||||
|
||||
return strength;
|
||||
}
|
||||
}
|
|
@ -8,6 +8,38 @@ namespace KeepersCompound.Lightmapper;
|
|||
|
||||
class Program
|
||||
{
|
||||
// Super simple for now
|
||||
private record Light
|
||||
{
|
||||
public Vector3 position;
|
||||
public Vector3 color;
|
||||
public float innerRadius;
|
||||
public float radius;
|
||||
public float r2;
|
||||
|
||||
public bool spotlight;
|
||||
public Vector3 spotlightDir;
|
||||
public float spotlightInnerAngle;
|
||||
public float spotlightOuterAngle;
|
||||
|
||||
public int objId;
|
||||
public int lightTableIndex;
|
||||
public bool anim;
|
||||
|
||||
public WorldRep.LightTable.LightData ToLightData(float lightScale)
|
||||
{
|
||||
return new WorldRep.LightTable.LightData
|
||||
{
|
||||
Location = position,
|
||||
Direction = spotlightDir,
|
||||
Color = color / lightScale,
|
||||
InnerAngle = spotlightInnerAngle,
|
||||
OuterAngle = spotlightOuterAngle,
|
||||
Radius = radius == float.MaxValue ? 0 : radius,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Timing.Reset();
|
||||
|
@ -103,8 +135,8 @@ class Program
|
|||
{
|
||||
// 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);
|
||||
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;
|
||||
|
@ -162,15 +194,22 @@ class Program
|
|||
var sz = brush.size;
|
||||
var light = new Light
|
||||
{
|
||||
Position = brush.position,
|
||||
Color = Utils.HsbToRgb(sz.Y, sz.Z, Math.Min(sz.X, 255.0f)),
|
||||
Radius = float.MaxValue,
|
||||
R2 = float.MaxValue,
|
||||
LightTableIndex = lightTable.LightCount,
|
||||
position = brush.position,
|
||||
color = Utils.HsbToRgb(sz.Y, sz.Z, Math.Min(sz.X, 255.0f)),
|
||||
radius = float.MaxValue,
|
||||
r2 = float.MaxValue,
|
||||
lightTableIndex = lightTable.LightCount,
|
||||
};
|
||||
|
||||
lights.Add(light);
|
||||
lightTable.AddLight(light.ToLightData(32.0f));
|
||||
lightTable.AddLight(new WorldRep.LightTable.LightData
|
||||
{
|
||||
Location = light.position,
|
||||
Direction = light.spotlightDir,
|
||||
Color = light.color / 32.0f, // TODO: This is based on light_scale config var
|
||||
InnerAngle = -1.0f,
|
||||
Radius = 0,
|
||||
});
|
||||
}
|
||||
|
||||
private static void ProcessObjectLight(
|
||||
|
@ -192,9 +231,9 @@ class Program
|
|||
|
||||
var baseLight = new Light
|
||||
{
|
||||
Position = brush.position,
|
||||
SpotlightDir = -Vector3.UnitZ,
|
||||
SpotlightInnerAngle = -1.0f,
|
||||
position = brush.position,
|
||||
spotlightDir = -Vector3.UnitZ,
|
||||
spotlightInnerAngle = -1.0f,
|
||||
};
|
||||
|
||||
if (propModelName != null)
|
||||
|
@ -207,11 +246,11 @@ class Program
|
|||
var model = new ModelFile(modelPath);
|
||||
if (model.TryGetVhot(ModelFile.VhotId.LightPosition, out var vhot))
|
||||
{
|
||||
baseLight.Position += vhot.Position;
|
||||
baseLight.position += vhot.Position;
|
||||
}
|
||||
if (model.TryGetVhot(ModelFile.VhotId.LightDirection, out vhot))
|
||||
{
|
||||
baseLight.SpotlightDir = vhot.Position;
|
||||
baseLight.spotlightDir = vhot.Position;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -224,31 +263,31 @@ class Program
|
|||
rot *= Matrix4x4.CreateRotationY(float.DegreesToRadians(brush.angle.Y));
|
||||
rot *= Matrix4x4.CreateRotationZ(float.DegreesToRadians(brush.angle.Z));
|
||||
|
||||
baseLight.Spotlight = true;
|
||||
baseLight.SpotlightDir = Vector3.Transform(baseLight.SpotlightDir, rot);
|
||||
baseLight.SpotlightInnerAngle = (float)Math.Cos(float.DegreesToRadians(propSpotlight.InnerAngle));
|
||||
baseLight.SpotlightOuterAngle = (float)Math.Cos(float.DegreesToRadians(propSpotlight.OuterAngle));
|
||||
baseLight.spotlight = true;
|
||||
baseLight.spotlightDir = Vector3.Transform(baseLight.spotlightDir, rot);
|
||||
baseLight.spotlightInnerAngle = (float)Math.Cos(float.DegreesToRadians(propSpotlight.InnerAngle));
|
||||
baseLight.spotlightOuterAngle = (float)Math.Cos(float.DegreesToRadians(propSpotlight.OuterAngle));
|
||||
}
|
||||
|
||||
if (propLight != null)
|
||||
{
|
||||
var light = new Light
|
||||
{
|
||||
Position = baseLight.Position + propLight.Offset,
|
||||
Color = Utils.HsbToRgb(propLightColor.Hue, propLightColor.Saturation, propLight.Brightness),
|
||||
InnerRadius = propLight.InnerRadius,
|
||||
Radius = propLight.Radius,
|
||||
R2 = propLight.Radius * propLight.Radius,
|
||||
Spotlight = baseLight.Spotlight,
|
||||
SpotlightDir = baseLight.SpotlightDir,
|
||||
SpotlightInnerAngle = baseLight.SpotlightInnerAngle,
|
||||
SpotlightOuterAngle = baseLight.SpotlightOuterAngle,
|
||||
LightTableIndex = lightTable.LightCount,
|
||||
position = baseLight.position + propLight.Offset,
|
||||
color = Utils.HsbToRgb(propLightColor.Hue, propLightColor.Saturation, propLight.Brightness),
|
||||
innerRadius = propLight.InnerRadius,
|
||||
radius = propLight.Radius,
|
||||
r2 = propLight.Radius * propLight.Radius,
|
||||
spotlight = baseLight.spotlight,
|
||||
spotlightDir = baseLight.spotlightDir,
|
||||
spotlightInnerAngle = baseLight.spotlightInnerAngle,
|
||||
spotlightOuterAngle = baseLight.spotlightOuterAngle,
|
||||
lightTableIndex = lightTable.LightCount,
|
||||
};
|
||||
if (propLight.Radius == 0)
|
||||
{
|
||||
light.Radius = float.MaxValue;
|
||||
light.R2 = float.MaxValue;
|
||||
light.radius = float.MaxValue;
|
||||
light.r2 = float.MaxValue;
|
||||
}
|
||||
|
||||
lights.Add(light);
|
||||
|
@ -262,23 +301,23 @@ class Program
|
|||
|
||||
var light = new Light
|
||||
{
|
||||
Position = baseLight.Position + propAnimLight.Offset,
|
||||
Color = Utils.HsbToRgb(propLightColor.Hue, propLightColor.Saturation, propAnimLight.MaxBrightness),
|
||||
InnerRadius = propAnimLight.InnerRadius,
|
||||
Radius = propAnimLight.Radius,
|
||||
R2 = propAnimLight.Radius * propAnimLight.Radius,
|
||||
Spotlight = baseLight.Spotlight,
|
||||
SpotlightDir = baseLight.SpotlightDir,
|
||||
SpotlightInnerAngle = baseLight.SpotlightInnerAngle,
|
||||
SpotlightOuterAngle = baseLight.SpotlightOuterAngle,
|
||||
Anim = true,
|
||||
ObjId = id,
|
||||
LightTableIndex = propAnimLight.LightTableLightIndex,
|
||||
position = baseLight.position + propAnimLight.Offset,
|
||||
color = Utils.HsbToRgb(propLightColor.Hue, propLightColor.Saturation, propAnimLight.MaxBrightness),
|
||||
innerRadius = propAnimLight.InnerRadius,
|
||||
radius = propAnimLight.Radius,
|
||||
r2 = propAnimLight.Radius * propAnimLight.Radius,
|
||||
spotlight = baseLight.spotlight,
|
||||
spotlightDir = baseLight.spotlightDir,
|
||||
spotlightInnerAngle = baseLight.spotlightInnerAngle,
|
||||
spotlightOuterAngle = baseLight.spotlightOuterAngle,
|
||||
anim = true,
|
||||
objId = id,
|
||||
lightTableIndex = propAnimLight.LightTableLightIndex,
|
||||
};
|
||||
if (propAnimLight.Radius == 0)
|
||||
{
|
||||
light.Radius = float.MaxValue;
|
||||
light.R2 = float.MaxValue;
|
||||
light.radius = float.MaxValue;
|
||||
light.r2 = float.MaxValue;
|
||||
}
|
||||
|
||||
lights.Add(light);
|
||||
|
@ -420,7 +459,7 @@ class Program
|
|||
// 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;
|
||||
var centerDirection = renderPoly.Center - light.position;
|
||||
if (Vector3.Dot(plane.Normal, centerDirection) >= 0)
|
||||
{
|
||||
continue;
|
||||
|
@ -429,15 +468,15 @@ class Program
|
|||
// 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
|
||||
var planeDist = MathUtils.DistanceFromPlane(plane, light.Position);
|
||||
if (planeDist > light.Radius)
|
||||
var planeDist = MathUtils.DistanceFromPlane(plane, light.position);
|
||||
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))
|
||||
if (!MathUtils.Intersects(new MathUtils.Sphere(light.position, light.radius), aabb))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
@ -450,64 +489,59 @@ class Program
|
|||
pos += x * 0.25f * renderPoly.TextureVectors.Item1;
|
||||
pos += y * 0.25f * renderPoly.TextureVectors.Item2;
|
||||
|
||||
// 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 = renderPoly.Center - 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
|
||||
// We need to clip the point to slightly inside of the poly
|
||||
// to avoid three problems:
|
||||
// 1. Darkened spots from lightmap pixels who's center is outside
|
||||
// of the polygon but is partially contained in the polygon
|
||||
// 2. Darkened spots from linear filtering of points outside of 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(scene, pos, renderPoly.Center + plane.Normal * 0.25f);
|
||||
if (!inPoly)
|
||||
{
|
||||
var p2d = planeMapper.MapTo2d(pos);
|
||||
p2d = MathUtils.ClipPointToPoly2d(p2d, v2ds);
|
||||
pos = planeMapper.MapTo3d(p2d);
|
||||
}
|
||||
var p2d = planeMapper.MapTo2d(pos);
|
||||
p2d = MathUtils.ClipPointToPoly2d(p2d, v2ds);
|
||||
pos = planeMapper.MapTo3d(p2d);
|
||||
|
||||
// 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 ((pos - light.Position).LengthSquared() > light.R2)
|
||||
var direction = pos - light.position;
|
||||
if (direction.LengthSquared() > light.r2)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// We cast from the light to the pixel because the light has
|
||||
// no mesh in the scene to hit
|
||||
var hit = TraceRay(scene, light.Position, pos);
|
||||
var hitResult = scene.Trace(new Ray
|
||||
{
|
||||
Origin = light.position,
|
||||
Direction = Vector3.Normalize(direction),
|
||||
});
|
||||
|
||||
// cheeky epsilon
|
||||
// TODO: Some pixels aren't hitting and I'm not sure why
|
||||
var hit = hitResult && Math.Abs(hitResult.Distance - direction.Length()) < MathUtils.Epsilon;
|
||||
if (hit)
|
||||
{
|
||||
// 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
|
||||
if (light.Anim)
|
||||
if (light.anim)
|
||||
{
|
||||
// TODO: Don't recalculate this for every point lol
|
||||
var paletteIdx = cell.AnimLights.IndexOf((ushort)light.LightTableIndex);
|
||||
var paletteIdx = cell.AnimLights.IndexOf((ushort)light.lightTableIndex);
|
||||
if (paletteIdx == -1)
|
||||
{
|
||||
paletteIdx = cell.AnimLightCount;
|
||||
cell.AnimLightCount++;
|
||||
cell.AnimLights.Add((ushort)light.LightTableIndex);
|
||||
cell.AnimLights.Add((ushort)light.lightTableIndex);
|
||||
}
|
||||
info.AnimLightBitmask |= 1u << paletteIdx;
|
||||
layer = paletteIdx + 1;
|
||||
}
|
||||
var strength = light.StrengthAtPoint(pos, plane);
|
||||
lightmap.AddLight(layer, x, y, light.Color, strength, hdr);
|
||||
var strength = CalculateLightStrengthAtPoint(light, pos, plane);
|
||||
lightmap.AddLight(layer, x, y, light.color, strength, hdr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -518,17 +552,6 @@ class Program
|
|||
});
|
||||
}
|
||||
|
||||
private static bool TraceRay(Raytracer scene, Vector3 origin, Vector3 target)
|
||||
{
|
||||
var direction = target - origin;
|
||||
var hitResult = scene.Trace(new Ray
|
||||
{
|
||||
Origin = origin,
|
||||
Direction = Vector3.Normalize(direction),
|
||||
});
|
||||
return hitResult && Math.Abs(hitResult.Distance - direction.Length()) < MathUtils.Epsilon;
|
||||
}
|
||||
|
||||
private static void SetCellLightIndices(WorldRep wr, Light[] lights)
|
||||
{
|
||||
// TODO: Move this functionality to the LGS library
|
||||
|
@ -553,13 +576,61 @@ class Program
|
|||
var cellSphere = new MathUtils.Sphere(cell.SphereCenter, cell.SphereRadius);
|
||||
foreach (var light in lights)
|
||||
{
|
||||
if (MathUtils.Intersects(cellSphere, new MathUtils.Sphere(light.Position, light.Radius)))
|
||||
if (MathUtils.Intersects(cellSphere, new MathUtils.Sphere(light.position, light.radius)))
|
||||
{
|
||||
cell.LightIndexCount++;
|
||||
cell.LightIndices.Add((ushort)light.LightTableIndex);
|
||||
cell.LightIndices.Add((ushort)light.lightTableIndex);
|
||||
cell.LightIndices[0]++;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static float CalculateLightStrengthAtPoint(Light light, Vector3 point, Plane plane)
|
||||
{
|
||||
// Calculate light strength at a given point. As far as I can tell
|
||||
// this is exact to Dark (I'm a genius??). It's just an inverse distance
|
||||
// falloff with diffuse angle, except we have to scale the length.
|
||||
var dir = light.position - point;
|
||||
var angle = Vector3.Dot(Vector3.Normalize(dir), plane.Normal);
|
||||
var len = dir.Length();
|
||||
var slen = len / 4.0f;
|
||||
var strength = (angle + 1.0f) / slen;
|
||||
|
||||
// Inner radius starts a linear falloff to 0 at the radius
|
||||
if (light.innerRadius != 0 && len > light.innerRadius)
|
||||
{
|
||||
strength *= (light.radius - len) / (light.radius - light.innerRadius);
|
||||
}
|
||||
|
||||
// This is basically the same as how inner radius works. It just applies
|
||||
// a linear falloff to 0 between the inner angle and outer angle.
|
||||
if (light.spotlight)
|
||||
{
|
||||
var spotAngle = Vector3.Dot(-Vector3.Normalize(dir), light.spotlightDir);
|
||||
var inner = light.spotlightInnerAngle;
|
||||
var outer = light.spotlightOuterAngle;
|
||||
|
||||
// In an improperly configured spotlight inner and outer angles might be the
|
||||
// same. So to avoid division by zero (and some clamping) we explicitly handle
|
||||
// some cases
|
||||
float spotlightMultiplier;
|
||||
if (spotAngle >= inner)
|
||||
{
|
||||
spotlightMultiplier = 1.0f;
|
||||
}
|
||||
else if (spotAngle <= outer)
|
||||
{
|
||||
spotlightMultiplier = 0.0f;
|
||||
}
|
||||
else
|
||||
{
|
||||
spotlightMultiplier = (spotAngle - outer) / (inner - outer);
|
||||
}
|
||||
|
||||
strength *= spotlightMultiplier;
|
||||
}
|
||||
|
||||
return strength;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue