Skip to content

Commit

Permalink
improved generic roughness
Browse files Browse the repository at this point in the history
  • Loading branch information
4sval committed Dec 31, 2022
1 parent 9cf6c32 commit 9a0e6aa
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 88 deletions.
31 changes: 12 additions & 19 deletions FModel/Resources/default.frag
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ struct Parameters
bool HasAo;

vec4 EmissiveRegion;
float Specular;
float Roughness;
float RoughnessMin;
float RoughnessMax;
float EmissiveMult;
float UVScale;
};

struct BaseLight
Expand Down Expand Up @@ -101,19 +100,14 @@ int LayerToIndex()
return clamp(int(fTexLayer), 0, uUvCount - 1);
}

vec2 ScaledTexCoords()
{
return fTexCoords * uParameters.UVScale;
}

vec4 SamplerToVector(sampler2D s, vec2 coords)
{
return texture(s, coords);
}

vec4 SamplerToVector(sampler2D s)
{
return SamplerToVector(s, ScaledTexCoords());
return SamplerToVector(s, fTexCoords);
}

vec3 ComputeNormals(int layer)
Expand Down Expand Up @@ -153,7 +147,7 @@ vec3 CalcLight(int layer, vec3 normals, vec3 position, vec3 color, float attenua
{
vec3 fLambert = SamplerToVector(uParameters.Diffuse[layer].Sampler).rgb * uParameters.Diffuse[layer].Color.rgb;
vec3 specular_masks = SamplerToVector(uParameters.SpecularMasks[layer].Sampler).rgb;
float roughness = max(0.0f, specular_masks.b * uParameters.Roughness);
float roughness = mix(uParameters.RoughnessMin, uParameters.RoughnessMax, specular_masks.b);

vec3 l = normalize(uViewPos - fPos);

Expand All @@ -174,11 +168,11 @@ vec3 CalcLight(int layer, vec3 normals, vec3 position, vec3 color, float attenua

vec3 specBrdfNom = ggxDistribution(roughness, nDotH) * geomSmith(roughness, nDotL) * geomSmith(roughness, nDotV) * f;
float specBrdfDenom = 4.0 * nDotV * nDotL + 0.0001;
vec3 specBrdf = uParameters.Specular * specular_masks.r * specBrdfNom / specBrdfDenom;
vec3 specBrdf = specBrdfNom / specBrdfDenom;

vec3 diffuseBrdf = fLambert;
if (!global) diffuseBrdf = kD * fLambert / PI;
return (diffuseBrdf + specBrdf) * color * nDotL * attenuation;
return (diffuseBrdf + specBrdf) * color * attenuation * nDotL;
}

vec3 CalcBaseLight(int layer, vec3 normals, BaseLight base, float attenuation, bool global)
Expand Down Expand Up @@ -223,13 +217,11 @@ void main()
}
else if (bVertexColors[3])
{
FragColor = vec4(fNormal, 1);
int layer = LayerToIndex();
vec3 normals = ComputeNormals(layer);
FragColor = vec4(normals, 1);
}
else if (bVertexColors[4])
{
FragColor = vec4(fTangent, 1);
}
else if (bVertexColors[5])
{
FragColor = vec4(fTexCoords, 0, 1);
}
Expand All @@ -248,10 +240,11 @@ void main()
vec3 color = uParameters.Ao.ColorBoost.Color * uParameters.Ao.ColorBoost.Exponent;
result = mix(result, result * color, m.b);
}
result = mix(result * m.r * uParameters.Ao.AmbientOcclusion, result, m.g);
result = vec3(uParameters.Ao.AmbientOcclusion) * result * m.r;
result += CalcLight(layer, normals, vec3(0.0), vec3(0.25), m.g, false);
}

vec2 coords = ScaledTexCoords();
vec2 coords = fTexCoords;
if (coords.x > uParameters.EmissiveRegion.x &&
coords.y > uParameters.EmissiveRegion.y &&
coords.x < uParameters.EmissiveRegion.z &&
Expand Down
2 changes: 1 addition & 1 deletion FModel/Views/Snooper/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void Render(Camera cam)
if (ShowGrid) _grid.Render(viewMatrix, projMatrix, cam.Near, cam.Far);

_shader.Render(viewMatrix, cam.Position, projMatrix);
for (int i = 0; i < 6; i++)
for (int i = 0; i < 5; i++)
_shader.SetUniform($"bVertexColors[{i}]", i == VertexColor);

// render model pass
Expand Down
40 changes: 17 additions & 23 deletions FModel/Views/Snooper/Shading/Material.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ public class Material : IDisposable
public AoParams Ao;
public bool HasAo;

public float Specular = 1f;
public float Roughness = 0.5f;
public float RoughnessMin = 0f;
public float RoughnessMax = 1f;
public float EmissiveMult = 1f;
public float UVScale = 1f;

public Material()
{
Expand Down Expand Up @@ -106,24 +105,22 @@ public void Setup(Options options, int uvCount)
}
}

