【虚幻引擎UE】UE5 两种球体绘制方法

news2025/7/13 23:52:30

一、网格球体绘制

在这里插入图片描述
在这里插入图片描述
center 中心点向量
segments参数越大,线条越多
radius是球体半径
thickness 厚度可以不用管
Depth Priority 是渲染深度可以不用管
F Life Time 是持续时间

C++代码如下——

.cpp

#include "drawBallFunc.h"
#include "Components/LineBatchComponent.h"
#include "Engine/World.h"
#include "EngineGlobals.h"
#include "PrimitiveViewRelevance.h"
#include "PrimitiveSceneProxy.h"
#include "Engine/Engine.h"
#include "MaterialShared.h"
#include "Materials/Material.h"
#include "Engine/CollisionProfile.h"
#include "SceneManagement.h"
#include "DynamicMeshBuilder.h"

void UdrawBallFunc::drawCustomBallFunc(UObject* WorldContextObject, const FVector& Center, int32 Segments, float Radius, const FColor& Color, uint8 DepthPriority, float fLifeTime, float Thickness)
{

	ULineBatchComponent* const LineBatcher = WorldContextObject->GetWorld()->PersistentLineBatcher;
	//LineBatcher->DrawCircle(Center, X, Y, Color, Radius, Segments, DepthPriority);
	//LineBatcher->DrawPoint(Center, Color, Radius, DepthPriority, fLifeTime);
	// Need at least 4 segments
	Segments = FMath::Max(Segments, 4);

	FVector Vertex1, Vertex2, Vertex3, Vertex4;
	const float AngleInc = 2.f * PI / float(Segments);
	int32 NumSegmentsY = Segments;
	float Latitude = AngleInc;
	int32 NumSegmentsX;
	float Longitude;
	float SinY1 = 0.0f, CosY1 = 1.0f, SinY2, CosY2;
	float SinX, CosX;

	TArray<FBatchedLine> Lines;
	Lines.Empty(NumSegmentsY * Segments * 2);
	while (NumSegmentsY--)
	{
		SinY2 = FMath::Sin(Latitude);
		CosY2 = FMath::Cos(Latitude);

		Vertex1 = FVector(SinY1, 0.0f, CosY1) * Radius + Center;
		Vertex3 = FVector(SinY2, 0.0f, CosY2) * Radius + Center;
		Longitude = AngleInc;

		NumSegmentsX = Segments;
		while (NumSegmentsX--)
		{
			SinX = FMath::Sin(Longitude);
			CosX = FMath::Cos(Longitude);

			Vertex2 = FVector((CosX * SinY1), (SinX * SinY1), CosY1) * Radius + Center;
			Vertex4 = FVector((CosX * SinY2), (SinX * SinY2), CosY2) * Radius + Center;

			Lines.Add(FBatchedLine(Vertex1, Vertex2, Color, fLifeTime, Thickness, DepthPriority));
			Lines.Add(FBatchedLine(Vertex1, Vertex3, Color, fLifeTime, Thickness, DepthPriority));

			Vertex1 = Vertex2;
			Vertex3 = Vertex4;
			Longitude += AngleInc;
		}
		SinY1 = SinY2;
		CosY1 = CosY2;
		Latitude += AngleInc;
	}
	LineBatcher->DrawLines(Lines);
}

.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "drawBallFunc.generated.h"

/**
 *
 */
UCLASS()
class DRAWBALL_API UdrawBallFunc : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()
		UFUNCTION(BlueprintCallable, Category = "Custom", meta = (Keywords = "draw"))
		static void drawCustomBallFunc(UObject* WorldContextObject, const FVector& Center, int32 Segments, float Radius, const FColor& Color, uint8 DepthPriority, float fLifeTime, float Thickness);
};

二、球体模型绘制

在这里插入图片描述

1、创建一个默认球体
在这里插入图片描述
2、创建球体对象生成器,并输入缩放向量(后面根据需要,也可以补充一下材质替换的代码)
在这里插入图片描述3、传入半径*2是最终尺寸 在这里插入图片描述

