Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ jobs:
ROOT: ./src

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: 📂 Setup .NET Core
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
dotnet-version: 10.0.x

- name: 📂 Files
working-directory: ${{env.ROOT}}
Expand All @@ -28,7 +28,7 @@ jobs:
working-directory: ${{env.ROOT}}/RenderBox
run: dotnet publish -p:PublishProfile=FolderProfile -c Release -o out

- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v4
if: github.ref == 'refs/heads/master'
with:
name: builds
Expand All @@ -45,17 +45,17 @@ jobs:
NUGET_AUTH_TOKEN: ${{secrets.token}}

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- uses: actions/download-artifact@v2
- uses: actions/download-artifact@v4
with:
name: builds
path: ${{env.ROOT}}

- name: 📂 Setup .NET Core
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
dotnet-version: 10.0.x

- name: 📂 Pack RenderBox
working-directory: ${{env.ROOT}}/RenderBox/out
Expand Down
6 changes: 6 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"sdk": {
"version": "10.0.300",
"rollForward": "latestFeature"
}
}
4 changes: 2 additions & 2 deletions src/RenderBox.Benchmarks/RenderBox.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.5" />
<PackageReference Include="BenchmarkDotNet" Version="0.15.6" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/RenderBox.Shared/Core/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private static byte GetChannel(float n)
public override int GetHashCode() => GetRaw();

[MethodImpl(Runtime.IMPL_OPTIONS)]
public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (obj is Color color)
{
Expand Down
149 changes: 132 additions & 17 deletions src/RenderBox.Shared/Core/Vector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,8 @@ public Vector2(float x) : this(x, x) { }
public float Length => MathHelpres.FastSqrt(x * x + y * y);

[MethodImpl(Runtime.IMPL_OPTIONS)]
public override bool Equals(object obj)
=> EqualsInternal(obj as Vector2?);

[MethodImpl(Runtime.IMPL_OPTIONS)]
private bool EqualsInternal(Vector2? vector)
=> vector.HasValue && vector.Value == this;
public override bool Equals(object? obj)
=> obj is Vector2 vector && vector == this;


public override int GetHashCode() => HashCode.Combine(x, y);
Expand Down Expand Up @@ -177,12 +173,8 @@ public Vector3(float x) : this(x, x, x) { }
public float Length => MathHelpres.FastSqrt(x * x + y * y + z * z);

[MethodImpl(Runtime.IMPL_OPTIONS)]
public override bool Equals(object obj)
=> EqualsInternal(obj as Vector3?);

[MethodImpl(Runtime.IMPL_OPTIONS)]
private bool EqualsInternal(Vector3? vector)
=> vector.HasValue && vector.Value == this;
public override bool Equals(object? obj)
=> obj is Vector3 vector && vector == this;

public override int GetHashCode() => HashCode.Combine(x, y, z);
}
Expand Down Expand Up @@ -289,15 +281,138 @@ public float this[int i]
public static bool operator !=(Quaternion a, Quaternion b) => !(a == b);


public override bool Equals(object obj)
=> EqualsInternal(obj as Quaternion?);

private bool EqualsInternal(Quaternion? vector)
=> vector.HasValue && vector.Value == this;
public override bool Equals(object? obj)
=> obj is Quaternion vector && vector == this;

public override int GetHashCode() => HashCode.Combine(M);
}

public struct Matrix4x4
{
private readonly float[] M;

public static Matrix4x4 Identity => new(true);

public Matrix4x4(bool identity) : this()
{
M = new float[16];

if (!identity) return;

M[0] = 1.0f;
M[5] = 1.0f;
M[10] = 1.0f;
M[15] = 1.0f;
}

public float this[int i]
{
get => M[i];
set => M[i] = value;
}

public static Matrix4x4 CreateRotation(Vector3 rotation)
{
return CreateRotationZ(rotation.z) * CreateRotationY(rotation.y) * CreateRotationX(rotation.x);
}

public static Matrix4x4 CreateRotationX(float angle)
{
var result = Identity;
var sin = MathF.Sin(angle);
var cos = MathF.Cos(angle);

result[5] = cos;
result[6] = sin;
result[9] = -sin;
result[10] = cos;

return result;
}

public static Matrix4x4 CreateRotationY(float angle)
{
var result = Identity;
var sin = MathF.Sin(angle);
var cos = MathF.Cos(angle);

result[0] = cos;
result[2] = -sin;
result[8] = sin;
result[10] = cos;

return result;
}

public static Matrix4x4 CreateRotationZ(float angle)
{
var result = Identity;
var sin = MathF.Sin(angle);
var cos = MathF.Cos(angle);

result[0] = cos;
result[1] = sin;
result[4] = -sin;
result[5] = cos;

return result;
}

public static Matrix4x4 operator *(Matrix4x4 a, Matrix4x4 b)
{
var result = new Matrix4x4(false);

for (var row = 0; row < 4; row++)
{
for (var column = 0; column < 4; column++)
{
var value = 0f;
for (var i = 0; i < 4; i++)
{
value += a[row + i * 4] * b[i + column * 4];
}

result[row + column * 4] = value;
}
}

return result;
}

public Matrix4x4 Transpose()
{
var result = new Matrix4x4(false);

for (var row = 0; row < 4; row++)
{
for (var column = 0; column < 4; column++)
{
result[row + column * 4] = M[column + row * 4];
}
}

return result;
}

public Vector3 TransformPoint(Vector3 point)
{
return Transform(point, 1);
}

public Vector3 TransformDirection(Vector3 direction)
{
return Transform(direction, 0);
}

private Vector3 Transform(Vector3 vector, float w)
{
return new Vector3(
M[0] * vector.x + M[4] * vector.y + M[8] * vector.z + M[12] * w,
M[1] * vector.x + M[5] * vector.y + M[9] * vector.z + M[13] * w,
M[2] * vector.x + M[6] * vector.y + M[10] * vector.z + M[14] * w);
}
}

