在UE中自由绘制基本图元的几种方法

1. 在UMG中可以使用UWidgetBlueprintLibrary类来绘制,

    UWidgetBlueprintLibrary类提供了一系列的静态函数可以使用。

    但只提供了DrawLine,DrawLines,DrawBox,DrawText,DrawTextFormatted函数,

    如果要自己绘制的圆形,需要在DrawLine的基础上自己来写。

    实际上UWidgetBlueprintLibrary类是对FSlateDrawElement的封装,所以也可以直接调用FSlateDrawElement

    FSlateDrawElement提供了MakeBox,MakeText,MakeSpline这些函数

    这种方式绘制的话有局限性:

    1)这种方法只能在UMG中绘制,因为它需要UMG的Onpaint函数传入context参数。

    2)用DrawLine自己封装的画圆函数锯齿严重,如果开启反锯齿参数,圆上居然有缺口的现象


2. 在Actor的Event tick函数里,可以使用drawdebug函数族来绘制图形。


3. 继承UMeshComponent组件来实现一个自己的组件,并添加到一个Actor中。

   在这里可以使用PDI函数来绘制。


4. 直接使用UProceduralMeshComponent组件并添加到Actor中。

   UProceduralMeshComponent提供了CreateMeshSection_LinearColor函数,把我们需要绘制的图型的顶点坐标,

   UV坐标,顶点颜色传入这个函数即可绘制。这样我们可以随意自由的绘制任何我们需要的图形。

   UProceduralMeshComponent并没有暴露给蓝图,因此需要在自己的Actor类里来创建出组件。

   另外需要调用SetMaterial(0, GEngine->VertexColorMaterial)这个函数,顶点的颜色才能生效。

   另外UProceduralMeshComponent提供了SetRelativeRotation了函数可以对组件进行旋转。

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的示例,演示如何在Qt使用鼠标绘制直线图: ```cpp #include <QtGui> #include <Widgets> class Line : public QGraphicsLineItem { public: Line(const QLineF& line) : QGraphicsLineItem(line) {} QRectF boundingRect() const override { return pen().widthF() + 20.0 + QGraphicsLineItem::boundingRect(); } QPainterPath shape() const override { QPainterPath path = QGraphicsLineItem::shape(); path.addRect(boundingRect()); return path; } void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override { if (option->state & QStyle::State_Selected) { painter->setPen(QPen(option->palette.windowText(), 0, Qt::DotLine)); painter->drawRect(boundingRect()); } QGraphicsLineItem::paint(painter, option, widget); } }; class GraphicsView : public QGraphicsView { public: GraphicsView(QWidget* parent = nullptr) : QGraphicsView(parent), m_isDrawing(false), m_line(nullptr) { setRenderHint(QPainter::Antialiasing); setDragMode(QGraphicsView::RubberBandDrag); setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing); setViewportUpdateMode(QGraphicsView::SmartViewportUpdate); setSelectionMode(QGraphicsView::SingleSelection); } protected: void mousePressEvent(QMouseEvent* event) override { if (event->button() == Qt::LeftButton) { m_startPos = mapToScene(event->pos()); m_isDrawing = true; } QGraphicsView::mousePressEvent(event); } void mouseMoveEvent(QMouseEvent* event) override { if (m_isDrawing) { if (!m_line) { m_line = new Line(QLineF(m_startPos, mapToScene(event->pos()))); scene()->addItem(m_line); } else { m_line->setLine(QLineF(m_startPos, mapToScene(event->pos()))); } } QGraphicsView::mouseMoveEvent(event); } void mouseReleaseEvent(QMouseEvent* event) override { if (event->button() == Qt::LeftButton && m_isDrawing) { m_isDrawing = false; m_line = nullptr; } QGraphicsView::mouseReleaseEvent(event); } private: bool m_isDrawing; QPointF m_startPos; Line* m_line; }; int main(int argc, char* argv[]) { QApplication app(argc, argv); QGraphicsScene scene; GraphicsView view(&scene); view.setRenderHint(QPainter::Antialiasing); view.setScene(&scene); view.setDragMode(QGraphicsView::RubberBandDrag); QGraphicsLineItem* line = new QGraphicsLineItem(QLineF(0.0, 0.0, 100.0, 100.0)); line->setPen(QPen(Qt::red, 2)); scene.addItem(line); QGraphicsEllipseItem* ellipse = new QGraphicsEllipseItem(QRectF(0.0, 0.0, 100.0, 100.0)); ellipse->setPen(QPen(Qt::blue, 2)); ellipse->setBrush(QBrush(Qt::green)); scene.addItem(ellipse); view.setSceneRect(-150, -150, 300, 300); view.setFixedSize(300, 300); view.show(); return app.exec(); } ``` 该示例演示了如何使用QGraphicsView和QGraphicsScene来绘制直线。在鼠标按下时,记录起始位置,并在鼠标移动时绘制直线。在鼠标释放时,停止绘制。该示例还演示了如何添加其他类型的图元,如椭圆和直线。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值