UE4 绘制Gizmo
Unity的Gizmos可以很方便的在编辑器下进行调试,Unreal中也有一些办法可以达到效果。
本文主要参考:https://zhuanlan.zhihu.com/p/363625037,进行了一些简化。并在Unreal 4.27中实现。
具体流程如下:
- 需要绘制Gizmo的Actor挂载继承UPrimitiveComponent的组件;该组件重写了CreateSceneProxy方法,这个方法里可以拿到PDI绘制
- 然后进行这个组件的编写(继承UPrimitiveComponent实际上也继承了USceneComponent),手动挂载到Actor上就可以绘制Gizmo了
- 再在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的更多相关文章
- Unity3D脚本中文系列教程(十七)
http://dong2008hong.blog.163.com/blog/static/469688272014032332976/ ◆ Static function PrefixLabel(to ...
- 从Unity中的Attribute到AOP(八)
本文将讲一下在UnityEditor命名空间下的一些特性. CallBackOrder,这个Attribute是所有带callback index的Attribute的基类,由于官方也没有给出详细的说 ...
- Unreal学习笔记2-绘制简单三角形
目录 1. 概述 2. 详论 2.1. 代码实现 2.2. 解析:Component 2.3. 解析:材质 2.4. 解析:包围盒 2.5. 解析:Section 3. 其他 4. 参考 1. 概述 ...
- UE4 Tutorial - Custom Mesh Component 用于绘制自定义网格的插件CustomMeshComponent
UE4 中用于绘制自定义网格的插件CustomMeshComponent. 转载: UE4 Tutorial - Custom Mesh Component Over the last few w ...
- unity gizmo绘制圆形帮助调试
using UnityEngine; using System.Collections; using System; public class LearnGrazio : MonoBehaviour ...
- UE4射线的碰撞与绘制
http://blog.csdn.net/qq992817263/article/details/51800657 //起点 终点 FHitResult RayGetHitResult(FVector ...
- [UE4][Canvas]用C++代码绘制血条(HealthBar)
转自:http://aigo.iteye.com/blog/2275110 参考自Epic官方项目StrategyGame 血条效果: StrategyHUD.h StrategyHUD.cpp
- 【UE4 C++】绘制函数 Debug drawing functions
基于UKismetSystemLibrary /** Draw a debug line */ UFUNCTION(BlueprintCallable, Category="Renderin ...
- UE4命令行使用,解释
命令行在外部 从命令行运行编辑项目 1 导航到您的[LauncherInstall][VersionNumber]\Engine\Binaries\Win64 目录中. 2 右键单击上 UE4Edit ...
- UE4 在C++ 动态生成几何、BSP体、Brush ---- Mesh_Generation
截至UE4 4.10 runtime 无法生成BSP类 ,只能通过自定义的Mesh的Vertex 进行绘制 ( Google 考证,能改UE4源码的请忽略 ) 可用到的 UE4 集成的Render ...
随机推荐
- #树状数组,并查集#CF920F SUM and REPLACE
题目 分析 由于\(a_i=1或2\)时\(d(a_i)=a_i\),且其余情况修改后答案只会越来越小, 考虑用树状数组维护区间和,用并查集跳过\(a_i=1或2\)的情况 代码 #include & ...
- Techwalk攻略 | 来北京与OpenHarmony技术大会一起技术漫游!
去北京Citywalk已经不是新鲜事? 不如来第二届OpenHarmony技术大会一起Techwalk! 大会即将开幕请速速收藏以下打卡攻略↓ 点击链接,观看线上直播
- Go 语言中的 Switch 语句详解
switch语句 使用switch语句来选择要执行的多个代码块中的一个. 在Go中的switch语句类似于C.C++.Java.JavaScript和PHP中的switch语句.不同之处在于它只执行匹 ...
- C# 中一维数组和二维数组详解
String[][]是二维数组.例如:String[][] str=new String[4][4],这是创建了一个有bai4行4列元素的数组. String[]代表一维数组.例如:String[] ...
- 聊聊ChatGLM3多用户并发API调用的问题
转载请备注出处:https://www.cnblogs.com/zhiyong-ITNote 背景 目前在公司内部4张A10的GPU服务器上部署了ChatGLM3开源模型:然后部署了官方默认的web_ ...
- js推送网页到扩展屏上
需求: 电脑上有两个屏幕,想在主屏上的网页中点击一个按钮,副屏就可以显示需要推送过去的网页 实现方法: 本方法使用的是js来实现的,亲测可行,支持火狐,但是不支持谷歌 demo: 主屏网页:1.htm ...
- RestfulApi 学习笔记——分页和排序(六)
前言 分页和排序时一些非常常规的操作,同样也有一些我们注意的点. 正文 分页 先来谈及分页. 看下前端传递的参数. public class EmployeeDtoParameters { priva ...
- Java:得到指定年份、月份、周次的最后一天
使用Java的工具类Calendar 通过Calendar可以进行很多的日期操作 /** * 得到指定年份的最后一天 * @param year */ public static void getLa ...
- 《c#高级编程》第2章C#2.0中的更改(二)——匿名类型
一.概念 C#中的匿名类型是一种特殊类型,可以在运行时动态创建一个对象,该对象可以包含多个属性,这些属性的名称和类型可以在创建时指定.相对于定义具体的类,匿名类型更加灵活和简洁. C#的匿名类型通常用 ...
- 三端一体计算方案:Unify SQL Engine
简介: 本文将介绍数仓建设过程中面对三种计算模式,较低的研发效率.不可控的数据质量,以及臃肿数据接口服务的困境的解决方案. 背景 在漫长的数仓建设过程中,实时数仓与离线数仓分别由不同的团队进行独立建设 ...