응애맘마조

231124 강의 본문

공부/UE강의

231124 강의

TH.Wert 2023. 11. 25. 20:01

예비군을 갔다 왔습니다. 이제 다시 수업을 듣고 강의를 작성합니다.

#include "Common.hlsl"

struct PixelInput
{
    float4 Position : SV_POSITION;
	float2 Uv : UV0;
};

static const float2 arrBasePos[4] =
{
    float2(-1.0f, +1.0f),
	float2(+1.0f, +1.0f),
	float2(-1.0f, -1.0f),
	float2(+1.0f, -1.0f)
};

static const float2 arrUv[4] =
{
    float2(0.0f, 0.0f),
	float2(+1.0f, 0.0f),
	float2(0.0f, 1.0f),
	float2(+1.0f, 1.0f)
};
PixelInput VS(uint vertexID : SV_VertexID)
{
    PixelInput output;
    output.Position = float4(arrBasePos[vertexID].xy, 0.0f, 1.0f);
    output.Uv = output.Position.xy;
	
    return output;
}

Texture2D diffuseTexture : register(t10);
Texture2D specularTexture : register(t11);
Texture2D normalTexture : register(t12);
Texture2D emissiveTexture : register(t13);
Texture2D ambientTexture : register(t14);
Texture2D depthTexture : register(t15);

SamplerState SamplerDefault
{
	Filter = MIN_MAG_MIP_LINEAR;
	AddressU = Wrap;
	AddressV = Wrap;
};

float ConvertDepthToLinear(float depth)
{
    float linearDepth = PSProj._43 / (depth - PSProj._33);

    return linearDepth;
}
float3 CalcWorldPos(float2 csPos, float linearDepth)
{
    float4 position;
    float2 temp;
    
    temp.x = 1 / PSProj._11;
    temp.y = 1 / PSProj._22;
    
    position.xy = csPos.xy * temp * linearDepth;
    position.z = linearDepth;
    position.w = 1.0f;
	
    return mul(position, InvView).xyz;
}
float4 PS(PixelInput input) : SV_TARGET
{
    int3 location3 = int3(input.Position.xy, 0);
	
    float4 BaseColor = diffuseTexture.Load(location3);
    float3 Normal = normalize(normalTexture.Load(location3).xyz * 2.0f - 1.0f);
    float4 Specular = specularTexture.Load(location3);
    float depth = depthTexture.Load(location3).x;
    float linearDepth = ConvertDepthToLinear(depth);
    float3 wPosition = CalcWorldPos(input.Uv, linearDepth);

    float3 emissive = emissiveTexture.Load(location3).xyz;
    float3 ambient = ambientTexture.Load(location3).xyz;
	
	float4 Result = float4(DeferredDirLighting(BaseColor.rgb, Specular,
    Normal, wPosition.xyz),
    BaseColor.a);

	for (int i = 0; i < lights[0].Size; i++)
	{
        [flatten]
		if (!lights[i].isActive)
			continue;
        
        [flatten]
		if (lights[i].Type == 0)
		{
			Result.rgb += DeferredPointLighting(BaseColor.rgb, Specular,
			Normal, wPosition.xyz, i);
        }
		else if (lights[i].Type == 1)
		{
			Result.rgb += DeferredSpotLighting(BaseColor.rgb, Specular,
            Normal, wPosition.xyz, i);
        }
	}
	
	Result.rgb += emissive;
	Result.rgb += ambient;
	
	return result;
}

전에 했던 디퍼드 렌더링에 약간 문제가 있었습니다. 캐릭터를 복사해서 넣었을 때 광원이 따로 적용되지 않고 한 번에 적용되는 문제였습니다.

따로 적용된 조명효과

언리얼 엔진에서는 캐릭터를 코드로 생성하는 것을 했었습니다. 전에 예비군을 가서 이전 내용은 자세히는 모릅니다.

#include "MyActor.h"
#include "Components/StaticMeshComponent.h"
#include "UObject/ConstructorHelpers.h"
#include "Materials/MaterialInstanceConstant.h"
#include "Global.h"

AMyActor::AMyActor()
{
	PrimaryActorTick.bCanEverTick = true;

	Mesh = CreateDefaultSubobject<UStaticMeshComponent>("Mesh");
	RootComponent = Mesh;

	UStaticMesh* mesh;
	CHelpers::GetAsset<UStaticMesh>(&mesh, "StaticMesh'/Game/Scene2/Mesh/Cone.Cone'");
	Mesh->SetStaticMesh(mesh);
}

void AMyActor::BeginPlay()
{
	Super::BeginPlay();
	UMaterialInstanceConstant* material;
	CHelpers::GetAssetDynamic<UMaterialInstanceConstant>(&material,
		L"MaterialInstanceConstant'/Game/Scene2/Material/MT_Plane.MT_Plane'");
	Material = UMaterialInstanceDynamic::Create(material, this);
	Mesh->SetMaterial(0, Material);
	UKismetSystemLibrary::K2_SetTimer(this, "ChangeColor", 1.0f, true);
}

void AMyActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

void AMyActor::ChangeColor()
{
	FLinearColor color;
	color.R = UKismetMathLibrary::RandomFloatInRange(0, 1);
	color.G = UKismetMathLibrary::RandomFloatInRange(0, 1);
	color.B = UKismetMathLibrary::RandomFloatInRange(0, 1);
	color.A = 1;

	Material->SetVectorParameterValue("BaseColor", color);
}

강의 자료는 이거지만 실행하면 랜덤으로 오브젝트가 생성되는 것이 나왔습니다. 추후에 가능한 대로 작성하겠습니다.

 

읽어주셔서 감사합니다.

'공부 > UE강의' 카테고리의 다른 글

231128 강의  (0) 2023.11.28
231127 강의  (0) 2023.11.27
231117 강의  (0) 2023.11.17
231116 강의  (0) 2023.11.17
231115 강의  (0) 2023.11.15
Comments