How to make a `SceneViewExtension`

Hello. I am trying to make a SceneViewExtension that can modify the projection matrix, and hopefully, make a camera component that supports lens shift.

This is what I have now:

// LensShiftCameraComponent.h

#pragma once

#include "CoreMinimal.h"
#include "Camera/CameraComponent.h"
#include "SceneViewExtension.h"
#include "LensShiftCameraComponent.generated.h"



/**
 *
 */
class ObliqueProjectionExtension : public FSceneViewExtensionBase
{
private:
	float m_x, m_y;

public:
	ObliqueProjectionExtension(const FAutoRegister& AutoRegister) : FSceneViewExtensionBase(AutoRegister) {}

	~ObliqueProjectionExtension() {}

	void SetupViewProjectionMatrix(FSceneViewProjectionData& InOutProjectionData) override {
		auto m = InOutProjectionData.ProjectionMatrix.M;
		m[0][2] = m_x;
		m[1][2] = m_y;
	}

	void SetObliqueness(float x, float y) { m_x = x; m_y = y; }

	void SetupViewFamily(FSceneViewFamily& InViewFamily) override {}

	void SetupView(FSceneViewFamily& InViewFamily, FSceneView& InView) override {}

	void BeginRenderViewFamily(FSceneViewFamily& InViewFamily) override {}
};

/**
 *
 */
UCLASS()
class METALMAX3D_API ULensShiftCameraComponent : public UCameraComponent
{
	GENERATED_BODY()

private:
	TSharedRef<ObliqueProjectionExtension, ESPMode::ThreadSafe> ope;

public:

	UPROPERTY(Category = CameraOptions, EditAnywhere, BlueprintReadWrite)
	float LensShiftX;

	UPROPERTY(Category = CameraOptions, EditAnywhere, BlueprintReadWrite)
	float LensShiftY;

	ULensShiftCameraComponent() { 
		ope = FSceneViewExtensions::NewExtension<ObliqueProjectionExtension>();
		PrimaryComponentTick.bCanEverTick = true;
	}

	void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) {
		ope->SetObliqueness(LensShiftX, LensShiftY);
	}

};

It does not compile and says C2512: ObliqueProjectionExtension::ObliqueProjectionExtension does not have a default constructor at line:
ope = FSceneViewExtensions::NewExtension<ObliqueProjectionExtension>();.

But the example code in SceneViewExtension.h says “your first argument must be FAutoRegister, and you must pass it to FSceneViewExtensionBase constructor”.

Please help me.

The error stems from

TSharedRef<ObliqueProjectionExtension, ESPMode::ThreadSafe> ope;

TSharedRef cannot be null. Try it with the value already set


UCLASS()
class METALMAX3D_API ULensShiftCameraComponent : public UCameraComponent
{
	GENERATED_BODY()

private:
	TSharedRef<ObliqueProjectionExtension, ESPMode::ThreadSafe> ope = FSceneViewExtensions::NewExtension<ObliqueProjectionExtension>();

public:

	UPROPERTY(Category = CameraOptions, EditAnywhere, BlueprintReadWrite)
		float LensShiftX;

	UPROPERTY(Category = CameraOptions, EditAnywhere, BlueprintReadWrite)
		float LensShiftY;
	
	ULensShiftCameraComponent() {
		//ope = FSceneViewExtensions::NewExtension<ObliqueProjectionExtension>();

		PrimaryComponentTick.bCanEverTick = true;
	}
	
	void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) {
		ope->SetObliqueness(LensShiftX, LensShiftY);
	}

};

It compiles ok but the test is up to you.

TSharedRef cannot be null
TSharedPtr can be null

1 Like

just in case someone else is looking for a more general answer:

here

https://dev.epicgames.com/community/learning/knowledge-base/0ql6/unreal-engine-using-sceneviewextension-to-extend-the-rendering-system

and here

This topic has been moved from International to Programming & Scripting.

When posting, please review the categories to ensure your topic is posted in the most relevant space.