Unity的Gizmos可以很方便的在编辑器下进行调试,Unreal中也有一些办法可以达到效果。

本文主要参考:https://zhuanlan.zhihu.com/p/363625037,进行了一些简化。并在Unreal 4.27中实现。

具体流程如下:

  1. 需要绘制Gizmo的Actor挂载继承UPrimitiveComponent的组件;该组件重写了CreateSceneProxy方法,这个方法里可以拿到PDI绘制
  2. 然后进行这个组件的编写(继承UPrimitiveComponent实际上也继承了USceneComponent),手动挂载到Actor上就可以绘制Gizmo了
  3. 再在Actor的构造函数中编写自动挂载该组件的逻辑,方便使用

先来编写绘制Gizmo的组件,这里命名为UMyPrimitiveComponent:

MyPrimitiveComponent.h (MYPROJECT_API注意替换)

//MyPrimitiveComponent.h

#pragma once

#include "CoreMinimal.h"
#include "Components/PrimitiveComponent.h"
#include "PrimitiveSceneProxy.h"
#include "MyPrimitiveComponent.generated.h" UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class MYPROJECT_API UMyPrimitiveComponent : public UPrimitiveComponent
{
GENERATED_BODY() public:
//绘制逻辑主要在这里
virtual FPrimitiveSceneProxy* CreateSceneProxy() override; //如果要在非选中情况下始终绘制的话,需要有Bounds信息,所以要重写该函数
virtual FBoxSphereBounds CalcBounds(const FTransform& LocalToWorld) const;
};

MyPrimitiveComponent.cpp

//MyPrimitiveComponent.cpp
#include "MyPrimitiveComponent.h" FPrimitiveSceneProxy* UMyPrimitiveComponent::CreateSceneProxy()
{
class FMySceneProxy : public FPrimitiveSceneProxy
{
public: SIZE_T GetTypeHash() const override
{
static size_t UniquePointer;
return reinterpret_cast<size_t>(&UniquePointer);
} FMySceneProxy(const UPrimitiveComponent* InComponent) : FPrimitiveSceneProxy(InComponent)
{
CacheInComponent = InComponent;
bWillEverBeLit = false;
} virtual void GetDynamicMeshElements(const TArray<const FSceneView*>& Views, const FSceneViewFamily& ViewFamily, uint32 VisibilityMap, FMeshElementCollector& Collector) const override
{
QUICK_SCOPE_CYCLE_COUNTER(STAT_Draw3DAgentSceneProxy_GetDynamicMeshElements); for (int32 ViewIndex = 0; ViewIndex < Views.Num(); ViewIndex++)
{
if (VisibilityMap & (1 << ViewIndex))
{
const FSceneView* View = Views[ViewIndex];
FPrimitiveDrawInterface* PDI = Collector.GetPDI(ViewIndex); //拿到Actor的矩阵绘制,而不是Component自己的
const FMatrix& LocalToWorld = CacheInComponent->GetTypedOuter<AActor>()->GetTransform().ToMatrixWithScale(); //绘制函数可以去看下PrimitiveDrawingUtils.cpp
DrawOrientedWireBox(PDI
, LocalToWorld.TransformPosition(FVector::ZeroVector)
, LocalToWorld.GetScaledAxis(EAxis::X)
, LocalToWorld.GetScaledAxis(EAxis::Y)
, LocalToWorld.GetScaledAxis(EAxis::Z)
, FVector(100.0, 100.0, 100.0)
, FLinearColor(1.0, 0.0, 0.0, 1.0)
, SDPG_World
, 1.0);
}
}
}
virtual FPrimitiveViewRelevance GetViewRelevance(const FSceneView* View) const override
{
/*const bool bVisibleForSelection = IsSelected();
const bool bShowForCollision = View->Family->EngineShowFlags.Collision && IsCollisionEnabled(); FPrimitiveViewRelevance Result;
Result.bDrawRelevance = (IsShown(View) && bVisibleForSelection) || bShowForCollision;
Result.bDynamicRelevance = true;
Result.bShadowRelevance = IsShadowCast(View);
Result.bEditorPrimitiveRelevance = true;
Result.bEditorNoDepthTestPrimitiveRelevance = true; */
//上面这段表示选中绘制Gizmo FPrimitiveViewRelevance Result;
Result.bDrawRelevance = true;
Result.bDynamicRelevance = true;
Result.bShadowRelevance =false;
Result.bEditorPrimitiveRelevance = true;
Result.bEditorNoDepthTestPrimitiveRelevance = true;
//这段表示始终绘制 return Result;
} virtual uint32 GetMemoryFootprint(void) const override { return(sizeof(*this) + GetAllocatedSize()); }
uint32 GetAllocatedSize(void) const { return(FPrimitiveSceneProxy::GetAllocatedSize()); } private:
const UPrimitiveComponent* CacheInComponent;
}; return new FMySceneProxy(this);
} FBoxSphereBounds UMyPrimitiveComponent::CalcBounds(const FTransform& LocalToWorld) const
{
return FBoxSphereBounds(FBox(FVector(-50, -50, -50), FVector(50, 50, 50))).TransformBy(LocalToWorld);
//因为缩放是1,这里填的就是实际尺寸,也可以遍历所有组件取最大Bounds.
}

然后手动再挂载一下组件,就有效果了:

如果需要像Unity那样;直接就有Gizmo,可以在Actor构造函数中编写逻辑自动挂载组件:

AMyTestActor::AMyTestActor()
{
UBBoxActorGizmoComponent* Gizmo = CreateDefaultSubobject<UBBoxActorGizmoComponent>(TEXT("Gizmo"));
if (Gizmo)
{
Gizmo->SetupAttachment(GetRootComponent());
}
}

UE4 绘制Gizmo的更多相关文章

  1. Unity3D脚本中文系列教程(十七)

    http://dong2008hong.blog.163.com/blog/static/469688272014032332976/ ◆ Static function PrefixLabel(to ...

  2. 从Unity中的Attribute到AOP(八)

    本文将讲一下在UnityEditor命名空间下的一些特性. CallBackOrder,这个Attribute是所有带callback index的Attribute的基类,由于官方也没有给出详细的说 ...

  3. Unreal学习笔记2-绘制简单三角形

    目录 1. 概述 2. 详论 2.1. 代码实现 2.2. 解析:Component 2.3. 解析:材质 2.4. 解析:包围盒 2.5. 解析:Section 3. 其他 4. 参考 1. 概述 ...

  4. UE4 Tutorial - Custom Mesh Component 用于绘制自定义网格的插件CustomMeshComponent

    UE4 中用于绘制自定义网格的插件CustomMeshComponent. 转载: UE4 Tutorial - Custom Mesh Component   Over the last few w ...

  5. unity gizmo绘制圆形帮助调试

    using UnityEngine; using System.Collections; using System; public class LearnGrazio : MonoBehaviour ...

  6. UE4射线的碰撞与绘制

    http://blog.csdn.net/qq992817263/article/details/51800657 //起点 终点 FHitResult RayGetHitResult(FVector ...

  7. [UE4][Canvas]用C++代码绘制血条(HealthBar)

    转自:http://aigo.iteye.com/blog/2275110 参考自Epic官方项目StrategyGame 血条效果: StrategyHUD.h StrategyHUD.cpp

  8. 【UE4 C++】绘制函数 Debug drawing functions

    基于UKismetSystemLibrary /** Draw a debug line */ UFUNCTION(BlueprintCallable, Category="Renderin ...

  9. UE4命令行使用,解释

    命令行在外部 从命令行运行编辑项目 1 导航到您的[LauncherInstall][VersionNumber]\Engine\Binaries\Win64 目录中. 2 右键单击上 UE4Edit ...

  10. UE4 在C++ 动态生成几何、BSP体、Brush ---- Mesh_Generation

    截至UE4  4.10 runtime 无法生成BSP类 ,只能通过自定义的Mesh的Vertex 进行绘制 ( Google 考证,能改UE4源码的请忽略 ) 可用到的 UE4 集成的Render ...

随机推荐

  1. LeetCode 416. 分割等和子集(bitset优化)

    LeetCode 416. 分割等和子集 1 题目描述 给你一个只包含正整数的非空数组nums.请你判断是否可以将这个数组分割成两个子集,使得两个子集的元素和相等. 1.1 输入测试 示例 1: 输入 ...

  2. #线段树#洛谷 4681 [THUSC2015]平方运算

    题目 给定一个数列 \(a\),维护以下两种操作 区间取平方 \(a[i]=a[i]^2\bmod p\) 区间和(不取模) \(p\) 为给定的小于 \(10^4\) 的数,\(n\leq 10^5 ...

  3. 全平台GPU通用AI视频补帧超分教程

    全平台GPU通用AI视频补帧超分教程 本教程只发布于https://www.cnblogs.com/Icys 注意:本教程需要一定的命令行和视频编码知识,请谨慎食用. 软件准备 realcugan-n ...

  4. Java 异常处理与正则表达式详解,实例演练及最佳实践

    Java 异常 - Try...Catch 在 Java 代码执行期间,可能会发生各种错误,包括程序员编码错误.用户输入错误以及其他不可预料的状况. 当错误发生时,Java 通常会停止并生成错误消息, ...

  5. 一种提升深度多视角行人检测的泛化性能的方法 Bringing Generalization to Deep Multi-View Pedestrian Detection

    一种提升深度多视角行人检测的泛化性能的方法 Bringing Generalization to Deep Multi-View Pedestrian Detection 论文url: https:/ ...

  6. MySQL—MySQL架构

    MySQL-MySQL架构 MySQL逻辑架构图如下: Connectors连接器:负责跟客户端建立连接: Management Serveices & Utilities系统管理和控制工具: ...

  7. PTA三次作业

    1.前言: 第一次作业难度较大,从无到有的设计,涉及到的主要类有Paper,Question,AnswerPaper,Main,主要题目方向为字符串判断与字符串处理(提取有效信息),判断对错算总分,配 ...

  8. 【知识点】如何快速开发、部署 Serverless 应用?

    简介: 本文将详细介绍如何开发和部署 Serverless 应用,并通过阿里云函数计算控制台与开发者工具 Serverless Devs 进行应用的初始化.部署:最后分享应用的调试,通过科学发布.可观 ...

  9. Lindorm-Operator云原生实践

    简介: Kubernetes 的CRD 机制(CustomResourceDefinition)支持通过自定义的controller来管理资源的生命周期,这样就可以像操作pod,deployment一 ...

  10. 性能提升 57% ,SMC-R 透明加速 TCP 实战解析 | 龙蜥技术

    ​简介:SMC-R 是如何加速 TCP 应用? 编者按:TCP 协议作为当前使用最为广泛的网络协议,场景遍布移动通信.数据中心等.对于数据中心场景,通过弹性 RDMA 实现高性能网络协议 SMC-R, ...