good enough for now

This commit is contained in:
4sval
2022-10-27 15:50:15 +02:00
parent dfea705c5e
commit 12eba5e730
9 changed files with 55 additions and 43 deletions

View File

@@ -3,12 +3,13 @@
in vec3 fPos;
in vec3 fNormal;
in vec2 fTexCoords;
in float fTexIndex;
in vec4 fColor;
struct Material {
sampler2D diffuseMap;
sampler2D normalMap;
sampler2D specularMap;
sampler2D diffuseMap[4];
sampler2D normalMap[4];
sampler2D specularMap[4];
sampler2D emissionMap;
bool useSpecularMap;
@@ -36,9 +37,21 @@ uniform bool display_vertex_colors;
out vec4 FragColor;
vec4 getValueFromSamplerArray(sampler2D array[4]) {
if (fTexIndex < 1.0) {
return texture(array[0], fTexCoords);
} if (fTexIndex < 2.0) {
return texture(array[2], fTexCoords);
} if (fTexIndex < 3.0) {
return texture(array[1], fTexCoords);
} else {
return texture(array[3], fTexCoords);
}
}
vec3 getNormalFromMap()
{
vec3 tangentNormal = texture(material.normalMap, fTexCoords).xyz * 2.0 - 1.0;
vec3 tangentNormal = getValueFromSamplerArray(material.normalMap).xyz * 2.0 - 1.0;
vec3 q1 = dFdx(fPos);
vec3 q2 = dFdy(fPos);
@@ -73,10 +86,10 @@ void main()
}
else
{
vec3 result = light.ambient * vec3(texture(material.diffuseMap, fTexCoords));
vec3 result = light.ambient * vec3(getValueFromSamplerArray(material.diffuseMap));
// diffuse
vec4 diffuse_map = texture(material.diffuseMap, fTexCoords);
vec4 diffuse_map = getValueFromSamplerArray(material.diffuseMap);
result += light.diffuse * diff * diffuse_map.rgb;
// specular
@@ -85,7 +98,7 @@ void main()
vec3 n_view_direction = normalize(viewPos - fPos);
vec3 reflect_direction = reflect(-n_light_direction, n_normal_map);
float metallic = pow(max(dot(n_view_direction, reflect_direction), 0.0f), material.metallic_value);
vec3 specular_map = vec3(texture(material.specularMap, fTexCoords));
vec3 specular_map = vec3(getValueFromSamplerArray(material.specularMap));
result += specular_map.r * light.specular * (metallic * specular_map.b);
result += material.roughness_value * specular_map.g;
}

View File

@@ -3,11 +3,12 @@
layout (location = 1) in vec3 vPos;
layout (location = 2) in vec3 vNormal;
layout (location = 3) in vec2 vTexCoords;
layout (location = 4) in vec4 vColor;
layout (location = 5) in ivec4 vBoneIds;
layout (location = 6) in vec4 vWeights;
layout (location = 7) in mat4 vInstanceMatrix;
layout (location = 11) in vec3 vMorphTarget;
layout (location = 4) in float vTexIndex;
layout (location = 5) in vec4 vColor;
layout (location = 6) in ivec4 vBoneIds;
layout (location = 7) in vec4 vWeights;
layout (location = 8) in mat4 vInstanceMatrix;
layout (location = 12) in vec3 vMorphTarget;
uniform mat4 uView;
uniform mat4 uProjection;
@@ -16,6 +17,7 @@ uniform float uMorphTime;
out vec3 fPos;
out vec3 fNormal;
out vec2 fTexCoords;
out float fTexIndex;
out vec4 fColor;
void main()
@@ -26,5 +28,6 @@ void main()
fPos = vec3(vInstanceMatrix * vec4(pos, 1.0));
fNormal = mat3(transpose(inverse(vInstanceMatrix))) * vNormal;
fTexCoords = vTexCoords;
fTexIndex = vTexIndex;
fColor = vColor;
}

View File

