Skip to content

Commit

Permalink
raw texture mode init
Browse files Browse the repository at this point in the history
  • Loading branch information
PassiveModding committed Sep 29, 2024
1 parent a82213c commit e0a09e8
Show file tree
Hide file tree
Showing 15 changed files with 194 additions and 62 deletions.
10 changes: 3 additions & 7 deletions Meddle/Meddle.Plugin/Models/Composer/CharacterComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ public class CharacterComposer
private static readonly object StaticFileLock = new();
private readonly SkeletonUtils.PoseMode poseMode;
private readonly bool includePose;
private readonly bool computeCharacterTextures;
private readonly TextureMode textureMode;

public CharacterComposer(DataProvider dataProvider, Configuration config, Action<ProgressEvent>? progress = null)
{
this.dataProvider = dataProvider;
this.progress = progress;
includePose = config.IncludePose;
poseMode = config.PoseMode;
computeCharacterTextures = config.ComputeCharacterTextures;
textureMode = config.TextureMode;

lock (StaticFileLock)
{
Expand Down Expand Up @@ -76,11 +76,6 @@ private void HandleModel(GenderRace genderRace, CustomizeParameter customizePara
try
{
var materialInfo = modelInfo.Materials[i];
if (!computeCharacterTextures)
{
materialBuilders[i] = new MaterialBuilder(materialInfo.Path.FullPath);
continue;
}

progress?.Invoke(new ProgressEvent(modelInfo.GetHashCode(), $"{materialInfo.Path.GamePath}", i + 1, modelInfo.Materials.Length));
var mtrlData = dataProvider.LookupData(materialInfo.Path.FullPath);
Expand Down Expand Up @@ -117,6 +112,7 @@ private void HandleModel(GenderRace genderRace, CustomizeParameter customizePara

material.SetCustomizeParameters(customizeParameter);
material.SetCustomizeData(customizeData);
material.SetTextureMode(textureMode);

materialBuilders[i] = material.Compose(dataProvider);
}
Expand Down
37 changes: 30 additions & 7 deletions Meddle/Meddle.Plugin/Models/Composer/MaterialSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -420,17 +420,17 @@ private MeddleMaterialBuilder GetMaterialBuilder(DataProvider dataProvider)
return new LightshaftMaterialBuilder(mtrlName, this, dataProvider);
case "character.shpk":
case "characterlegacy.shpk":
return new CharacterMaterialBuilder(mtrlName, this, dataProvider, colorTable);
return new CharacterMaterialBuilder(mtrlName, this, dataProvider, colorTable, textureMode);
case "charactertattoo.shpk":
return new CharacterTattooMaterialBuilder(mtrlName, this, dataProvider, customizeParameters ?? new CustomizeParameter());
return new CharacterTattooMaterialBuilder(mtrlName, this, dataProvider, customizeParameters ?? new CustomizeParameter(), textureMode);
case "characterocclusion.shpk":
return new CharacterOcclusionMaterialBuilder(mtrlName, this, dataProvider);
case "skin.shpk":
return new SkinMaterialBuilder(mtrlName, this, dataProvider, customizeParameters ?? new CustomizeParameter(), customizeData ?? new CustomizeData());
return new SkinMaterialBuilder(mtrlName, this, dataProvider, customizeParameters ?? new CustomizeParameter(), customizeData ?? new CustomizeData(), textureMode);
case "hair.shpk":
return new HairMaterialBuilder(mtrlName, this, dataProvider, customizeParameters ?? new CustomizeParameter());
return new HairMaterialBuilder(mtrlName, this, dataProvider, customizeParameters ?? new CustomizeParameter(), textureMode);
case "iris.shpk":
return new IrisMaterialBuilder(mtrlName, this, dataProvider, customizeParameters ?? new CustomizeParameter());
return new IrisMaterialBuilder(mtrlName, this, dataProvider, customizeParameters ?? new CustomizeParameter(), textureMode);
default:
return new GenericMaterialBuilder(mtrlName, this, dataProvider);
}
Expand Down Expand Up @@ -461,7 +461,11 @@ public Dictionary<string, object> ComposeExtras()
var extrasDict = new Dictionary<string, object>
{
{"ShaderPackage", ShpkName},
{"Material", MtrlPath}
{"Material", MtrlPath},
{"HashStr", HashStr()},
{"RenderBackfaces", RenderBackfaces},
{"IsTransparent", IsTransparent},
{"CharacterTextureMode", textureMode}
};
AddConstants();
AddSamplers();
Expand All @@ -476,6 +480,17 @@ public Dictionary<string, object> ComposeExtras()
void AddCustomizeParameters()
{
if (customizeParameters == null) return;
extrasDict["LeftIrisColor"] = customizeParameters.LeftColor.AsFloatArray();
extrasDict["RightIrisColor"] = customizeParameters.RightColor.AsFloatArray();
extrasDict["MainColor"] = customizeParameters.MainColor.AsFloatArray();
extrasDict["SkinColor"] = customizeParameters.SkinColor.AsFloatArray();
extrasDict["MeshColor"] = customizeParameters.MeshColor.AsFloatArray();
extrasDict["LipColor"] = customizeParameters.LipColor.AsFloatArray();
extrasDict["SkinColor"] = customizeParameters.SkinColor.AsFloatArray();
extrasDict["FacePaintUVOffset"] = customizeParameters.FacePaintUVOffset;
extrasDict["FacePaintUVMultiplier"] = customizeParameters.FacePaintUVMultiplier;
extrasDict["MuscleTone"] = customizeParameters.MuscleTone;
extrasDict["OptionColor"] = customizeParameters.OptionColor;
extrasDict["CustomizeParameters"] = JsonNode.Parse(JsonSerializer.Serialize(customizeParameters, JsonOptions))!;
}

Expand All @@ -494,6 +509,8 @@ void AddStainColor()
void AddCustomizeData()
{
if (customizeData == null) return;
extrasDict["Highlights"] = customizeData.Highlights;
extrasDict["LipStick"] = customizeData.LipStick;
extrasDict["CustomizeData"] = JsonNode.Parse(JsonSerializer.Serialize(customizeData, JsonOptions))!;
}

Expand All @@ -517,7 +534,7 @@ void AddShaderKeys()
ShaderCategory.CategorySkinType => IsDefinedOrHex((SkinType)value),
ShaderCategory.CategoryDiffuseAlpha => IsDefinedOrHex((DiffuseAlpha)value),
ShaderCategory.CategorySpecularType => IsDefinedOrHex((SpecularMode)value),
ShaderCategory.GetValuesTextureType => IsDefinedOrHex((TextureMode)value),
ShaderCategory.GetValuesTextureType => IsDefinedOrHex((Meddle.Utils.Export.TextureMode)value),
ShaderCategory.CategoryFlowMapType => IsDefinedOrHex((FlowType)value),
ShaderCategory.CategoryBgVertexPaint => IsDefinedOrHex((BgVertexPaint)value),
_ => $"0x{value:X8}"
Expand Down Expand Up @@ -562,4 +579,10 @@ void AddConstants()
}
}
}

