-
-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
203 additions
and
242 deletions.
There are no files selected for viewing
Submodule CUE4Parse
updated
18 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
using System; | ||
using CUE4Parse_Conversion.Textures; | ||
using CUE4Parse.UE4.Assets.Exports.Material; | ||
using CUE4Parse.UE4.Assets.Exports.Texture; | ||
using FModel.Settings; | ||
using OpenTK.Graphics.OpenGL4; | ||
using OpenTK.Mathematics; | ||
|
||
namespace FModel.Views.Snooper; | ||
|
||
public class Material : IDisposable | ||
{ | ||
private int _handle; | ||
|
||
private Vector3 _ambientLight; | ||
|
||
public readonly CMaterialParams2 Parameters; | ||
public readonly int UvNumber; | ||
|
||
public Texture[] Diffuse; | ||
public Texture[] Normals; | ||
public Texture[] SpecularMasks; | ||
public Texture Emissive; | ||
|
||
public Vector4 DiffuseColor; | ||
public Vector4 EmissionColor; | ||
public bool HasSpecularMap; | ||
public bool HasDiffuseColor; | ||
|
||
public Material() | ||
{ | ||
DiffuseColor = Vector4.Zero; | ||
EmissionColor = Vector4.Zero; | ||
} | ||
|
||
public Material(int numUvs, UMaterialInterface unrealMaterial) : this() | ||
{ | ||
UvNumber = numUvs; | ||
Parameters = new CMaterialParams2(); | ||
SwapMaterial(unrealMaterial); | ||
} | ||
|
||
public void SwapMaterial(UMaterialInterface unrealMaterial) | ||
{ | ||
unrealMaterial.GetParams(Parameters); | ||
} | ||
|
||
public void Setup(Cache cache) | ||
{ | ||
_handle = GL.CreateProgram(); | ||
|
||
int index; | ||
var platform = UserSettings.Default.OverridedPlatform; | ||
void Add(Texture[] array, UTexture2D original) | ||
{ | ||
var guid = original.LightingGuid; | ||
if (cache.TryGetTexture(guid, out var texture)) | ||
{ | ||
array[index++] = texture; | ||
} | ||
else if (original.GetFirstMip() is { } mip) | ||
{ | ||
TextureDecoder.DecodeTexture(mip, original.Format, original.isNormalMap, platform, out var data, out _); | ||
|
||
var t = new Texture(data, mip.SizeX, mip.SizeY, original); | ||
cache.AddTexture(guid, t); | ||
array[index++] = t; | ||
} | ||
} | ||
|
||
index = 0; | ||
Diffuse = new Texture[UvNumber]; | ||
foreach (var d in Parameters.GetDiffuseTextures()) | ||
if (index < UvNumber && d is UTexture2D original) | ||
Add(Diffuse, original); | ||
|
||
index = 0; | ||
Normals = new Texture[UvNumber]; | ||
foreach (var n in Parameters.GetNormalsTextures()) | ||
if (index < UvNumber && n is UTexture2D original) | ||
Add(Normals, original); | ||
|
||
index = 0; | ||
SpecularMasks = new Texture[UvNumber]; | ||
foreach (var s in Parameters.GetSpecularMasksTextures()) | ||
if (index < UvNumber && s is UTexture2D original) | ||
Add(SpecularMasks, original); | ||
|
||
// diffuse light is based on normal map, so increase ambient if no normal map | ||
_ambientLight = new Vector3(Normals[0] == null ? 1.0f : 0.2f); | ||
HasSpecularMap = SpecularMasks[0] != null; | ||
HasDiffuseColor = DiffuseColor != Vector4.Zero; | ||
} | ||
|
||
public void Render(Shader shader) | ||
{ | ||
var unit = 0; | ||
|
||
for (var i = 0; i < Diffuse.Length; i++) | ||
{ | ||
shader.SetUniform("material.diffuseMap", unit); | ||
Diffuse[i]?.Bind(TextureUnit.Texture0 + unit++); | ||
} | ||
|
||
for (var i = 0; i < Normals.Length; i++) | ||
{ | ||
shader.SetUniform("material.normalMap", unit); | ||
Normals[i]?.Bind(TextureUnit.Texture0 + unit++); | ||
} | ||
|
||
for (var i = 0; i < SpecularMasks.Length; i++) | ||
{ | ||
shader.SetUniform("material.specularMap", unit); | ||
SpecularMasks[i]?.Bind(TextureUnit.Texture0 + unit++); | ||
} | ||
|
||
shader.SetUniform("material.useSpecularMap", HasSpecularMap); | ||
|
||
shader.SetUniform("material.hasDiffuseColor", HasDiffuseColor); | ||
shader.SetUniform("material.diffuseColor", DiffuseColor); | ||
|
||
shader.SetUniform("material.emissionColor", EmissionColor); | ||
|
||
shader.SetUniform("material.metallic_value", 1f); | ||
shader.SetUniform("material.roughness_value", 0f); | ||
|
||
shader.SetUniform("light.ambient", _ambientLight); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
for (int i = 0; i < Diffuse.Length; i++) | ||
{ | ||
Diffuse[i]?.Dispose(); | ||
} | ||
for (int i = 0; i < Normals.Length; i++) | ||
{ | ||
Normals[i]?.Dispose(); | ||
} | ||
for (int i = 0; i < SpecularMasks.Length; i++) | ||
{ | ||
SpecularMasks[i]?.Dispose(); | ||
} | ||
Emissive?.Dispose(); | ||
GL.DeleteProgram(_handle); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.