LodLights generator positions improvement

This commit is contained in:
dexy 2021-05-03 00:57:14 +10:00
parent 12dd40ea8e
commit 2c8311c936
3 changed files with 22 additions and 71 deletions

View File

@ -2083,6 +2083,7 @@ namespace CodeWalker.GameFiles
var dd = db as Drawable;
var fd = db as FragDrawable;
var skel = db.Skeleton;
LightAttributes_s[] lightAttrs = null;
Bounds b = null;
if (dd != null)
@ -2092,8 +2093,10 @@ namespace CodeWalker.GameFiles
}
else if (fd != null)
{
lightAttrs = fd.OwnerFragment?.LightAttributes?.data_items;
b = fd.OwnerFragment?.PhysicsLODGroup?.PhysicsLOD1?.Bound;
var frag = fd?.OwnerFragment;
skel = skel ?? frag?.Drawable?.Skeleton;
lightAttrs = frag?.LightAttributes?.data_items;
b = frag?.PhysicsLODGroup?.PhysicsLOD1?.Bound;
}
if (lightAttrs == null) return;
@ -2120,9 +2123,19 @@ namespace CodeWalker.GameFiles
for (int i = 0; i < lightAttrs.Length; i++)
{
ints[6] = (uint)(exts + i);
var la = lightAttrs[i];
var xform = Matrix.Identity;
if ((skel != null) && (skel.BonesMap.TryGetValue(la.BoneId, out Bone bone)))
{
xform = bone.AbsTransform;
}
var li = new LightInstance();
li.Attributes = lightAttrs[i];
li.Attributes = la;
li.Hash = ComputeLightHash(ints);
li.Position = Orientation.Multiply(xform.Multiply(la.Position)) + Position;
li.Direction = Orientation.Multiply(xform.MultiplyRot(la.Direction));
lightInsts[i] = li;
}
Lights = lightInsts;
@ -2200,6 +2213,8 @@ namespace CodeWalker.GameFiles
{
public LightAttributes_s Attributes { get; set; } //just for display purposes!
public uint Hash { get; set; }
public Vector3 Position { get; set; }
public Vector3 Direction { get; set; }
public override string ToString()
{

View File

@ -1122,6 +1122,7 @@ namespace CodeWalker.GameFiles
BonesMap[bone.Tag] = bone;
bone.UpdateAnimTransform();
bone.AbsTransform = bone.AnimTransform;
bone.BindTransformInv = (i < (TransformationsInverted?.Length ?? 0)) ? TransformationsInverted[i] : Matrix.Invert(bone.AnimTransform);
bone.BindTransformInv.M44 = 1.0f;
bone.UpdateSkinTransform();
@ -1759,6 +1760,7 @@ namespace CodeWalker.GameFiles
public Matrix AnimTransform;//absolute world transform, animated
public Matrix BindTransformInv;//inverse of bind pose transform
public Matrix SkinTransform;//transform to use for skin meshes
public Matrix AbsTransform;//original absolute transform from loaded file, calculated from bones hierarchy
public Vector4 TransformUnk { get; set; } //unknown value (column 4) from skeleton's transform array, used for IO purposes
public override void Read(ResourceDataReader reader, params object[] parameters)

View File

@ -124,66 +124,6 @@ namespace CodeWalker.Project.Panels
{
var elight = elights[li];
var la = elight.Attributes;
//transform this light with the entity position and orientation
//generate lights data from it!
//gotta transform the light position by the given bone! annoying
Bone bone = null;
Matrix xform = Matrix.Identity;
int boneidx = 0;
var skeleton = dwbl.Skeleton;
if (skeleton?.Bones?.Items != null)
{
for (int j = 0; j < skeleton.Bones.Items.Length; j++)
{
var tbone = skeleton.Bones.Items[j];
if (tbone.Tag == la.BoneId)
{
boneidx = j;
bone = tbone;
break;
}
}
if (bone != null)
{
var modeltransforms = skeleton.Transformations;
var fragtransforms = fphys?.OwnerFragPhysLod?.FragTransforms?.Matrices;
var fragtransformid = fphys?.OwnerFragPhysIndex ?? 0;
var fragoffset = new Vector4(fphys?.OwnerFragPhysLod.Unknown_30h ?? Vector3.Zero, 0.0f);
if ((fragtransforms != null) && (fragtransformid < fragtransforms.Length))
{
xform = fragtransforms[fragtransformid];
xform.Row4 += fragoffset;
}
else
{
//when using the skeleton's matrices, they need to be transformed by parent
xform = modeltransforms[boneidx];
xform.Column4 = Vector4.UnitW;
//xform = Matrix.Identity;
short[] pinds = skeleton.ParentIndices;
short parentind = ((pinds != null) && (boneidx < pinds.Length)) ? pinds[boneidx] : (short)-1;
while ((parentind >= 0) && (parentind < pinds.Length))
{
Matrix ptrans = (parentind < modeltransforms.Length) ? modeltransforms[parentind] : Matrix.Identity;
ptrans.Column4 = Vector4.UnitW;
xform = Matrix.Multiply(ptrans, xform);
parentind = ((pinds != null) && (parentind < pinds.Length)) ? pinds[parentind] : (short)-1;
}
}
}
}
Vector3 lpos = la.Position;
Vector3 ldir = la.Direction;
Vector3 bpos = xform.Multiply(lpos);
Vector3 bdir = xform.MultiplyRot(ldir);
Vector3 epos = ent.Orientation.Multiply(bpos) + ent.Position;
Vector3 edir = ent.Orientation.Multiply(bdir);
uint r = la.ColorR;
uint g = la.ColorG;
@ -192,12 +132,6 @@ namespace CodeWalker.Project.Panels
uint c = (i << 24) + (r << 16) + (g << 8) + b;
uint h = elight.Hash;
if (ent._CEntityDef.guid == 91259075)
{ } //h = 2324437992? should be:19112537
if (ent._CEntityDef.guid == 889043351)
{ } //h = 422028630 ? should be:4267224866
//any other way to know if it's a streetlight?
//var name = ent.Archetype.Name;
@ -223,9 +157,9 @@ namespace CodeWalker.Project.Panels
var light = new Light();
light.position = new MetaVECTOR3(epos);
light.position = new MetaVECTOR3(elight.Position);
light.colour = c;
light.direction = new MetaVECTOR3(edir);
light.direction = new MetaVECTOR3(elight.Direction);
light.falloff = la.Falloff;
light.falloffExponent = la.FalloffExponent;
light.timeAndStateFlags = t;