Skip to content

Commit

Permalink
Set up basic fixed function material support for Dat models.
Browse files Browse the repository at this point in the history
  • Loading branch information
MeltyPlayer committed Oct 27, 2023
1 parent 06000dd commit a83db84
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ void main() {
}

fragmentSrc.Append(
"""
if (fragColor.a < .95) {
discard;
$$"""
if (fragColor.a < {{GlslConstants.MIN_ALPHA_BEFORE_DISCARD_TEXT}}) {
discard;
}
}
}
""");
""");

this.FragmentShaderSource = fragmentSrc.ToString();
}
Expand Down
3 changes: 3 additions & 0 deletions FinModelUtility/Fin/Fin/src/shaders/glsl/GlslConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ public static class GlslConstants {
public const string UNIFORM_PROJECTION_MATRIX_NAME = "projectionMatrix";
public const string UNIFORM_BONE_MATRICES_NAME = "boneMatrices";
public const string UNIFORM_USE_LIGHTING_NAME = "useLighting";

public const float MIN_ALPHA_BEFORE_DISCARD = .95f;
public const string MIN_ALPHA_BEFORE_DISCARD_TEXT = ".95";
}
}
2 changes: 1 addition & 1 deletion FinModelUtility/Fin/Fin/src/shaders/glsl/GlslUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ vec3 getMergedDiffuseLightColor(vec3 vertexNormal) {
vec3 applyLightingColor(vec3 diffuseColor,{{(withAmbientOcclusion ? " float ambientOcclusionAmount," : "")}} vec3 vertexNormal) {
vec3 mergedDiffuseLightColor = getMergedDiffuseLightColor(vertexNormal);
vec3 mergedLightColor = {{(withAmbientOcclusion ? "ambientOcclusionAmount * " : "")}}min(ambientLightColor + mergedDiffuseLightColor, 1);
vec3 mergedLightColor = min({{(withAmbientOcclusion ? "ambientOcclusionAmount * " : "")}}ambientLightColor + mergedDiffuseLightColor, 1);
return diffuseColor * mergedLightColor;
}
""";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,16 @@ void main() {

// TODO: Is this right?
fragmentShaderSrc.Append(
"""
fragColor.rgb += emissiveColor.rgb;
fragColor.rgb = min(fragColor.rgb, 1);
if (fragColor.a < .95) {
discard;
}
}
""");
$$"""
fragColor.rgb += emissiveColor.rgb;
fragColor.rgb = min(fragColor.rgb, 1);
if (fragColor.a < {{GlslConstants.MIN_ALPHA_BEFORE_DISCARD_TEXT}}) {
discard;
}
}
""");

this.FragmentShaderSource = fragmentShaderSrc.ToString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public TextureShaderSourceGlsl(IModel model,
if (hasNormals) {
fragmentSrc.Append(
"""
in vec3 vertexNormal;
""");
}
Expand Down Expand Up @@ -78,14 +78,14 @@ void main() {
}

fragmentSrc.Append(
"""
if (fragColor.a < .95) {
discard;
$$"""
if (fragColor.a < {{GlslConstants.MIN_ALPHA_BEFORE_DISCARD_TEXT}}) {
discard;
}
}
}
""");
""");

this.FragmentShaderSource = fragmentSrc.ToString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,17 @@ private enum Channel {
};

private TValue? Combine_<TValue, TConstant, TTerm, TExpression>(
IFixedFunctionOps<TValue, TConstant, TTerm, TExpression> fixedFunctionOps,
IFixedFunctionOps<TValue, TConstant, TTerm, TExpression>
fixedFunctionOps,
IReadOnlyList<TValue?> sources,
TexCombineMode combineMode,
TexCombineScale combineScale)
where TValue : IValue<TValue, TConstant, TTerm, TExpression>
where TConstant : IConstant<TValue, TConstant, TTerm, TExpression>, TValue
where TConstant : IConstant<TValue, TConstant, TTerm, TExpression>,
TValue
where TTerm : ITerm<TValue, TConstant, TTerm, TExpression>, TValue
where TExpression : IExpression<TValue, TConstant, TTerm, TExpression>, TValue {
where TExpression : IExpression<TValue, TConstant, TTerm, TExpression>,
TValue {
// TODO: Implement dot-product ones
var combinedValue = combineMode switch {
TexCombineMode.Replace => sources[0],
Expand Down
102 changes: 82 additions & 20 deletions FinModelUtility/Formats/Dat/src/api/DatModelImporter.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
using System.Linq;
using System.Runtime.InteropServices.ComTypes;

using Assimp;
using Assimp.Unmanaged;

using dat.schema;
using dat.schema;

using fin.io;
using fin.language.equations.fixedFunction;
using fin.language.equations.fixedFunction.impl;
using fin.model;
using fin.model.impl;
using fin.model.io.importers;
using fin.shaders.glsl;
using fin.util.hex;

using gx;
Expand Down Expand Up @@ -79,11 +76,11 @@ public IModel ImportModel(DatModelFileBundle modelFileBundle) {
mObjOffset,
out finMaterial)) {
var tObjsAndOffsets = mObj.TObjsAndOffsets.ToArray();
if (tObjsAndOffsets.Length == 0) {
finMaterial = finMaterialManager.AddNullMaterial();
} else {
ITexture? firstTexture = null;
foreach (var (tObjOffset, tObj) in tObjsAndOffsets) {

var finTextures = new ITexture[tObjsAndOffsets.Length];
if (tObjsAndOffsets.Length > 0) {
for (var i = 0; i < tObjsAndOffsets.Length; i++) {
var (tObjOffset, tObj) = tObjsAndOffsets[i];
if (!finTexturesByTObjOffset.TryGetValue(
tObjOffset,
out var finTexture)) {
Expand All @@ -93,20 +90,23 @@ public IModel ImportModel(DatModelFileBundle modelFileBundle) {
finTexturesByTObjOffset[tObjOffset] = finTexture;
}

if (firstTexture == null) {
firstTexture = finTexture;
}
finTextures[i] = finTexture;
}

finMaterial =
finMaterialManager.AddTextureMaterial(firstTexture!);
}

finMaterial.Name = mObj.Name ?? mObjOffset.ToHex();
var fixedFunctionMaterial =
finMaterialManager.AddFixedFunctionMaterial();
finMaterial = fixedFunctionMaterial;

finMaterialsByMObjOffset[mObjOffset] = finMaterial;
this.PopulateFixedFunctionMaterial_(mObj,
finTextures,
fixedFunctionMaterial);
}

finMaterial.Name = mObj.Name ?? mObjOffset.ToHex();

finMaterialsByMObjOffset[mObjOffset] = finMaterial;

// Adds polygons
foreach (var pObj in dObj.PObjs) {
var vertexSpace = pObj.VertexSpace;
Expand Down Expand Up @@ -175,5 +175,67 @@ public IModel ImportModel(DatModelFileBundle modelFileBundle) {

return finModel;
}

private void PopulateFixedFunctionMaterial_(
MObj mObj,
IReadOnlyList<ITexture> finTextures,
IFixedFunctionMaterial fixedFunctionMaterial) {
var equations = fixedFunctionMaterial.Equations;

var colorOps = new ColorFixedFunctionOps(equations);
var scalarOps = new ScalarFixedFunctionOps(equations);

var vertexColor = equations.CreateOrGetColorInput(
FixedFunctionSource.VERTEX_COLOR_0);
var vertexAlpha = equations.CreateOrGetScalarInput(
FixedFunctionSource.VERTEX_ALPHA_0);

IColorValue textureColor = colorOps.One;
IScalarValue textureAlpha = scalarOps.One;
if (finTextures.Count > 0) {
fixedFunctionMaterial.SetTextureSource(0, finTextures[0]);
textureColor = equations.CreateOrGetColorInput(
FixedFunctionSource.TEXTURE_COLOR_0);
textureAlpha = equations.CreateOrGetScalarInput(
FixedFunctionSource.TEXTURE_ALPHA_0);
}

var material = mObj.Material;
var diffuseRgba = material.DiffuseColor;
var diffuseColor =
colorOps.Multiply(
equations.CreateOrGetColorInput(
FixedFunctionSource.LIGHT_0_COLOR),
equations.CreateColorConstant(diffuseRgba.Rf,
diffuseRgba.Gf,
diffuseRgba.Bf));

var ambientRgba = material.AmbientColor;
var ambientColor =
equations.CreateColorConstant(ambientRgba.Rf,
ambientRgba.Gf,
ambientRgba.Bf);

var lightColor = colorOps.Add(ambientColor, diffuseColor);

var outputColor = colorOps.Multiply(textureColor, vertexColor);
outputColor = colorOps.Multiply(outputColor, lightColor);

var outputAlpha = scalarOps.Multiply(textureAlpha, vertexAlpha);
outputAlpha = scalarOps.MultiplyWithConstant(outputAlpha, diffuseRgba.Af);
outputAlpha = scalarOps.MultiplyWithConstant(outputAlpha, material.Alpha);

equations.CreateColorOutput(FixedFunctionSource.OUTPUT_COLOR,
outputColor ?? colorOps.Zero);
equations.CreateScalarOutput(FixedFunctionSource.OUTPUT_ALPHA,
outputAlpha ?? scalarOps.Zero);

fixedFunctionMaterial.SetAlphaCompare(
AlphaOp.Or,
AlphaCompareType.GEqual,
GlslConstants.MIN_ALPHA_BEFORE_DISCARD,
AlphaCompareType.Never,
0);
}
}
}
9 changes: 0 additions & 9 deletions FinModelUtility/Formats/Dat/src/schema/Dat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,6 @@ uint jObjDataOffset
inverseBindMatrixValues[15] = 1;
jObj.InverseBindMatrix =
new FinMatrix4x4(inverseBindMatrixValues).TransposeInPlace();

var parentInverseBindMatrix =
parentJObj?.InverseBindMatrix ?? FinMatrix4x4.IDENTITY;
var thisInverseBindMatrix =
parentInverseBindMatrix.CloneAndInvert()
.MultiplyInPlace(jObj.InverseBindMatrix!);

var thisMatrix = thisInverseBindMatrix.CloneAndInvert();
;
}

var firstChildOffset = jObj.Data.FirstChildBoneOffset;
Expand Down
12 changes: 7 additions & 5 deletions FinModelUtility/Formats/Dat/src/schema/PObj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ private void ReadDisplayList_(IBinaryReader br) {
continue;
}

if (vertexAttribute == GxAttribute.PNMTXIDX &&
vertexFormat == GxAttributeType.DIRECT) {
weightId = br.ReadByte() / 3;
continue;
}

var value = vertexFormat switch {
GxAttributeType.DIRECT => br.ReadByte(),
GxAttributeType.INDEX_8 => br.ReadByte(),
Expand All @@ -224,10 +230,6 @@ private void ReadDisplayList_(IBinaryReader br) {
vertexDescriptor.Stride * value;

switch (vertexAttribute) {
case GxAttribute.PNMTXIDX: {
weightId = value;
break;
}
case GxAttribute.POS: {
position = br.SubreadAt(
offset,
Expand Down Expand Up @@ -261,7 +263,7 @@ private void ReadDisplayList_(IBinaryReader br) {

if (position != null) {
vertices[i] = new DatVertex {
WeightId = weightId / 3,
WeightId = weightId,
Position = position.Value,
Normal = normal,
Uv0 = uv0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ vec3 getMergedDiffuseLightColor(vec3 vertexNormal) {
vec3 applyLightingColor(vec3 diffuseColor, float ambientOcclusionAmount, vec3 vertexNormal) {
vec3 mergedDiffuseLightColor = getMergedDiffuseLightColor(vertexNormal);

vec3 mergedLightColor = ambientOcclusionAmount * min(ambientLightColor + mergedDiffuseLightColor, 1);
vec3 mergedLightColor = min(ambientOcclusionAmount * ambientLightColor + mergedDiffuseLightColor, 1);
return diffuseColor * mergedLightColor;
}

Expand Down

0 comments on commit a83db84

Please sign in to comment.