ModelRenderer - Add part highlighting

This commit is contained in:
miku-666
2024-10-27 16:46:12 +01:00
parent cc514d47bb
commit 254884e2a0
2 changed files with 56 additions and 3 deletions

View File

@@ -228,7 +228,7 @@ namespace PckStudio.Forms.Editor
exportToolStripMenuItem.Visible = e.Node is ModelNode;
editToolStripMenuItem.Visible = e.Node is ModelBoxNode;
removeToolStripMenuItem.Visible = e.Node is ModelPartNode || e.Node is ModelBoxNode;
if (e.Node is ModelNode modelNode)
if (e.Node is ModelNode modelNode && modelNode.Model.Name != modelViewport.CurrentModelName)
{
NamedTexture[] textures = GetModelTextures(modelNode.Model.Name).ToArray();
@@ -246,6 +246,14 @@ namespace PckStudio.Forms.Editor
modelViewport.LoadModel(modelNode.Model);
modelViewport.ResetCamera();
}
if (e.Node is ModelPartNode modelPartNode && modelPartNode.Parent is ModelNode parentNode && modelViewport.CurrentModelName == parentNode.Model.Name)
{
modelViewport.Highlight(modelPartNode.Part);
}
//if (e.Node is ModelBoxNode modelBoxNode)
//{
//}
}
private IEnumerable<NamedTexture> GetModelTextures(string modelName)

View File

@@ -48,10 +48,34 @@ namespace PckStudio.Rendering
}
public bool RenderModelBounds { get; set; }
public string CurrentModelName => _currentModelName;
private BoundingBox _maxBounds;
private string _currentModelName;
private Image _modelTexture;
private Texture2D _modelRenderTexture;
private List<CubeMeshCollection> _rootCollection;
private struct HighlightInfo
{
public static readonly HighlightInfo Empty = new HighlightInfo(Vector3.Zero, Vector3.Zero, BoundingBox.Empty);
public bool IsEmpty => BoundingBox.Volume.LengthSquared <= 0f;
public BoundingBox BoundingBox { get; }
public Vector3 Pivot { get; }
public Vector3 Rotation { get; }
public HighlightInfo(System.Numerics.Vector3 pivot, System.Numerics.Vector3 rotation, BoundingBox boundingBox)
: this(pivot.ToOpenTKVector(),rotation.ToOpenTKVector(), boundingBox)
{
}
public HighlightInfo(Vector3 pivot, Vector3 rotation, BoundingBox boundingBox)
{
Pivot = pivot;
Rotation = rotation;
BoundingBox = boundingBox;
}
}
private HighlightInfo _highlightingInfo;
public ModelRenderer() : base(fov: 60f)
{
@@ -93,6 +117,7 @@ namespace PckStudio.Rendering
public void LoadModel(Model model)
{
ResetHighlight();
_rootCollection?.Clear();
_maxBounds = model.GetParts()
@@ -141,6 +166,7 @@ namespace PckStudio.Rendering
shader.Bind();
shader.SetUniform2("TexSize", model.TextureSize);
}
_currentModelName = model.Name;
}
public override void ResetCamera(Vector3 offset)
@@ -216,12 +242,31 @@ namespace PckStudio.Rendering
DrawMesh(item, shader, item.GetTransform() * renderTransform);
}
_modelRenderTexture.Unbind();
if (!_highlightingInfo.IsEmpty)
{
Matrix4 highlightMatrix = Matrix4.Identity;
highlightMatrix *= Matrix4.CreateRotationX(MathHelper.DegreesToRadians(_highlightingInfo.Rotation.X));
highlightMatrix *= Matrix4.CreateRotationY(MathHelper.DegreesToRadians(_highlightingInfo.Rotation.Y));
highlightMatrix *= Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(_highlightingInfo.Rotation.Z));
highlightMatrix = Matrix4.CreateTranslation(_highlightingInfo.Pivot) * highlightMatrix.Pivoted(_highlightingInfo.Pivot * -1) * renderTransform;
DrawBoundingBox(highlightMatrix, _highlightingInfo.BoundingBox, Color.HotPink);
}
if (RenderModelBounds)
{
DrawBoundingBox(renderTransform, _maxBounds, Color.Red);
}
}
}
internal void Highlight(ModelPart part)
{
BoundingBox bb = part.GetBoxes().Select(b => new BoundingBox(b.Position, b.Position + b.Size)).GetEnclosingBoundingBox();
_highlightingInfo = new HighlightInfo(part.Translation, part.Rotation + part.AdditionalRotation, bb);
}
internal void ResetHighlight()
{
_highlightingInfo = HighlightInfo.Empty;
}
}
}