其他分享
首页 > 其他分享> > 每天学习亿点点day27, 在2D世界里对3D世界进行interact方法

每天学习亿点点day27, 在2D世界里对3D世界进行interact方法

作者:互联网

1. 实现目标: 

在一个Camera的视角下渲染的2D 平面上进行点击,影响其所在3D世界.

2.问题一:

如何把一个Camera渲染得到的画面弄到游戏中的一个surface上?

这个UE4早就想到了我们的需求,于是就有了rendering target这种玩意,简而言之就是可以啥都往上贴往上渲染,我们在很多游戏里面都可以看到的地上的血迹,脚印啥的,都可能是用rendering target做的.

这里我们的做法就是:

a.place 一个Scenecapturesomponent2d到场景里

 

 

b.新建一个rendering target asset,然后把这个rendering target赋给上面的Scenecapturecomponent

 

c.注意一定要右键点击b新建的rendering target然后选择创建material这样才是正确的,不然你单独新建一个material会出现UV问题

d. 然后在material里这样设置就ok了

 

 

 

 

 

用料SceneCaptureComponent2D 继承下来的新的comp然后在里面写Deproject的function,function如下所示,至于原理,你自己想想三大变换的原理然后逆变换就OK了

void UDepreojectableCapComp2D::CaptureComponent2D_DeProject(
    const FVector2D& ScreenPos,
    FVector& OutWorldOrigin,
    FVector& OutWorldDirection)
{
    class USceneCaptureComponent2D* Target = this;
    if ((Target == nullptr) || (Target->TextureTarget == nullptr))
    {
        return;
    }

    const FTransform& Transform = Target->GetComponentToWorld();
    FMatrix ViewMatrix = Transform.ToInverseMatrixWithScale();
    FVector ViewLocation = Transform.GetTranslation();

    // swap axis st. x=z,y=x,z=y (unreal coord space) so that z is up
    ViewMatrix = ViewMatrix * FMatrix(
        FPlane(0, 0, 1, 0),
        FPlane(1, 0, 0, 0),
        FPlane(0, 1, 0, 0),
        FPlane(0, 0, 0, 1));

    const float FOV = Target->FOVAngle * (float)PI / 360.0f;

    FIntPoint CaptureSize(Target->TextureTarget->GetSurfaceWidth(), Target->TextureTarget->GetSurfaceHeight());

    float XAxisMultiplier;
    float YAxisMultiplier;

    if (CaptureSize.X > CaptureSize.Y)
    {
        // if the viewport is wider than it is tall
        XAxisMultiplier = 1.0f;
        YAxisMultiplier = CaptureSize.X / (float)CaptureSize.Y;
    }
    else
    {
        // if the viewport is taller than it is wide
        XAxisMultiplier = CaptureSize.Y / (float)CaptureSize.X;
        YAxisMultiplier = 1.0f;
    }

    FMatrix    ProjectionMatrix = FReversedZPerspectiveMatrix(
        FOV,
        FOV,
        XAxisMultiplier,
        YAxisMultiplier,
        GNearClippingPlane,
        GNearClippingPlane
    );

    const FMatrix InverseViewMatrix = ViewMatrix.InverseFast();
    const FMatrix InvProjectionMatrix = ProjectionMatrix.Inverse();

    const FIntRect ViewRect = FIntRect(0, 0, CaptureSize.X, CaptureSize.Y);

    FSceneView::DeprojectScreenToWorld(ScreenPos, ViewRect, InverseViewMatrix, InvProjectionMatrix, OutWorldOrigin, OutWorldDirection);
}

3.

标签:CaptureSize,const,Target,day27,float,FMatrix,2D,rendering,interact
来源: https://www.cnblogs.com/Tonarinototoro/p/14891597.html