@@ -2,8 +2,8 @@
layout (location = 1) in vec3 vPos;
layout (location = 2) in vec3 vNormal;
layout (location = 7) in mat4 vInstanceMatrix;
layout (location = 11) in vec3 vMorphTarget;
layout (location = 8) in mat4 vInstanceMatrix;
layout (location = 12) in vec3 vMorphTarget;
uniform mat4 uView;
uniform mat4 uProjection;

View File

@@ -98,19 +98,19 @@ public class Material : IDisposable
for (var i = 0; i < Diffuse.Length; i++)
{
shader.SetUniform("material.diffuseMap", unit);
shader.SetUniform($"material.diffuseMap[{i}]", unit);
Diffuse[i]?.Bind(TextureUnit.Texture0 + unit++);
}
for (var i = 0; i < Normals.Length; i++)
{
shader.SetUniform("material.normalMap", unit);
shader.SetUniform($"material.normalMap[{i}]", unit);
Normals[i]?.Bind(TextureUnit.Texture0 + unit++);
}
for (var i = 0; i < SpecularMasks.Length; i++)
{
shader.SetUniform("material.specularMap", unit);
shader.SetUniform($"material.specularMap[{i}]", unit);
SpecularMasks[i]?.Bind(TextureUnit.Texture0 + unit++);
}

View File

@@ -21,7 +21,7 @@ public class Model : IDisposable
private BufferObject<Matrix4x4> _matrixVbo;
private VertexArrayObject<float, uint> _vao;
private readonly int _vertexSize = 9; // VertexIndex + Position + Normal + UV
private readonly int _vertexSize = 10; // VertexIndex + Position + Normal + UV + TextureIndex
private readonly uint[] _facesIndex = { 1, 0, 2 };
private const int _faceSize = 3; // just so we don't have to do .Length
@@ -79,7 +79,7 @@ public class Model : IDisposable
for (int m = 0; m < Materials.Length; m++)
{
if (materials[m].TryLoad(out var material) && material is UMaterialInterface unrealMaterial)
Materials[m] = new Material(1, unrealMaterial); // lod.NumTexCoords
Materials[m] = new Material(lod.NumTexCoords, unrealMaterial); // lod.NumTexCoords
}
if (lod.VertexColors is { Length: > 0})
@@ -124,6 +124,7 @@ public class Model : IDisposable
Vertices[baseIndex + count++] = vert.Normal.Y;
Vertices[baseIndex + count++] = vert.UV.U;
Vertices[baseIndex + count++] = vert.UV.V;
Vertices[baseIndex + count++] = lod.ExtraUV.IsValueCreated ? lod.ExtraUV.Value[0][indice].U : .5f;
if (HasVertexColors)
{
@@ -184,9 +185,10 @@ public class Model : IDisposable
_vao.VertexAttributePointer(1, 3, VertexAttribPointerType.Float, _vertexSize, 1); // position
_vao.VertexAttributePointer(2, 3, VertexAttribPointerType.Float, _vertexSize, 4); // normal
_vao.VertexAttributePointer(3, 2, VertexAttribPointerType.Float, _vertexSize, 7); // uv
_vao.VertexAttributePointer(4, 4, VertexAttribPointerType.Float, _vertexSize, 9); // color
_vao.VertexAttributePointer(5, 4, VertexAttribPointerType.Int, _vertexSize, 13); // boneids
_vao.VertexAttributePointer(6, 4, VertexAttribPointerType.Float, _vertexSize, 17); // boneweights
_vao.VertexAttributePointer(4, 1, VertexAttribPointerType.Float, _vertexSize, 9); // texture index
_vao.VertexAttributePointer(5, 4, VertexAttribPointerType.Float, _vertexSize, 10); // color
_vao.VertexAttributePointer(6, 4, VertexAttribPointerType.Int, _vertexSize, 14); // boneids
_vao.VertexAttributePointer(7, 4, VertexAttribPointerType.Float, _vertexSize, 18); // boneweights
{ // instanced models transform
TransformsCount = Transforms.Count;
@@ -213,7 +215,7 @@ public class Model : IDisposable
_morphVbo = new BufferObject<float>(Morphs[morph].Vertices, BufferTarget.ArrayBuffer);
}
_vao.Bind();
_vao.VertexAttributePointer(11, 3, VertexAttribPointerType.Float, 3, 0); // morph position
_vao.VertexAttributePointer(12, 3, VertexAttribPointerType.Float, 3, 0); // morph position
_vao.Unbind();
}

