Skip to content

Commit 3bb0f56

Browse files
committed
Fixes for Specular Material Handling.
1 parent 9c446e0 commit 3bb0f56

File tree

1 file changed

+98
-45
lines changed
  • xivModdingFramework/Materials/DataContainers

1 file changed

+98
-45
lines changed

xivModdingFramework/Materials/DataContainers/XivMtrl.cs

Lines changed: 98 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -519,44 +519,34 @@ public void SetMapInfo(XivTexType MapType, MapInfo info)
519519
// Deleting existing info.
520520
if (info == null)
521521
{
522-
if(paramIdx < 0)
523-
{
524-
// Didn't exist to start, nothing to do.
525-
return;
526-
}
527522

528-
// Remove texture from path list if it exists.
529-
if (TexturePathList.Count > oldInfo.TextureIndex)
530-
{
531-
TexturePathList.RemoveAt((int)oldInfo.TextureIndex);
532-
TexturePathUnknownList.RemoveAt((int)oldInfo.TextureIndex);
533-
}
534523

535-
// Remove Parameter List
536-
TextureDescriptorList.RemoveAt(paramIdx);
537-
538-
// Update other texture offsets
539-
for(var i = 0; i < TextureDescriptorList.Count; i++)
524+
if (paramIdx >= 0)
540525
{
541-
var p = TextureDescriptorList[i];
542-
if(p.TextureIndex > oldInfo.TextureIndex)
526+
// Remove texture from path list if it exists.
527+
if (TexturePathList.Count > oldInfo.TextureIndex)
543528
{
544-
p.TextureIndex--;
529+
TexturePathList.RemoveAt((int)oldInfo.TextureIndex);
530+
TexturePathUnknownList.RemoveAt((int)oldInfo.TextureIndex);
545531
}
546-
TextureDescriptorList[i] = p;
547-
}
548532

549-
// Remove struct1 entry for the removed map type, if it exists.
550-
for(var i = 0; i < TextureUsageList.Count; i++)
551-
{
552-
var s = TextureUsageList[i];
553-
if(s.TextureType == Mtrl.TextureUsageValues[MapType].TextureType)
533+
// Remove Parameter List
534+
TextureDescriptorList.RemoveAt(paramIdx);
535+
536+
// Update other texture offsets
537+
for (var i = 0; i < TextureDescriptorList.Count; i++)
554538
{
555-
TextureUsageList.RemoveAt(i);
556-
break;
539+
var p = TextureDescriptorList[i];
540+
if (p.TextureIndex > oldInfo.TextureIndex)
541+
{
542+
p.TextureIndex--;
543+
}
544+
TextureDescriptorList[i] = p;
557545
}
558546
}
559547

548+
RegenerateTextureUsageList();
549+
560550
return;
561551

562552
}
@@ -608,31 +598,94 @@ public void SetMapInfo(XivTexType MapType, MapInfo info)
608598
TexturePathList[(int) raw.TextureIndex] = info.path;
609599
}
610600

611-
// Update struct1 entry with the appropriate entry if it's not already there.
612-
bool foundEntry = false;
613-
for (var i = 0; i < TextureUsageList.Count; i++)
601+
RegenerateTextureUsageList();
602+
}
603+
604+
/// <summary>
605+
/// Regenerates/Cleans up the texture usage list based on the
606+
/// Texture Maps that are set in the Texture Description Fields.
607+
/// </summary>
608+
private void RegenerateTextureUsageList()
609+
{
610+
// Everything (should) have a normal, so this is redundant.
611+
// bool hasNormal = GetMapInfo(XivTexType.Normal) != null;
612+
bool hasSpec = GetMapInfo(XivTexType.Specular) != null;
613+
bool hasDiffuse = GetMapInfo(XivTexType.Diffuse) != null;
614+
bool hasMulti = GetMapInfo(XivTexType.Multi) != null;
615+
616+
ClearTextureUsage(XivTexType.Normal);
617+
ClearTextureUsage(XivTexType.Diffuse);
618+
ClearTextureUsage(XivTexType.Specular);
619+
ClearTextureUsage(XivTexType.Multi);
620+
621+
// These need to be set in relatively specific order
622+
// in order to use the maps correctly
623+
624+
SetTextureUsage(XivTexType.Normal);
625+
626+
if (hasDiffuse)
614627
{
615-
var s = TextureUsageList[i];
616-
if (s.TextureType == Mtrl.TextureUsageValues[MapType].TextureType)
617-
{
618-
foundEntry = true;
619-
break;
620-
}
628+
// Diffuse
629+
SetTextureUsage(XivTexType.Diffuse);
621630
}
622631

623-
if(!foundEntry)
632+
if (hasSpec || hasMulti)
624633
{
625-
var s1 = new TextureUsageStruct()
626-
{
627-
TextureType = Mtrl.TextureUsageValues[MapType].TextureType,
628-
Unknown = Mtrl.TextureUsageValues[MapType].Unknown
629-
};
630-
631-
TextureUsageList.Add(s1);
634+
// Including both of these seems to guarantee best results,
635+
// vs including only one or the other.
636+
SetTextureUsage(XivTexType.Multi);
637+
SetTextureUsage(XivTexType.Specular);
632638
}
633639

634640
}
635641

642+
/// <summary>
643+
/// Clears a texture usage value.
644+
/// </summary>
645+
/// <param name="usage"></param>
646+
private bool ClearTextureUsage(XivTexType usage)
647+
{
648+
var oldCount = TextureUsageList.Count;
649+
TextureUsageList = TextureUsageList.Where(x => x.TextureType != Mtrl.TextureUsageValues[usage].TextureType).ToList();
650+
return oldCount != TextureUsageList.Count;
651+
}
652+
653+
/// <summary>
654+
/// Adds or changes a texture usage value.
655+
/// </summary>
656+
/// <param name="usage"></param>
657+
/// <param name="unknownValue"></param>
658+
private void SetTextureUsage(XivTexType usage, uint? unknownValue = null)
659+
{
660+
if(unknownValue == null)
661+
{
662+
unknownValue = Mtrl.TextureUsageValues[usage].Unknown;
663+
}
664+
try
665+
{
666+
var val = TextureUsageList.First(x => x.TextureType == Mtrl.TextureUsageValues[usage].TextureType);
667+
if(val != null)
668+
{
669+
// Despite being named 'Struct' this is actually a class.
670+
val.Unknown = (uint) unknownValue;
671+
} else
672+
{
673+
TextureUsageList.Add(new TextureUsageStruct()
674+
{
675+
TextureType = Mtrl.TextureUsageValues[usage].TextureType,
676+
Unknown = (uint) unknownValue
677+
});
678+
}
679+
} catch(Exception ex)
680+
{
681+
TextureUsageList.Add(new TextureUsageStruct()
682+
{
683+
TextureType = Mtrl.TextureUsageValues[usage].TextureType,
684+
Unknown = (uint) unknownValue
685+
});
686+
}
687+
}
688+
636689
/// <summary>
637690
/// Retrieve all MapInfo structs for all textures associated with this MTRL,
638691
/// Including textures with unknown usage.

0 commit comments

Comments
 (0)