三、拓展学习

UE4 C++ 在屏幕上绘制线和文字的方法

拓展绘制方法!!!

绘制函数 Debug drawing functions相关方法 (仅限于开发调试和参考):

FORCEINLINE void FlushPersistentDebugLines(const UWorld* InWorld) {}
FORCEINLINE void DrawDebugLine(const UWorld* InWorld, FVector const& LineStart, FVector const& LineEnd, FColor const& Color, bool bPersistentLines = false, float LifeTime = -1.f, uint8 DepthPriority = 0, float Thickness = 0.f) {}
FORCEINLINE void DrawDebugPoint(const UWorld* InWorld, FVector const& Position, float Size, FColor const& PointColor, bool bPersistentLines = false, float LifeTime = -1.f, uint8 DepthPriority = 0) {}
FORCEINLINE void DrawDebugDirectionalArrow(const UWorld* InWorld, FVector const& LineStart, FVector const& LineEnd, float ArrowSize, FColor const& Color, bool bPersistentLines = false, float LifeTime = -1.f, uint8 DepthPriority = 0, float Thickness = 0.f) {}
FORCEINLINE void DrawDebugBox(const UWorld* InWorld, FVector const& Center, FVector const& Extent, FColor const& Color, bool bPersistentLines = false, float LifeTime = -1.f, uint8 DepthPriority = 0, float Thickness = 0.f) {}
FORCEINLINE void DrawDebugBox(const UWorld* InWorld, FVector const& Center, FVector const& Extent, const FQuat& Rotation, FColor const& Color, bool bPersistentLines = false, float LifeTime = -1.f, uint8 DepthPriority = 0, float Thickness = 0.f) {}
FORCEINLINE void DrawDebugCoordinateSystem(const UWorld* InWorld, FVector const& AxisLoc, FRotator const& AxisRot, float Scale, bool bPersistentLines = false, float LifeTime = -1.f, uint8 DepthPriority = 0, float Thickness = 0.f) {}
FORCEINLINE void DrawDebugCrosshairs(const UWorld* InWorld, FVector const& AxisLoc, FRotator const& AxisRot, float Scale, const FColor& Color = FColor::White, bool bPersistentLines = false, float LifeTime = -1.f, uint8 DepthPriority = 0) {}
FORCEINLINE void DrawDebugCircle(const UWorld* InWorld, const FMatrix& TransformMatrix, float Radius, int32 Segments, const FColor& Color, bool bPersistentLines = false, float LifeTime = -1.f, uint8 DepthPriority = 0, float Thickness = 0.f, bool bDrawAxis = true) {}
FORCEINLINE void DrawDebugCircle(const UWorld* InWorld, FVector Center, float Radius, int32 Segments, const FColor& Color, bool bPersistentLines = false, float LifeTime = -1.f, uint8 DepthPriority = 0, float Thickness = 0.f, FVector YAxis = FVector(0.f, 1.f, 0.f), FVector ZAxis = FVector(0.f, 0.f, 1.f), bool bDrawAxis = true) {}
FORCEINLINE void DrawDebugCircleArc(const UWorld* InWorld, FVector const& Center, float Radius, FVector const& Direction, float AngleWidth, int32 Segments, const FColor& Color, bool bPersistentLines = false, float LifeTime = -1.f, uint8 DepthPriority = 0, float Thickness = 0.f) {}
FORCEINLINE void DrawDebug2DDonut(const UWorld* InWorld, const FMatrix& TransformMatrix, float InnerRadius, float OuterRadius, int32 Segments, const FColor& Color, bool bPersistentLines = false, float LifeTime = -1.f, uint8 DepthPriority = 0, float Thickness = 0.f) {}
FORCEINLINE void DrawDebugSphere(const UWorld* InWorld, FVector const& Center, float Radius, int32 Segments, FColor const& Color, bool bPersistentLines = false, float LifeTime = -1.f, uint8 DepthPriority = 0, float Thickness = 0.f) {}
FORCEINLINE void DrawDebugCylinder(const UWorld* InWorld, FVector const& Start, FVector const& End, float Radius, int32 Segments, FColor const& Color, bool bPersistentLines = false, float LifeTime = -1.f, uint8 DepthPriority = 0, float Thickness = 0.f) {}
FORCEINLINE void DrawDebugCone(const UWorld* InWorld, FVector const& Origin, FVector const& Direction, float Length, float AngleWidth, float AngleHeight, int32 NumSides, FColor const& Color, bool bPersistentLines = false, float LifeTime = -1.f, uint8 DepthPriority = 0, float Thickness = 0.f) {}
FORCEINLINE void DrawDebugAltCone(const UWorld* InWorld, FVector const& Origin, FRotator const& Rotation, float Length, float AngleWidth, float AngleHeight, FColor const& DrawColor, bool bPersistentLines = false, float LifeTime = -1.f, uint8 DepthPriority = 0, float Thickness = 0.f) {}
FORCEINLINE void DrawDebugString(const UWorld* InWorld, FVector const& TextLocation, const FString& Text, class AActor* TestBaseActor = NULL, FColor const& TextColor = FColor::White, float Duration = -1.000000, bool bDrawShadow = false, float FontScale = 1.f) {}
FORCEINLINE void DrawDebugFrustum(const UWorld* InWorld, const FMatrix& FrustumToWorld, FColor const& Color, bool bPersistentLines = false, float LifeTime = -1.f, uint8 DepthPriority = 0, float Thickness = 0.f) {}
FORCEINLINE void DrawCircle(const UWorld* InWorld, const FVector& Base, const FVector& X, const FVector& Y, const FColor& Color, float Radius, int32 NumSides, bool bPersistentLines = false, float LifeTime = -1.f, uint8 DepthPriority = 0, float Thickness = 0) {}
FORCEINLINE void DrawDebugCapsule(const UWorld* InWorld, FVector const& Center, float HalfHeight, float Radius, const FQuat& Rotation, FColor const& Color, bool bPersistentLines = false, float LifeTime = -1.f, uint8 DepthPriority = 0, float Thickness = 0) {}
FORCEINLINE void DrawDebugCamera(const UWorld* InWorld, FVector const& Location, FRotator const& Rotation, float FOVDeg, float Scale = 1.f, FColor const& Color = FColor::White, bool bPersistentLines = false, float LifeTime = -1.f, uint8 DepthPriority = 0) {}
FORCEINLINE void FlushDebugStrings(const UWorld* InWorld) {}
FORCEINLINE void DrawDebugSolidBox(const UWorld* InWorld, FBox const& Box, FColor const& Color, const FTransform& Transform = FTransform::Identity, bool bPersistent = false, float LifeTime = -1.f, uint8 DepthPriority = 0) {}
FORCEINLINE void DrawDebugSolidBox(const UWorld* InWorld, FVector const& Center, FVector const& Extent, FColor const& Color, bool bPersistent = false, float LifeTime = -1.f, uint8 DepthPriority = 0) {}
FORCEINLINE void DrawDebugSolidBox(const UWorld* InWorld, FVector const& Center, FVector const& Extent, FQuat const& Rotation, FColor const& Color, bool bPersistent = false, float LifeTime = -1.f, uint8 DepthPriority = 0) {}
FORCEINLINE void DrawDebugMesh(const UWorld* InWorld, TArray<FVector> const& Verts, TArray<int32> const& Indices, FColor const& Color, bool bPersistent = false, float LifeTime = -1.f, uint8 DepthPriority = 0) {}
FORCEINLINE void DrawDebugSolidPlane(const UWorld* InWorld, FPlane const& P, FVector const& Loc, float Size, FColor const& Color, bool bPersistent = false, float LifeTime = -1, uint8 DepthPriority = 0) {}
FORCEINLINE void DrawDebugSolidPlane(const UWorld* InWorld, FPlane const& P, FVector const& Loc, FVector2D const& Extents, FColor const& Color, bool bPersistent = false, float LifeTime = -1, uint8 DepthPriority = 0) {}
FORCEINLINE void DrawDebugFloatHistory(UWorld const & WorldRef, FDebugFloatHistory const & FloatHistory, FTransform const & DrawTransform, FVector2D const & DrawSize, FColor const & DrawColor, bool const & bPersistent = false, float const & LifeTime = -1.f, uint8 const & DepthPriority = 0) {}
FORCEINLINE void DrawDebugFloatHistory(UWorld const & WorldRef, FDebugFloatHistory const & FloatHistory, FVector const & DrawLocation, FVector2D const & DrawSize, FColor const & DrawColor, bool const & bPersistent = false, float const & LifeTime = -1.f, uint8 const & DepthPriority = 0) {}
FORCEINLINE void DrawDebugCanvas2DLine(UCanvas* Canvas, const FVector& Start, const FVector& End, const FLinearColor& LineColor) {}
FORCEINLINE void DrawDebugCanvas2DLine(UCanvas* Canvas, const FVector2D& StartPosition, const FVector2D& EndPosition, const FLinearColor& LineColor, const float& LineThickness = 1.f) {}
FORCEINLINE void DrawDebugCanvas2DCircle(UCanvas* Canvas, const FVector2D& Center, float Radius, int32 NumSides, const FLinearColor& LineColor, const float& LineThickness = 1.f) {}
FORCEINLINE void DrawDebugCanvas2DBox(UCanvas* Canvas, const FBox2D& Box, const FLinearColor& LineColor, const float& LineThickness = 1.f) {}
FORCEINLINE void DrawDebugCanvasLine(UCanvas* Canvas, const FVector& Start, const FVector& End, const FLinearColor& LineColor) {}
FORCEINLINE void DrawDebugCanvasCircle(UCanvas* Canvas, const FVector& Base, const FVector& X, const FVector& Y, FColor Color, float Radius, int32 NumSides) {}
FORCEINLINE void DrawDebugCanvasWireSphere(UCanvas* Canvas, const FVector& Base, FColor Color, float Radius, int32 NumSides) {}
FORCEINLINE void DrawDebugCanvasWireCone(UCanvas* Canvas, const FTransform& Transform, float ConeRadius, float ConeAngle, int32 ConeSides, FColor Color) {}

