[UE4]UDecalComponent用法

4.11的TopDown Sample Project中,增加了一个新功能:用UDecalComponent显示印花图案,特点是可以在非平面的地形上,按照地面的凹凸地势自动贴紧地面。

定义:

/** A decal that projects to the cursor location. */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class UDecalComponent* CursorToWorld;

构造函数中对其初始化:

// Create a decal in the world to show the cursor's location
CursorToWorld = CreateDefaultSubobject<UDecalComponent>("CursorToWorld");
CursorToWorld->AttachTo(RootComponent);
static ConstructorHelpers::FObjectFinder<UMaterial> DecalMaterialAsset(TEXT("Material'/Game/TopDownCPP/Blueprints/M_Cursor_Decal.M_Cursor_Decal'"));
if (DecalMaterialAsset.Succeeded())
{
	CursorToWorld->SetDecalMaterial(DecalMaterialAsset.Object);
}
CursorToWorld->DecalSize = FVector(16.0f, 32.0f, 32.0f);
CursorToWorld->SetRelativeRotation(FRotator(90.0f, 0.0f, 0.0f).Quaternion());

跟随鼠标位置在地面上绘制:

void ANetworkTestCharacter::Tick(float DeltaSeconds)
{
	if (CursorToWorld != nullptr)
	{
		if (APlayerController* PC = Cast<APlayerController>(GetController()))
		{
			FHitResult TraceHitResult;
			PC->GetHitResultUnderCursor(ECC_Visibility, true, TraceHitResult);
			FVector CursorFV = TraceHitResult.ImpactNormal;
			FRotator CursorR = CursorFV.Rotation();
			CursorToWorld->SetWorldLocation(TraceHitResult.Location);
			CursorToWorld->SetWorldRotation(CursorR);
		}
	}
}

猜你喜欢

转载自aigo.iteye.com/blog/2285920
UE4