Skip to content

Commit

Permalink
feat: Solid Point + Toggle Render Type submenu
Browse files Browse the repository at this point in the history
- Added support for Solid Point regions
- Added Toggle Render Type to the Edit submenu
  • Loading branch information
vawser committed Jan 31, 2025
1 parent 348e7f0 commit 0b45849
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 11 deletions.
3 changes: 0 additions & 3 deletions Documentation/smithbox.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ Region Select:
- Once select is submitted, add all map objects within the bounding box of the region to the current selection, delete the 'selection' region.

Visualisations:
- Add ability to set temporary color override with hotkey for solid region

- Add the transparent solid regions for more types:
- Point
- Model Marker
- Sphere with Arrow
- Point Light
Expand Down
10 changes: 10 additions & 0 deletions src/StudioCore/Editors/MapEditor/MapEditorScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using StudioCore.Editors.MapEditor.Core;
using StudioCore.Editors.MapEditor.Enums;
using StudioCore.Editors.MapEditor.Framework;
using StudioCore.Editors.MapEditor.Helpers;
using StudioCore.Editors.MapEditor.PropertyEditor;
using StudioCore.Editors.MapEditor.Tools;
using StudioCore.Editors.MapEditor.Tools.AssetBrowser;
Expand Down Expand Up @@ -414,6 +415,15 @@ public void EditDropdown()
}
UIHelper.ShowHoverTooltip("Move the current selection to the camera position.");

///--------------------
// Toggle Render Type
///--------------------
if (ImGui.MenuItem("Toggle Render Type", KeyBindings.Current.VIEWPORT_ToggleRenderType.HintText))
{
VisualizationHelper.ToggleRenderType(Selection);
}
UIHelper.ShowHoverTooltip("Toggle the render type of the current selection.");

ImGui.Separator();

///--------------------
Expand Down
78 changes: 78 additions & 0 deletions src/StudioCore/Scene/DebugPrimitives/DbgPrimSolidPoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using Veldrid.Utilities;

namespace StudioCore.Scene.DebugPrimitives;

public class DbgPrimSolidPoint : DbgPrimSolid
{
private readonly DbgPrimGeometryData GeometryData;

public override BoundingBox Bounds => new BoundingBox(new Vector3(-1, -1, -1), new Vector3(1, 1, 1));

public DbgPrimSolidPoint(Transform location, float radius, Color color, int numVerticalSegments = 1, int numSidesPerSegment = 4)
{
if (GeometryData != null)
{
SetBuffers(GeometryData.GeomBuffer);
}
else
{
NameColor = color;
var vertices = new Vector3[numVerticalSegments + 2, numSidesPerSegment];

Vector3 topPoint = Vector3.UnitY * radius;
Vector3 bottomPoint = -Vector3.UnitY * radius;

for (int i = 0; i < numVerticalSegments; i++)
{
for (int j = 0; j < numSidesPerSegment; j++)
{
float horizontalAngle = j / (float)numSidesPerSegment * Utils.Pi * 2.0f;
float verticalAngle = (i + 1) / (float)(numVerticalSegments + 1) * Utils.Pi - Utils.PiOver2;
float altitude = (float)Math.Sin(verticalAngle);
float horizontalDist = (float)Math.Cos(verticalAngle);

vertices[i, j] = new Vector3(
(float)Math.Cos(horizontalAngle) * horizontalDist,
altitude,
(float)Math.Sin(horizontalAngle) * horizontalDist) * radius;
}
}

// Generate faces
for (int j = 0; j < numSidesPerSegment; j++)
{
int nextJ = (j + 1) % numSidesPerSegment;
// Bottom cap
AddTri(vertices[0, j], bottomPoint, vertices[0, nextJ], color);
// Top cap
AddTri(topPoint, vertices[numVerticalSegments - 1, j], vertices[numVerticalSegments - 1, nextJ], color);
}

for (int i = 0; i < numVerticalSegments - 1; i++)
{
for (int j = 0; j < numSidesPerSegment; j++)
{
int nextJ = (j + 1) % numSidesPerSegment;

// Two triangles forming a quad
AddTri(vertices[i, j], vertices[i + 1, j], vertices[i, nextJ], color);
AddTri(vertices[i + 1, j], vertices[i + 1, nextJ], vertices[i, nextJ], color);
}
}

GeometryData = new DbgPrimGeometryData { GeomBuffer = GeometryBuffer };
}

Renderer.AddBackgroundUploadTask((d, cl) =>
{
UpdatePerFrameResources(d, cl, null);
});
}
}
98 changes: 98 additions & 0 deletions src/StudioCore/Scene/DebugPrimitives/DbgPrimWirePoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using StudioCore.Scene;
using System;
using System.Drawing;
using System.Numerics;
using Veldrid.Utilities;

namespace StudioCore.Scene.DebugPrimitives;


