Rename Light fields

This commit is contained in:
Jarrod Doyle 2024-10-27 15:12:19 +00:00
parent 50f3c1e9e1
commit 025f4d0508
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
2 changed files with 83 additions and 83 deletions

View File

@ -5,31 +5,31 @@ namespace KeepersCompound.Lightmapper;
public class Light public class Light
{ {
public Vector3 position; public Vector3 Position;
public Vector3 color; public Vector3 Color;
public float innerRadius; public float InnerRadius;
public float radius; public float Radius;
public float r2; public float R2;
public bool spotlight; public bool Spotlight;
public Vector3 spotlightDir; public Vector3 SpotlightDir;
public float spotlightInnerAngle; public float SpotlightInnerAngle;
public float spotlightOuterAngle; public float SpotlightOuterAngle;
public int objId; public int ObjId;
public int lightTableIndex; public int LightTableIndex;
public bool anim; public bool Anim;
public WorldRep.LightTable.LightData ToLightData(float lightScale) public WorldRep.LightTable.LightData ToLightData(float lightScale)
{ {
return new WorldRep.LightTable.LightData return new WorldRep.LightTable.LightData
{ {
Location = position, Location = Position,
Direction = spotlightDir, Direction = SpotlightDir,
Color = color / lightScale, Color = Color / lightScale,
InnerAngle = spotlightInnerAngle, InnerAngle = SpotlightInnerAngle,
OuterAngle = spotlightOuterAngle, OuterAngle = SpotlightOuterAngle,
Radius = radius == float.MaxValue ? 0 : radius, Radius = Radius == float.MaxValue ? 0 : Radius,
}; };
} }
} }

View File

