Bootstrap

UE4 球上生成均匀的散点

1、在U++中创建一个组建Componet

.h

UFUNCTION(BlueprintCallable)
	void PointsInSphere(const float& R,const int& N,TArray<FTransform>& PointsTransforms);
UFUNCTION(BlueprintPure)
	static void LookAtCenter(const FVector& CenterLoc,const FVector& Loc,FTransform& Transform);

.cpp

void UtheComponent::PointsInSphere(const float& R,const int& N, TArray<FTransform>& PointsTransforms)
{
	if (PointsTransforms.Num()>0)
	{
		PointsTransforms.Empty();
	}
	//Golden angle
	const float Phi = (sqrt(5)-1)/2;

	float x,y,z,Radius;
	for (int n=0;n<N;n++)
	{
		//y=1-(n/float(N-1))*2;
		y=1-(n/float(N-1))*2+
			(FMath::FRandRange(0.5,0.6)-FMath::FRandRange(0.5,0.6));
		Radius = sqrt(1-y*y);
		
		x = Radius* cos(Phi*2*PI*n);
		z = Radius* sin(Phi*2*PI*n);

		FTransform ThePointLookTransform;
		LookAtCenter(Center_Loc,FVector(x,y,z)*R,ThePointLookTransform);
		PointsTransforms.Add(ThePointLookTransform);
	}
}
//朝向球心
void UTheComponent::LookAtCenter(const FVector& CenterLoc, const FVector& Loc, FTransform& Transform)
{
	FTransform StratTransform = FTransform(FRotator::ZeroRotator,Loc, FVector(0.01,0.1,0.1));
	FRotator Rot = UKismetMathLibrary::FindRelativeLookAtRotation(StratTransform,CenterLoc);
	Transform = FTransform(Rot,Loc, FVector(0.01,0.1,0.1));
}

2、创建一个Actor,添加HierarcalInstancedStaticMesh组件 和 U++中创建的UtheComponent,蓝图调用PointsInSphere函数

在这里插入图片描述

3、运行结果

在这里插入图片描述
在函数中不加上(FMath::FRandRange(0.5,0.6)-FMath::FRandRange(0.5,0.6))随机的运行结果:
在这里插入图片描述

4、参考

球面上均匀排列许多点
Evenly distributing n points on a sphere
Delaunay+Voronoi on a sphere

;