绘制函数LineBatcher drawing functions相关方法:

void ULineBatchComponent::DrawLines(const TArray<FBatchedLine>& InLines)
void ULineBatchComponent::DrawLine(const FVector& Start, const FVector& End, const FLinearColor& Color, uint8 DepthPriority, const float Thickness, const float LifeTime)

void ULineBatchComponent::DrawPoint(const FVector& Position,const FLinearColor& Color,float PointSize,
 uint8 DepthPriority,float LifeTime)
 
void ULineBatchComponent::DrawMesh(TArray<FVector> const& Verts, TArray<int32> const& Indices, FColor const& Color, uint8 DepthPriority, float LifeTime)

void ULineBatchComponent::DrawDirectionalArrow(const FMatrix& ArrowToWorld,FColor InColor,float Length,float ArrowSize,uint8 DepthPriority)

void ULineBatchComponent::DrawSolidBox(const FBox& Box, const FTransform& Xform, const FColor& Color, uint8 DepthPriority, float LifeTime)

void ULineBatchComponent::DrawBox(const FBox& Box, const FMatrix& TM, const FColor& Color, uint8 InDepthPriorityGroup)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/38973.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

机器学习中的数学基础(二)

机器学习中的数学基础&#xff08;二&#xff09;2 线代2.1 矩阵2.2 矩阵的秩2.3 内积与正交2.4 特征值与特征向量2.5 SVD矩阵分解2.5.1 要解决的问题2.5.2 基变换2.5.3 特征值分解2.5.4 奇异值分解&#xff08;SVD&#xff09;在看西瓜书的时候有些地方的数学推导&#xff08;…