View File

@@ -184,15 +184,19 @@ public class Renderer : IDisposable
if (textureData[j].Load() is not { } textureDataIdx)
continue;
if (textureDataIdx.TryGetValue(out FPackageIndex overrideMaterial, "OverrideMaterial") &&
overrideMaterial.TryLoad(out var material) && material is UMaterialInterface unrealMaterial)
unrealMaterial.GetParams(model.Sections[j].Material.Parameters);
if (textureDataIdx.TryGetValue(out FPackageIndex diffuse, "Diffuse") &&
diffuse.Load() is UTexture2D diffuseTexture)
model.Sections[j].Material.Parameters.Textures[CMaterialParams2.Diffuse[j]] = diffuseTexture;
model.Sections[j].Material.Parameters.Textures[CMaterialParams2.Diffuse[0]] = diffuseTexture;
if (textureDataIdx.TryGetValue(out FPackageIndex normal, "Normal") &&
normal.Load() is UTexture2D normalTexture)
model.Sections[j].Material.Parameters.Textures[CMaterialParams2.Normals[j]] = normalTexture;
model.Sections[j].Material.Parameters.Textures[CMaterialParams2.Normals[0]] = normalTexture;
if (textureDataIdx.TryGetValue(out FPackageIndex specular, "Specular") &&
specular.Load() is UTexture2D specularTexture)
model.Sections[j].Material.Parameters.Textures[CMaterialParams2.SpecularMasks[j]] = specularTexture;
model.Sections[j].Material.Parameters.Textures[CMaterialParams2.SpecularMasks[0]] = specularTexture;
}
}
if (staticMeshComp.TryGetValue(out FPackageIndex[] overrideMaterials, "OverrideMaterials"))

View File

@@ -144,16 +144,6 @@ public class Texture : IDisposable
GL.BindTexture(target, _handle);
}
public void SetMinFilter(int filter)
{
GL.TexParameterI(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, ref filter);
}
public void SetMagFilter(int filter)
{
GL.TexParameterI(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, ref filter);
}
public IntPtr GetPointer() => (IntPtr) _handle;
public void Dispose()

View File

@@ -41,19 +41,19 @@ public class VertexArrayObject<TVertexType, TIndexType> : IDisposable where TVer
Bind();
var size = sizeof(Vector4);
GL.EnableVertexAttribArray(7);
GL.VertexAttribPointer(7, 4, VertexAttribPointerType.Float, false, 4 * size, 0);
GL.EnableVertexAttribArray(8);
GL.VertexAttribPointer(8, 4, VertexAttribPointerType.Float, false, 4 * size, 1 * size);
GL.VertexAttribPointer(8, 4, VertexAttribPointerType.Float, false, 4 * size, 0);
GL.EnableVertexAttribArray(9);
GL.VertexAttribPointer(9, 4, VertexAttribPointerType.Float, false, 4 * size, 2 * size);
GL.VertexAttribPointer(9, 4, VertexAttribPointerType.Float, false, 4 * size, 1 * size);
GL.EnableVertexAttribArray(10);
GL.VertexAttribPointer(10, 4, VertexAttribPointerType.Float, false, 4 * size, 3 * size);
GL.VertexAttribPointer(10, 4, VertexAttribPointerType.Float, false, 4 * size, 2 * size);
GL.EnableVertexAttribArray(11);
GL.VertexAttribPointer(11, 4, VertexAttribPointerType.Float, false, 4 * size, 3 * size);
GL.VertexAttribDivisor(7, 1);
GL.VertexAttribDivisor(8, 1);
GL.VertexAttribDivisor(9, 1);
GL.VertexAttribDivisor(10, 1);
GL.VertexAttribDivisor(11, 1);
Unbind();
}