private TextureMode textureMode = TextureMode.Bake;
public void SetTextureMode(TextureMode computeTextures)
{
this.textureMode = computeTextures;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public BgColorChangeParams(Vector4? stainColor)
public record BgParams : IBgMaterialBuilderParams;


public class BgMaterialBuilder : MeddleMaterialBuilder, IVertexPaintMaterialBuilder
public class BgMaterialBuilder : MeddleMaterialBuilder
{
private readonly IBgMaterialBuilderParams bgParams;
private readonly MaterialSet set;
Expand Down Expand Up @@ -105,8 +105,6 @@ private DetailSet GetDetail(int detailId, Vector2 size)
}
}

public bool VertexPaint { get; private set; }


private bool GetDiffuseColor(out Vector4 diffuseColor)
{
Expand Down Expand Up @@ -163,8 +161,6 @@ public override MeddleMaterialBuilder Apply()
// only the green/y channel is used here for roughness
WithMetallicRoughness(specularMap0Texture, 0.0f, 1.0f);

IndexOfRefraction = 1.0f;

Extras = set.ComposeExtrasNode(extras.ToArray());
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ public class CharacterMaterialBuilder : MeddleMaterialBuilder
private readonly MaterialSet set;
private readonly DataProvider dataProvider;
private readonly IColorTableSet? colorTableSet;
private readonly TextureMode textureMode;

public CharacterMaterialBuilder(string name, MaterialSet set, DataProvider dataProvider, IColorTableSet? colorTableSet) : base(name)
public CharacterMaterialBuilder(string name, MaterialSet set, DataProvider dataProvider, IColorTableSet? colorTableSet, TextureMode textureMode) : base(name)
{
this.set = set;
this.dataProvider = dataProvider;
this.colorTableSet = colorTableSet;
this.textureMode = textureMode;
}

public override MeddleMaterialBuilder Apply()
private void ApplyComputed()
{
var textureMode = set.GetShaderKeyOrDefault(ShaderCategory.GetValuesTextureType, TextureMode.Default);
var textureMode = set.GetShaderKeyOrDefault(ShaderCategory.GetValuesTextureType, Meddle.Utils.Export.TextureMode.Default);
var specularMode = set.GetShaderKeyOrDefault(ShaderCategory.CategorySpecularType, SpecularMode.Default); // TODO: is default actually default
var flowType = set.GetShaderKeyOrDefault(ShaderCategory.CategoryFlowMapType, FlowType.Standard);

Expand All @@ -35,7 +37,7 @@ public override MeddleMaterialBuilder Apply()

var diffuseTexture = textureMode switch
{
TextureMode.Compatibility => set.TryGetTexture(dataProvider, TextureUsage.g_SamplerDiffuse, out var tex) ? tex.ToTexture(normalRes.Size) : throw new InvalidOperationException("Missing diffuse texture"),
Meddle.Utils.Export.TextureMode.Compatibility => set.TryGetTexture(dataProvider, TextureUsage.g_SamplerDiffuse, out var tex) ? tex.ToTexture(normalRes.Size) : throw new InvalidOperationException("Missing diffuse texture"),
_ => new SKTexture(normalRes.Width, normalRes.Height)
};

Expand All @@ -58,13 +60,13 @@ public override MeddleMaterialBuilder Apply()

var blended = ((ColorTableSet?)colorTableSet)!.Value.ColorTable
.GetBlendedPair(indexColor.Red, indexColor.Green);
if (textureMode == TextureMode.Compatibility)
if (textureMode == Meddle.Utils.Export.TextureMode.Compatibility)
{
var diffuse = diffuseTexture![x, y].ToVector4();
diffuse *= new Vector4(blended.Diffuse, normal.Z);
diffuseTexture[x, y] = (diffuse with {W = normal.Z}).ToSkColor();
}
else if (textureMode == TextureMode.Default)
else if (textureMode == Meddle.Utils.Export.TextureMode.Default)
{
var diffuse = new Vector4(blended.Diffuse, normal.Z);
diffuseTexture[x, y] = diffuse.ToSkColor();
Expand All @@ -89,6 +91,20 @@ public override MeddleMaterialBuilder Apply()

WithBaseColor(dataProvider.CacheTexture(diffuseTexture, $"Computed/{set.ComputedTextureName("diffuse")}"));
WithNormal(dataProvider.CacheTexture(normalTexture, $"Computed/{set.ComputedTextureName("normal")}"));

}

public override MeddleMaterialBuilder Apply()
{
if (textureMode == TextureMode.Bake)
{
ApplyComputed();
}
else
{
ApplyRaw(set, dataProvider);
}

IndexOfRefraction = set.GetConstantOrThrow<float>(MaterialConstant.g_GlassIOR);

var alphaThreshold = set.GetConstantOrDefault(MaterialConstant.g_AlphaThreshold, 0.0f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public class CharacterTattooMaterialBuilder : MeddleMaterialBuilder
private readonly DataProvider dataProvider;
private readonly CustomizeParameter customizeParameter;

public CharacterTattooMaterialBuilder(string name, MaterialSet set, DataProvider dataProvider, CustomizeParameter customizeParameter) : base(name)
public CharacterTattooMaterialBuilder(
string name, MaterialSet set, DataProvider dataProvider, CustomizeParameter customizeParameter,
TextureMode textureMode) : base(name)
{
this.set = set;
this.dataProvider = dataProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public override MeddleMaterialBuilder Apply()
WithAlpha(AlphaMode.MASK, alphaThreshold);

var setTypes = new HashSet<TextureUsage>();
// TODO:
foreach (var textureUsage in set.TextureUsageDict)
{
var texData = dataProvider.LookupData(textureUsage.Value.FullPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ public class HairMaterialBuilder : MeddleMaterialBuilder
private readonly MaterialSet set;
private readonly DataProvider dataProvider;
private readonly CustomizeParameter parameters;
private readonly TextureMode textureMode;

public HairMaterialBuilder(string name, MaterialSet set, DataProvider dataProvider, CustomizeParameter parameters) : base(name)
public HairMaterialBuilder(
string name, MaterialSet set, DataProvider dataProvider, CustomizeParameter parameters,
TextureMode textureMode) : base(name)
{
this.set = set;
this.dataProvider = dataProvider;
this.parameters = parameters;
this.textureMode = textureMode;
}

public override MeddleMaterialBuilder Apply()
private void ApplyComputed()
{
var hairType = set.GetShaderKeyOrDefault(ShaderCategory.CategoryHairType, HairType.Hair);

Expand Down Expand Up @@ -65,13 +69,23 @@ public override MeddleMaterialBuilder Apply()
WithBaseColor(dataProvider.CacheTexture(diffuseTexture, $"Computed/{set.ComputedTextureName("diffuse")}"));
WithNormal(dataProvider.CacheTexture(normalTexture, $"Computed/{set.ComputedTextureName("normal")}"));
WithMetallicRoughness(dataProvider.CacheTexture(metallicRoughnessTexture, $"Computed/{set.ComputedTextureName("metallicRoughness")}"));
}

public override MeddleMaterialBuilder Apply()
{
if (textureMode == TextureMode.Bake)
{
ApplyComputed();
}
else
{
ApplyRaw(set, dataProvider);
}

WithAlpha(AlphaMode.MASK, set.GetConstantOrThrow<float>(MaterialConstant.g_AlphaThreshold));
IndexOfRefraction = set.GetConstantOrThrow<float>(MaterialConstant.g_GlassIOR);

Extras = set.ComposeExtrasNode(
("hairColor", hairColor.AsFloatArray()),
("bonusColor", bonusColor.AsFloatArray())
);
Extras = set.ComposeExtrasNode();
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ public class IrisMaterialBuilder : MeddleMaterialBuilder
private readonly MaterialSet set;
private readonly DataProvider dataProvider;
private readonly CustomizeParameter parameters;
private readonly TextureMode textureMode;

public IrisMaterialBuilder(string name, MaterialSet set, DataProvider dataProvider, CustomizeParameter parameters) : base(name)
public IrisMaterialBuilder(
string name, MaterialSet set, DataProvider dataProvider, CustomizeParameter parameters,
TextureMode textureMode) : base(name)
{
this.set = set;
this.dataProvider = dataProvider;
this.parameters = parameters;
this.textureMode = textureMode;
}

private SKTexture GetCubeMap(int index)
Expand All @@ -29,7 +33,7 @@ private SKTexture GetCubeMap(int index)
return ImageUtils.GetTexData(cubeMap, index, 0, 0).ToTexture();
}

public override MeddleMaterialBuilder Apply()
private void ApplyComputed()
{
if (!set.TryGetTextureStrict(dataProvider, TextureUsage.g_SamplerNormal, out var normalRes))
throw new InvalidOperationException("Missing normal texture");
Expand Down Expand Up @@ -79,16 +83,25 @@ public override MeddleMaterialBuilder Apply()
WithSpecularFactor(dataProvider.CacheTexture(specularTexture, $"Computed/{set.ComputedTextureName("specular")}"), 0.2f);
WithSpecularColor(dataProvider.CacheTexture(specularTexture, $"Computed/{set.ComputedTextureName("specular")}"));
WithEmissive(dataProvider.CacheTexture(emissiveTexture, $"Computed/{set.ComputedTextureName("emissive")}"));
}

public override MeddleMaterialBuilder Apply()
{
if (textureMode == TextureMode.Bake)
{
ApplyComputed();
}
else
{
ApplyRaw(set, dataProvider);
}

IndexOfRefraction = set.GetConstantOrThrow<float>(MaterialConstant.g_GlassIOR);
var alphaThreshold = set.GetConstantOrThrow<float>(MaterialConstant.g_AlphaThreshold);
if (alphaThreshold > 0)
WithAlpha(AlphaMode.MASK, alphaThreshold);

Extras = set.ComposeExtrasNode(
("leftIrisColor", leftIrisColor.AsFloatArray()),
("rightIrisColor", parameters.RightColor.AsFloatArray())
);

Extras = set.ComposeExtrasNode();
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public override MeddleMaterialBuilder Apply()
this.WithBaseColor(new Vector4(1, 1, 1, 0));
WithAlpha(AlphaMode.BLEND, 0.5f);

IndexOfRefraction = 1.0f;
Extras = set.ComposeExtrasNode();
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
using SharpGLTF.Materials;
using Meddle.Utils;
using Meddle.Utils.Materials;
using SharpGLTF.Materials;

namespace Meddle.Plugin.Models.Composer.Materials;

public abstract class MeddleMaterialBuilder : MaterialBuilder
public abstract class MeddleMaterialBuilder : MaterialBuilder, IVertexPaintMaterialBuilder
{
public MeddleMaterialBuilder(string name) : base(name)
{
IndexOfRefraction = 1.0f;
}

public abstract MeddleMaterialBuilder Apply();

internal void ApplyRaw(MaterialSet set, DataProvider dataProvider)
{
VertexPaint = true;

var hashStr = set.HashStr();
foreach (var texture in set.TextureUsageDict)
{
if (set.TryGetTexture(dataProvider, texture.Key, out var tex))
{
var texName = $"{hashStr}/{set.ComputedTextureName(texture.Value.GamePath)}";
var builder = dataProvider.CacheTexture(tex.ToTexture(), texName);
var mapped = GenericMaterialBuilder.MapTextureUsageToChannel(texture.Key);
if (mapped != null)
{
WithChannelImage(mapped.Value, builder);
}
}
}
}

public bool VertexPaint { get; internal set; }
}
Loading

0 comments on commit e0a09e8

Please sign in to comment.