使用redis快速实现session共享,springboot

1.引入依赖 <dependency><groupId>org.springframework.session</groupId><artifactId>spring-session-data-redis</artifactId> </dependency> <!-- 引入 redis 依赖 --> <dependency><groupId>org.springframework.b…

8.2 数据结构——插入排序

1、基本思想&#xff1a;每步将一个排序的对象&#xff0c;按其关键码大小插入到前面已经排好序的一组对象的适当位置上&#xff0c;直到对象全部插入为止。即边插入边排序&#xff0c;保证子序列中随时都是有序的。 2、基本操作&#xff1a; &#xff08;1&#xff09;在有序…

嗯哦哎辟 NOIP 2022 游寄

虽然上次不是假的&#xff0c;但这次是真的寄了。 Day 0 虽然是南京本地人&#xff0c;但因疫情原因&#xff0c;晚上决定去住了酒店。 看了一眼考场&#xff0c;感觉位置小得离谱。不愧是 NOI 2022 团体总分第十的“强省”江苏。 刚开始去了 409&#xff0c;发现房间里一股…

非凡社群管理之社群管理如何制定规则

1、加人规则&#xff1a;我们上篇文章里说到了&#xff0c;拉人前也是要进行一个明确定位的&#xff0c;不能什么人都拉&#xff0c;这就是我们常说的“设门槛”&#xff0c;避免占用群资源以及后期花费精力对其进行筛除。常用到的方式有这么几种&#xff1a;邀请式&#xff08…

