このドキュメントでは、接触時にカラーが変化し、ゆっくりと消えていく Light アクタの設定方法について説明します。
この設定は、オーバーラップ トリガーとして機能する Box コンポーネントと Point Light アクタを操作する Timeline コンポーネントを含む Point Light コンポーネントを使用して行います。
Fading Light アクタを作成する
ブランク テンプレートで C++ プロジェクトを新規作成して「FadingLights」と名前を付けます。
コンテンツ ブラウザで「C++ Classes」フォルダをクリックし、[+Add (+追加)] ボタンをクリックして [New C++ Class (新規 C++ クラス)] を選択します。
アクタを親クラスとして選択します。
作成したアクタに「LightActor」という名前を付けます。
新しいアクタを作成すると、Visual Studio では自動的に「
LightActor.h」ファイルと「LightActor.cpp」ファイルが開かれます。LightActor.hファイルに移動し、次のように宣言します。LightActor.h
C++#include "Components/TimelineComponent.h"次に、
LightActorクラスの定義に次のコードを追加します。LightActor.h
C++public: UPROPERTY(EditAnywhere) UCurveFloat* PointLightFloatCurve; UPROPERTY(EditAnywhere) UCurveLinearColor* PointLightColorCurve; protected:LightActor.cppに移動し、次のクラス ライブラリを追加します。LightActor.cpp
C++#include "Components/BoxComponent.h" #include "Components/PointLightComponent.h"ALightActor::ALightActorのコンストラクタで、次のように宣言します。LightActor.cpp
C++ALightActor::ALightActor() { // 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 our Default Components PointLightComp = CreateDefaultSubobject<UPointLightComponent>(TEXT("PointLightComp")); LightTimelineComp = CreateDefaultSubobject<UTimelineComponent>(TEXT("LightTimelineComp")); LightOverlapVolume = CreateDefaultSubobject<UBoxComponent>(TEXT("LightOverlapVolume"));次に、Point Light コンポーネントの
UFunctionメソッドを実装します。LightActor.cpp
C++void ALightActor::UpdateLightBrightness(float BrightnessOutput) { PointLightComp->SetLightBrightness(BrightnessOutput * 20.0f); } void ALightActor::UpdateLightColor(FLinearColor ColorOutput) { PointLightComp->SetLightColor(ColorOutput); }次に、
BeginPlayメソッドに次のコードを追加します。LightActor.cpp
C++void ALightActor::BeginPlay() { Super::BeginPlay(); //Binding our float and color track to their respective functions UpdateBrightnessTrack.BindDynamic(this, &ALightActor::UpdateLightBrightness); UpdateColorTrack.BindDynamic(this, &ALightActor::UpdateLightColor); //If we have a float curve, bind it's graph to our update function if (PointLightFloatCurve)コードをコンパイルします。
コンテンツ ブラウザで、「C++ Classes」フォルダに移動します。
LightActor を右クリックして [Create Blueprint Class based on LightActor (LightActor に基づいてブループリント クラスを作成する)] を選択し、ブループリント アクタの名前を「BP_LightActor」にします。
BP_LightActor のクラスのデフォルトが、次のように表示されます。
作成中のコード
LightActor.h
//Copyright 1998-2022 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "Components/TimelineComponent.h"
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "LightActor.generated.h"
UCLASS()
class FADINGLIGHTS_API ALightActor : public AActor
LightActor.cpp
//Copyright 1998-2022 Epic Games, Inc. All Rights Reserved.
#include "LightActor.h"
#include "Components/BoxComponent.h"
#include "Components/PointLightComponent.h"
// Sets default values
ALightActor::ALightActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
コリジョン オーバーラップ イベントを作成してバインディングする
Box コンポーネントには、アクタがコリジョンの境界に立ち入る際に TimelineComponent をトリガーする機能が必要です。
LightActor.hファイルのクラス定義に移動し、BrightnessMultiplierの下で次のように宣言します。LightActor.h
C++protected: UFUNCTION() void OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);次に、
LightActor.cppファイルに移動してOnOverlapBegin関数を実装します。LightActor.cpp
C++void ALightActor::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) { LightTimelineComp->Play(); }BeginPlayメソッドで Overlap 関数をバインドします。LightActor.cpp
C++//Binding our Box Component to our Light Actor's Overlap Function LightOverlapVolume->OnComponentBeginOverlap.AddDynamic(this, &ALightActor::OnOverlapBegin);コードをコンパイルします。
完成コード
LightActor.h
//Copyright 1998-2022 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "Components/TimelineComponent.h"
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "LightActor.generated.h"
UCLASS()
class FADINGLIGHTS_API ALightActor : public AActor
LightActor.cpp
//Copyright 1998-2022 Epic Games, Inc. All Rights Reserved.
// Fill out your copyright notice in the Description page of Project Settings.
#include "LightActor.h"
#include "Components/BoxComponent.h"
#include "Components/PointLightComponent.h"
// Sets default values
ALightActor::ALightActor()
Brightness トラックを設定する
プレイヤーが Light アクタの Box コンポーネントの境界に重なる場合、Timeline コンポーネントでは Point Light コンポーネントの輝度の値を操作するための Float カーブが必要になります。
明るさの初期値は「5000」で、5 秒間で「0」まで減少します。
コンテンツ ブラウザに移動し、[+Add (+追加)] > [Miscellaneous (その他)] > [Curve (カーブ)] を選択します。
[CurveFloat] を選択してアセットの名前を「BrightnessCurveFloat」にします。
[BrightnessCurveFloat] をダブルクリックしてタイムライン エディタを開きます。
グラフを右クリックして [Add Key (キーを追加)] を選択し、2 つのキーを Float カーブに追加します。 1 つ目のキーの時間値を (0, 5000) に調節します。 2 つ目のキーの時間値を (5, 0) に調節します。 BrightnessCurveFloat は以下のようになります。
[BrightnessCurveFloat] を保存してからコンテンツ ブラウザに戻り、[BP_LightActor] をダブルクリックして [Class Defaults (クラスのデフォルト)] を開きます。
[Details] パネルに移動し、[Point Light Float Curve] のドロップダウン メニューから [Brightness Curve Float] を選択します。
[Compile (コンパイル)] ボタンと、[Save (保存)] ボタンを順にクリックします。
カラー トラックを設定する
プレイヤーが Light アクタの Box コンポーネントの境界に重なる場合、PointLight タイムラインでは Point Light コンポーネントのカラー プロパティを操作するための線形カラー カーブ トラックが必要になります。
コンテンツ ブラウザに移動し、[+Add (+追加)] > [Miscellaneous (その他)] > [Curve (カーブ)] を選択します。
[CurveLinearColor] を選択してアセットの名前を「LinearColorCurve」にします。
[LinearColorCurve] をダブルクリックしてタイムライン エディタを開きます。
最初のカラー キーをダブルクリックして、RGB の値を (R:1、G:0.665、B:0.015) に変更します。
2 つ目のカラー キーをダブルクリックして、RGB の値を (R:0、G:0、B:0) に変更します。
グラフで 2 つ目の点を選択して時間を 5 秒に設定します。
LinearColorCurve は以下のようになります。
[LinearColorCurve] を保存してからコンテンツ ブラウザに戻り、[BP_LightActor] をダブルクリックして [Class Defaults] を開きます。
[Details] パネルに移動し、[Point Light Float Curve] のドロップダウン メニューから [Brightness Curve Float] を選択します。
[Compile (コンパイル)] ボタンと、[Save (保存)] ボタンを順にクリックします。
レベル設定
作成したコードの機能を最善の状態で実行するには、レベルからすべての光源アクタを削除する必要があります。
コンテンツ ブラウザで BP_LightActor アセットに移動して、それを選択してレベルにドラッグします。
[World Outliner (アウトライナ)] で BP_LightActor を選択して、[Details] パネルに移動し、[Location] 設定を (0, 0, 300)、[Scale] 設定を (10, 10, 10) に変更します。
[World Outliner] で DirectionalLight アクタを削除します。
レベルは以下のようになります。
最終結果
これで、Light アクタとレベルを設定できました。Play (PIE) をクリックすると観戦者のポーンの所有権が自動的に得られます。
観戦者のポーンを制御し、Light アクタの Box コンポーネントの境界に移動することができます。
Timeline コンポーネントの Play 関数がトリガーされると、ライトのカラーおよび明るさは 5 秒間で変化していきます。