Skip to content

Commit

Permalink
somewhat usable
Browse files Browse the repository at this point in the history
  • Loading branch information
4sval committed Oct 19, 2022
1 parent 59973f9 commit 4da6b2d
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 82 deletions.
2 changes: 1 addition & 1 deletion FModel/ViewModels/CUE4ParseViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public Snooper SnooperViewer
WindowBorder = WindowBorder.Resizable,
StartVisible = false,
StartFocused = false,
Title = "Title"
Title = "3D Viewer"
});
});
}
Expand Down
3 changes: 1 addition & 2 deletions FModel/Views/Snooper/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ public Cache()
public bool TryGetModel(FGuid guid, out Model model) => _models.TryGetValue(guid, out model);
public bool TryGetTexture(FGuid guid, out Texture texture) => _textures.TryGetValue(guid, out texture);

public void Setup(FGuid guid) => _models[guid].Setup();
public void Setup()
{
foreach (var model in _models.Values)
{
if (model.IsSetup) continue;
model.Setup();
}
}

public void Render(Shader shader)
{
foreach (var model in _models.Values)
Expand Down
3 changes: 3 additions & 0 deletions FModel/Views/Snooper/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class Model : IDisposable
public readonly List<Transform> Transforms;

public bool Show;
public bool IsSetup;
public bool IsSelected;
public bool DisplayVertexColors;
public bool DisplayBones;
Expand Down Expand Up @@ -201,6 +202,8 @@ public void Setup()
{
Sections[section].Setup();
}

IsSetup = true;
}

public void Render(Shader shader)
Expand Down
5 changes: 0 additions & 5 deletions FModel/Views/Snooper/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ public class Options
public int SelectedSection { get; private set; }
public int SelectedMorph { get; private set; }

public bool Append { get; set; }
public bool Close { get; set; }

public Options()
{
Reset();
Expand All @@ -25,8 +22,6 @@ public void Reset()
SelectedModelInstance = 0;
SelectedSection = 0;
SelectedMorph = 0;
Append = false;
Close = false;
}

public void SelectModel(FGuid guid)
Expand Down
110 changes: 47 additions & 63 deletions FModel/Views/Snooper/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace FModel.Views.Snooper;
public class Renderer : IDisposable
{
private Shader _shader;
private Shader _outline;
// private Shader _outline; // fix stutter
private Vector3 _diffuseLight;
private Vector3 _specularLight;

Expand All @@ -30,34 +30,16 @@ public Renderer()
Settings = new Options();
}

public void Load(CancellationToken cancellationToken, UObject export)
public Camera Load(CancellationToken cancellationToken, UObject export)
{
switch (export)
return export switch
{
case UStaticMesh st:
{
LoadStaticMesh(st);
break;
}
case USkeletalMesh sk:
{
LoadSkeletalMesh(sk);
break;
}
case UMaterialInstance mi:
{
LoadMaterialInstance(mi);
break;
}
case UWorld wd:
{
LoadWorld(cancellationToken, wd);
// _camera = new Camera(new Vector3(0f, 5f, 5f), Vector3.Zero, 0.01f, 1000f, 5f);
break;
}
default:
throw new ArgumentOutOfRangeException(nameof(export));
}
UStaticMesh st => LoadStaticMesh(st),
USkeletalMesh sk => LoadSkeletalMesh(sk),
UMaterialInstance mi => LoadMaterialInstance(mi),
UWorld wd => LoadWorld(cancellationToken, wd),
_ => throw new ArgumentOutOfRangeException(nameof(export))
};
}

