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

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;

相关文章:

  • MongoDB:listDatabases failed : not master and slaveOk=false
  • WPF-APP全局生命周期事件
  • RocketMq中RouteInfoManger组件的源码分析
  • 【java】作业1
  • Ai知识点总结
  • 每日一题——不同路径的数目与矩阵最小路径和
  • 性格测评小程序07用户登录
  • 【第14章:神经符号集成与可解释AI—14.2 可解释AI技术:LIME、SHAP等的实现与应用案例】
  • 2025年2月16日笔记
  • NSSCTF Pwn [HUBUCTF 2022 新生赛]singout WP
  • 二〇二四年终总结
  • 搭建Deepseek推理服务
  • dify新版,chatflow对deepseek的适配情况
  • bps是什么意思
  • 网络安全:从攻击到防御的全景解析
  • AI视频创作教程:如何用AI让古画动起来。
  • 动量突破均值回归策略
  • 【PYTORCH】官方的turoria实现中英文翻译
  • 水务+AI应用探索(一)| FastGPT+DeepSeek 本地部署
  • 团体程序设计天梯赛-练习集——L1-041 寻找250
  • 陕西澄城樱桃在上海推介,向长三角消费者发出“甜蜜之邀”
  • 关税风暴下,3G资本拟94亿美元私有化美国鞋履巨头斯凯奇,溢价30%
  • 中国公民免签赴马来西亚的停留天数如何计算?使馆明确
  • 躺着玩手机真有意思,我“瞎”之前最喜欢了
  • 五一档7.47亿收官:《水饺皇后》领跑;男观众占比增多
  • 贵州召开全省安全生产电视电话会议:以最严要求最实举措守牢安全底线