Skip to content

Commit

Permalink
Add Node Graph (#275)
Browse files Browse the repository at this point in the history
* start implementing node graph

* update Sekai

* remove previous rendering impl

* add coordinate providing interfaces

* add `Node`

* add `Effect`

* add material-related interfaces

* add `ShaderMaterial`

* add `UnlitMaterial`

* add `Behavior`

* add `Renderable`

* add `RenderGroup`s

* add `RenderObject`s

* add `RenderData`

* add `RenderQueue` and `RenderContext`

* add `RenderTarget`

* add `Renderer`

* add `SortedFilteredCollection<T>`

* add `Drawable`

* add projectors

* add `World`

* add `Window` node

* add executable

* ensure material resources are applied

* remove `Renderable`

* cleanup pass

* add `ServiceLocator`

* add `Node.Services`

* make `Window` override `Services`

* refactor `World`
  • Loading branch information
LeNitrous authored Jul 16, 2023
1 parent 9abca7a commit b434ee6
Show file tree
Hide file tree
Showing 48 changed files with 2,970 additions and 2,577 deletions.
26 changes: 26 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"version": "0.2.0",
"configurations": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/dotnet/vscode-csharp/blob/main/debugger-launchjson.md
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/source/Vignette.Desktop/bin/Debug/net7.0/Vignette.Desktop.dll",
"args": [],
"cwd": "${workspaceFolder}/source/Vignette.Desktop",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}
41 changes: 41 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/source/Vignette.Desktop/Vignette.Desktop.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/source/Vignette.Desktop/Vignette.Desktop.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/source/Vignette.Desktop/Vignette.Desktop.csproj"
],
"problemMatcher": "$msCompile"
}
]
}
7 changes: 7 additions & 0 deletions source/Vignette.Desktop/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright (c) Cosyne
// Licensed under GPL 3.0 with SDK Exception. See LICENSE for details.

using Sekai;
using Vignette;

Host.Run<VignetteGame>(new HostOptions { Name = "Vignette" });
4 changes: 3 additions & 1 deletion source/Vignette.Desktop/Vignette.Desktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Sekai.Desktop" Version="0.1.0-alpha.8" />
<PackageReference Include="Sekai.OpenGL" Version="0.1.0-alpha.9" />
<PackageReference Include="Sekai.OpenAL" Version="0.1.0-alpha.9" />
<PackageReference Include="Sekai.Desktop" Version="0.1.0-alpha.9" />
</ItemGroup>

</Project>
100 changes: 100 additions & 0 deletions source/Vignette/Behavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright (c) Cosyne
// Licensed under GPL 3.0 with SDK Exception. See LICENSE for details.

using System;

namespace Vignette;

/// <summary>
/// A <see cref="Node"/> that processes itself per-frame.
/// </summary>
public abstract class Behavior : Node, IComparable<Behavior>
{
/// <summary>
/// The processing order for this <see cref="Behavior"/>.
/// </summary>
public int Order
{
get => order;
set
{
if (order.Equals(value))
{
return;
}

order = value;
OrderChanged?.Invoke(this, EventArgs.Empty);
}
}

/// <summary>
/// Whether this <see cref="Behavior"/> should be enabled or not affecting <see cref="Update(TimeSpan)"/> calls.
/// </summary>
public bool Enabled
{
get => enabled;
set
{
if (enabled.Equals(value))
{
return;
}

enabled = value;
EnabledChanged?.Invoke(this, EventArgs.Empty);
}
}

/// <summary>
/// Called when <see cref="Order"/> has been changed.
/// </summary>
public event EventHandler? OrderChanged;

/// <summary>
/// Called when <see cref="Enabled"/> has been changed.
/// </summary>
public event EventHandler? EnabledChanged;

private int order;
private bool enabled = true;

/// <summary>
/// Called once in the update loop after the <see cref="Node"/> has entered the node graph.
/// </summary>
public virtual void Load()
{
}

/// <summary>
/// Called every frame to perform updates on this <see cref="Behavior"/>.
/// </summary>
/// <param name="elapsed">The time elapsed between frames.</param>
public virtual void Update(TimeSpan elapsed)
{
}

/// <summary>
/// Called once in the update loop before the <see cref="Node"/> exits the node graph.
/// </summary>
public virtual void Unload()
{
}

public int CompareTo(Behavior? other)
{
if (other is null)
{
return -1;
}

int value = Depth.CompareTo(other.Depth);

if (value != 0)
{
return value;
}

return Order.CompareTo(other.Order);
}
}
106 changes: 106 additions & 0 deletions source/Vignette/Camera.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright (c) Cosyne
// Licensed under GPL 3.0 with SDK Exception. See LICENSE for details.

