Skip to content

Commit

Permalink
Set up basic color blending logic for Dat materials, though this is c…
Browse files Browse the repository at this point in the history
…urrently wrong.
  • Loading branch information
MeltyPlayer committed Oct 27, 2023
1 parent e49174c commit 31d83cc
Show file tree
Hide file tree
Showing 9 changed files with 344 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Numerics;

using fin.math;
using fin.math.matrix.four;
using fin.math.matrix.three;
using fin.math.rotations;
using fin.model;
using fin.shaders.glsl;

Expand All @@ -12,13 +14,16 @@ public class CachedTextureUniformData {
public required int TextureIndex { get; init; }
public required ITexture? FinTexture { get; init; }
public required GlTexture GlTexture { get; init; }
public required IReadOnlyFinMatrix3x2 Transform { get; init; }

public required IReadOnlyFinMatrix3x2? Transform2d { get; init; }
public required IReadOnlyFinMatrix4x4? Transform3d { get; init; }

public required bool HasFancyData { get; init; }
public required int SamplerLocation { get; init; }
public required int ClampMinLocation { get; init; }
public required int ClampMaxLocation { get; init; }
public required int TransformLocation { get; init; }
public required int Transform2dLocation { get; init; }
public required int Transform3dLocation { get; init; }
}

public abstract class BGlMaterialShader<TMaterial> : IGlMaterialShader
Expand Down Expand Up @@ -221,7 +226,8 @@ protected void SetUpTexture(
int samplerLocation;
int clampMinLocation = -1;
int clampMaxLocation = -1;
int transformLocation = -1;
int transform2dLocation = -1;
int transform3dLocation = -1;

var hasFancyData = GlslUtil.RequiresFancyTextureData(finTexture);
if (!hasFancyData) {
Expand All @@ -233,26 +239,36 @@ protected void SetUpTexture(
this.impl_.GetUniformLocation($"{textureName}.clampMin");
clampMaxLocation =
this.impl_.GetUniformLocation($"{textureName}.clampMax");
transformLocation =
this.impl_.GetUniformLocation($"{textureName}.transform");
transform2dLocation =
this.impl_.GetUniformLocation($"{textureName}.transform2d");
transform3dLocation =
this.impl_.GetUniformLocation($"{textureName}.transform3d");
}

var isTransform3d = finTexture?.IsTransform3d ?? false;

var cachedTextureUniformData = new CachedTextureUniformData {
TextureIndex = textureIndex,
FinTexture = finTexture,
GlTexture = glTexture,
Transform = CalculateTextureTransform_(finTexture),
Transform2d = isTransform3d
? null
: CalculateTextureTransform2d_(finTexture),
Transform3d = isTransform3d
? CalculateTextureTransform3d_(finTexture)
: null,
HasFancyData = hasFancyData,
SamplerLocation = samplerLocation,
ClampMinLocation = clampMinLocation,
ClampMaxLocation = clampMaxLocation,
TransformLocation = transformLocation,
Transform2dLocation = transform2dLocation,
Transform3dLocation = transform3dLocation,
};

this.cachedTextureUniformDatas_.AddLast(cachedTextureUniformData);
}

private static IReadOnlyFinMatrix3x2 CalculateTextureTransform_(
private static IReadOnlyFinMatrix3x2 CalculateTextureTransform2d_(
ITexture? texture) {
if (texture == null) {
return FinMatrix3x2.IDENTITY;
Expand All @@ -279,11 +295,48 @@ private static IReadOnlyFinMatrix3x2 CalculateTextureTransform_(
}

return FinMatrix3x2Util.FromTrss(offset,
textureRotationRadians,
textureRotationRadians?.Z,
scale,
null);
}

private static IReadOnlyFinMatrix4x4 CalculateTextureTransform3d_(
ITexture? texture) {
if (texture == null) {
return FinMatrix4x4.IDENTITY;
}

var textureOffset = texture.Offset;
var textureScale = texture.Scale;
var textureRotationRadians = texture.RotationRadians;

if (textureOffset == null &&
textureScale == null &&
textureRotationRadians == null) {
return FinMatrix4x4.IDENTITY;
}

Position? offset = null;
if (textureOffset != null) {
offset =
new Position(textureOffset.X, textureOffset.Y, textureOffset.Z);
}

Quaternion? rotation = null;
if (textureRotationRadians != null) {
rotation = QuaternionUtil.CreateZyx(textureRotationRadians.X,
textureRotationRadians.Y,
textureRotationRadians.Z);
}

Scale? scale = null;
if (textureScale != null) {
scale = new(textureScale.X, textureScale.Y, textureScale.Z);
}

return FinMatrix4x4Util.FromTrs(offset, rotation, scale);
}

private unsafe void BindTextureAndSetUpUniforms_(
CachedTextureUniformData uniformData) {
uniformData.GlTexture.Bind(uniformData.TextureIndex);
Expand Down Expand Up @@ -319,9 +372,14 @@ private unsafe void BindTextureAndSetUpUniforms_(
GL.Uniform2(uniformData.ClampMinLocation, clampMin);
GL.Uniform2(uniformData.ClampMaxLocation, clampMax);

var mat = uniformData.Transform.Impl;
var ptr = (float*) &mat;
GL.UniformMatrix2x3(uniformData.TransformLocation, 1, true, ptr);
if (!(uniformData.FinTexture?.IsTransform3d ?? false)) {
var mat2d = uniformData.Transform2d!.Impl;
var ptr = (float*) &mat2d;
GL.UniformMatrix2x3(uniformData.Transform2dLocation, 1, true, ptr);
} else {
var mat3d = uniformData.Transform3d!.Impl;
GlTransform.UniformMatrix4(uniformData.Transform3dLocation, mat3d);
}
}
}
}
Expand Down
17 changes: 15 additions & 2 deletions FinModelUtility/Fin/Fin/src/math/rotations/QuaternionUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,27 @@

using fin.model;

using Quaternion = System.Numerics.Quaternion;

namespace fin.math.rotations {
public static class QuaternionUtil {
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Quaternion Create(IRotation rotation)
=> QuaternionUtil.CreateZyx(rotation.XRadians,
rotation.YRadians,
rotation.ZRadians);
rotation.YRadians,
rotation.ZRadians);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Quaternion CreateXyz(
float xRadians,
float yRadians,
float zRadians) {
return Quaternion.CreateFromAxisAngle(Vector3.UnitX, xRadians) *
Quaternion.CreateFromAxisAngle(Vector3.UnitY, yRadians) *
Quaternion.CreateFromAxisAngle(Vector3.UnitZ, zRadians);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Quaternion CreateZyx(
float xRadians,
float yRadians,
Expand Down
19 changes: 11 additions & 8 deletions FinModelUtility/Fin/Fin/src/model/MaterialInterfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,16 +351,19 @@ public interface ITexture {
IReadOnlyVector2? ClampS { get; set; }
IReadOnlyVector2? ClampT { get; set; }

IReadOnlyVector2? Offset { get; }
ITexture SetOffset(float x, float y);
bool IsTransform3d { get; }

IReadOnlyVector2? Scale { get; }
ITexture SetScale(float x, float y);
IReadOnlyVector3? Offset { get; }
ITexture SetOffset2d(float x, float y);
ITexture SetOffset3d(float x, float y, float z);

float? RotationRadians { get; }
float? RotationDegrees { get; }
ITexture SetRotationRadians(float rotationRadians);
ITexture SetRotationDegrees(float rotationDegrees);
IReadOnlyVector3? Scale { get; }
ITexture SetScale2d(float x, float y);
ITexture SetScale3d(float x, float y, float z);

IReadOnlyVector3? RotationRadians { get; }
ITexture SetRotationRadians2d(float rotationRadians);
ITexture SetRotationRadians3d(float xRadians, float yRadians, float zRadians);

// TODO: Support fixed # of repeats
// TODO: Support animated textures
Expand Down
45 changes: 33 additions & 12 deletions FinModelUtility/Fin/Fin/src/model/impl/material/TextureImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,32 +71,53 @@ public ImageTransparencyType TransparencyType
public IReadOnlyVector2? ClampS { get; set; }
public IReadOnlyVector2? ClampT { get; set; }

public IReadOnlyVector2? Offset { get; private set; }

public ITexture SetOffset(float x, float y) {
this.Offset = new Vector2f { X = x, Y = y };
public bool IsTransform3d { get; private set; }


public IReadOnlyVector3? Offset { get; private set; }

public ITexture SetOffset2d(float x, float y) {
this.Offset = new Vector3f { X = x, Y = y };
return this;
}

public ITexture SetOffset3d(float x, float y, float z) {
this.Offset = new Vector3f { X = x, Y = y, Z = z };
this.IsTransform3d = true;
return this;
}


public IReadOnlyVector2? Scale { get; private set; }
public IReadOnlyVector3? Scale { get; private set; }

public ITexture SetScale(float x, float y) {
this.Scale = new Vector2f { X = x, Y = y };
public ITexture SetScale2d(float x, float y) {
this.Scale = new Vector3f { X = x, Y = y };
return this;
}

public ITexture SetScale3d(float x, float y, float z) {
this.Scale = new Vector3f { X = x, Y = y, Z = z };
this.IsTransform3d = true;
return this;
}


public float? RotationRadians { get; private set; }
public float? RotationDegrees => this.RotationRadians / MathF.PI * 180;
public IReadOnlyVector3? RotationRadians { get; private set; }

public ITexture SetRotationRadians(float rotationRadians) {
this.RotationRadians = rotationRadians;
public ITexture SetRotationRadians2d(float rotationRadians) {
this.RotationRadians = new Vector3f { Z = rotationRadians };
return this;
}

public ITexture SetRotationDegrees(float rotationDegrees)
=> SetRotationRadians(rotationDegrees / 180 * MathF.PI);
public ITexture SetRotationRadians3d(float xRadians,
float yRadians,
float zRadians) {
this.RotationRadians = new Vector3f
{ X = xRadians, Y = yRadians, Z = zRadians };
this.IsTransform3d = true;
return this;
}

public override int GetHashCode()
=> new FluentHash()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static TextureBuilder UseTexture(this ChannelBuilder channelBuilder,
textureBuilder.WithTransform(
new Vector2(finTexture.Offset?.X ?? 0, finTexture.Offset?.Y ?? 0),
new Vector2(finTexture.Scale?.X ?? 1, finTexture.Scale?.Y ?? 1),
finTexture.RotationDegrees ?? 0);
finTexture.RotationRadians?.Z ?? 0);

return textureBuilder;
}
Expand Down
11 changes: 9 additions & 2 deletions FinModelUtility/Fin/Fin/src/shaders/glsl/GlslUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ struct Texture {
sampler2D sampler;
vec2 clampMin;
vec2 clampMax;
mat2x3 transform;
mat2x3 transform2d;
mat4 transform3d;
};
""";
}
Expand All @@ -182,10 +183,16 @@ public static string ReadColorFromTexture(
return $"texture({textureName}, {uvConverter(rawUvName)})";
}

string transformedUv;
if (!(finTexture?.IsTransform3d ?? false)) {
transformedUv = $"({textureName}.transform2d * {rawUvName}).xy";
} else {
transformedUv = $"({textureName}.transform3d * vec4({rawUvName}, 0, 1)).xy";
}
return
$"texture({textureName}.sampler, " +
"clamp(" +
$"{uvConverter($"({textureName}.transform * {rawUvName}).xy")}, " +
$"{uvConverter(transformedUv)}, " +
$"{textureName}.clampMin, " +
$"{textureName}.clampMax" +
")" + // clamp
Expand Down
Loading

0 comments on commit 31d83cc

Please sign in to comment.