shadowmap

// 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!"));
	// ...
}



在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值