Skip to content

Commit

Permalink
Fixed one more issue with an extra line in some GLSL files.
Browse files Browse the repository at this point in the history
  • Loading branch information
MeltyPlayer committed Dec 3, 2024
1 parent 5bbcb45 commit 611b1b2
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using fin.model;
using fin.model.impl;
using fin.model.util;
using fin.util.image;

using NUnit.Framework;

Expand All @@ -13,10 +14,29 @@
namespace fin.shaders.glsl;

public class StandardShaderSourceGlslTests {
[Test]
public void TestWithoutNormalsNoMask()
=> AssertGlsl_(
false,
false,
(m, t) => { },
"""
#version 430
out vec4 fragColor;
in vec4 vertexColor0;
void main() {
fragColor = vertexColor0;
}
""");

[Test]
public void TestWithoutNormalsNoTextures()
=> AssertGlsl_(
false,
true,
(m, t) => { },
"""
#version 430
Expand All @@ -38,6 +58,7 @@ void main() {
public void TestWithoutNormalsDiffuseOnly()
=> AssertGlsl_(
false,
true,
(m, t) => m.DiffuseTexture = t,
"""
#version 430
Expand All @@ -63,6 +84,7 @@ void main() {
public void TestWithoutNormalsEmissiveOnly()
=> AssertGlsl_(
false,
true,
(m, t) => m.EmissiveTexture = t,
"""
#version 430
Expand Down Expand Up @@ -90,29 +112,30 @@ void main() {
[Test]
public void TestWithNormalsNoTextures()
=> AssertGlsl_(
true,
true,
(m, t) => { },
$$"""
#version 430
{{GlslUtil.GetLightHeader(true)}}
uniform float shininess;
out vec4 fragColor;
in vec4 vertexColor0;
in vec3 vertexPosition;
in vec3 vertexNormal;
in vec3 tangent;
in vec3 binormal;
{{GlslUtil.GetGetIndividualLightColorsFunction()}}
{{GlslUtil.GetGetMergedLightColorsFunction()}}
{{GlslUtil.GetApplyMergedLightColorsFunction(false)}}
void main() {
fragColor = vertexColor0;
Expand All @@ -129,73 +152,75 @@ void main() {
[Test]
public void TestWithNormalsDiffuseOnly()
=> AssertGlsl_(
true,
true,
(m, t) => m.DiffuseTexture = t,
$$"""
#version 430
{{GlslUtil.GetLightHeader(true)}}
#version 430
uniform sampler2D diffuseTexture;
uniform float shininess;
out vec4 fragColor;
{{GlslUtil.GetLightHeader(true)}}
in vec4 vertexColor0;
in vec3 vertexPosition;
in vec3 vertexNormal;
in vec3 tangent;
in vec3 binormal;
in vec2 uv0;
uniform sampler2D diffuseTexture;
uniform float shininess;
{{GlslUtil.GetGetIndividualLightColorsFunction()}}
{{GlslUtil.GetGetMergedLightColorsFunction()}}
{{GlslUtil.GetApplyMergedLightColorsFunction(false)}}
void main() {
vec4 diffuseColor = texture(diffuseTexture, uv0);
fragColor = diffuseColor * vertexColor0;
// Have to renormalize because the vertex normals can become distorted when interpolated.
vec3 fragNormal = normalize(vertexNormal);
fragColor.rgb = mix(fragColor.rgb, applyMergedLightingColors(vertexPosition, fragNormal, shininess, fragColor, vec4(1)).rgb, useLighting);
if (fragColor.a < .95) {
discard;
out vec4 fragColor;
in vec4 vertexColor0;
in vec3 vertexPosition;
in vec3 vertexNormal;
in vec3 tangent;
in vec3 binormal;
in vec2 uv0;
{{GlslUtil.GetGetIndividualLightColorsFunction()}}
{{GlslUtil.GetGetMergedLightColorsFunction()}}
{{GlslUtil.GetApplyMergedLightColorsFunction(false)}}
void main() {
vec4 diffuseColor = texture(diffuseTexture, uv0);
fragColor = diffuseColor * vertexColor0;
// Have to renormalize because the vertex normals can become distorted when interpolated.
vec3 fragNormal = normalize(vertexNormal);
fragColor.rgb = mix(fragColor.rgb, applyMergedLightingColors(vertexPosition, fragNormal, shininess, fragColor, vec4(1)).rgb, useLighting);
if (fragColor.a < .95) {
discard;
}
}
}
""");
""");

[Test]
public void TestWithNormalsEmissiveOnly()
=> AssertGlsl_(
true,
true,
(m, t) => m.EmissiveTexture = t,
$$"""
#version 430
{{GlslUtil.GetLightHeader(true)}}
uniform sampler2D emissiveTexture;
uniform float shininess;
out vec4 fragColor;
in vec4 vertexColor0;
in vec3 vertexPosition;
in vec3 vertexNormal;
in vec3 tangent;
in vec3 binormal;
in vec2 uv0;
{{GlslUtil.GetGetIndividualLightColorsFunction()}}
{{GlslUtil.GetGetMergedLightColorsFunction()}}
{{GlslUtil.GetApplyMergedLightColorsFunction(false)}}
void main() {
fragColor = vertexColor0;
Expand All @@ -215,6 +240,7 @@ void main() {

private static void AssertGlsl_(
bool withNormals,
bool masked,
Action<IStandardMaterial, IReadOnlyTexture> createMaterial,
string expectedSource) {
var model = ModelImpl.CreateForViewer();
Expand All @@ -234,6 +260,9 @@ private static void AssertGlsl_(
skin.AddMesh().AddPoints(v).SetMaterial(material);
}

material.TransparencyType
= masked ? TransparencyType.MASK : TransparencyType.OPAQUE;

var actualSource = new StandardShaderSourceGlsl(
model,
material,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ void main() {

fragmentShaderSrc.AppendLine(
$" fragColor = {(hasDiffuseTexture ? "diffuseColor * " : "")}{GlslConstants.IN_VERTEX_COLOR_NAME}0;");
fragmentShaderSrc.AppendLine();

if (hasNormals) {
fragmentShaderSrc.AppendLine();
if (hasAmbientOcclusionTexture) {
fragmentShaderSrc.AppendLine(
$" vec4 ambientOcclusionColor = {GlslUtil.ReadColorFromTexture("ambientOcclusionTexture", $"{GlslConstants.IN_UV_NAME}{ambientOcclusionTexture?.UvIndex ?? 0}", ambientOcclusionTexture, animations)};");
Expand All @@ -168,21 +168,21 @@ void main() {
// TODO: Is this right?
fragmentShaderSrc.AppendLine(
$" fragColor.rgb = mix(fragColor.rgb, applyMergedLightingColors(vertexPosition, fragNormal, {GlslConstants.UNIFORM_SHININESS_NAME}, fragColor, {(hasSpecularTexture ? $"{GlslUtil.ReadColorFromTexture("specularTexture", "uv0", specularTexture, animations)}" : "vec4(1)")}{(hasAmbientOcclusionTexture ? ", ambientOcclusionColor.r" : "")}).rgb, {GlslConstants.UNIFORM_USE_LIGHTING_NAME});");
fragmentShaderSrc.AppendLine();
}

if (hasEmissiveTexture) {
fragmentShaderSrc.AppendLine();
fragmentShaderSrc.AppendLine(
$" vec4 emissiveColor = {GlslUtil.ReadColorFromTexture("emissiveTexture", $"{GlslConstants.IN_UV_NAME}{emissiveTexture?.UvIndex ?? 0}", emissiveTexture, animations)};");
fragmentShaderSrc.AppendLine(
"""
fragColor.rgb += emissiveColor.rgb;
fragColor.rgb = min(fragColor.rgb, 1);
""");
fragmentShaderSrc.AppendLine();
}

if (material.TransparencyType == TransparencyType.MASK) {
fragmentShaderSrc.AppendLine();
fragmentShaderSrc.AppendLine(
$$"""
if (fragColor.a < {{GlslConstants.MIN_ALPHA_BEFORE_DISCARD_TEXT}}) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ in vec4 vertexColor0;

void main() {
fragColor = vertexColor0;

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ in vec4 vertexColor0;

void main() {
fragColor = vertexColor0;

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ in vec4 vertexColor0;

void main() {
fragColor = vertexColor0;

}

0 comments on commit 611b1b2

Please sign in to comment.