public static class VectorMath
{
#region Vector2
Expand Down
27 changes: 27 additions & 0 deletions src/RenderBox.Shared/Modules/PathTracer/Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace RenderBox.Shared.Modules.PathTracer
public class Camera
{
public Vector3 Position { get; set; }
public Vector3 Rotation { get; set; }
public Vector3 Target { get; set; }
public int MaxBounceDepth { get; set; } = 3;
public float FOV { get; set; } = 90;
Expand All @@ -21,8 +22,13 @@ public Camera(Vector3 position)

Target = new Vector3(0.0, 0.0, 0.0);
Position = position;
Rotation = Vector3.Zero;

ViewMatrix = new Quaternion(true);
PosMatrix = new Quaternion(true);
BiasMatrix = GetBiasMatrixInverse();
ViewPosMatrix = new Quaternion(true);
RayMatrix = new Quaternion(true);
}

public void LookAt(Vector3 position, Vector3 target, bool rotateAround)
Expand Down Expand Up @@ -52,6 +58,27 @@ public void CalculateRayMatrix()
RayMatrix = ViewMatrix * PosMatrix * BiasMatrix * ViewPosMatrix;
}

public Vector3 TransformDirection(Vector3 direction)
{
return Matrix4x4.CreateRotation(Rotation).TransformDirection(direction);
}

public Camera SetRotation(Vector3 rotation)
{
Rotation = rotation;
return this;
}

public Camera SetRotationDegrees(Vector3 rotation)
{
Rotation = new Vector3(
MathHelpres.DegToRad(rotation.x),
MathHelpres.DegToRad(rotation.y),
MathHelpres.DegToRad(rotation.z));

return this;
}

private Quaternion GetBiasMatrix()
{
var B = new Quaternion(false);
Expand Down
2 changes: 1 addition & 1 deletion src/RenderBox.Shared/Modules/PathTracer/Light.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Light
public float LinearAttenuation { get; set; }
public float QuadraticAttenuation { get; set; }

public IShape Shape { get; set; }
public Shape? Shape { get; set; }

public Light(Color color, double intensity)
{
Expand Down
2 changes: 1 addition & 1 deletion src/RenderBox.Shared/Modules/PathTracer/Ray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public struct Hit
{
public Vector3 Position { get; set; }
public Vector3 Normal { get; set; }
public Shape HitObject { get; set; }
public Shape? HitObject { get; set; }

public bool IsHitting => HitObject != null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/RenderBox.Shared/Modules/PathTracer/Scene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class Scene
public Color BackgroundColor { get; set; } = new Color(.2f, .2f, .2f);
public Color AmbientColor { get; set; } = new Color(.1f, .1f, .1f);
public List<Shape> Shapes { get; set; } = new List<Shape>();
public IEnumerable<Light> Lights { get; private set; }
public IEnumerable<Light> Lights { get; private set; } = Array.Empty<Light>();

public bool LightingEnabled { get; set; } = true;
public bool ShadowsEnabled { get; set; } = true;
Expand All @@ -22,7 +22,7 @@ public Scene()

public void UpdateLights()
{
Lights = Shapes.Where(x => x.Light != null).Select(x => x.Light).ToArray();
Lights = Shapes.Select(x => x.Light).Where(x => x?.Shape is not null).Cast<Light>().ToArray();
}
}
}
8 changes: 4 additions & 4 deletions src/RenderBox.Shared/Modules/PathTracer/Scenes/CornellBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ public CornellBox()

new Sphere(new Vector3(1.8, 0, 0), .2f, Color.White),

new Box(new Vector3(0, -1.5, -1), Color.Yellow),
new Box(new Vector3(1, -1.5, -1), Color.Red),
new Box(new Vector3(-1, -1.5, -1), Color.Blue),
new Box(new Vector3(0, -1.5, -1), Color.Yellow).SetRotationDegrees(new Vector3(0, 35, 0)),
new Box(new Vector3(1, -1.5, -1), Color.Red).SetRotationDegrees(new Vector3(0, -25, 0)),
new Box(new Vector3(-1, -1.5, -1), Color.Blue).SetRotationDegrees(new Vector3(0, 0, 25)),

new Box(new Vector3(-1.6, -1.9, 1.4), Color.White, new Vector3(0.24f, 0.24f, 0.24f)) { Material = metal },
new Box(new Vector3(-1.6, -1.9, 1.4), Color.White, new Vector3(0.24f, 0.24f, 0.24f)) { Material = metal }.SetRotationDegrees(new Vector3(0, 45, 0)),
});

UpdateLights();
Expand Down
Loading
Loading