Skip to content

Commit 655e2bb

Browse files
[Entities] Add TryGetComponentEntity static method;
[Math] Change AABB CreateFromPoints to use Span; [Rendering] Fix MeshRenderer dispose not validating if the mesh is valid; [Serialization] Prevent exception when getting Guid Asset Paths when they have null guid; [Serialization] Fix handling of null items when deserializing arrays; [Utilities] Fix meshes generated with CubicMeshBuilder not having calculated bounds; [Editor] Add Noise Generator Settings Editor; [Editor] Fix inconsistent EditorGUI Changed handling for dropdowns; [Test Project] Fix invalid asset data for PlayerMovementInputs asset;
1 parent 67611ad commit 655e2bb

File tree

13 files changed

+163
-115
lines changed

13 files changed

+163
-115
lines changed

Engine/Core/Entities/Entity+Components.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,4 +351,22 @@ public readonly void SetComponent(IComponent component)
351351

352352
World.Current.SetComponent(this, component);
353353
}
354+
355+
/// <summary>
356+
/// Attempts to get a component's entity
357+
/// </summary>
358+
/// <param name="component">The component</param>
359+
/// <param name="entity">The entity</param>
360+
/// <returns>Whether the entity was found</returns>
361+
public static bool TryGetComponentEntity(IComponent component, out Entity entity)
362+
{
363+
if(World.Current == null)
364+
{
365+
entity = default;
366+
367+
return false;
368+
}
369+
370+
return World.Current.TryGetComponentEntity(component, out entity);
371+
}
354372
}

Engine/Core/Math/AABB.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Numerics;
1+
using System;
2+
using System.Numerics;
23
using System.Runtime.InteropServices;
34

45
namespace Staple;
@@ -100,7 +101,7 @@ public static AABB CreateFromMinMax(Vector3 min, Vector3 max)
100101
/// </summary>
101102
/// <param name="points">The points to validate</param>
102103
/// <returns>The AABB</returns>
103-
public static AABB CreateFromPoints(params Vector3[] points)
104+
public static AABB CreateFromPoints(Span<Vector3> points)
104105
{
105106
var min = Vector3.One * 999999;
106107
var max = Vector3.One * -999999;

Engine/Core/Rendering/Mesh/MeshRenderer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public sealed class MeshRenderer : Renderable, IComponentDisposable
2525

2626
public void DisposeComponent()
2727
{
28-
if(mesh.Guid == null)
28+
if(mesh != null && mesh.Guid == null)
2929
{
3030
mesh.Destroy();
3131
}

Engine/Core/Serialization/Asset/AssetSerialization.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ internal static partial class AssetSerialization
118118
/// <returns>The estimatd valid path</returns>
119119
public static string GetAssetPathFromCache(string path)
120120
{
121+
if(path == null)
122+
{
123+
return null;
124+
}
125+
121126
var matches = cachePathRegex.Matches(path);
122127

123128
if (matches.Count > 0)
@@ -145,6 +150,11 @@ public static object GetGuidAsset(
145150
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.Interfaces)]
146151
Type type, string guid)
147152
{
153+
if (guid == null)
154+
{
155+
return null;
156+
}
157+
148158
var methods = type.GetMethods();
149159

150160
foreach (var method in methods)
@@ -160,7 +170,7 @@ public static object GetGuidAsset(
160170

161171
try
162172
{
163-
var result = method.Invoke(null, new object[] { guid });
173+
var result = method.Invoke(null, [ guid ]);
164174

165175
if (result == null || (result.GetType() != type && result.GetType().GetInterface(type.FullName) == null))
166176
{

Engine/Core/Serialization/Serializer/StapleSerializer.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,11 @@ atn is string arrayTypeName &&
11181118
{
11191119
foreach (var item in array)
11201120
{
1121-
if (item.GetType().IsAssignableTo(elementType) ||
1121+
if(item == null)
1122+
{
1123+
list.Add(null);
1124+
}
1125+
else if (item.GetType().IsAssignableTo(elementType) ||
11221126
elementType.IsPrimitive)
11231127
{
11241128
list.Add(item);

Engine/Core/Utilities/CubicMeshBuilder.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Numerics;
4+
using System.Runtime.InteropServices;
45

56
namespace Staple.Utilities;
67

@@ -322,6 +323,8 @@ public Mesh BuildMesh(bool addUVs)
322323
changed = true,
323324
};
324325

326+
mesh.bounds = AABB.CreateFromPoints(CollectionsMarshal.AsSpan(vertices));
327+
325328
if (addUVs)
326329
{
327330
mesh.uv = uvs.ToArray();
File renamed without changes.

Engine/Editor/Editors/CustomPropertyDrawerAttribute.cs renamed to Engine/Editor/Attributes/CustomPropertyDrawerAttribute.cs

File renamed without changes.
File renamed without changes.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System.Numerics;
2+
3+
namespace Staple.Editor;
4+
5+
[CustomEditor(typeof(NoiseGeneratorSettings))]
6+
public class NoiseGeneratorSettingsEditor : StapleAssetEditor
7+
{
8+
private const int TextureSize = 512;
9+
10+
private Texture previewTexture;
11+
12+
public override void OnInspectorGUI()
13+
{
14+
base.OnInspectorGUI();
15+
16+
if(target is not NoiseGeneratorSettings settings)
17+
{
18+
return;
19+
}
20+
21+
if(EditorGUI.Changed || previewTexture == null)
22+
{
23+
previewTexture?.Destroy();
24+
25+
var generator = settings.MakeGenerator();
26+
27+
var pixels = new Color[TextureSize * TextureSize];
28+
29+
for(int y = -TextureSize / 2, yIndex = 0; y < TextureSize / 2; y++, yIndex += TextureSize)
30+
{
31+
for(var x = -TextureSize / 2; x < TextureSize / 2; x++)
32+
{
33+
pixels[x + TextureSize / 2 + yIndex] = Color.White * (generator.GetNoise(x, y) * 0.5f + 0.5f);
34+
}
35+
}
36+
37+
var buffer = new byte[pixels.Length * 4];
38+
39+
for(var i = 0; i < pixels.Length; i++)
40+
{
41+
var c = (Color32)pixels[i];
42+
43+
buffer[i * 4] = c.r;
44+
buffer[i * 4 + 1] = c.g;
45+
buffer[i * 4 + 2] = c.b;
46+
buffer[i * 4 + 3] = c.a;
47+
}
48+
49+
previewTexture = Texture.CreatePixels(null, buffer, TextureSize, TextureSize, new(), TextureFormat.RGBA8);
50+
}
51+
52+
if(previewTexture != null)
53+
{
54+
var width = EditorGUI.RemainingHorizontalSpace();
55+
56+
EditorGUI.Texture(previewTexture, new Vector2(width, width));
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)