Skip to content

Commit

Permalink
Fixed some more material bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
MeltyPlayer committed Oct 27, 2023
1 parent 3f2e441 commit e49174c
Show file tree
Hide file tree
Showing 18 changed files with 103 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class CachedTextureUniformData {
public required GlTexture GlTexture { get; init; }
public required IReadOnlyFinMatrix3x2 Transform { 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; }
Expand Down Expand Up @@ -217,19 +218,35 @@ protected void SetUpTexture(
int textureIndex,
ITexture? finTexture,
GlTexture glTexture) {
int samplerLocation;
int clampMinLocation = -1;
int clampMaxLocation = -1;
int transformLocation = -1;

var hasFancyData = GlslUtil.RequiresFancyTextureData(finTexture);
if (!hasFancyData) {
samplerLocation = this.impl_.GetUniformLocation($"{textureName}");
} else {
samplerLocation =
this.impl_.GetUniformLocation($"{textureName}.sampler");
clampMinLocation =
this.impl_.GetUniformLocation($"{textureName}.clampMin");
clampMaxLocation =
this.impl_.GetUniformLocation($"{textureName}.clampMax");
transformLocation =
this.impl_.GetUniformLocation($"{textureName}.transform");
}

var cachedTextureUniformData = new CachedTextureUniformData {
TextureIndex = textureIndex,
FinTexture = finTexture,
GlTexture = glTexture,
Transform = CalculateTextureTransform_(finTexture),
SamplerLocation =
this.impl_.GetUniformLocation($"{textureName}.sampler"),
ClampMinLocation =
this.impl_.GetUniformLocation($"{textureName}.clampMin"),
ClampMaxLocation =
this.impl_.GetUniformLocation($"{textureName}.clampMax"),
TransformLocation =
this.impl_.GetUniformLocation($"{textureName}.transform"),
HasFancyData = hasFancyData,
SamplerLocation = samplerLocation,
ClampMinLocation = clampMinLocation,
ClampMaxLocation = clampMaxLocation,
TransformLocation = transformLocation,
};

this.cachedTextureUniformDatas_.AddLast(cachedTextureUniformData);
Expand Down Expand Up @@ -272,38 +289,40 @@ private unsafe void BindTextureAndSetUpUniforms_(
uniformData.GlTexture.Bind(uniformData.TextureIndex);
GL.Uniform1(uniformData.SamplerLocation, uniformData.TextureIndex);

OpenTK.Vector2 clampMin = new(-10000);
OpenTK.Vector2 clampMax = new(10000);
if (uniformData.HasFancyData) {
OpenTK.Vector2 clampMin = new(-10000);
OpenTK.Vector2 clampMax = new(10000);

if (uniformData.FinTexture?.WrapModeU == WrapMode.MIRROR_CLAMP) {
clampMin.X = -1;
clampMax.X = 2;
}
if (uniformData.FinTexture?.WrapModeU == WrapMode.MIRROR_CLAMP) {
clampMin.X = -1;
clampMax.X = 2;
}

if (uniformData.FinTexture?.WrapModeV == WrapMode.MIRROR_CLAMP) {
clampMin.Y = -1;
clampMax.Y = 2;
}
if (uniformData.FinTexture?.WrapModeV == WrapMode.MIRROR_CLAMP) {
clampMin.Y = -1;
clampMax.Y = 2;
}

var clampS = uniformData.FinTexture?.ClampS;
var clampT = uniformData.FinTexture?.ClampT;
var clampS = uniformData.FinTexture?.ClampS;
var clampT = uniformData.FinTexture?.ClampT;

if (clampS != null) {
clampMin.X = clampS.X;
clampMax.X = clampS.Y;
}
if (clampS != null) {
clampMin.X = clampS.X;
clampMax.X = clampS.Y;
}

if (clampT != null) {
clampMin.Y = clampT.X;
clampMax.Y = clampT.Y;
}
if (clampT != null) {
clampMin.Y = clampT.X;
clampMax.Y = clampT.Y;
}

GL.Uniform2(uniformData.ClampMinLocation, clampMin);
GL.Uniform2(uniformData.ClampMaxLocation, clampMax);
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);
var mat = uniformData.Transform.Impl;
var ptr = (float*) &mat;
GL.UniformMatrix2x3(uniformData.TransformLocation, 1, true, ptr);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -726,19 +726,25 @@ private bool IsInRange_(FixedFunctionSource value,
private string GetTextureValue_(int textureIndex,
IReadOnlyList<ITexture> textures) {
var texture = textures[textureIndex];

var uvText = texture?.UvType switch {
UvType.STANDARD => $"uv{texture.UvIndex}",
UvType.SPHERICAL => "(asin(normalUv) / 3.14159 + 0.5)",
UvType.LINEAR => "(acos(normalUv) / 3.14159)",
_ => throw new ArgumentOutOfRangeException()
var textureName = $"texture{textureIndex}";
return texture.UvType switch {
UvType.STANDARD
=> GlslUtil.ReadColorFromTexture(textureName,
$"uv{texture.UvIndex}",
texture),
UvType.SPHERICAL
=> GlslUtil.ReadColorFromTexture(
textureName,
"normalUv",
uv => $"asin({uv}) / 3.14159 + 0.5",
texture),
UvType.LINEAR
=> GlslUtil.ReadColorFromTexture(
textureName,
"normalUv",
uv => $"acos({uv}) / 3.14159",
texture),
};

var textureText =
GlslUtil.ReadColorFromTexture($"texture{textureIndex}",
uvText,
texture);
return textureText;
}

private void PrintColorTernaryOperator_(
Expand Down
16 changes: 12 additions & 4 deletions FinModelUtility/Fin/Fin/src/shaders/glsl/GlslUtil.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Numerics;
using System;
using System.Numerics;
using System.Text;

using fin.model;
Expand Down Expand Up @@ -168,16 +169,23 @@ public static string GetTypeOfTexture(ITexture? finTexture)

public static string ReadColorFromTexture(
string textureName,
string uvName,
string rawUvName,
ITexture? finTexture)
=> ReadColorFromTexture(textureName, rawUvName, t => t, finTexture);

public static string ReadColorFromTexture(
string textureName,
string rawUvName,
Func<string, string> uvConverter,
ITexture? finTexture) {
if (!RequiresFancyTextureData(finTexture)) {
return $"texture({textureName}, {uvName})";
return $"texture({textureName}, {uvConverter(rawUvName)})";
}

return
$"texture({textureName}.sampler, " +
"clamp(" +
$"({textureName}.transform * {uvName}).xy, " +
$"{uvConverter($"({textureName}.transform * {rawUvName}).xy")}, " +
$"{textureName}.clampMin, " +
$"{textureName}.clampMax" +
")" + // clamp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void main() {

vec3 colorComponent = texture(texture1, uv0).rgb*individualLightColors[0].rgb + individualLightColors[0].rgb*texture(texture1, uv0).rgb;

float alphaComponent = (vertexColor0.a*texture(texture2, (asin(normalUv) / 3.14159 + 0.5)).a + scalar_3dsAlpha0)*scalar_3dsAlpha1;
float alphaComponent = (vertexColor0.a*texture(texture2, asin(normalUv) / 3.14159 + 0.5).a + scalar_3dsAlpha0)*scalar_3dsAlpha1;

fragColor = vec4(colorComponent, alphaComponent);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void main() {

vec3 colorComponent = texture(texture1, uv0).rgb*individualLightColors[0].rgb + individualLightColors[0].rgb*texture(texture1, uv0).rgb;

float alphaComponent = (vertexColor0.a*texture(texture2, (asin(normalUv) / 3.14159 + 0.5)).a + scalar_3dsAlpha0)*scalar_3dsAlpha1;
float alphaComponent = (vertexColor0.a*texture(texture2, asin(normalUv) / 3.14159 + 0.5).a + scalar_3dsAlpha0)*scalar_3dsAlpha1;

fragColor = vec4(colorComponent, alphaComponent);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void main() {
individualLightColors[i] = lightColor;
}

vec3 colorComponent = (color_3dsColor3 + vec3(texture(texture0, uv0).rgb.g))*(vec3(texture(texture1, uv0).rgb.b)*texture(texture2, (asin(normalUv) / 3.14159 + 0.5)).rgb + color_3dsColor2*vec3(1 + -1*vertexColor0.rgb.r*vertexColor0.rgb.r*2) + vertexColor0.rgb)*texture(texture1, uv0).rgb*vec3(2)*individualLightColors[0].rgb;
vec3 colorComponent = (color_3dsColor3 + vec3(texture(texture0, uv0).rgb.g))*(vec3(texture(texture1, uv0).rgb.b)*texture(texture2, asin(normalUv) / 3.14159 + 0.5).rgb + color_3dsColor2*vec3(1 + -1*vertexColor0.rgb.r*vertexColor0.rgb.r*2) + vertexColor0.rgb)*texture(texture1, uv0).rgb*vec3(2)*individualLightColors[0].rgb;

float alphaComponent = vertexColor0.a*texture(texture1, uv0).a*scalar_3dsAlpha1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void main() {
individualLightColors[i] = lightColor;
}

vec3 colorComponent = (texture(texture0, uv0).rgb + texture(texture1, (asin(normalUv) / 3.14159 + 0.5)).rgb)*individualLightColors[0].rgb*vec3(2)*vec3(0.49803921580314636);
vec3 colorComponent = (texture(texture0, uv0).rgb + texture(texture1, asin(normalUv) / 3.14159 + 0.5).rgb)*individualLightColors[0].rgb*vec3(2)*vec3(0.49803921580314636);

float alphaComponent = vertexColor0.a*texture(texture0, uv0).a*scalar_3dsAlpha3;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ in vec2 uv0;
out vec4 fragColor;

void main() {
vec3 colorComponent = (vertexColor0.rgb*texture(texture0, uv0).rgb*vec3(2) + texture(texture1, (asin(normalUv) / 3.14159 + 0.5)).rgb)*vec3(0.49803921580314636);
vec3 colorComponent = (vertexColor0.rgb*texture(texture0, uv0).rgb*vec3(2) + texture(texture1, asin(normalUv) / 3.14159 + 0.5).rgb)*vec3(0.49803921580314636);

float alphaComponent = vertexColor0.a*texture(texture0, uv0).a;

Expand Down
25 changes: 6 additions & 19 deletions FinModelUtility/Formats/Gx/src/GxWrapMode.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
using fin.model;

namespace gx {
[Flags]
public enum GxWrapMode : byte {
GX_CLAMP,
GX_REPEAT,
GX_MIRROR,
}

public static class GxWrapModeExtensions {
public static WrapMode ToFinWrapMode(this GxWrapMode gxWrapMode) {
var mirror = (gxWrapMode & GxWrapMode.GX_MIRROR) != 0;
var repeat = (gxWrapMode & GxWrapMode.GX_REPEAT) != 0;

if (mirror && repeat) {
return WrapMode.MIRROR_REPEAT;
}

if (mirror) {
return WrapMode.MIRROR_CLAMP;
}

if (repeat) {
return WrapMode.REPEAT;
}

return WrapMode.CLAMP;
}
public static WrapMode ToFinWrapMode(this GxWrapMode gxWrapMode)
=> gxWrapMode switch {
GxWrapMode.GX_CLAMP => WrapMode.CLAMP,
GxWrapMode.GX_REPEAT => WrapMode.REPEAT,
GxWrapMode.GX_MIRROR => WrapMode.MIRROR_REPEAT,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void main() {
individualLightColors[i] = lightColor;
}

vec3 colorComponent = clamp(vec3(0.5)*texture(texture2.sampler, clamp((texture2.transform * (acos(normalUv) / 3.14159)).xy, texture2.clampMin, texture2.clampMax)).rgb + vec3(0.5) + color_GxMaterialColor0*clamp((individualLightColors[0].rgb + color_GxAmbientColor0), 0, 1)*texture(texture0, uv0).rgb*vec3(2)*clamp((vec3(0.5)*texture(texture2.sampler, clamp((texture2.transform * (acos(normalUv) / 3.14159)).xy, texture2.clampMin, texture2.clampMax)).rgb + vec3(0.5)), 0, 1) + vec3(-0.5), 0, 1);
vec3 colorComponent = clamp(vec3(0.5)*texture(texture2.sampler, clamp(acos((texture2.transform * normalUv).xy) / 3.14159, texture2.clampMin, texture2.clampMax)).rgb + vec3(0.5) + color_GxMaterialColor0*clamp((individualLightColors[0].rgb + color_GxAmbientColor0), 0, 1)*texture(texture0, uv0).rgb*vec3(2)*clamp((vec3(0.5)*texture(texture2.sampler, clamp(acos((texture2.transform * normalUv).xy) / 3.14159, texture2.clampMin, texture2.clampMax)).rgb + vec3(0.5)), 0, 1) + vec3(-0.5), 0, 1);

float alphaComponent = texture(texture3.sampler, clamp((texture3.transform * uv0).xy, texture3.clampMin, texture3.clampMax)).a + -1*(1 + -1*scalar_GxAlpha0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void main() {
individualLightColors[i] = lightColor;
}

vec3 colorComponent = clamp(vec3(0.5)*texture(texture2.sampler, clamp((texture2.transform * (acos(normalUv) / 3.14159)).xy, texture2.clampMin, texture2.clampMax)).rgb + vec3(0.5) + color_GxMaterialColor0*clamp((individualLightColors[0].rgb + color_GxAmbientColor0), 0, 1)*texture(texture0, uv0).rgb*vec3(2)*clamp((vec3(0.5)*texture(texture2.sampler, clamp((texture2.transform * (acos(normalUv) / 3.14159)).xy, texture2.clampMin, texture2.clampMax)).rgb + vec3(0.5)), 0, 1) + vec3(-0.5), 0, 1);
vec3 colorComponent = clamp(vec3(0.5)*texture(texture2.sampler, clamp(acos((texture2.transform * normalUv).xy) / 3.14159, texture2.clampMin, texture2.clampMax)).rgb + vec3(0.5) + color_GxMaterialColor0*clamp((individualLightColors[0].rgb + color_GxAmbientColor0), 0, 1)*texture(texture0, uv0).rgb*vec3(2)*clamp((vec3(0.5)*texture(texture2.sampler, clamp(acos((texture2.transform * normalUv).xy) / 3.14159, texture2.clampMin, texture2.clampMax)).rgb + vec3(0.5)), 0, 1) + vec3(-0.5), 0, 1);

float alphaComponent = texture(texture3.sampler, clamp((texture3.transform * uv0).xy, texture3.clampMin, texture3.clampMax)).a + -1*(1 + -1*scalar_GxAlpha0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void main() {
individualLightColors[i] = lightColor;
}

vec3 colorComponent = clamp(vec3(0.5)*texture(texture2.sampler, clamp((texture2.transform * (acos(normalUv) / 3.14159)).xy, texture2.clampMin, texture2.clampMax)).rgb + vec3(0.5) + color_GxMaterialColor0*clamp((individualLightColors[0].rgb + color_GxAmbientColor0), 0, 1)*texture(texture0.sampler, clamp((texture0.transform * uv0).xy, texture0.clampMin, texture0.clampMax)).rgb*vec3(2)*clamp((vec3(0.5)*texture(texture2.sampler, clamp((texture2.transform * (acos(normalUv) / 3.14159)).xy, texture2.clampMin, texture2.clampMax)).rgb + vec3(0.5)), 0, 1) + vec3(-0.5), 0, 1);
vec3 colorComponent = clamp(vec3(0.5)*texture(texture2.sampler, clamp(acos((texture2.transform * normalUv).xy) / 3.14159, texture2.clampMin, texture2.clampMax)).rgb + vec3(0.5) + color_GxMaterialColor0*clamp((individualLightColors[0].rgb + color_GxAmbientColor0), 0, 1)*texture(texture0.sampler, clamp((texture0.transform * uv0).xy, texture0.clampMin, texture0.clampMax)).rgb*vec3(2)*clamp((vec3(0.5)*texture(texture2.sampler, clamp(acos((texture2.transform * normalUv).xy) / 3.14159, texture2.clampMin, texture2.clampMax)).rgb + vec3(0.5)), 0, 1) + vec3(-0.5), 0, 1);

float alphaComponent = texture(texture3.sampler, clamp((texture3.transform * uv0).xy, texture3.clampMin, texture3.clampMax)).a + -1*(1 + -1*scalar_GxAlpha0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void main() {
individualLightColors[i] = lightColor;
}

vec3 colorComponent = clamp(texture(texture1.sampler, clamp((texture1.transform * uv1).xy, texture1.clampMin, texture1.clampMax)).rgb*clamp(clamp((texture(texture0.sampler, clamp((texture0.transform * uv0).xy, texture0.clampMin, texture0.clampMax)).rgb + clamp((vec3(1) + vec3(-1)*texture(texture2.sampler, clamp((texture2.transform * (acos(normalUv) / 3.14159)).xy, texture2.clampMin, texture2.clampMax)).rgb), 0, 1)*(vec3(1) + vec3(-1)*vec3(texture(texture0.sampler, clamp((texture0.transform * uv0).xy, texture0.clampMin, texture0.clampMax)).a))), 0, 1)*vertexColor0.rgb*clamp((individualLightColors[0].rgb + color_GxAmbientColor0), 0, 1), 0, 1)*vec3(2), 0, 1);
vec3 colorComponent = clamp(texture(texture1.sampler, clamp((texture1.transform * uv1).xy, texture1.clampMin, texture1.clampMax)).rgb*clamp(clamp((texture(texture0.sampler, clamp((texture0.transform * uv0).xy, texture0.clampMin, texture0.clampMax)).rgb + clamp((vec3(1) + vec3(-1)*texture(texture2.sampler, clamp(acos((texture2.transform * normalUv).xy) / 3.14159, texture2.clampMin, texture2.clampMax)).rgb), 0, 1)*(vec3(1) + vec3(-1)*vec3(texture(texture0.sampler, clamp((texture0.transform * uv0).xy, texture0.clampMin, texture0.clampMax)).a))), 0, 1)*vertexColor0.rgb*clamp((individualLightColors[0].rgb + color_GxAmbientColor0), 0, 1), 0, 1)*vec3(2), 0, 1);

float alphaComponent = vertexColor0.a;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct Texture {
vec2 clampMax;
mat2x3 transform;
};
uniform Texture texture0;
uniform sampler2D texture0;
uniform Texture texture1;
uniform Texture texture2;
uniform vec3 color_GxAmbientColor0;
Expand Down Expand Up @@ -49,7 +49,7 @@ void main() {
individualLightColors[i] = lightColor;
}

vec3 colorComponent = clamp(vertexColor0.rgb*clamp((individualLightColors[0].rgb + color_GxAmbientColor0), 0, 1)*clamp((texture(texture2.sampler, clamp((texture2.transform * uv1).xy, texture2.clampMin, texture2.clampMax)).rgb + texture(texture1.sampler, clamp((texture1.transform * (acos(normalUv) / 3.14159)).xy, texture1.clampMin, texture1.clampMax)).rgb*texture(texture0.sampler, clamp((texture0.transform * uv0).xy, texture0.clampMin, texture0.clampMax)).rgb*vertexColor0.rgb*clamp((individualLightColors[0].rgb + color_GxAmbientColor0), 0, 1)*vec3(0.5)*vec3(2)), 0, 1)*vec3(2), 0, 1);
vec3 colorComponent = clamp(vertexColor0.rgb*clamp((individualLightColors[0].rgb + color_GxAmbientColor0), 0, 1)*clamp((texture(texture2.sampler, clamp((texture2.transform * uv1).xy, texture2.clampMin, texture2.clampMax)).rgb + texture(texture1.sampler, clamp(acos((texture1.transform * normalUv).xy) / 3.14159, texture1.clampMin, texture1.clampMax)).rgb*texture(texture0, uv0).rgb*vertexColor0.rgb*clamp((individualLightColors[0].rgb + color_GxAmbientColor0), 0, 1)*vec3(0.5)*vec3(2)), 0, 1)*vec3(2), 0, 1);

float alphaComponent = vertexColor0.a;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void main() {
individualLightColors[i] = lightColor;
}

vec3 colorComponent = clamp(clamp((clamp(vec3(texture(texture1.sampler, clamp((texture1.transform * (acos(normalUv) / 3.14159)).xy, texture1.clampMin, texture1.clampMax)).a)*clamp(vertexColor0.rgb*clamp((individualLightColors[0].rgb + color_GxAmbientColor0), 0, 1)*texture(texture0, uv0).rgb, 0, 1), 0, 1) + vertexColor0.rgb*clamp((individualLightColors[0].rgb + color_GxAmbientColor0), 0, 1)*texture(texture2, uv0).rgb), 0, 1)*texture(texture3.sampler, clamp((texture3.transform * uv1).xy, texture3.clampMin, texture3.clampMax)).rgb*vec3(2), 0, 1);
vec3 colorComponent = clamp(clamp((clamp(vec3(texture(texture1.sampler, clamp(acos((texture1.transform * normalUv).xy) / 3.14159, texture1.clampMin, texture1.clampMax)).a)*clamp(vertexColor0.rgb*clamp((individualLightColors[0].rgb + color_GxAmbientColor0), 0, 1)*texture(texture0, uv0).rgb, 0, 1), 0, 1) + vertexColor0.rgb*clamp((individualLightColors[0].rgb + color_GxAmbientColor0), 0, 1)*texture(texture2, uv0).rgb), 0, 1)*texture(texture3.sampler, clamp((texture3.transform * uv1).xy, texture3.clampMin, texture3.clampMax)).rgb*vec3(2), 0, 1);

float alphaComponent = vertexColor0.a*texture(texture2, uv0).a;

Expand Down
Loading

0 comments on commit e49174c

Please sign in to comment.