public class DbgPrimWirePoint : DbgPrimWire
{
private static readonly DbgPrimGeometryData GeometryData = null;

public DbgPrimWirePoint(Transform location, float radius, Color color, int numVerticalSegments = 1,
int numSidesPerSegment = 4)
{
NameColor = color;

if (GeometryData != null)
{
SetBuffers(GeometryData.GeomBuffer);
}
else
{
Vector3 topPoint = Vector3.UnitY * radius;
Vector3 bottomPoint = -Vector3.UnitY * radius;
var points = new Vector3[numVerticalSegments, numSidesPerSegment];

for (var i = 0; i < numVerticalSegments; i++)
{
for (var j = 0; j < numSidesPerSegment; j++)
{
var horizontalAngle = 1.0f * j / numSidesPerSegment * Utils.Pi * 2.0f;
var verticalAngle = 1.0f * (i + 1) / (numVerticalSegments + 1) * Utils.Pi - Utils.PiOver2;
var altitude = (float)Math.Sin(verticalAngle);
var horizontalDist = (float)Math.Cos(verticalAngle);
points[i, j] = new Vector3((float)Math.Cos(horizontalAngle) * horizontalDist, altitude,
(float)Math.Sin(horizontalAngle) * horizontalDist) * radius;
}
}

for (var i = 0; i < numVerticalSegments; i++)
{
for (var j = 0; j < numSidesPerSegment; j++)
{
// On the bottom, we must connect each to the bottom point
if (i == 0)
{
AddLine(points[i, j], bottomPoint, color);
}

// On the top, we must connect each point to the top
// Note: this isn't "else if" because with 2 segments,
// these are both true for the only ring
if (i == numVerticalSegments - 1)
{
AddLine(points[i, j], topPoint, color);
}

// Make vertical lines that connect from this
// horizontal ring to the one above
// Since we are connecting
// (current) -> (the one above current)
// we dont need to do this for the very last one.
if (i < numVerticalSegments - 1)
{
AddLine(points[i, j], points[i + 1, j], color);
}


// Make lines that connect points horizontally
//---- if we reach end, we must wrap around,
//---- otherwise, simply make line to next one
if (j == numSidesPerSegment - 1)
{
AddLine(points[i, j], points[i, 0], color);
}
else
{
AddLine(points[i, j], points[i, j + 1], color);
}
}
}

Renderer.AddBackgroundUploadTask((d, cl) =>
{
UpdatePerFrameResources(d, cl, null);
});
}
}

public bool RayCast(Ray ray, Matrix4x4 transform, out float dist)
{
var radius = Vector3.TransformNormal(Vector3.UnitX, transform).Length();
Vector3 pos = Vector3.Transform(Vector3.Zero, transform);
return Utils.RaySphereIntersection(ref ray, pos, radius, out dist);
}
}
15 changes: 15 additions & 0 deletions src/StudioCore/Scene/Helpers/BoundingBoxHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,33 @@ public static BoundingBox GetDerivedBoundingBox(Entity s)
if (shape is MSB.Shape.Circle)
{
var circleShape = (MSB.Shape.Circle)shape;

var height = 1.0f;
var radius = circleShape.Radius;

framingBounds = GenerateBoundingBox(position, radius, height);
}

// Rectangle
if (shape is MSB.Shape.Rectangle)
{
var rectShape = (MSB.Shape.Rectangle)shape;

var width = rectShape.Width;
var depth = rectShape.Depth;
var height = 1.0f;

framingBounds = GenerateBoundingBox(position, width, height, depth);
}

// Point
if (shape is MSB.Shape.Point)
{
var pointShape = (MSB.Shape.Point)shape;

var radius = 1.0f;

framingBounds = GenerateBoundingBox(position, radius);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/StudioCore/Scene/Helpers/DrawableHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public static RenderableProxy GetRegionDrawable(RenderScene scene, MapContainer
// CIRCLE
else if (r.Shape is MSB.Shape.Circle)
{
mesh = RenderableHelper.GetSolidBoxRegionProxy(scene);
mesh = RenderableHelper.GetSolidCylinderRegionProxy(scene);
}
// COMPOSITE
else if (r.Shape is MSB.Shape.Composite)
Expand Down
20 changes: 13 additions & 7 deletions src/StudioCore/Scene/Helpers/RenderableHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ namespace StudioCore.Scene.Helpers;

public static class RenderableHelper
{
private static DbgPrimSolidBox? _regionSolidBox;
private static DbgPrimWireBox? _regionBox;
private static DbgPrimSolidBox? _regionSolidBox;

private static DbgPrimSolidCylinder? _regionSolidCylinder;
private static DbgPrimWireCylinder? _regionCylinder;
private static DbgPrimSolidCylinder? _regionSolidCylinder;

private static DbgPrimSolidSphere? _regionSolidSphere;
private static DbgPrimWireSphere? _regionSphere;
private static DbgPrimSolidSphere? _regionSolidSphere;

private static DbgPrimWireSphere? _regionPoint;
private static DbgPrimWirePoint? _regionPoint;
private static DbgPrimSolidPoint? _regionSolidPoint;

private static DbgPrimWireSphere? _pointLight;
private static DbgPrimWireSpotLight? _spotLight;
Expand Down Expand Up @@ -81,17 +82,22 @@ public static void InitializeDebugMeshes()
1.0f,
Color.Blue);

_regionPoint = new DbgPrimWireSphere(
_regionPoint = new DbgPrimWirePoint(
Transform.Default,
1.0f,
Color.Yellow,
1,
4);

_regionSolidPoint = new DbgPrimSolidPoint(
Transform.Default,
1.0f,
Color.Blue);

_dmyPoint = new DbgPrimWireSphere(
Transform.Default,
0.05f,
Color.Yellow,
Color.Blue,
1,
4);

Expand Down Expand Up @@ -284,7 +290,7 @@ public static DebugPrimitiveRenderableProxy GetSolidPointRegionProxy(RenderScene
var highlightColor = CFG.Current.GFX_Renderable_Point_HighlightColor;
var transparency = CFG.Current.GFX_Renderable_Point_Alpha;

DebugPrimitiveRenderableProxy r = new(scene.OpaqueRenderables, _regionPoint);
DebugPrimitiveRenderableProxy r = new(scene.OpaqueRenderables, _regionSolidPoint);

r.BaseColor = ColorHelper.GetTransparencyColor(baseColor, transparency);
r.HighlightedColor = ColorHelper.GetTransparencyColor(highlightColor, transparency);
Expand Down

0 comments on commit 0b45849

Please sign in to comment.