using System;
using System.Numerics;
using Sekai.Mathematics;
using Vignette.Graphics;

namespace Vignette;

public class Camera : Node, IProjector
{
/// <summary>
/// The near plane distance.
/// </summary>
public float NearPlane = 0.1f;

/// <summary>
/// The far plane distance.
/// </summary>
public float FarPlane = 1000f;

/// <summary>
/// The camera's aspect ratio.
/// </summary>
/// <remarks>Used when <see cref="ProjectionMode"/> is <see cref="CameraProjectionMode.Perspective"/>.</remarks>
public float AspectRatio = 16.0f / 9.0f;

/// <summary>
/// The camera's field of view.
/// </summary>
public float FieldOfView = 60.0f;

/// <summary>
/// The camera's view size.
/// </summary>
public SizeF ViewSize = SizeF.Zero;

/// <summary>
/// The camera's view scale.
/// </summary>
public Vector2 ViewScale = Vector2.One;

/// <summary>
/// The camera's top left position.
/// </summary>
public Vector2 ViewTopLeft = Vector2.Zero;

/// <summary>
/// The camera projection mode.
/// </summary>
public CameraProjectionMode ProjectionMode = CameraProjectionMode.OrthographicOffCenter;

/// <summary>
/// The camera's rendering groups.
/// </summary>
public RenderGroup Groups { get; set; } = RenderGroup.Default;

/// <summary>
/// The camera's view frustum.
/// </summary>
public BoundingFrustum Frustum => BoundingFrustum.FromMatrix(((IProjector)this).ProjMatrix);

Matrix4x4 IProjector.ViewMatrix => Matrix4x4.CreateLookAt(Position, Position + Vector3.Transform(-Vector3.UnitZ, Matrix4x4.CreateFromYawPitchRoll(Rotation.Y, Rotation.X, Rotation.Z)), Vector3.UnitY);

Matrix4x4 IProjector.ProjMatrix => ProjectionMode switch
{
CameraProjectionMode.Perspective => Matrix4x4.CreatePerspective(ViewSize.Width * ViewScale.X, ViewSize.Height * ViewScale.Y, NearPlane, FarPlane),
CameraProjectionMode.PerspectiveOffCenter => Matrix4x4.CreatePerspectiveOffCenter(ViewTopLeft.X, ViewSize.Width * ViewScale.X, ViewSize.Height * ViewScale.Y, ViewTopLeft.Y, NearPlane, FarPlane),
CameraProjectionMode.PerspectiveFieldOfView => Matrix4x4.CreatePerspectiveFieldOfView(FieldOfView, AspectRatio, NearPlane, FarPlane),
CameraProjectionMode.Orthographic => Matrix4x4.CreateOrthographic(ViewSize.Width * ViewScale.X, ViewSize.Height * ViewScale.Y, NearPlane, FarPlane),
CameraProjectionMode.OrthographicOffCenter => Matrix4x4.CreateOrthographicOffCenter(ViewTopLeft.X, ViewSize.Width * ViewScale.X, ViewSize.Height * ViewScale.Y, ViewTopLeft.Y, NearPlane, FarPlane),
_ => throw new InvalidOperationException($"Unknown {nameof(ProjectionMode)} {ProjectionMode}."),
};
}

/// <summary>
/// An enumeration of camera projection modes.
/// </summary>
public enum CameraProjectionMode
{
/// <summary>
/// Orthographic projection.
/// </summary>
Orthographic,

/// <summary>
/// Custom orthographic projection.
/// </summary>
OrthographicOffCenter,

/// <summary>
/// Perspective projection.
/// </summary>
Perspective,

/// <summary>
/// Custom perspective projection.
/// </summary>
PerspectiveOffCenter,

/// <summary>
/// Perspective field of view projection.
/// </summary>
PerspectiveFieldOfView,
}
Loading

0 comments on commit b434ee6

Please sign in to comment.