UE5绘制正弦波

在UE5中可视化上面发射的正弦线,你可以使用ULineBatchComponent来绘制线条。这个组件允许你在每一帧绘制多条线段,非常适合用来表示正弦波。

首先,确保你的AMySineWaveActor类包含了ULineBatchComponent作为成员变量。然后,在Tick函数中更新正弦波的状态,并使用ULineBatchComponent来绘制表示正弦波的线段。

以下是修改后的代码示例:

// MySineWaveActor.h
#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/LineBatchComponent.h"
#include "MySineWaveActor.generated.h"

UCLASS()
class YOURPROJECTNAME_API AMySineWaveActor : public AActor
{
    GENERATED_BODY()

public:    
    // Sets default values for this actor's properties
    AMySineWaveActor();

protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

public:    
    // Called every frame
    virtual void Tick(float DeltaTime) override;

    // Sine wave parameters
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sine Wave")
    float Frequency = 1.0f;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sine Wave")
    float Amplitude = 1.0f;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sine Wave")
    float Phase = 0.0f;

    // Line batch component for drawing the sine wave
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true"))
    ULineBatchComponent* LineBatchComponent;

    // Function to calculate sine wave value
    float CalculateSineWave(float Time);
};

// MySineWaveActor.cpp
#include "MySineWaveActor.h"
#include "Math/UnrealMathUtility.h"

AMySineWaveActor::AMySineWaveActor()
{
    // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;

    // Create and attach the line batch component
    LineBatchComponent = CreateDefaultSubobject<ULineBatchComponent>(TEXT("LineBatchComponent"));
    LineBatchComponent->SetupAttachment(RootComponent);
}

void AMySineWaveActor::BeginPlay()
{
    Super::BeginPlay();

    // Optionally, you can set the material for the lines here
    // LineBatchComponent->SetMaterial(...);
}

void AMySineWaveActor::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

    // Calculate sine wave values
    const float StartTime = 0.0f;
    const float EndTime = 10.0f; // Or some other end time based on your needs
    const int32 NumPoints = 100;
    const float TimeStep = (EndTime - StartTime) / (NumPoints - 1);

    TArray<FVector> Points;
    Points.Reserve(NumPoints);

    for (int32 i = 0; i < NumPoints; ++i)
    {
        const float Time = StartTime + i * TimeStep;
        const float SineValue = CalculateSineWave(Time);
        const FVector Point(Time * Frequency, SineValue, 0.0f); // Adjust the scaling as needed
        Points.Add(Point);
    }

    // Draw the sine wave using the line batch component
    LineBatchComponent->Clear();
    if (Points.Num() >= 2)
    {
        for (int32 i = 1; i < Points.Num(); ++i)
        {
            LineBatchComponent->DrawLine(Points[i - 1], Points[i], FColor::White, FColor::White, 0.1f); // Adjust the colors and thickness as needed
        }
    }
}

float AMySineWaveActor::CalculateSineWave(float Time)
{
    return Amplitude * FMath::Sin(Frequency * Time + Phase);
}

在这个示例中,AMySineWaveActor类包含了一个ULineBatchComponent成员变量,用于在每一帧绘制表示正弦波的线段。在`Tick

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

空谷传声~

您的鼓励是我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值