Skip to content

Commit 93f764b

Browse files
committed
Assets: More GLTF loading, completed rount 1 of mesh loading and initial PBR material loading.
1 parent af6c8ae commit 93f764b

File tree

18 files changed

+278
-101
lines changed

18 files changed

+278
-101
lines changed

Directory.Packages.props

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838

3939
<!-- Asset Pipeline -->
4040
<PackageVersion Include="Vortice.Dxc.Native" Version="1.0.4" />
41-
<PackageVersion Include="SharpGLTF.Core" Version="1.0.6" />
4241
<PackageVersion Include="Silk.NET.Assimp" Version="2.23.0" />
4342
<PackageVersion Include="Vortice.SPIRV.Reflect" Version="1.0.6" />
4443
<PackageVersion Include="Alimer.Bindings.MeshOptimizer" Version="1.2.0" />

assets/Shaders/ShaderTypes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ struct ALIGNMENT PBRMaterialData
5959
struct ALIGNMENT InstanceData
6060
{
6161
float4x4 worldMatrix;
62-
float4 color;
62+
//float4 color;
6363
uint32_t materialIndex;
6464
//float4x4 inverseWorldMatrix;
6565
};

assets/Shaders/VertexCommon.slang

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ VertexOutput vertexMain(in VertexInput input,
1212
InstanceData instance = instanceDataBuffer[instanceId];
1313

1414
float4x4 worldMatrix = instance.worldMatrix;
15-
const float4 instanceColor = instance.color;
15+
//const float4 instanceColor = instance.color;
1616
const uint materialIndex = instance.materialIndex;
1717
float4 worldPosition = mul(float4(input.Position, 1.0f), worldMatrix);
1818

@@ -37,7 +37,8 @@ VertexOutput vertexMain(in VertexInput input,
3737
#if defined(VERTEX_COLOR)
3838
output.Color = input.Color;
3939
#else
40-
output.Color = instanceColor.xyz; // float3(1.0f);
40+
// output.Color = instanceColor.xyz;
41+
output.Color = float3(1.0f);
4142
#endif
4243
return output;
4344
}

samples/Alimer.Samples/Engine/ScenePBRRendererSample.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,9 @@ public ScenePBRRendererSample(IServiceRegistry services)
3232
root.Children.Add(_cameraEntity);
3333

3434
// GLTF mesh
35-
PhysicallyBasedMaterial sharedMaterial = new()
36-
{
37-
BaseColorFactor = Colors.White,
38-
};
39-
4035
string meshesPath = Path.Combine(AppContext.BaseDirectory, "Assets", "Meshes");
4136

42-
MeshImporter meshImporter = new();
37+
MeshImporter meshImporter = new(GraphicsDevice);
4338
MeshMetadata meshMetadata = new()
4439
{
4540
FileFullPath = Path.Combine(meshesPath, "DamagedHelmet.glb")
@@ -68,7 +63,12 @@ public ScenePBRRendererSample(IServiceRegistry services)
6863
_damagedHelmetEntity = new("Damaged Helmet", new Vector3(0.0f, 2.0f, 0.0f));
6964

7065
MeshComponent meshComponent = new(damagedHelmetMesh);
71-
meshComponent.Materials.Add(sharedMaterial);
66+
67+
foreach(var material in meshAsset.Materials)
68+
{
69+
meshComponent.Materials.Add(ToDispose(material));
70+
}
71+
7272
_damagedHelmetEntity.AddComponent(meshComponent);
7373
root.Children.Add(_damagedHelmetEntity);
7474
}

samples/Alimer.Samples/Graphics/DrawMeshSample.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public DrawMeshSample(IServiceRegistry services, Window mainWindow)
4343
_texture = ToDispose(Texture.FromFile(GraphicsDevice, Path.Combine(texturesPath, "10points.png")));
4444
//_texture = ToDispose(Texture.FromFile(GraphicsDevice, Path.Combine(texturesPath, "environment.hdr")));
4545

46-
MeshImporter meshImporter = new();
46+
MeshImporter meshImporter = new(GraphicsDevice);
4747
MeshMetadata meshMetadata = new()
4848
{
4949
FileFullPath = Path.Combine(meshesPath, "DamagedHelmet.glb")

src/Alimer/Assets/Asset.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ namespace Alimer.Assets;
88
/// </summary>
99
public abstract class Asset
1010
{
11+
/// <summary>
12+
/// Gets or sets the name of the asset.
13+
/// </summary>
14+
public string? Name { get;set; }
15+
1116
public Guid Id { get; set; } = Guid.NewGuid();
1217

1318
public override string ToString()

src/Alimer/Engine/Components/MeshComponent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public MeshComponent(Mesh mesh)
3131

3232
public Mesh? Mesh { get; set; }
3333

34-
public IList<Material> Materials { get; } = [];
34+
public List<Material> Materials { get; } = [];
3535
public BoundingBox LocalBoundingBox => _localBoundingBox;
3636

3737
public BoundingBox WorldBoundingBox

src/Alimer/Rendering/Material.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,33 @@ public enum MaterialAlphaMode
1515
/// <summary>
1616
/// Base material class.
1717
/// </summary>
18-
public abstract class Material : Asset
18+
public abstract class Material : Asset, IDisposableObject
1919
{
20+
private volatile uint _isDisposed = 0;
21+
22+
/// <inheritdoc />
23+
public bool IsDisposed => _isDisposed != 0;
2024
public MaterialAlphaMode AlphaMode { get;set; } = MaterialAlphaMode.Opaque;
2125
public bool Transparent => AlphaMode == MaterialAlphaMode.Blend;
26+
27+
~Material()
28+
{
29+
Dispose(false);
30+
}
31+
32+
/// <inheritdoc />
33+
public void Dispose()
34+
{
35+
if (Interlocked.Exchange(ref _isDisposed, 1) == 0)
36+
{
37+
Dispose(disposing: true);
38+
GC.SuppressFinalize(this);
39+
}
40+
}
41+
42+
/// <inheritdoc cref="Dispose()" />
43+
/// <param name="disposing"><c>true</c> if the method was called from <see cref="Dispose()" />; otherwise, <c>false</c>.</param>
44+
protected virtual void Dispose(bool disposing)
45+
{
46+
}
2247
}

src/Alimer/Rendering/Mesh.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
// Copyright (c) Amer Koleci and Contributors.
22
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information.
33

4-
using System.Buffers.Binary;
54
using System.Numerics;
6-
using System.Runtime.CompilerServices;
75
using System.Runtime.InteropServices;
86
using Alimer.Graphics;
7+
using Alimer.Serialization;
98
using CommunityToolkit.Diagnostics;
10-
using XenoAtom.Collections;
119

1210
namespace Alimer.Rendering;
1311

1412
// TODO: Mesh on GPU with CPU access
1513

16-
public sealed unsafe partial class Mesh : DisposableObject
14+
public sealed unsafe partial class Mesh : DisposableObject, IBinarySerializable
1715
{
1816
//private readonly UnsafeList<Vector3> _positions = [];
1917
private VertexAttribute[] _vertexAttributes;
@@ -373,6 +371,10 @@ public bool RecalculateTangents()
373371
return false;
374372
}
375373

374+
public void Serialize(BinarySerializer serializer)
375+
{
376+
}
377+
376378
public unsafe class CpuBuffer
377379
{
378380
protected byte* _data;

src/Alimer/Rendering/MeshData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Alimer.Rendering;
88

9-
public struct MeshData
9+
public struct MeshData
1010
{
1111
public MeshData()
1212
{

0 commit comments

Comments
 (0)