// Fill out your copyright notice in the Description page of Project Settings.
#include "ShadowMapActorComponent.h"
#include "Components/SceneCaptureComponent2D.h"
#include "Engine/World.h"
#include "Materials/MaterialParameterCollection.h"
#include "Materials/MaterialParameterCollectionInstance.h"
#include "Kismet/GameplayStatics.h"
// Sets default values for this component's properties
UShadowMapActorComponent::UShadowMapActorComponent()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
// ...
}
// Called when the game starts
void UShadowMapActorComponent::BeginPlay()
{
Super::BeginPlay();
UE_LOG(LogTemp, Warning, TEXT("Hello"));
AActor* MyActor = GetOwner();
UActorComponent* ActorComponent = MyActor->GetComponentByClass(USceneCaptureComponent2D::StaticClass());
if (ActorComponent) {
USceneCaptureComponent2D* SceneCaptureComponent2D = Cast< USceneCaptureComponent2D>(ActorComponent);
UE_LOG(LogTemp, Warning, TEXT("Component Name: %s"), *MyActor->GetName());
if (!SceneCaptureComponent2D)
UE_LOG(LogTemp, Warning, TEXT("Error !"));
FMinimalViewInfo ViewInfo;
MyActor->CalcCamera(0, ViewInfo);
//SceneCaptureComponent2D->GetCameraView(0.0f, ViewInfo);
FVector Location = ViewInfo.Location;
FVector RightVector = MyActor->GetActorRightVector();
FVector ForwardVector = MyActor->GetActorForwardVector();
FVector UpVector = MyActor->GetActorUpVector();
FMatrix ViewMatrix = FMatrix(
FPlane(RightVector.X, UpVector.X, ForwardVector.X, 0),
FPlane(RightVector.Y, UpVector.Y, ForwardVector.Y, 0),
FPlane(RightVector.Z, UpVector.Z, ForwardVector.Z, 0),
FPlane(-FVector::DotProduct(RightVector, Location), -FVector::DotProduct(UpVector, Location), -FVector::DotProduct(ForwardVector, Location), 1)
);
/*UE_LOG(LogTemp, Warning, TEXT("Right Vector : %.2f %.2f %.2f"), RightVector.X, RightVector.Y, RightVector.Z);
UE_LOG(LogTemp, Warning, TEXT("Up Vector : %.2f %.2f %.2f"), UpVector.X, UpVector.Y, UpVector.Z);
UE_LOG(LogTemp, Warning, TEXT("Forward Vector : %.2f %.2f %.2f"), ForwardVector.X, ForwardVector.Y, ForwardVector.Z);*/
FTransform Transform = SceneCaptureComponent2D->GetComponentTransform();
FVector OutForward = Transform.GetUnitAxis(EAxis::X); // 前向量
FVector OutRight = Transform.GetUnitAxis(EAxis::Y); // 右向量
FVector OutUp = Transform.GetUnitAxis(EAxis::Z);
UE_LOG(LogTemp, Warning, TEXT("Scene Right Vector : %.2f %.2f %.2f"), OutRight.X, OutRight.Y, OutRight.Z);
UE_LOG(LogTemp, Warning, TEXT("Scene Up Vector : %.2f %.2f %.2f"), OutUp.X, OutUp.Y, OutUp.Z);
UE_LOG(LogTemp, Warning, TEXT("Scene Forward Vector : %.2f %.2f %.2f"), OutForward.X, OutForward.Y, OutForward.Z);
UE_LOG(LogTemp, Warning, TEXT("ViewInfo : FOV=%.2f AspectRatio=%.2f Width=%.2f"), ViewInfo.FOV, ViewInfo.AspectRatio, ViewInfo.OrthoWidth);
/*NOTE:真正的透视投影*/
/*FMatrix ProjectionMatrix = FPerspectiveMatrix(
FMath::DegreesToRadians(ViewInfo.FOV / 2),
//ViewInfo.AspectRatio,
//1.0f,
//GNearClippingPlane
ViewInfo.OrthoWidth,
ViewInfo.OrthoWidth / ViewInfo.AspectRatio,
ViewInfo.OrthoNearClipPlane,
ViewInfo.OrthoFarClipPlane
);*/
//正交投影
/*FMatrix ProjectionMatrix = FMatrix(
FPlane(2.0f / SceneCaptureComponent2D->OrthoWidth, 0, 0, 0),
FPlane(0, -2.0f / SceneCaptureComponent2D->OrthoWidth, 0, 0),
FPlane(0, 0, 0, 0),
FPlane(0, 0, 0, 0)
);*/
//看这个是真正的正交投影
FMatrix ProjectionMatrix=FOrthoMatrix(ViewInfo.OrthoWidth, ViewInfo.OrthoWidth / ViewInfo.AspectRatio, 1.0, 0.0);
//透视投影
/*FMatrix ProjectionMatrix = FMatrix(
FPlane(1.0f/FMath::DegreesToRadians(ViewInfo.FOV / 2.0f), 0, 0, 0),
FPlane(0, -1.0f / FMath::DegreesToRadians(ViewInfo.FOV / 2.0f), 0, 0),
FPlane(0, 0, 0, 0),
FPlane(0, 0, 0, 0)
);*/
UMaterialParameterCollection* litMaterialSet = LoadObject< UMaterialParameterCollection>(nullptr, TEXT("MaterialParameterCollection'/Game/zhanghaonan/ShadowMap/MPC_ShadowMap.MPC_ShadowMap'"));
if (litMaterialSet)
{
UMaterialParameterCollectionInstance* instance = GetWorld()->GetParameterCollectionInstance(litMaterialSet);
instance->SetVectorParameterValue(FName("LightPos"), FLinearColor(Location.X, Location.Y, Location.Z, 0));
for (int i = 0; i < 4; ++i) {
FName ParameterName = FName("Proj" + FString::FromInt(i));
FLinearColor ParameterValue = FLinearColor(ProjectionMatrix.M[i][0], ProjectionMatrix.M[i][1], ProjectionMatrix.M[i][2], ProjectionMatrix.M[i][3]);
UE_LOG(LogTemp, Warning, TEXT("Set Proj: %.2f %.2f %.2f %.2f"), ParameterValue.R, ParameterValue.G, ParameterValue.B, ParameterValue.A);
instance->SetVectorParameterValue(ParameterName, ParameterValue);
ParameterValue = FLinearColor(-123, -123, -432, 432);
instance->GetVectorParameterValue(ParameterName, ParameterValue);
UE_LOG(LogTemp, Warning, TEXT("Get Proj: %.2f %.2f %.2f %.2f"), ParameterValue.R, ParameterValue.G, ParameterValue.B, ParameterValue.A);
}
for (int i = 0; i < 4; ++i) {
FName ParameterName = FName("View" + FString::FromInt(i));
FLinearColor ParameterValue = FLinearColor(ViewMatrix.M[i][0], ViewMatrix.M[i][1], ViewMatrix.M[i][2], ViewMatrix.M[i][3]);
UE_LOG(LogTemp, Warning, TEXT("Set View: %.2f %.2f %.2f %.2f"), ParameterValue.R, ParameterValue.G, ParameterValue.B, ParameterValue.A);
instance->SetVectorParameterValue(ParameterName, ParameterValue);
ParameterValue = FLinearColor(-123, -123, -432, 432);
instance->GetVectorParameterValue(ParameterName, ParameterValue);
UE_LOG(LogTemp, Warning, TEXT("Get View: %.2f %.2f %.2f %.2f"), ParameterValue.R, ParameterValue.G, ParameterValue.B, ParameterValue.A);
}
}
else
UE_LOG(LogTemp, Warning, TEXT("No Material Set"));
}
else
UE_LOG(LogTemp, Warning, TEXT("Component casting failed!"));
}
// Called every frame
void UShadowMapActorComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
UE_LOG(LogTemp, Warning, TEXT("Hello"));
AActor* MyActor = GetOwner();
UActorComponent* ActorComponent = MyActor->GetComponentByClass(USceneCaptureComponent2D::StaticClass());
if (ActorComponent) {
USceneCaptureComponent2D* SceneCaptureComponent2D = Cast< USceneCaptureComponent2D>(ActorComponent);
UE_LOG(LogTemp, Warning, TEXT("Component Name: %s"), *MyActor->GetName());
if (!SceneCaptureComponent2D)
UE_LOG(LogTemp, Warning, TEXT("Error !"));
FMinimalViewInfo ViewInfo;
MyActor->CalcCamera(0, ViewInfo);
//SceneCaptureComponent2D->GetCameraView(0.0f, ViewInfo);
FVector Location = ViewInfo.Location;
FVector RightVector = MyActor->GetActorRightVector();
FVector ForwardVector = MyActor->GetActorForwardVector();
FVector UpVector = MyActor->GetActorUpVector();
FMatrix ViewMatrix = FMatrix(
FPlane(RightVector.X, UpVector.X, ForwardVector.X, 0),
FPlane(RightVector.Y, UpVector.Y, ForwardVector.Y, 0),
FPlane(RightVector.Z, UpVector.Z, ForwardVector.Z, 0),
FPlane(-FVector::DotProduct(RightVector, Location), -FVector::DotProduct(UpVector, Location), -FVector::DotProduct(ForwardVector, Location), 1)
);
/*UE_LOG(LogTemp, Warning, TEXT("Right Vector : %.2f %.2f %.2f"), RightVector.X, RightVector.Y, RightVector.Z);
UE_LOG(LogTemp, Warning, TEXT("Up Vector : %.2f %.2f %.2f"), UpVector.X, UpVector.Y, UpVector.Z);
UE_LOG(LogTemp, Warning, TEXT("Forward Vector : %.2f %.2f %.2f"), ForwardVector.X, ForwardVector.Y, ForwardVector.Z);*/
FTransform Transform = SceneCaptureComponent2D->GetComponentTransform();
FVector OutForward = Transform.GetUnitAxis(EAxis::X); // 前向量
FVector OutRight = Transform.GetUnitAxis(EAxis::Y); // 右向量
FVector OutUp = Transform.GetUnitAxis(EAxis::Z);
UE_LOG(LogTemp, Warning, TEXT("Scene Right Vector : %.2f %.2f %.2f"), OutRight.X, OutRight.Y, OutRight.Z);
UE_LOG(LogTemp, Warning, TEXT("Scene Up Vector : %.2f %.2f %.2f"), OutUp.X, OutUp.Y, OutUp.Z);
UE_LOG(LogTemp, Warning, TEXT("Scene Forward Vector : %.2f %.2f %.2f"), OutForward.X, OutForward.Y, OutForward.Z);
UE_LOG(LogTemp, Warning, TEXT("ViewInfo : FOV=%.2f AspectRatio=%.2f Width=%.2f"), ViewInfo.FOV, ViewInfo.AspectRatio, ViewInfo.OrthoWidth);
/*NOTE:真正的透视投影*/
/*FMatrix ProjectionMatrix = FPerspectiveMatrix(
FMath::DegreesToRadians(ViewInfo.FOV / 2),
//ViewInfo.AspectRatio,
//1.0f,
//GNearClippingPlane
ViewInfo.OrthoWidth,
ViewInfo.OrthoWidth / ViewInfo.AspectRatio,
ViewInfo.OrthoNearClipPlane,
ViewInfo.OrthoFarClipPlane
);*/
//正交投影
/*FMatrix ProjectionMatrix = FMatrix(
FPlane(2.0f / SceneCaptureComponent2D->OrthoWidth, 0, 0, 0),
FPlane(0, -2.0f / SceneCaptureComponent2D->OrthoWidth, 0, 0),
FPlane(0, 0, 0, 0),
FPlane(0, 0, 0, 0)
);*/
//看这个是真正的正交投影
FMatrix ProjectionMatrix = FOrthoMatrix(ViewInfo.OrthoWidth, ViewInfo.OrthoWidth / ViewInfo.AspectRatio, 1.0, 0.0);
//透视投影
/*FMatrix ProjectionMatrix = FMatrix(
FPlane(1.0f/FMath::DegreesToRadians(ViewInfo.FOV / 2.0f), 0, 0, 0),
FPlane(0, -1.0f / FMath::DegreesToRadians(ViewInfo.FOV / 2.0f), 0, 0),
FPlane(0, 0, 0, 0),
FPlane(0, 0, 0, 0)
);*/
UMaterialParameterCollection* litMaterialSet = LoadObject< UMaterialParameterCollection>(nullptr, TEXT("MaterialParameterCollection'/Game/zhanghaonan/ShadowMap/MPC_ShadowMap.MPC_ShadowMap'"));
if (litMaterialSet)
{
UMaterialParameterCollectionInstance* instance = GetWorld()->GetParameterCollectionInstance(litMaterialSet);
instance->SetVectorParameterValue(FName("LightPos"), FLinearColor(Location.X, Location.Y, Location.Z, 0));
for (int i = 0; i < 4; ++i) {
FName ParameterName = FName("Proj" + FString::FromInt(i));
FLinearColor ParameterValue = FLinearColor(ProjectionMatrix.M[i][0], ProjectionMatrix.M[i][1], ProjectionMatrix.M[i][2], ProjectionMatrix.M[i][3]);
UE_LOG(LogTemp, Warning, TEXT("Set Proj: %.2f %.2f %.2f %.2f"), ParameterValue.R, ParameterValue.G, ParameterValue.B, ParameterValue.A);
instance->SetVectorParameterValue(ParameterName, ParameterValue);
ParameterValue = FLinearColor(-123, -123, -432, 432);
instance->GetVectorParameterValue(ParameterName, ParameterValue);
UE_LOG(LogTemp, Warning, TEXT("Get Proj: %.2f %.2f %.2f %.2f"), ParameterValue.R, ParameterValue.G, ParameterValue.B, ParameterValue.A);
}
for (int i = 0; i < 4; ++i) {
FName ParameterName = FName("View" + FString::FromInt(i));
FLinearColor ParameterValue = FLinearColor(ViewMatrix.M[i][0], ViewMatrix.M[i][1], ViewMatrix.M[i][2], ViewMatrix.M[i][3]);
UE_LOG(LogTemp, Warning, TEXT("Set View: %.2f %.2f %.2f %.2f"), ParameterValue.R, ParameterValue.G, ParameterValue.B, ParameterValue.A);
instance->SetVectorParameterValue(ParameterName, ParameterValue);
ParameterValue = FLinearColor(-123, -123, -432, 432);
instance->GetVectorParameterValue(ParameterName, ParameterValue);
UE_LOG(LogTemp, Warning, TEXT("Get View: %.2f %.2f %.2f %.2f"), ParameterValue.R, ParameterValue.G, ParameterValue.B, ParameterValue.A);
}
}
else
UE_LOG(LogTemp, Warning, TEXT("No Material Set"));
}
else
UE_LOG(LogTemp, Warning, TEXT("Component casting failed!"));
// ...
}