// scalars
if (Parameters.TryGetScalar(out var specular, "Specular", "Specular Intensity", "Spec"))
Specular = specular;

if (Parameters.TryGetScalar(out var roughnessMin, "RoughnessMin", "SpecRoughnessMin") &&
Parameters.TryGetScalar(out var roughnessMax, "RoughnessMax", "SpecRoughnessMax"))
Roughness = (roughnessMin + roughnessMax) / 2f;
if (Parameters.TryGetScalar(out var roughnessMin, "RoughnessMin", "SpecRoughnessMin"))
RoughnessMin = roughnessMin;
if (Parameters.TryGetScalar(out var roughnessMax, "RoughnessMax", "SpecRoughnessMax"))
RoughnessMax = roughnessMax;
if (Parameters.TryGetScalar(out var roughness, "Rough", "Roughness", "Ro Multiplier", "RO_mul", "Roughness_Mult"))
Roughness = roughness;
{
var d = roughness / 2;
RoughnessMin = roughness - d;
RoughnessMax = roughness + d;
}

if (Parameters.TryGetScalar(out var emissiveMultScalar, "emissive mult", "Emissive_Mult", "EmissiveIntensity", "EmissionIntensity"))
EmissiveMult = emissiveMultScalar;
else if (Parameters.TryGetLinearColor(out var emissiveMultColor, "Emissive Multiplier", "EmissiveMultiplier"))
EmissiveMult = emissiveMultColor.R;

if (Parameters.TryGetScalar(out var uvScale, "UV Scale"))
UVScale = uvScale;

if (Parameters.TryGetLinearColor(out var EmissiveUVs,
"EmissiveUVs_RG_UpperLeftCorner_BA_LowerRightCorner",
"Emissive Texture UVs RG_TopLeft BA_BottomRight",
Expand Down Expand Up @@ -228,10 +225,9 @@ public void Render(Shader shader)
shader.SetUniform("uParameters.HasAo", HasAo);

shader.SetUniform("uParameters.EmissiveRegion", EmissiveRegion);
shader.SetUniform("uParameters.Specular", Specular);
shader.SetUniform("uParameters.Roughness", Roughness);
shader.SetUniform("uParameters.RoughnessMin", RoughnessMin);
shader.SetUniform("uParameters.RoughnessMax", RoughnessMax);
shader.SetUniform("uParameters.EmissiveMult", EmissiveMult);
shader.SetUniform("uParameters.UVScale", UVScale);
}

private const string _mult = "x %.2f";
Expand All @@ -244,14 +240,12 @@ public void ImGuiParameters()
if (ImGui.BeginTable("parameters", 2))
{
var id = 1;
SnimGui.Layout("Specular");ImGui.PushID(id++);
ImGui.DragFloat("", ref Specular, _step, _zero, 1.0f, _mult, _clamp);
ImGui.PopID();SnimGui.Layout("Roughness");ImGui.PushID(id++);
ImGui.DragFloat("", ref Roughness, _step, _zero, 1.0f, _mult, _clamp);
SnimGui.Layout("Roughness Min");ImGui.PushID(id++);
ImGui.DragFloat("", ref RoughnessMin, _step, _zero, 1.0f, _mult, _clamp);
ImGui.PopID();SnimGui.Layout("Roughness Max");ImGui.PushID(id++);
ImGui.DragFloat("", ref RoughnessMax, _step, _zero, 1.0f, _mult, _clamp);
ImGui.PopID();SnimGui.Layout("Emissive Multiplier");ImGui.PushID(id++);
ImGui.DragFloat("", ref EmissiveMult, _step, _zero, _infinite, _mult, _clamp);
ImGui.PopID();SnimGui.Layout("UV Scale");ImGui.PushID(id++);
ImGui.DragFloat("", ref UVScale, _step, _zero, _infinite, _mult, _clamp);
ImGui.PopID();

if (HasAo)
Expand Down
4 changes: 3 additions & 1 deletion FModel/Views/Snooper/Shading/Texture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,13 @@ public Texture(string[] textures) : this(TextureType.Cubemap)
ProcessPixels(textures[t], TextureTarget.TextureCubeMapPositiveX + t);
}

GL.TexParameter(_target, TextureParameterName.TextureMinFilter, (int) TextureMinFilter.Linear);
GL.TexParameter(_target, TextureParameterName.TextureMinFilter, (int) TextureMinFilter.LinearMipmapLinear);
GL.TexParameter(_target, TextureParameterName.TextureMagFilter, (int) TextureMinFilter.Linear);
GL.TexParameter(_target, TextureParameterName.TextureWrapR, (int) TextureWrapMode.ClampToEdge);
GL.TexParameter(_target, TextureParameterName.TextureWrapS, (int) TextureWrapMode.ClampToEdge);
GL.TexParameter(_target, TextureParameterName.TextureWrapT, (int) TextureWrapMode.ClampToEdge);

GL.GenerateMipmap(GenerateMipmapTarget.TextureCubeMap);
}