@ -103,8 +103,8 @@ class Program
{ {
// We need to update the object property so it knows its mapping range // We need to update the object property so it knows its mapping range
// TODO: Handle nulls // TODO: Handle nulls
var light = lights.Find(l => l.anim && l.lightTableIndex == lightIdx); var light = lights.Find(l => l.Anim && l.LightTableIndex == lightIdx);
var prop = animLightChunk.properties.Find(p => p.objectId == light.objId); var prop = animLightChunk.properties.Find(p => p.objectId == light.ObjId);
prop.LightTableLightIndex = lightIdx; prop.LightTableLightIndex = lightIdx;
prop.LightTableMapIndex = (ushort)worldRep.LightingTable.AnimMapCount; prop.LightTableMapIndex = (ushort)worldRep.LightingTable.AnimMapCount;
prop.CellsReached = (ushort)animCellMaps.Count; prop.CellsReached = (ushort)animCellMaps.Count;
@ -162,19 +162,19 @@ class Program
var sz = brush.size; var sz = brush.size;
var light = new Light var light = new Light
{ {
position = brush.position, Position = brush.position,
color = Utils.HsbToRgb(sz.Y, sz.Z, Math.Min(sz.X, 255.0f)), Color = Utils.HsbToRgb(sz.Y, sz.Z, Math.Min(sz.X, 255.0f)),
radius = float.MaxValue, Radius = float.MaxValue,
r2 = float.MaxValue, R2 = float.MaxValue,
lightTableIndex = lightTable.LightCount, LightTableIndex = lightTable.LightCount,
}; };
lights.Add(light); lights.Add(light);
lightTable.AddLight(new WorldRep.LightTable.LightData lightTable.AddLight(new WorldRep.LightTable.LightData
{ {
Location = light.position, Location = light.Position,
Direction = light.spotlightDir, Direction = light.SpotlightDir,
Color = light.color / 32.0f, // TODO: This is based on light_scale config var Color = light.Color / 32.0f, // TODO: This is based on light_scale config var
InnerAngle = -1.0f, InnerAngle = -1.0f,
Radius = 0, Radius = 0,
}); });
@ -199,9 +199,9 @@ class Program
var baseLight = new Light var baseLight = new Light
{ {
position = brush.position, Position = brush.position,
spotlightDir = -Vector3.UnitZ, SpotlightDir = -Vector3.UnitZ,
spotlightInnerAngle = -1.0f, SpotlightInnerAngle = -1.0f,
}; };
if (propModelName != null) if (propModelName != null)
@ -214,11 +214,11 @@ class Program
var model = new ModelFile(modelPath); var model = new ModelFile(modelPath);
if (model.TryGetVhot(ModelFile.VhotId.LightPosition, out var vhot)) 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)) if (model.TryGetVhot(ModelFile.VhotId.LightDirection, out vhot))
{ {
baseLight.spotlightDir = vhot.Position; baseLight.SpotlightDir = vhot.Position;
} }
} }
@ -231,31 +231,31 @@ class Program
rot *= Matrix4x4.CreateRotationY(float.DegreesToRadians(brush.angle.Y)); rot *= Matrix4x4.CreateRotationY(float.DegreesToRadians(brush.angle.Y));
rot *= Matrix4x4.CreateRotationZ(float.DegreesToRadians(brush.angle.Z)); rot *= Matrix4x4.CreateRotationZ(float.DegreesToRadians(brush.angle.Z));
baseLight.spotlight = true; baseLight.Spotlight = true;
baseLight.spotlightDir = Vector3.Transform(baseLight.spotlightDir, rot); baseLight.SpotlightDir = Vector3.Transform(baseLight.SpotlightDir, rot);
baseLight.spotlightInnerAngle = (float)Math.Cos(float.DegreesToRadians(propSpotlight.InnerAngle)); baseLight.SpotlightInnerAngle = (float)Math.Cos(float.DegreesToRadians(propSpotlight.InnerAngle));
baseLight.spotlightOuterAngle = (float)Math.Cos(float.DegreesToRadians(propSpotlight.OuterAngle)); baseLight.SpotlightOuterAngle = (float)Math.Cos(float.DegreesToRadians(propSpotlight.OuterAngle));
} }
if (propLight != null) if (propLight != null)
{ {
var light = new Light var light = new Light
{ {
position = baseLight.position + propLight.Offset, Position = baseLight.Position + propLight.Offset,
color = Utils.HsbToRgb(propLightColor.Hue, propLightColor.Saturation, propLight.Brightness), Color = Utils.HsbToRgb(propLightColor.Hue, propLightColor.Saturation, propLight.Brightness),
innerRadius = propLight.InnerRadius, InnerRadius = propLight.InnerRadius,
radius = propLight.Radius, Radius = propLight.Radius,
r2 = propLight.Radius * propLight.Radius, R2 = propLight.Radius * propLight.Radius,
spotlight = baseLight.spotlight, Spotlight = baseLight.Spotlight,
spotlightDir = baseLight.spotlightDir, SpotlightDir = baseLight.SpotlightDir,
spotlightInnerAngle = baseLight.spotlightInnerAngle, SpotlightInnerAngle = baseLight.SpotlightInnerAngle,
spotlightOuterAngle = baseLight.spotlightOuterAngle, SpotlightOuterAngle = baseLight.SpotlightOuterAngle,
lightTableIndex = lightTable.LightCount, LightTableIndex = lightTable.LightCount,
}; };
if (propLight.Radius == 0) if (propLight.Radius == 0)
{ {
light.radius = float.MaxValue; light.Radius = float.MaxValue;
light.r2 = float.MaxValue; light.R2 = float.MaxValue;
} }
lights.Add(light); lights.Add(light);
@ -269,23 +269,23 @@ class Program
var light = new Light var light = new Light
{ {
position = baseLight.position + propAnimLight.Offset, Position = baseLight.Position + propAnimLight.Offset,
color = Utils.HsbToRgb(propLightColor.Hue, propLightColor.Saturation, propAnimLight.MaxBrightness), Color = Utils.HsbToRgb(propLightColor.Hue, propLightColor.Saturation, propAnimLight.MaxBrightness),
innerRadius = propAnimLight.InnerRadius, InnerRadius = propAnimLight.InnerRadius,
radius = propAnimLight.Radius, Radius = propAnimLight.Radius,
r2 = propAnimLight.Radius * propAnimLight.Radius, R2 = propAnimLight.Radius * propAnimLight.Radius,
spotlight = baseLight.spotlight, Spotlight = baseLight.Spotlight,
spotlightDir = baseLight.spotlightDir, SpotlightDir = baseLight.SpotlightDir,
spotlightInnerAngle = baseLight.spotlightInnerAngle, SpotlightInnerAngle = baseLight.SpotlightInnerAngle,
spotlightOuterAngle = baseLight.spotlightOuterAngle, SpotlightOuterAngle = baseLight.SpotlightOuterAngle,
anim = true, Anim = true,
objId = id, ObjId = id,
lightTableIndex = propAnimLight.LightTableLightIndex, LightTableIndex = propAnimLight.LightTableLightIndex,
}; };
if (propAnimLight.Radius == 0) if (propAnimLight.Radius == 0)
{ {
light.radius = float.MaxValue; light.Radius = float.MaxValue;
light.r2 = float.MaxValue; light.R2 = float.MaxValue;
} }
lights.Add(light); lights.Add(light);
@ -427,7 +427,7 @@ class Program
// Check if plane normal is facing towards the light // Check if plane normal is facing towards the light
// If it's not then we're never going to be (directly) lit by this // If it's not then we're never going to be (directly) lit by this
// light. // light.
var centerDirection = renderPoly.Center - light.position; var centerDirection = renderPoly.Center - light.Position;
if (Vector3.Dot(plane.Normal, centerDirection) >= 0) if (Vector3.Dot(plane.Normal, centerDirection) >= 0)
{ {
continue; continue;
@ -436,15 +436,15 @@ class Program
// If there aren't *any* points on the plane that are in range of the light // 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. // then none of the lightmap points will be so we can discard.
// The more compact a map is the less effective this is // The more compact a map is the less effective this is
var planeDist = MathUtils.DistanceFromPlane(plane, light.position); var planeDist = MathUtils.DistanceFromPlane(plane, light.Position);
if (planeDist > light.radius) if (planeDist > light.Radius)
{ {
continue; continue;
} }
// If the poly of the lightmap doesn't intersect the light radius then // If the poly of the lightmap doesn't intersect the light radius then
// none of the lightmap points will so we can discard. // 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; continue;
} }
@ -472,8 +472,8 @@ class Program
// If we're out of range there's no point casting a ray // If we're out of range there's no point casting a ray
// There's probably a better way to discard the entire lightmap // There's probably a better way to discard the entire lightmap
// if we're massively out of range // if we're massively out of range
var direction = pos - light.position; var direction = pos - light.Position;
if (direction.LengthSquared() > light.r2) if (direction.LengthSquared() > light.R2)
{ {
continue; continue;
} }
@ -482,7 +482,7 @@ class Program
// no mesh in the scene to hit // no mesh in the scene to hit
var hitResult = scene.Trace(new Ray var hitResult = scene.Trace(new Ray
{ {
Origin = light.position, Origin = light.Position,
Direction = Vector3.Normalize(direction), Direction = Vector3.Normalize(direction),
}); });
@ -495,21 +495,21 @@ class Program
// Firstly we need to add the light to the cells anim light palette // 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 // Secondly we need to set the appropriate bit of the lightmap's
// bitmask. Finally we need to check if the lightmap needs another layer // 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 // 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) if (paletteIdx == -1)
{ {
paletteIdx = cell.AnimLightCount; paletteIdx = cell.AnimLightCount;
cell.AnimLightCount++; cell.AnimLightCount++;
cell.AnimLights.Add((ushort)light.lightTableIndex); cell.AnimLights.Add((ushort)light.LightTableIndex);
} }
info.AnimLightBitmask |= 1u << paletteIdx; info.AnimLightBitmask |= 1u << paletteIdx;
layer = paletteIdx + 1; layer = paletteIdx + 1;
} }
var strength = CalculateLightStrengthAtPoint(light, pos, plane); var strength = CalculateLightStrengthAtPoint(light, pos, plane);
lightmap.AddLight(layer, x, y, light.color, strength, hdr); lightmap.AddLight(layer, x, y, light.Color, strength, hdr);
} }
} }
} }
@ -544,10 +544,10 @@ class Program
var cellSphere = new MathUtils.Sphere(cell.SphereCenter, cell.SphereRadius); var cellSphere = new MathUtils.Sphere(cell.SphereCenter, cell.SphereRadius);
foreach (var light in lights) 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.LightIndexCount++;
cell.LightIndices.Add((ushort)light.lightTableIndex); cell.LightIndices.Add((ushort)light.LightTableIndex);
cell.LightIndices[0]++; cell.LightIndices[0]++;
} }
} }
@ -559,25 +559,25 @@ class Program
// Calculate light strength at a given point. As far as I can tell // 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 // 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. // falloff with diffuse angle, except we have to scale the length.
var dir = light.position - point; var dir = light.Position - point;
var angle = Vector3.Dot(Vector3.Normalize(dir), plane.Normal); var angle = Vector3.Dot(Vector3.Normalize(dir), plane.Normal);
var len = dir.Length(); var len = dir.Length();
var slen = len / 4.0f; var slen = len / 4.0f;
var strength = (angle + 1.0f) / slen; var strength = (angle + 1.0f) / slen;
// Inner radius starts a linear falloff to 0 at the radius // Inner radius starts a linear falloff to 0 at the radius
if (light.innerRadius != 0 && len > light.innerRadius) if (light.InnerRadius != 0 && len > light.InnerRadius)
{ {
strength *= (light.radius - len) / (light.radius - light.innerRadius); strength *= (light.Radius - len) / (light.Radius - light.InnerRadius);
} }
// This is basically the same as how inner radius works. It just applies // 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. // a linear falloff to 0 between the inner angle and outer angle.
if (light.spotlight) if (light.Spotlight)
{ {
var spotAngle = Vector3.Dot(-Vector3.Normalize(dir), light.spotlightDir); var spotAngle = Vector3.Dot(-Vector3.Normalize(dir), light.SpotlightDir);
var inner = light.spotlightInnerAngle; var inner = light.SpotlightInnerAngle;
var outer = light.spotlightOuterAngle; var outer = light.SpotlightOuterAngle;
// In an improperly configured spotlight inner and outer angles might be the // 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 // same. So to avoid division by zero (and some clamping) we explicitly handle