Skip to content

Commit

Permalink
Fixed some more reflection/bump map bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
MeltyPlayer committed Dec 21, 2024
1 parent b0f9d05 commit 333ce48
Show file tree
Hide file tree
Showing 162 changed files with 914 additions and 1,339 deletions.
41 changes: 38 additions & 3 deletions FinModelUtility/Fin/Fin/src/image/BumpMapUtils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using fin.image.formats;
using fin.model;

using SixLabors.ImageSharp.PixelFormats;

Expand All @@ -10,7 +11,9 @@ public static class BumpMapUtils {
/// https://stackoverflow.com/questions/10652797/whats-the-logic-behind-creating-a-normal-map-from-a-texture
/// </summary>
public static unsafe Rgb24Image ConvertBumpMapImageToNormalImage(
IReadOnlyImage image) {
IReadOnlyImage image,
WrapMode wrapModeS,
WrapMode wrapModeT) {
var normalImage =
new Rgb24Image(PixelFormat.RGB888, image.Width, image.Height);
using var normalImageLock = normalImage.UnsafeLock();
Expand All @@ -36,6 +39,14 @@ public static unsafe Rgb24Image ConvertBumpMapImageToNormalImage(
out _,
out _,
out _);
} else if (wrapModeS is WrapMode.REPEAT) {
bumpGetHandler(
image.Width - 1,
y,
out leftIntensity,
out _,
out _,
out _);
} else {
leftIntensity = centerIntensity;
}
Expand All @@ -49,6 +60,14 @@ public static unsafe Rgb24Image ConvertBumpMapImageToNormalImage(
out _,
out _,
out _);
} else if (wrapModeS is WrapMode.REPEAT) {
bumpGetHandler(
0,
y,
out rightIntensity,
out _,
out _,
out _);
} else {
rightIntensity = centerIntensity;
}
Expand All @@ -62,6 +81,14 @@ public static unsafe Rgb24Image ConvertBumpMapImageToNormalImage(
out _,
out _,
out _);
} else if (wrapModeT is WrapMode.REPEAT) {
bumpGetHandler(
x,
image.Height - 1,
out upIntensity,
out _,
out _,
out _);
} else {
upIntensity = centerIntensity;
}
Expand All @@ -75,15 +102,23 @@ public static unsafe Rgb24Image ConvertBumpMapImageToNormalImage(
out _,
out _,
out _);
} else if (wrapModeT is WrapMode.REPEAT) {
bumpGetHandler(
x,
0,
out downIntensity,
out _,
out _,
out _);
} else {
downIntensity = centerIntensity;
}

var xIntensity
= ((leftIntensity / 255f - rightIntensity / 255f + 1) * .5f) *
= ((rightIntensity / 255f - leftIntensity / 255f + 1) * .5f) *
255;
var yIntensity
= ((upIntensity / 255f - downIntensity / 255f + 1) * .5f) * 255;
= ((downIntensity / 255f - upIntensity / 255f + 1) * .5f) * 255;

normalImageScan0[y * image.Width + x]
= new Rgb24((byte) xIntensity, (byte) yIntensity, 255);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using fin.model;
using fin.shaders.glsl;
using fin.util.asserts;
using fin.util.enumerables;
using fin.util.image;

namespace fin.language.equations.fixedFunction;
Expand Down Expand Up @@ -36,6 +37,13 @@ public void Print(
sb.AppendLine(GlslConstants.FLOAT_PRECISION);
sb.AppendLine();

var usesSphericalReflectionMapping
= shaderRequirements.UsesSphericalReflectionMapping;
if (usesSphericalReflectionMapping) {
sb.AppendLine(GlslUtil.GetMatricesHeader(model));
sb.AppendLine();
}

var hasIndividualLights =
Enumerable
.Range(0, MaterialConstants.MAX_LIGHTS)
Expand All @@ -60,6 +68,7 @@ public void Print(
]);

var dependsOnLights = dependsOnMergedLights || dependsOnAnIndividualLight;
var dependsOnNormals = dependsOnLights || usesSphericalReflectionMapping;