【C++】类和对象(下)(再谈构造函数 初始化列表 explicit关键字 static成员 特性 友元 友元函数 友元类 内部类 匿名对象)

文章目录再谈构造函数初始化列表explicit关键字static成员特性友元友元函数友元类内部类匿名对象再谈构造函数 我们之前学习构造函数的时候&#xff0c;调用构造之后对象中就已经有了一个初始值&#xff0c;但不能说它是对对象像成员变量的初始化&#xff0c;构造函数体中的语…

windows10不支持Miracast无线投屏(不能进行无线投影)

电脑屏幕小看视频不爽&#xff0c;想把电脑屏幕投屏到电视上&#xff08;单独买一块高质量显示屏太贵&#xff0c;而且没有大尺寸的电视看的爽&#xff09;&#xff0c;但是windows提示不支持Miracast&#xff0c;跟着下面步骤教你解决问题。 当链接电视时出现下图提示不支持Mi…

SpringBoot 3.0 来啦!

SpringBoot 3.0 来啦&#xff01;&#xff01; 大家好&#xff0c;我 是 Ding Jiaxiong。 没赶上热乎的&#xff0c;晚了两天&#xff0c;2022年11月24日&#xff0c;SpringBoot 3.0 正式发布了&#xff01; 文章目录SpringBoot 3.0 来啦&#xff01;&#xff01;1 看看官网2…

ARM 37 个通用寄存器详解

一、简介 1、ARM 总共有 37 个寄存器&#xff0c;但是每种模式下最多只能看到 18 个寄存器&#xff0c;其他寄存器虽然名字相同&#xff0c;但是在当前模式不可见。 2、例如&#xff0c;对 r13 这个名字来说&#xff0c;在 ARM 中共有 6 个名叫 r13&#xff08;又叫 sp&#x…

SpringBoot SpringBoot 原理篇 1 自动配置 1.10 bean 的加载方式【八】

SpringBoot 【黑马程序员SpringBoot2全套视频教程&#xff0c;springboot零基础到项目实战&#xff08;spring boot2完整版&#xff09;】 SpringBoot 原理篇 文章目录SpringBootSpringBoot 原理篇1 自动配置1.10 bean 的加载方式【八】1.10.1 BeanDefinitionRegistryPostPro…

ABAP学习笔记之——第五章:内表

