forked from Misaki/GhostEngine
Added `InternalsVisibleTo` attribute for "Ghost.Graphics" and "Ghost.Editor" in `AssemblyInfo.cs`. Added a new `EngineAssemblyAttribute` in `EngineAssemblyAttribute.cs`. Added a reference to `Misaki.HighPerformance.Unsafe` in `Ghost.Core.csproj`. Added a new `Bounds` struct to represent axis-aligned bounding boxes in `Bounds.cs`. Added new `Color32` and `Color128` structs for color representation in `Color.cs`. Changed the namespace from `Ghost.Editor.Controls` to `Ghost.Editor.Core.Controls` in multiple files. Changed the implicit conversion operator in `ConstPtr<T>` to use a more descriptive parameter name in `ConstPtr.cs`. Changed the `Mesh` class to use `Color128` instead of `Color32` for color representation. Enhanced the `TypeCache` class to load types from assemblies marked with `EngineAssemblyAttribute`. Enhanced the `ProjectService` class to improve the `GetAllProjectAsync` method by filtering out bad projects. Enhanced the `GraphicsPipeline` class to support both DX12 and D3D12 graphics APIs. Enhanced the `Shader` class to include methods for compiling HLSL shaders and managing root signatures. Enhanced the `MeshRenderPass` class to utilize the new shader compilation methods. Refactored the `AppStateMachine` class to use private fields instead of static fields for state management. Refactored the `ComponentDataView` class to use the new namespace and improve organization. Refactored project references in `Ghost.Graphics.csproj` to include new dependencies and remove outdated ones. Made various adjustments to ensure consistency and improve code quality across multiple files.
138 lines
4.8 KiB
C#
138 lines
4.8 KiB
C#
using Ghost.Graphics.Data;
|
|
using System.Numerics;
|
|
|
|
namespace Ghost.Graphics.Utilities;
|
|
|
|
public static class MeshBuilder
|
|
{
|
|
/// <summary>
|
|
/// Creates a unit cube centered at the origin with size 1.
|
|
/// </summary>
|
|
public static Mesh CreateCube(float size = 1.0f, Color128 color = default, Vector3 offset = default)
|
|
{
|
|
var half = size * 0.5f;
|
|
var mesh = new Mesh(24, 36);
|
|
|
|
var corners = new Vector3[]
|
|
{
|
|
new Vector3(-half, -half, -half) + offset, new Vector3( half, -half, -half) + offset,
|
|
new Vector3( half, half, -half) + offset, new Vector3(-half, half, -half) + offset,
|
|
new Vector3(-half, -half, half) + offset, new Vector3( half, -half, half) + offset,
|
|
new Vector3( half, half, half) + offset, new Vector3(-half, half, half) + offset
|
|
};
|
|
|
|
int[][] faces =
|
|
[
|
|
[0,1,2,3],
|
|
[5,4,7,6],
|
|
[4,0,3,7],
|
|
[1,5,6,2],
|
|
[3,2,6,7],
|
|
[4,5,1,0]
|
|
];
|
|
|
|
var uvs = new Vector2[] { new(0, 0), new(1, 0), new(1, 1), new(0, 1) };
|
|
|
|
foreach (var face in faces)
|
|
{
|
|
var baseIndex = mesh.VertexCount;
|
|
for (var i = 0; i < 4; i++)
|
|
{
|
|
var vertex = new Vertex
|
|
{
|
|
Position = corners[face[i]].AsVector4(),
|
|
Normal = Vector4.Zero,
|
|
Tangent = Vector4.Zero,
|
|
Color = color,
|
|
UV = uvs[i].AsVector4()
|
|
};
|
|
mesh.AddVertex(new(corners[face[i]].AsVector4(), Vector4.Zero, Vector4.Zero, color, uvs[i].AsVector4()));
|
|
}
|
|
|
|
mesh.AddTriangle((int)baseIndex + 0, (int)baseIndex + 1, (int)baseIndex + 2);
|
|
mesh.AddTriangle((int)baseIndex + 0, (int)baseIndex + 2, (int)baseIndex + 3);
|
|
}
|
|
|
|
mesh.ComputeNormal();
|
|
mesh.ComputeTangents();
|
|
return mesh;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a plane on the XZ axis centered at the origin.
|
|
/// </summary>
|
|
public static Mesh CreatePlane(float width = 1.0f, float depth = 1.0f, Color128 color = default)
|
|
{
|
|
var hw = width * 0.5f;
|
|
var hd = depth * 0.5f;
|
|
var mesh = new Mesh(4, 6);
|
|
|
|
mesh.AddVertex(new(new(-hw, 0.0f, -hd, 0.0f), Vector4.Zero, Vector4.Zero, color, new(0.0f)));
|
|
mesh.AddVertex(new(new(hw, 0.0f, -hd, 0.0f), Vector4.Zero, Vector4.Zero, color, new(1.0f, 0.0f, 0.0f, 0.0f)));
|
|
mesh.AddVertex(new(new(hw, 0.0f, hd, 0.0f), Vector4.Zero, Vector4.Zero, color, new(1.0f, 1.0f, 0.0f, 0.0f)));
|
|
mesh.AddVertex(new(new(-hw, 0.0f, hd, 0.0f), Vector4.Zero, Vector4.Zero, color, new(0.0f, 1.0f, 0.0f, 0.0f)));
|
|
|
|
mesh.AddTriangle(0, 1, 2);
|
|
mesh.AddTriangle(0, 2, 3);
|
|
|
|
mesh.ComputeNormal();
|
|
mesh.ComputeTangents();
|
|
return mesh;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a UV sphere centered at the origin.
|
|
/// </summary>
|
|
public static Mesh CreateSphere(int latitudeSegments = 16, int longitudeSegments = 24, float radius = 0.5f, Color128 color = default)
|
|
{
|
|
var mesh = new Mesh((latitudeSegments + 1) * (longitudeSegments + 1), latitudeSegments * longitudeSegments * 6);
|
|
|
|
// Vertices
|
|
for (var lat = 0; lat <= latitudeSegments; lat++)
|
|
{
|
|
var theta = (float)lat / latitudeSegments * MathF.PI;
|
|
var sinTheta = MathF.Sin(theta);
|
|
var cosTheta = MathF.Cos(theta);
|
|
|
|
for (var lon = 0; lon <= longitudeSegments; lon++)
|
|
{
|
|
var phi = (float)lon / longitudeSegments * 2 * MathF.PI;
|
|
var sinPhi = MathF.Sin(phi);
|
|
var cosPhi = MathF.Cos(phi);
|
|
|
|
var x = cosPhi * sinTheta;
|
|
var y = cosTheta;
|
|
var z = sinPhi * sinTheta;
|
|
|
|
mesh.AddVertex(
|
|
new()
|
|
{
|
|
Position = new Vector4(x, y, z, 0.0f) * radius,
|
|
Normal = Vector4.Zero,
|
|
Tangent = Vector4.Zero,
|
|
Color = color,
|
|
UV = new Vector4((float)lon / longitudeSegments, (float)lat / latitudeSegments, 0.0f, 0.0f)
|
|
});
|
|
}
|
|
}
|
|
|
|
// Indices
|
|
for (var lat = 0; lat < latitudeSegments; lat++)
|
|
{
|
|
for (var lon = 0; lon < longitudeSegments; lon++)
|
|
{
|
|
var i0 = lat * (longitudeSegments + 1) + lon;
|
|
var i1 = i0 + longitudeSegments + 1;
|
|
var i2 = i1 + 1;
|
|
var i3 = i0 + 1;
|
|
|
|
mesh.AddTriangle(i0, i1, i2);
|
|
mesh.AddTriangle(i0, i2, i3);
|
|
}
|
|
}
|
|
|
|
mesh.ComputeNormal();
|
|
mesh.ComputeTangents();
|
|
return mesh;
|
|
}
|
|
} |