【UE】UEC++ CurveFloat、CurveVector、CurveLinearColor

目录

【UE】UEC++ CurveFloat、CurveVector、CurveLinearColor

一、CurveFloat

二、CurveVector

三、CurveLinearColor

四、材质设置

五、运行结果


【UE】UEC++ CurveFloat、CurveVector、CurveLinearColor

一、CurveFloat

【UE】UEC++ CurveFloat、CurveVector、CurveLinearColor_第1张图片

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "CurveActor.generated.h"

UCLASS()
class CURVEPROJECT_API ACurveActor : public AActor
{
	GENERATED_BODY()
public:	
	ACurveActor();
	virtual void Tick(float DeltaTime) override;
protected:
	virtual void BeginPlay() override;
private:
	UPROPERTY(EditAnywhere)
	UStaticMeshComponent* Sphere;
	UPROPERTY(EditAnywhere)
	UStaticMesh* Mesh;
	UPROPERTY(EditAnywhere)
	UMaterial* Material;
	UPROPERTY()
	UMaterialInstanceDynamic* MaterialInstance;
private:
	UPROPERTY(EditAnywhere)
	class UCurveFloat* CurveFloat;
	UPROPERTY()
	float CurveFloatValue;
	UPROPERTY()
	float CurveFloatTime;
public:
	UFUNCTION()
	void UpdateByCurveFloat(float InDeltaTime);
};
#include "CurveActor.h"

#include "Kismet/KismetMaterialLibrary.h"

ACurveActor::ACurveActor()
{
	PrimaryActorTick.bCanEverTick = true;
	
	Mesh = ConstructorHelpers::FObjectFinder(TEXT("StaticMesh'/Engine/EditorMeshes/EditorSphere.EditorSphere'")).Object;
	Material = ConstructorHelpers::FObjectFinder(TEXT("Material'/Game/Materials/M_Curve.M_Curve'")).Object;
	
	Sphere = CreateDefaultSubobject(TEXT("Sphere"));
	Sphere->SetupAttachment(RootComponent);
	SetRootComponent(Sphere);
	Sphere->SetStaticMesh(Mesh);
}

void ACurveActor::BeginPlay()
{
	Super::BeginPlay();
	MaterialInstance = UKismetMaterialLibrary::CreateDynamicMaterialInstance(this,Material); 
	Sphere->SetMaterial(0,MaterialInstance);
}

void ACurveActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	UpdateByCurveFloat(DeltaTime);
}

void ACurveActor::UpdateByCurveFloat(float InDeltaTime)
{
	if(!CurveFloat)return;
	
	CurveFloatValue = CurveFloat->GetFloatValue(CurveFloatTime);
	MaterialInstance->SetScalarParameterValue(FName(TEXT("Opacity")), CurveFloatValue);
	float MinTime;
	float MaxTime;
	CurveFloat->GetTimeRange(MinTime, MaxTime);
	if(MaxTime <=(CurveFloatTime+=InDeltaTime))
	{
		CurveFloatTime=0;
	}
}

二、CurveVector

【UE】UEC++ CurveFloat、CurveVector、CurveLinearColor_第2张图片

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "CurveActor.generated.h"

UCLASS()
class CURVEPROJECT_API ACurveActor : public AActor
{
	GENERATED_BODY()
public:	
	ACurveActor();
	virtual void Tick(float DeltaTime) override;
protected:
	virtual void BeginPlay() override;
private:
	UPROPERTY(EditAnywhere)
	UStaticMeshComponent* Sphere;
	UPROPERTY(EditAnywhere)
	UStaticMesh* Mesh;
	UPROPERTY(EditAnywhere)
	UMaterial* Material;
	UPROPERTY()
	UMaterialInstanceDynamic* MaterialInstance;
private:
	UPROPERTY(EditAnywhere)
	class UCurveVector* CurveVector;
	UPROPERTY()
	FVector3d CurveVectorValue;
	UPROPERTY()
	float CurveVectorTime;
public:
	UFUNCTION()
	void UpdateByCurveVector(float InDeltaTime);
};
// Fill out your copyright notice in the Description page of Project Settings.

#include "CurveActor.h"

#include "Curves/CurveVector.h"
#include "Kismet/KismetMaterialLibrary.h"