public Texture(string texture) : this(TextureType.Normal)
Expand Down
63 changes: 21 additions & 42 deletions FModel/Views/Snooper/Shading/TextureHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@ public static class TextureHelper
private static readonly string _game = Services.ApplicationService.ApplicationView.CUE4Parse.Provider.GameName;

/// <summary>
/// Red : Specular (if possible)
/// Blue : Roughness
/// Red : Specular (not used anymore)
/// Green : Metallic
/// Blue : Roughness
/// </summary>
public static void FixChannels(UTexture2D o, FTexture2DMipMap mip, ref byte[] data)
{
// only if it makes a big difference pls
switch (_game)
{
case "hk_project":
case "gameface":
case "divineknockout":
{
unsafe
{
Expand All @@ -34,10 +32,11 @@ public static void FixChannels(UTexture2D o, FTexture2DMipMap mip, ref byte[] da
}
break;
}
// R: Roughness
// G: Metallic
// R: Metallic
// G: Roughness
// B: Whatever (AO / S / E / ...)
case "ccff7r":
case "shootergame":
case "divineknockout":
{
unsafe
{
Expand All @@ -46,51 +45,31 @@ public static void FixChannels(UTexture2D o, FTexture2DMipMap mip, ref byte[] da
{
for (var i = 0; i < mip.SizeX * mip.SizeY; i++)
{
(d[offset + 1], d[offset + 2]) = (d[offset + 2], d[offset + 1]); // RBG
(d[offset], d[offset + 1]) = (d[offset + 1], d[offset]); // BRG
(d[offset], d[offset + 1]) = (d[offset + 1], d[offset]); // GRB
(d[offset], d[offset + 2]) = (d[offset + 2], d[offset]); // RBG
offset += 4;
}
}
}
break;
}
case "shootergame":
// R: Roughness
// G: Metallic
// B: Whatever (AO / S / E / ...)
case "ccff7r":
{
var packedPBRType = o.Name[(o.Name.LastIndexOf('_') + 1)..];
switch (packedPBRType)
unsafe
{
case "MRAE": // R: Metallic, G: Roughness, B: AO (0-127) & Emissive (128-255) (Character PBR)
unsafe
{
var offset = 0;
fixed (byte* d = data)
{
for (var i = 0; i < mip.SizeX * mip.SizeY; i++)
{
(d[offset], d[offset + 1]) = (d[offset + 1], d[offset]); // RMAE
// (d[offset], d[offset + 2]) = (d[offset + 2], d[offset]); // AEMR
offset += 4;
}
}
}
break;
case "MRAS": // R: Metallic, G: Roughness, B: AO, A: Specular (Legacy PBR)
case "MRA": // R: Metallic, G: Roughness, B: AO (Environment PBR)
case "MRS": // R: Metallic, G: Roughness, B: Specular (Weapon PBR)
unsafe
var offset = 0;
fixed (byte* d = data)
{
for (var i = 0; i < mip.SizeX * mip.SizeY; i++)
{
var offset = 0;
fixed (byte* d = data)
{
for (var i = 0; i < mip.SizeX * mip.SizeY; i++)
{
(d[offset], d[offset + 2]) = (d[offset + 2], d[offset]); // SRM
(d[offset + 1], d[offset + 2]) = (d[offset + 2], d[offset + 1]); // SMR
offset += 4;
}
}
(d[offset + 1], d[offset + 2]) = (d[offset + 2], d[offset + 1]); // RBG
(d[offset], d[offset + 1]) = (d[offset + 1], d[offset]); // BRG
offset += 4;
}
break;
}
}
break;
}
Expand Down
2 changes: 1 addition & 1 deletion FModel/Views/Snooper/SnimGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private void DrawWorld(Snooper s)
ImGui.Checkbox("", ref s.Renderer.ShowLights);
ImGui.PopID();Layout("Vertex Colors");ImGui.PushID(4);
ImGui.Combo("vertex_colors", ref s.Renderer.VertexColor,
"Default\0Diffuse Only\0Colors\0Normals\0Tangent\0Texture Coordinates\0");
"Default\0Diffuse Only\0Colors\0Normals\0Texture Coordinates\0");
ImGui.PopID();

ImGui.EndTable();
Expand Down

0 comments on commit 9a0e6aa

Please sign in to comment.