Skip to content

Commit 99a6d5a

Browse files
committed
Scene viewport auto resize
1 parent 509d958 commit 99a6d5a

File tree

7 files changed

+228
-253
lines changed

7 files changed

+228
-253
lines changed

Nanoforge/Gui/ViewModels/Documents/MapEditorDocumentViewModel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private void LoadMap(TaskDialog taskDialog)
114114
//TODO: Count object class instances
115115
//TODO: Initialize inspectors if necessary in this port. In the previous version it caused a hitch when loading xml files. May not be necessary in avalonia with async.
116116

117-
Scene.Init(new Vector2(1920, 1080));
117+
Scene.Init(renderer.Context);
118118
Scene.Camera!.TargetPosition = new Vector3(65.97262f, 296.2423f, -592.8933f);
119119
renderer.ActiveScenes.Add(Scene);
120120
ImportAndLoadTime = DateTime.Now - loadingStart;

Nanoforge/Gui/ViewModels/Documents/RendererTestDocumentViewModel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public void SceneInit(TaskDialogViewModel status)
8484
_skybox.Position = Vector3.Zero;
8585
_skybox.Scale = new Vector3(25000.0f);
8686

87-
Scene.Init(new Vector2(1920, 1080));
87+
Scene.Init(renderer.Context);
8888
renderer.ActiveScenes.Add(Scene);
8989
SceneInitialized = true;
9090

Nanoforge/Gui/Views/Controls/Viewport3D.axaml

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
Loaded="Control_OnLoaded" PointerMoved="InputElement_OnPointerMoved" PointerPressed="InputElement_OnPointerPressed" PointerReleased="InputElement_OnPointerReleased"
1212
PointerEntered="InputElement_OnPointerEntered" PointerExited="InputElement_OnPointerExited">
1313

14-
<Panel>
15-
<Image Name="ViewportImage" IsVisible="{Binding #Viewport.SceneInitialized}"/>
14+
<Panel VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
15+
<Image Name="ViewportImage" IsVisible="{Binding #Viewport.SceneInitialized}" SizeChanged="ViewportImage_OnSizeChanged"
16+
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" StretchDirection="Both" Stretch="UniformToFill"/>
1617
<TextBlock IsVisible="{Binding !#Viewport.SceneInitialized}"
1718
Text="{Binding #Viewport.LoadingStatus}" FontSize="32" FontWeight="Bold" VerticalAlignment="Center" HorizontalAlignment="Center"/>
1819
</Panel>

Nanoforge/Gui/Views/Controls/Viewport3D.axaml.cs

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics.CodeAnalysis;
23
using System.Numerics;
34
using System.Threading.Tasks;
45
using System.Windows.Input;
@@ -10,7 +11,6 @@
1011
using Avalonia.Platform;
1112
using Avalonia.Rendering.Composition;
1213
using Avalonia.Threading;
13-
using CommunityToolkit.Mvvm.Input;
1414
using Nanoforge.FileSystem;
1515
using Nanoforge.Render;
1616
using Serilog;
@@ -66,8 +66,6 @@ public string LoadingStatus
6666
private Vector2 _mousePositionDelta = Vector2.Zero;
6767
private bool _mouseOverViewport = false;
6868
private bool _mouseMovedThisFrame = false;
69-
private bool _sceneInitStarted = false;
70-
private Task? _sceneInitTask;
7169

7270
public Viewport3D()
7371
{
@@ -79,8 +77,8 @@ public Viewport3D()
7977
}
8078

8179
_renderer = (Application.Current as App)!.Renderer;
82-
_rendererOutput = new WriteableBitmap(new PixelSize(1920, 1080), new Vector(96, 96), PixelFormat.Bgra8888);
83-
80+
CreateRendererOutputTexture((int)ViewportImage.Width, (int)ViewportImage.Height);
81+
8482
if (_renderer != null)
8583
{
8684
var mainWindow = MainWindow.Instance;
@@ -95,7 +93,16 @@ public Viewport3D()
9593
Log.Error("Renderer is null. If you're not in the xaml designer and you see this then there's a big problem.");
9694
}
9795
}
98-
96+
97+
[MemberNotNull(nameof(_rendererOutput))]
98+
private void CreateRendererOutputTexture(int width, int height)
99+
{
100+
width = Math.Max(1, width);
101+
height = Math.Max(1, height);
102+
_rendererOutput = new WriteableBitmap(new PixelSize(width, height), new Vector(96, 96), PixelFormat.Bgra8888);
103+
ViewportImage.Source = _rendererOutput;
104+
}
105+
99106
private void Control_OnLoaded(object? sender, RoutedEventArgs e)
100107
{
101108
Update();
@@ -144,7 +151,7 @@ private void RenderFrame()
144151
_renderer.RenderFrame(Scene!);
145152

146153
using var buffer = _rendererOutput!.Lock();
147-
_renderer.GetRenderImage(buffer.Address);
154+
Scene!.GetRenderImage(buffer.Address);
148155

149156
_lastUpdate = DateTime.Now;
150157
}
@@ -181,4 +188,10 @@ private void InputElement_OnPointerExited(object? sender, PointerEventArgs e)
181188
{
182189
_mouseOverViewport = false;
183190
}
191+
192+
private void ViewportImage_OnSizeChanged(object? sender, SizeChangedEventArgs e)
193+
{
194+
CreateRendererOutputTexture((int)e.NewSize.Width, (int)e.NewSize.Height);
195+
Scene?.ViewportResize(new Vector2((float)e.NewSize.Width, (float)e.NewSize.Height));
196+
}
184197
}

Nanoforge/Render/RenderContext.cs

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public unsafe class RenderContext : IDisposable
3434
#else
3535
public const bool EnableValidationLayers = false;
3636
#endif
37+
38+
public RenderPass PrimaryRenderPass;
3739

3840
private readonly string[] _validationLayers = new[]
3941
{

0 commit comments

Comments
 (0)