• UE4C++学习篇(十八)-- SceneCaptureComponent组件处理小地图


    一般在我们的地图上面会有对应场景的小地图,做法有很多种,这次分享一下使用SceneCaptureComponent2D这个组件去处理小地图。

    这个的原理是使用摄像机,创建一个RenderTarget和对应材质,通过实时拍摄画面,传递到创建的UI中。

    1.创建拍摄带有SceneCaptureComponent2D组件的C++类:

    1. // Fill out your copyright notice in the Description page of Project Settings.
    2. #pragma once
    3. #include "CoreMinimal.h"
    4. #include "Engine/SceneCapture2D.h"
    5. #include "MiniMapActor.generated.h"
    6. class ARoleBase;
    7. /**
    8. *
    9. */
    10. UCLASS()
    11. class RPGGAME_API AMiniMapActor : public ASceneCapture2D
    12. {
    13. GENERATED_BODY()
    14. public:
    15. AMiniMapActor();
    16. virtual void BeginPlay() override;
    17. /*更新小地图Actor位置*/
    18. void UpdateMiniMapActorLocation();
    19. protected:
    20. virtual void Tick( float DeltaSeconds) override;
    21. protected:
    22. ARoleBase* PlayerRef;
    23. UPROPERTY(EditAnywhere,Category = "Properties")
    24. float CameraDistance;
    25. };
    1. // Fill out your copyright notice in the Description page of Project Settings.
    2. #include "Other/MiniMapActor.h"
    3. #include "Kismet/GameplayStatics.h"
    4. #include "Gameplay/RoleBase.h"
    5. #include "Components/SceneCaptureComponent2D.h"
    6. AMiniMapActor::AMiniMapActor()
    7. {
    8. PrimaryActorTick.bCanEverTick = true;
    9. GetCaptureComponent2D()->SetRelativeRotation(FRotator(-90.0f,0.0f,0.0f));
    10. CameraDistance = 1000.0f;
    11. }
    12. void AMiniMapActor::BeginPlay()
    13. {
    14. Super::BeginPlay();
    15. PlayerRef = Cast(UGameplayStatics::GetPlayerPawn(this,0));
    16. }
    17. void AMiniMapActor::UpdateMiniMapActorLocation()
    18. {
    19. if (!PlayerRef)
    20. {
    21. GEngine->AddOnScreenDebugMessage(-1,5.0f,FColor::Red,TEXT("11111111111"));
    22. return;
    23. }
    24. const FVector PlayerLocation = PlayerRef->GetActorLocation();
    25. const FVector NewLocation = FVector(PlayerLocation.X,PlayerLocation.Y,PlayerLocation.Z + CameraDistance);
    26. SetActorLocation(NewLocation);
    27. }
    28. void AMiniMapActor::Tick(float DeltaSeconds)
    29. {
    30. Super::Tick(DeltaSeconds);
    31. UpdateMiniMapActorLocation();
    32. }

    2.创建渲染使用的RenderTarget:

    并创建出对应的材质:

     注意选择使用遮罩和UI,注意圆形遮罩的中心点。

     3.创建UserWidget,将RenderTarget材质赋值:

     上述的代码中有将摄像机和角色绑定为相对位置,这样角色到一些地图的高处的时候就仍然会保持相对位置,不会穿模。

    效果图:

     上面有一个对应角色位置的小图标,我这里是用代码每帧处理的:

    获取到角色引用之后,每帧设置提示小图标角度即可。

    1. const FRotator PlayerRotation = PlayerRef->GetActorRotation();
    2. PlayerTip->SetRenderTransformAngle(PlayerRotation.Yaw);

  • 相关阅读:
    CleanMyMac X4.12.1苹果电脑系统优化软件更新功能介绍
    Rust的Slice切片
    HBase 进阶
    2021年PHP-Laravel面试题问卷题 答案记录
    JavaWeb的基本概念
    【DevOps】Git 图文详解(五):远程仓库
    Qt状态机框架
    神经网络控制器设计原理,神经网络控制系统设计
    使用Keepalived实现双机热备(虚拟漂移IP地址)详细介绍
    立体仓库-提升机保养程序
  • 原文地址:https://blog.csdn.net/qq_42597694/article/details/126898989