var dependsOnAmbientLight = equations.DoOutputsDependOn(
[
Expand All @@ -83,7 +92,8 @@ public void Print(
dependsOnIndividualTextures
.Select((dependsOnTexture, i) => (i, dependsOnTexture))
.Where(tuple => tuple.dependsOnTexture)
.Select(tuple => textures[tuple.i]),
.Select(tuple => textures[tuple.i])
.ConcatIfNonnull(material.NormalTexture),
this.animations_);

var hadUniform = false;
Expand Down Expand Up @@ -126,21 +136,13 @@ public void Print(
}
};

if (shaderRequirements.UsesSphericalReflectionMapping) {
AppendLineBetweenUniformsAndIns();
sb.AppendLine(
$"in vec2 {GlslConstants.IN_SPHERICAL_REFLECTION_UV_NAME};");
}

if (shaderRequirements.UsesLinearReflectionMapping) {
if (dependsOnLights) {
AppendLineBetweenUniformsAndIns();
sb.AppendLine(
$"in vec2 {GlslConstants.IN_LINEAR_REFLECTION_UV_NAME};");
sb.AppendLine("in vec3 vertexPosition;");
}

if (dependsOnLights) {
if (dependsOnNormals) {
AppendLineBetweenUniformsAndIns();
sb.AppendLine("in vec3 vertexPosition;");
sb.AppendLine("in vec3 vertexNormal;");

if (hasNormalTexture &&
Expand Down Expand Up @@ -193,7 +195,7 @@ public void Print(
sb.AppendLine("void main() {");

// Calculate lighting
if (dependsOnLights) {
if (dependsOnNormals) {
if (!hasNormalTexture) {
sb.AppendLine(
"""
Expand All @@ -204,10 +206,10 @@ public void Print(
} else {
sb.AppendLine(
"""
// Have to renormalize because the vertex normals can become distorted when interpolated.
vec3 fragNormal = normalize(vertexNormal);
// Have to renormalize because the vertex normals can become distorted when interpolated.
vec3 fragNormal = normalize(vertexNormal);
""");
""");

if (shaderRequirements.TangentType is TangentType.CALCULATED) {
// Shamelessly stolen from:
Expand All @@ -234,6 +236,13 @@ var texCoordName
""");
}

if (usesSphericalReflectionMapping) {
sb.AppendLine($"""
vec2 {GlslConstants.IN_SPHERICAL_REFLECTION_UV_NAME} = acos(normalize({GlslConstants.UNIFORM_PROJECTION_MATRIX_NAME} * {GlslConstants.UNIFORM_VIEW_MATRIX_NAME} * vec4(fragNormal, 0)).xy) / 3.14159;
""");
}

// TODO: Optimize this if the shader depends on merged lighting as well as individual lights for some reason.
if (dependsOnAnIndividualLight) {
sb.AppendLine(
Expand Down Expand Up @@ -897,12 +906,6 @@ private string GetTextureValue_(int textureIndex,
GlslConstants.IN_SPHERICAL_REFLECTION_UV_NAME,
texture,
this.animations_),
UvType.LINEAR
=> GlslUtil.ReadColorFromTexture(
textureName,
GlslConstants.IN_LINEAR_REFLECTION_UV_NAME,
texture,
this.animations_),
};
}

Expand Down
2 changes: 1 addition & 1 deletion FinModelUtility/Fin/Fin/src/model/MaterialInterfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ IFixedFunctionMaterial SetAlphaCompare(
public enum UvType {
STANDARD,
SPHERICAL,
LINEAR,
LINEAR
}

public enum WrapMode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ public override bool Equals(object? other) {
return this.Name == otherTexture.Name &&
this.Image == otherTexture.Image &&
this.WrapModeU == otherTexture.WrapModeU &&
this.WrapModeV == otherTexture.WrapModeV;
this.WrapModeV == otherTexture.WrapModeV &&
this.UvType == otherTexture.UvType &&
this.UvIndex == otherTexture.UvIndex;
}

return false;
Expand Down
1 change: 0 additions & 1 deletion FinModelUtility/Fin/Fin/src/shaders/glsl/GlslConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public static class GlslConstants {
public const string IN_UV_NAME = "uv";
public const string IN_VERTEX_COLOR_NAME = "vertexColor";
public const string IN_SPHERICAL_REFLECTION_UV_NAME = "sphericalReflectionUv";
public const string IN_LINEAR_REFLECTION_UV_NAME = "linearReflectionUv";

public const float MIN_ALPHA_BEFORE_DISCARD = .95f;
public const string MIN_ALPHA_BEFORE_DISCARD_TEXT = ".95";
Expand Down
Loading

0 comments on commit 333ce48

Please sign in to comment.