Skip to content

Instantly share code, notes, and snippets.

@embiem
Last active August 26, 2020 02:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save embiem/52f3c31f0508899d15a27f07207151a4 to your computer and use it in GitHub Desktop.
Save embiem/52f3c31f0508899d15a27f07207151a4 to your computer and use it in GitHub Desktop.
Equally distributed Spline Mesh in UE4
// Used further down in the OnConstruction call to set values on one spline mesh
void ALane::BuildTrackElement(int32 atDistance, USplineMeshComponent* SplineMesh, UStaticMesh* LaneElementMesh, ESplineMeshAxis::Type LaneElementMeshForwardAxis)
{
FVector currLocation, currTangent, nextLocation, nextTangent;
currLocation = SplineComponent->GetLocationAtDistanceAlongSpline(atDistance, ESplineCoordinateSpace::Type::Local);
currTangent = SplineComponent->GetTangentAtDistanceAlongSpline(atDistance, ESplineCoordinateSpace::Type::Local);
nextLocation = SplineComponent->GetLocationAtDistanceAlongSpline(atDistance + MeshSpacing, ESplineCoordinateSpace::Type::Local);
nextTangent = SplineComponent->GetTangentAtDistanceAlongSpline(atDistance + MeshSpacing, ESplineCoordinateSpace::Type::Local);
FRotator curRotation, nextRotation;
curRotation = SplineComponent->GetRotationAtDistanceAlongSpline(atDistance, ESplineCoordinateSpace::Type::Local);
nextRotation = SplineComponent->GetRotationAtDistanceAlongSpline(atDistance + MeshSpacing, ESplineCoordinateSpace::Type::Local);
SplineMesh->SetForwardAxis(LaneElementMeshForwardAxis);
SplineMesh->SetStaticMesh(LaneElementMesh);
SplineMesh->SetStartAndEnd(currLocation, currTangent, nextLocation, nextTangent);
SplineMesh->SetStartScale(FVector2D::UnitVector);
SplineMesh->SetEndScale(FVector2D::UnitVector);
SplineMesh->SetStartRoll(FMath::DegreesToRadians(curRotation.Roll));
SplineMesh->SetEndRoll(FMath::DegreesToRadians(nextRotation.Roll));
SplineMesh->SetCollisionEnabled(ECollisionEnabled::Type::NoCollision);
}
void ALane::OnConstruction(const FTransform& Transform)
{
int32 SplineMeshComponentsNum = AddedSplineMeshComponents.Num();
float SplineLength = SplineComponent->GetSplineLength();
int32 NumOfSplineMeshes = FMath::FloorToInt(SplineLength / MeshSpacing);
do
{
if (SplineMeshComponentsNum > NumOfSplineMeshes)
{
AddedSplineMeshComponents[SplineMeshComponentsNum - 1]->DetachFromComponent(FDetachmentTransformRules::KeepRelativeTransform);
AddedSplineMeshComponents[SplineMeshComponentsNum - 1]->UnregisterComponent();
AddedSplineMeshComponents[SplineMeshComponentsNum - 1]->DestroyComponent();
AddedSplineMeshComponents.RemoveAt(SplineMeshComponentsNum - 1);
SplineMeshComponentsNum--;
}
else if (SplineMeshComponentsNum < NumOfSplineMeshes)
{
USplineMeshComponent* newMeshComp = NewObject<USplineMeshComponent>(this);
newMeshComp->CreationMethod = EComponentCreationMethod::Instance;
newMeshComp->SetMobility(EComponentMobility::Movable);
newMeshComp->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);
newMeshComp->ResetRelativeTransform();
AddedSplineMeshComponents.Add(newMeshComp);
SplineMeshComponentsNum++;
}
} while (SplineMeshComponentsNum != NumOfSplineMeshes);
for (int32 i = 0; i < NumOfSplineMeshes; i++)
{
BuildTrackElement(i * MeshSpacing, AddedSplineMeshComponents[i], LaneMesh, LaneMeshesForwardAxis;
}
}
class ALane : public AActor
{
public:
virtual void OnConstruction(const FTransform& Transform) override;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
class USplineComponent* SplineComponent;
UPROPERTY(EditAnywhere, Category = "Config")
UStaticMesh* LaneMesh;
UPROPERTY(EditAnywhere, Category = "Config")
TEnumAsByte<ESplineMeshAxis::Type> LaneMeshesForwardAxis;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Config")
int32 MeshSpacing = 10000;
virtual void BuildTrackElement(int32 atDistance, USplineMeshComponent* SplineMesh, UStaticMesh* LaneElementMesh, ESplineMeshAxis::Type LaneElementMeshForwardAxis);
private:
int32 NumOfSplinePoints;
UPROPERTY(VisibleInstanceOnly, Category = "Debug")
TArray<class USplineMeshComponent*> AddedSplineMeshComponents;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment