当前位置: 首页 > article >正文

UE5.3 C++ 通过Spline样条实现三维连线,自己UV贴图。

一.制作了基于USplineComponent的画线插件,就是我们常说的样条线。

直接看怎么用,关于插件实现细节,后续会更新,看思路就行。通过ID,管理每一条线。移除删掉上一帧的线条Mesh。第一个点,是本身直接放过去。第二个点是使用AddSplinePoint放进去。让后算两个点的距离,通过材质参数集合,设置UV。最后生成三角形面构成线条。

void UDrawLineSubsystem::UpdateLinePath(int id,FVector First, FVector Second)
{
	int32 pointsNum = 2;
	if (pointsNum <= 1) return;
	
	int32 tmpPathID = id;
	if (LinePathMap.Find(tmpPathID))
	{
		ASplineActor* tmpFlightPath = *LinePathMap.Find(tmpPathID);
		tmpFlightPath->SetActorHiddenInGame(true);
		LinePathMap.Remove(tmpPathID);
		//删除旧的同序号航迹线
		tmpFlightPath->Destroy();
	}
	
	//	//生成轨迹线
	UClass* tmpSplineClass = LoadClass<ASplineActor>(nullptr, TEXT("/Script/Engine.Blueprint'/HMSLineSystem/Blueprints/BP_ConnectLine.BP_ConnectLine_C'"));
	ASplineActor* LinePathActor = Cast<ASplineActor>(GetWorld()->SpawnActor(tmpSplineClass));
	if (!LinePathActor)
	{
		UE_LOG(LogTemp, Error, TEXT("failed to spawn spline actor! UProject2444System::UpdateFlightPath"));
		return;
	}

	LinePathActor->DeleteSplinePointByIndex(1);
		if (!Georeference)
			InitialGeoreference();
		if (!Georeference) return;
	
		if (!First.IsZero())
		{
			//处理点位信息
			FVector tmpDataPosition = FVector(First);
			FVector tmpUELocation = Georeference->TransformLongitudeLatitudeHeightPositionToUnreal(tmpDataPosition);
			
			LinePathActor->SetActorLocation(tmpUELocation);
			First = tmpUELocation;
			//FTransform LocalTransform;
			//LocalTransform.SetLocation(tmpUELocation);
			//LinePathActor->AddSplinePoint(LocalTransform, EPointTransformMode::world);
		}
		if (!Second.IsZero())
		{
			//处理点位信息
			FVector tmpDataPosition = FVector(Second);
			FVector tmpUELocation = Georeference->TransformLongitudeLatitudeHeightPositionToUnreal(tmpDataPosition);

			FTransform LocalTransform;
			LocalTransform.SetLocation(tmpUELocation);
			LinePathActor->AddSplinePoint(LocalTransform, EPointTransformMode::world);
			Second = tmpUELocation;
		}
		LinePathActor->SplineMeshData.MeshSlice = 1;
		LinePathActor->SplineMeshData.FacesCount = 2;
		LinePathActor->SplineMeshData.EdgeLength = 200;
		LinePathActor->SplineMeshData.MeshMode = EMeshMode::BaseOnPoint;
		LinePathActor->SetAllPointsMode(ESplineMode::Curve);
		LinePathActor->SetAllPointsMode(LinePathActor->SplineMeshData.SplineMode);
		LinePathMap.Add(tmpPathID, LinePathActor);
		//TPADataMap.Add(tmpPathID, _TPAData);
	
	///Script/Engine.MaterialInstanceConstant'/Game/ZOthers/Other/Materials/M_Border_Anim_10.M_Border_Anim_10'
	//	//设置材质
		UMaterial* SplineMaterial;
		SplineMaterial = LoadObject<UMaterial>(nullptr, TEXT("/Script/Engine.Material'/Game/ZOthers/Other/Materials/Link.Link'"));
	//	switch (tmpPathID)
	//	{
	//	case 1:	SplineMaterial = LoadObject<UMaterial>(nullptr, FLIGHT_PATH_MATERIAL1); break;
	//	case 2:	SplineMaterial = LoadObject<UMaterial>(nullptr, FLIGHT_PATH_MATERIAL2); break;
	//	case 3:	SplineMaterial = LoadObject<UMaterial>(nullptr, FLIGHT_PATH_MATERIAL3); break;
	//	default:SplineMaterial = LoadObject<UMaterial>(nullptr, FLIGHT_PATH_MATERIAL1); break;
	//	}
		if (SplineMaterial)
			LinePathActor->SplineMeshMaterial = SplineMaterial;
	//
	//	//描绘航迹线
		if (!M_ParamCollection)
		{
			UMaterialParameterCollection* MPC = LoadObject<UMaterialParameterCollection>(nullptr, TEXT("/Script/Engine.MaterialParameterCollection'/Game/ZOthers/Other/Materials/1'"));
			M_ParamCollection = GetWorld()->GetParameterCollectionInstance(MPC);
		}
		if (!M_ParamCollection) return;

		double dis = FVector::Dist(First,Second);
		dis *= 0.01;
		if (id == int(DTypeIdPlatform::A))
		{
			M_ParamCollection->SetScalarParameterValue(FName("ATOB"), dis);
		}

		LinePathActor->CreateSplineMesh();
}

二.材质参数集合在C++使用

材质参数需要的头文件

#include "Materials/MaterialParameterCollection.h"
#include "Materials/MaterialParameterCollectionInstance.h"

材质参数集合,它的改动,在上面的代码里。它在材质里的如下图,只设置了V的平铺,没设置U的平铺。

	UMaterialParameterCollectionInstance* M_ParamCollection;


http://www.kler.cn/a/553892.html

相关文章:

  • Linux-----进程(多任务)
  • Pycharm中查找与替换
  • 介绍cherrypick
  • FFmpeg 源码编译安装
  • Zookeeper(58)如何在Zookeeper中实现分布式锁?
  • 《剑指数据库:MySQL安装布阵全解》
  • 1.buuctf [BJDCTF2020]EasySearch
  • 【c++】c++内存管理
  • 老游戏回顾:d2
  • 数据库连接管理--Java连接数据库的几种方式
  • python concurrent.futures
  • 【2025最新计算机毕业设计】基于SSM的社区老人服务平台 可定制开发【提供源码+答辩PPT+文档+项目部署】
  • 【LLAMA】羊驼从LLAMA1到LLAMA3梳理
  • SpringBoot+uniApp日历备忘录小程序系统 附带详细运行指导视频
  • [Android]文件描述符的binder传送
  • 迅为iTOP-RK3576开发板/核心板6TOPS算力4K视频编解码
  • Redis 键对应的命令详解
  • mysql实现原理 - 字符集和排序规则
  • Python安装与环境配置全程详细教学(包含Windows版和Mac版)
  • [网络] 如何开机自动配置静态IP,并自动启动程序