public void Swap(UMaterialInstance unrealMaterial)
Expand All @@ -72,7 +54,7 @@ public void Swap(UMaterialInstance unrealMaterial)
public void Setup()
{
_shader = new Shader();
_outline = new Shader("outline");
// _outline = new Shader("outline");
_diffuseLight = new Vector3(0.75f);
_specularLight = new Vector3(0.5f);

Expand All @@ -84,10 +66,10 @@ public void Render(Camera cam)
var viewMatrix = cam.GetViewMatrix();
var projMatrix = cam.GetProjectionMatrix();

_outline.Use();
_outline.SetUniform("uView", viewMatrix);
_outline.SetUniform("uProjection", projMatrix);
_outline.SetUniform("viewPos", cam.Position);
// _outline.Use();
// _outline.SetUniform("uView", viewMatrix);
// _outline.SetUniform("uProjection", projMatrix);
// _outline.SetUniform("viewPos", cam.Position);

_shader.Use();
_shader.SetUniform("uView", viewMatrix);
Expand All @@ -104,62 +86,62 @@ public void Render(Camera cam)
_shader.SetUniform("light.specular", _specularLight);

Cache.Render(_shader);
GL.Enable(EnableCap.StencilTest); // I don't get why this must be here but it works now so...
Cache.Outline(_outline);
// GL.Enable(EnableCap.StencilTest); // I don't get why this must be here but it works now so...
// Cache.Outline(_outline);
}

// private void SetupCamera(FBox box)
// {
// var far = box.Max.Max();
// var center = box.GetCenter();
// var position = new Vector3(0f, center.Z, box.Max.Y * 3);
// var speed = far / 2f;
// if (speed > _previousSpeed)
// {
// _camera = new Camera(position, center, 0.01f, far * 50f, speed);
// _previousSpeed = _camera.Speed;
// }
// }

private void LoadStaticMesh(UStaticMesh original)
private Camera SetupCamera(FBox box)
{
var far = box.Max.Max();
var center = box.GetCenter();
return new Camera(
new Vector3(0f, center.Z, box.Max.Y * 3),
new Vector3(center.X, center.Z, center.Y),
0.01f, far * 50f, far / 2f);
}

private Camera LoadStaticMesh(UStaticMesh original)
{
var guid = original.LightingGuid;
if (Cache.TryGetModel(guid, out var model))
{
model.AddInstance(Transform.Identity);
return null;
}
else if (original.TryConvert(out var mesh))
{
Cache.AddModel(guid, new Model(original.Name, original.ExportType, mesh));
// SetupCamera(mesh.BoundingBox *= Constants.SCALE_DOWN_RATIO);
Settings.SelectModel(guid);
}

if (!original.TryConvert(out var mesh))
return null;

Cache.AddModel(guid, new Model(original.Name, original.ExportType, mesh));
Settings.SelectModel(guid);
return SetupCamera(mesh.BoundingBox *= Constants.SCALE_DOWN_RATIO);
}

private void LoadSkeletalMesh(USkeletalMesh original)
private Camera LoadSkeletalMesh(USkeletalMesh original)
{
var guid = Guid.NewGuid();
if (Cache.HasModel(guid) || !original.TryConvert(out var mesh)) return;
if (Cache.HasModel(guid) || !original.TryConvert(out var mesh)) return null;

Cache.AddModel(guid, new Model(original.Name, original.ExportType, original.MorphTargets, mesh));
// SetupCamera(mesh.BoundingBox *= Constants.SCALE_DOWN_RATIO);
Settings.SelectModel(guid);
return SetupCamera(mesh.BoundingBox *= Constants.SCALE_DOWN_RATIO);
}

private void LoadMaterialInstance(UMaterialInstance original)
private Camera LoadMaterialInstance(UMaterialInstance original)
{
var guid = Guid.NewGuid();
if (Cache.HasModel(guid)) return;
if (Cache.HasModel(guid)) return null;

Cache.AddModel(guid, new Cube(original));
// SetupCamera(new FBox(new FVector(-.65f), new FVector(.65f)));
Settings.SelectModel(guid);
return SetupCamera(new FBox(new FVector(-.65f), new FVector(.65f)));
}