ACurveActor::ACurveActor()
{
	PrimaryActorTick.bCanEverTick = true;
	
	Mesh = ConstructorHelpers::FObjectFinder(TEXT("StaticMesh'/Engine/EditorMeshes/EditorSphere.EditorSphere'")).Object;
	Material = ConstructorHelpers::FObjectFinder(TEXT("Material'/Game/Materials/M_Curve.M_Curve'")).Object;
	
	Sphere = CreateDefaultSubobject(TEXT("Sphere"));
	Sphere->SetupAttachment(RootComponent);
	SetRootComponent(Sphere);
	Sphere->SetStaticMesh(Mesh);
}

void ACurveActor::BeginPlay()
{
	Super::BeginPlay();
	MaterialInstance = UKismetMaterialLibrary::CreateDynamicMaterialInstance(this,Material); 
	Sphere->SetMaterial(0,MaterialInstance);
}

void ACurveActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	UpdateByCurveVector(DeltaTime);
}

void ACurveActor::UpdateByCurveVector(float InDeltaTime)
{
	if(!CurveVector)return;

	CurveVectorValue = CurveVector->GetVectorValue(CurveVectorTime);
	SetActorScale3D(CurveVectorValue);
	float MinTime;
	float MaxTime;
	CurveVector->GetTimeRange(MinTime, MaxTime);
	if(MaxTime <=(CurveVectorTime+=InDeltaTime))
	{
		CurveVectorTime=0;
	}
}

三、CurveLinearColor

【UE】UEC++ CurveFloat、CurveVector、CurveLinearColor_第3张图片

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "CurveActor.generated.h"

UCLASS()
class CURVEPROJECT_API ACurveActor : public AActor
{
	GENERATED_BODY()
public:	
	ACurveActor();
	virtual void Tick(float DeltaTime) override;
protected:
	virtual void BeginPlay() override;
private:
	UPROPERTY(EditAnywhere)
	UStaticMeshComponent* Sphere;
	UPROPERTY(EditAnywhere)
	UStaticMesh* Mesh;
	UPROPERTY(EditAnywhere)
	UMaterial* Material;
	UPROPERTY()
	UMaterialInstanceDynamic* MaterialInstance;
private:
	UPROPERTY(EditAnywhere)
	class UCurveLinearColor* CurveLinearColor;
	UPROPERTY()
	FLinearColor CurveLinearColorValue;
	UPROPERTY()
	float CurveLinearColorTime;
public:
	UFUNCTION()
	void UpdateByCurveLinearColor(float InDeltaTime);
};
// Fill out your copyright notice in the Description page of Project Settings.

#include "CurveActor.h"

#include "Curves/CurveLinearColor.h"
#include "Kismet/KismetMaterialLibrary.h"

ACurveActor::ACurveActor()
{
	PrimaryActorTick.bCanEverTick = true;
	
	Mesh = ConstructorHelpers::FObjectFinder(TEXT("StaticMesh'/Engine/EditorMeshes/EditorSphere.EditorSphere'")).Object;
	Material = ConstructorHelpers::FObjectFinder(TEXT("Material'/Game/Materials/M_Curve.M_Curve'")).Object;
	
	Sphere = CreateDefaultSubobject(TEXT("Sphere"));
	Sphere->SetupAttachment(RootComponent);
	SetRootComponent(Sphere);
	Sphere->SetStaticMesh(Mesh);
}

void ACurveActor::BeginPlay()
{
	Super::BeginPlay();
	MaterialInstance = UKismetMaterialLibrary::CreateDynamicMaterialInstance(this,Material); 
	Sphere->SetMaterial(0,MaterialInstance);
}

void ACurveActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	UpdateByCurveLinearColor(DeltaTime);
}

void ACurveActor::UpdateByCurveLinearColor(float InDeltaTime)
{
	if(CurveLinearColor == nullptr || MaterialInstance == nullptr)return;
	
	CurveLinearColorValue = CurveLinearColor->GetLinearColorValue(CurveLinearColorTime);
	MaterialInstance->SetVectorParameterValue(FName(TEXT("Color")), CurveLinearColorValue);
	float MinTime;
	float MaxTime;
	CurveLinearColor->GetTimeRange(MinTime, MaxTime);
	if(MaxTime <=(CurveLinearColorTime+=InDeltaTime))
	{
		CurveLinearColorTime=0;
	}
}

四、材质设置

【UE】UEC++ CurveFloat、CurveVector、CurveLinearColor_第4张图片

五、运行结果

你可能感兴趣的:(UE引擎基础,UE,UEC++,C++,Curve,Float,Vector,LinearColor)