内表&#xff1a; 内表是可以在程序内部定义且使用的表&#xff0c;属于本地表。 与C语言比较&#xff1a; C语言的数组和内表比较&#xff1a; 内表是动态数组(Dynamic Data Object) INITIALSIZE 语句并非实际占用内存空间&#xff0c;而只是预约(RESERVE)内存空间。 创建…

Python每日一练 06

Python每日一练 06 文章目录Python每日一练 06while循环实例一、斐波那契数列前n项实例二、Leibniz公式计算圆周率while循环 循环结构表示程序重复执行某个或某些操作&#xff0c;直到某条件为假&#xff08;或为真&#xff09;时才可终止循环。 在问题求解过程中&#xff0c;…

[算法笔记]最长递增子序列和编辑距离

最长递增子序列 例如对于 a[] {2,1,5,3,6,4,8,9,7}其最长递增子序列为{1,3,4,8,9}所以长度&#xff08;或者说是结果&#xff09;为5。 对于a[0...n-1]&#xff0c;用dp[i]表示a[0...i]中以a[i]结尾的最长递增子序列长度 其状态状态方程&#xff1a; dp[i]1 // 0≤i≤…

【无人机通信优化】基于粒子群算法的多跳无线网络部署优化附matlab代码

✅作者简介&#xff1a;热爱科研的Matlab仿真开发者&#xff0c;修心和技术同步精进&#xff0c;matlab项目合作可私信。 &#x1f34e;个人主页&#xff1a;Matlab科研工作室 &#x1f34a;个人信条&#xff1a;格物致知。 更多Matlab仿真内容点击&#x1f447; 智能优化算法 …

[附源码]SSM计算机毕业设计时事资讯平台JAVA

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; SSM mybatis Maven Vue 等等组成&#xff0c;B/S模式 M…

Date对象

文章目录Date日期对象Date对象的创建格式化日期3.获取Date总的毫秒数(时间戳)&#xff0c;是距离1970年1月1日过了多少毫秒数。二&#xff1a;常用时间获取方法三&#xff1a;日期设置方法四&#xff1a;时间转字符串菜鸟工具&#xff1a;https://www.runoob.com/jsref/jsref-o…

在Express框架使用ORM模型访问关系型数据库

一、ORM模型&#xff1a;设计思想&#xff0c;主要目的是简化计算机程序访问数据库 1、ORM&#xff1a;对象关系模型(对象关系映射) Object Releastion Model,将程序中的对象和数据库中关系(表格)进行映射。可以使开发者在程序中方便的对数据库进行操作(用户在程序操作对对象实…

【网页制作课作业】用HTML+CSS制作一个简单的学校网页(9页)

&#x1f389;精彩专栏推荐 &#x1f4ad;文末获取联系 ✍️ 作者简介: 一个热爱把逻辑思维转变为代码的技术博主 &#x1f482; 作者主页: 【主页——&#x1f680;获取更多优质源码】 &#x1f393; web前端期末大作业&#xff1a; 【&#x1f4da;毕设项目精品实战案例 (10…

一文熟悉 Go 的分支结构(if - else-if - else、switch)

哈喽大家好&#xff0c;我是陈明勇&#xff0c;今天分享的知识是 Go 的分支结构。如果本文对你有帮助&#xff0c;不妨点个赞&#xff0c;如果你是 Go 语言初学者&#xff0c;不妨点个关注&#xff0c;一起成长一起进步&#xff0c;如果本文有错误的地方&#xff0c;欢迎指出&a…

Python爬虫脚本+XML解析实现自动保存某商城的商品图

文章目录 1.背景介绍2.代码分析2.1.创建图片保存的目录2.2.定一下载函数2.3.发送请求解析数据2.源代码(全)1.背景介绍 Python脚本可以实现数据的爬取,而XML可以解析网页数据。将Python爬虫脚本与XML解析功能相结合,可以实现自动保存某商城的商品图功能。 注:本功能仅用于…