private void LoadWorld(CancellationToken cancellationToken, UWorld original)
private Camera LoadWorld(CancellationToken cancellationToken, UWorld original)
{
var ret = new Camera(new Vector3(0f, 5f, 5f), Vector3.Zero, 0.01f, 1000f, 5f);
if (original.PersistentLevel.Load<ULevel>() is not { } persistentLevel)
return;
return ret;

var length = persistentLevel.Actors.Length;
for (var i = 0; i < length; i++)
Expand Down Expand Up @@ -228,12 +210,14 @@ private void LoadWorld(CancellationToken cancellationToken, UWorld original)
Cache.AddModel(guid, model);
}
}
Services.ApplicationService.ApplicationView.Status.UpdateStatusLabel($"Actor {length}/{length}");
return ret;
}

public void Dispose()
{
_shader.Dispose();
_outline.Dispose();
// _outline.Dispose();
Cache.Dispose();
}
}
32 changes: 22 additions & 10 deletions FModel/Views/Snooper/Snooper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,23 @@ public Snooper(GameWindowSettings gwSettings, NativeWindowSettings nwSettings) :
public void SwapMaterial(UMaterialInstance mi) => _renderer.Swap(mi);
public void LoadExport(CancellationToken cancellationToken, UObject export)
{
_renderer.Load(cancellationToken, export);
_camera = new Camera(new Vector3(0f, 5f, 5f), Vector3.Zero, 0.01f, 1000f, 5f);
var newCamera = _renderer.Load(cancellationToken, export);
if (newCamera == null || !(newCamera.Speed > _previousSpeed)) return;

_camera = newCamera;
_previousSpeed = _camera.Speed;
}

private unsafe void WindowShouldClose(bool value)
private unsafe void WindowShouldClose(bool value, bool clear)
{
if (clear)
{
_renderer.Cache.DisposeModels();
_renderer.Cache.ClearModels();
_renderer.Settings.Reset();
_previousSpeed = 0f;
}

GLFW.SetWindowShouldClose(WindowPtr, value); // start / stop game loop
CursorState = value ? CursorState.Normal : CursorState.Grabbed;
IsVisible = !value;
Expand All @@ -49,7 +60,7 @@ public override void Run()
{
Application.Current.Dispatcher.Invoke(delegate
{
WindowShouldClose(false);
WindowShouldClose(false, false);
base.Run();
});
}
Expand All @@ -58,11 +69,12 @@ protected override void OnLoad()
{
if (_init)
{
_renderer.Cache.Setup(_renderer.Settings.SelectedModel);
_renderer.Cache.Setup();
return;
}

base.OnLoad();
CenterWindow();

GL.ClearColor(Color4.Red);
GL.Enable(EnableCap.Blend);
Expand Down Expand Up @@ -109,7 +121,7 @@ private void ClearWhatHasBeenDrawn()
protected override void OnMouseMove(MouseMoveEventArgs e)
{
base.OnMouseMove(e);
if (!IsFocused)
if (!IsVisible)
return;

const float lookSensitivity = 0.1f;
Expand All @@ -120,7 +132,7 @@ protected override void OnMouseMove(MouseMoveEventArgs e)
protected override void OnUpdateFrame(FrameEventArgs e)
{
base.OnUpdateFrame(e);
if (!IsFocused)
if (!IsVisible)
return;

var multiplier = KeyboardState.IsKeyDown(Keys.LeftShift) ? 2f : 1f;
Expand All @@ -142,10 +154,10 @@ protected override void OnUpdateFrame(FrameEventArgs e)
if (KeyboardState.IsKeyDown(Keys.C))
_camera.ModifyZoom(+.5f);

if (KeyboardState.IsKeyPressed(Keys.H))
IsVisible = false;
if (KeyboardState.IsKeyPressed(Keys.R))
WindowShouldClose(true, false);
if (KeyboardState.IsKeyPressed(Keys.Escape))
WindowShouldClose(true);
WindowShouldClose(true, true);
}

protected override void OnResize(ResizeEventArgs e)
Expand Down

0 comments on commit 4da6b2d

Please sign in to comment.