# Howto modify the projection matrix

**URL:** <https://forums.unrealengine.com/t/howto-modify-the-projection-matrix/287457>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [July 5, 2014, 7:03am UTC](https://forums.unrealengine.com/t/howto-modify-the-projection-matrix/287457 "2014-07-05T07:03:14Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![hurricane86](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@hurricane86](https://forums.unrealengine.com/u/hurricane86)\
**Post date:** [July 5, 2014, 7:03am UTC](https://forums.unrealengine.com/t/howto-modify-the-projection-matrix/287457/1 "2014-07-05T07:03:14Z")

</div>

Hi guys,

I’m trying to create a **runtime plugin** to modify the projection matrix every frame (without touching the engine).

BUT:

- i can’t override **LocalPlayer::CalcSceneView** / **LocalPlayer::GetProjectionData** because they aren’t virtual.
- i can’t override **ISceneViewExtension** because seems that this extensions are used just for the HMD device (have i to create a fake **IHeadMountedDisplay** implementation?)

thanks a lot,

Martin

---

<div class="post-metadata">

**Author:** ![Dieselhead](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Dieselhead](https://forums.unrealengine.com/u/Dieselhead)\
**Post date:** [July 8, 2014, 1:02pm UTC](https://forums.unrealengine.com/t/howto-modify-the-projection-matrix/287457/2 "2014-07-08T13:02:38Z")

</div>

Have you looked at [AActor::CalcCamera()](https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/GameFramework/AActor/CalcCamera/index.html) and [UCameraComponent::GetCameraView()](https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Camera/UCameraComponent/GetCameraView/index.html) ?

---

<div class="post-metadata">

**Author:** ![hurricane86](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@hurricane86](https://forums.unrealengine.com/u/hurricane86)\
**Post date:** [July 8, 2014, 4:11pm UTC](https://forums.unrealengine.com/t/howto-modify-the-projection-matrix/287457/3 "2014-07-08T16:11:49Z")

</div>

Hi,

yes, both give me as output a FMinimalViewInfo that doesn’t contains any info about the projection matrix. anyway it’s a read only struct

thanks

---

<div class="post-metadata">

**Author:** ![hurricane86](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@hurricane86](https://forums.unrealengine.com/u/hurricane86)\
**Post date:** [July 8, 2014, 6:03pm UTC](https://forums.unrealengine.com/t/howto-modify-the-projection-matrix/287457/5 "2014-07-08T18:03:21Z")

</div>

what im trying now is to extend UGameViewportClient

I’ll ovveride the Draw function. this function will be a copy of UGameViewportClient::Draw but after LocalPlayer-\>CalcSceneView I put my code …

Not very elegant … but seems the only option that I have…

---

<div class="post-metadata">

**Author:** ![KennyBobby](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@KennyBobby](https://forums.unrealengine.com/u/KennyBobby)\
**Post date:** [August 1, 2014, 12:39pm UTC](https://forums.unrealengine.com/t/howto-modify-the-projection-matrix/287457/6 "2014-08-01T12:39:32Z")

</div>

I am trying to set the projection matrix too. Did you manage to do it?

---

<div class="post-metadata">

**Author:** ![KennyBobby](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@KennyBobby](https://forums.unrealengine.com/u/KennyBobby)\
**Post date:** [August 1, 2014, 12:40pm UTC](https://forums.unrealengine.com/t/howto-modify-the-projection-matrix/287457/7 "2014-08-01T12:40:01Z")

</div>

I am trying to set the projection matrix too. Did you manage to do it?

---

<div class="post-metadata">

**Author:** ![hurricane86](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@hurricane86](https://forums.unrealengine.com/u/hurricane86)\
**Post date:** [August 1, 2014, 12:50pm UTC](https://forums.unrealengine.com/t/howto-modify-the-projection-matrix/287457/8 "2014-08-01T12:50:19Z")

</div>

Yes! If you don’t want to recompile the engine, the only solution is to extend UGameViewportClient class, overwrite the method ::Draw … it’s not a proper overwrite because you have to do a 1:1 copy …then after the call to LocalPlayer-\>CalcSceneView you have the View struct that contains the projection matrix (if you touch the proj matrix be sure to update related matrices …es viewprojmatrix…etc)  
When you have done this you have to set your viewport class in Edit-\>Project Settings-\>General Settings

---

<div class="post-metadata">

**Author:** ![KennyBobby](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@KennyBobby](https://forums.unrealengine.com/u/KennyBobby)\
**Post date:** [August 1, 2014, 9:48pm UTC](https://forums.unrealengine.com/t/howto-modify-the-projection-matrix/287457/9 "2014-08-01T21:48:39Z")

</div>

Thank you sooooo much 🙂

---

<div class="post-metadata">

**Author:** ![vimaxus](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@vimaxus](https://forums.unrealengine.com/u/vimaxus)\
**Post date:** [November 28, 2014, 9:20am UTC](https://forums.unrealengine.com/t/howto-modify-the-projection-matrix/287457/10 "2014-11-28T09:20:49Z")

</div>

Thank you very much! (kinda late)  
Did you or KennyBobby run into the blur problem mentioned [here](https://forums.unrealengine.com/showthread.php?29599-Camera-Projection-Matrices):  
and fixed it?  
Also some ups [here](https://answers.unrealengine.com/questions/137655/feature-request-modifying-the-projection-matrix.html) would be welcome.

It’s driving me insane, I lost a week trying to figure it out. I reached a point where I would pay for a plugin that would accept 2 floats for a basic camera shift.

---

<div class="post-metadata">

**Author:** ![hurricane86](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@hurricane86](https://forums.unrealengine.com/u/hurricane86)\
**Post date:** [November 28, 2014, 11:11am UTC](https://forums.unrealengine.com/t/howto-modify-the-projection-matrix/287457/11 "2014-11-28T11:11:29Z")

</div>

I think that the ue4 projection matrix has some “requirements” …i have fixed all rendering problems doing these “magic stuffs”:

```
result.M[2][2] = 0.0f;
result.M[3][0] = 0.0f;
result.M[3][1] = 0.0f;

result *= 1.0f / result.M[0][0];
result.M[3][2] = GNearClippingPlane;

```

where result is your off-axis proj matrix

Other important point: be sure to update all related matrices:

```
	View->ProjectionMatrixUnadjustedForRHI = CalculateOffAxisProjectionMatrix(Viewport, location);

	FVector viewOrigin(0.0f, 0.0f, View->ProjectionMatrixUnadjustedForRHI.M[3][3]);

	View->ViewMatrices.ViewMatrix.SetOrigin(View->ViewMatrices.ViewMatrix.GetOrigin() - viewOrigin);

	View->InvViewMatrix = View->ViewMatrices.ViewMatrix.InverseSafe();
	View->ViewMatrices.ViewOrigin += View->InvViewMatrix.TransformPosition(-viewOrigin);
	View->ViewMatrices.PreViewTranslation = -View->ViewMatrices.ViewOrigin;
	View->ViewMatrices.ProjMatrix = _AdjustProjectionMatrixForRHI(View->ProjectionMatrixUnadjustedForRHI);
	View->ViewProjectionMatrix = View->ViewMatrices.GetViewProjMatrix();
	View->InvViewProjectionMatrix = View->ViewMatrices.GetInvProjMatrix() * View->InvViewMatrix;
	FMatrix TranslatedViewMatrix = FTranslationMatrix(-View->ViewMatrices.PreViewTranslation) * View->ViewMatrices.ViewMatrix;
	View->ViewMatrices.TranslatedViewProjectionMatrix = TranslatedViewMatrix * View->ViewMatrices.ProjMatrix;
	View->ViewMatrices.InvTranslatedViewProjectionMatrix = View->ViewMatrices.TranslatedViewProjectionMatrix.InverseSafe();
	View->ShadowViewMatrices = View->ViewMatrices;

	//View->InvDeviceZToWorldZTransform = CreateInvDeviceZToWorldZTransform(View->ProjectionMatrixUnadjustedForRHI);

	GetViewFrustumBounds(View->ViewFrustum, View->ViewProjectionMatrix, false);
```

---

<div class="post-metadata">

**Author:** ![TyLandercasper\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TyLandercasper\_1](https://forums.unrealengine.com/u/TyLandercasper_1)\
**Post date:** [December 13, 2014, 7:29pm UTC](https://forums.unrealengine.com/t/howto-modify-the-projection-matrix/287457/12 "2014-12-13T19:29:50Z")

</div>

Thank you so much for going through all of the trouble of figuring this out!

Any chance you could put up the complete code solution so all of us lazy googlers can just rip your work off whole sale and take all of the credit?

It would be much appreciated!

---

<div class="post-metadata">

**Author:** ![vimaxus](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@vimaxus](https://forums.unrealengine.com/u/vimaxus)\
**Post date:** [December 30, 2014, 7:05pm UTC](https://forums.unrealengine.com/t/howto-modify-the-projection-matrix/287457/13 "2014-12-30T19:05:56Z")

</div>

Thank you for all your help.  
Unfortunately, as showed [here](https://forums.unrealengine.com/showthread.php?29599-Camera-Projection-Matr), I’m still getting the motion blur.

I used all your suggestions with the following exceptions:  
I had some errors with \_AdjustProjectionMatrixForRHI so I copy-pasted it with the default values and also I used the non-safe inverts.

The only things that could be the cause of what I am seeing is maybe because I commented out SCOPE\_CYCLE\_COUNTER because I couldn’t get rid of some related errors (STAT\_HudTime, STAT\_UIDrawingTime)  
Or maybe you inserted the corrections elsewhere?  
I put it as mentioned in the NumViews for, right after FSceneView\* View = LocalPlayer-\>CalcSceneView(&ViewFamily,…

On another note I got some limited success by modifying the engine (scenecapture2d). It’s a virtual function so I tried extending and overriding but since it’s FScene I’m supposed to extend and replace I can’t solve all the dependencies.

---

<div class="post-metadata">

**Author:** ![hurricane86](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@hurricane86](https://forums.unrealengine.com/u/hurricane86)\
**Post date:** [December 30, 2014, 7:58pm UTC](https://forums.unrealengine.com/t/howto-modify-the-projection-matrix/287457/14 "2014-12-30T19:58:19Z")

</div>

Try to use the code attached here

Some parts are marked with (@TODO) and there you have to make some changes…

good luck!

---

<div class="post-metadata">

**Author:** ![hurricane86](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@hurricane86](https://forums.unrealengine.com/u/hurricane86)\
**Post date:** [December 30, 2014, 7:59pm UTC](https://forums.unrealengine.com/t/howto-modify-the-projection-matrix/287457/15 "2014-12-30T19:59:09Z")

</div>

see my reply below

---

<div class="post-metadata">

**Author:** ![vimaxus](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@vimaxus](https://forums.unrealengine.com/u/vimaxus)\
**Post date:** [December 30, 2014, 8:06pm UTC](https://forums.unrealengine.com/t/howto-modify-the-projection-matrix/287457/16 "2014-12-30T20:06:59Z")

</div>

Thank you, thank you, thank you, thank you!  
I now have everything I need, I have to make it to work.  
Thank you once again!

---

<div class="post-metadata">

**Author:** ![TyLandercasper\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TyLandercasper\_1](https://forums.unrealengine.com/u/TyLandercasper_1)\
**Post date:** [December 31, 2014, 7:09pm UTC](https://forums.unrealengine.com/t/howto-modify-the-projection-matrix/287457/17 "2014-12-31T19:09:52Z")

</div>

Thanks man! I ended up figuring it out and I did something similar. Since it’ll cover some of your todo’s I’ll add it here:

What I ended up doing was creating a SetOffAxisMatrix blueprint option so that I could dynamically update it.

I also made a special function which I’ll attach below: (forgive the c# code, I’m not to good with C++)

The function takes 3 parameters: The height of the screen, the width of the screen, and the position of the eye relative to the center of the screen, all in MM. (so 0,0,0 would mean you’re in the middle of the tv, 0,0,500 would mean your head is 500mm back from the center of the screen and -20,0,500 would mean your head is back and slightly to the left of the center of the scren) I think the math reduces away the differences between mm,cm or Unreal world units so it doesn’t matter what you use so long as its all the same scale.

Thanks again man, I couldn’t have done this without your help!

```
	public static Matrix4 UpdateFrustrum(float ScreenWidth,float ScreenHeight,Vector3 eyePosition)
	{
		float headX = eyePosition.X / ScreenHeight;
		float headY = eyePosition.Y / ScreenHeight;
		float headDist = eyePosition.Z / ScreenHeight;
		float screenAspect = ScreenWidth / ScreenHeight;

		float nearPlane = 1f;
		Matrix4 resultM = Matrix4.CreatePerspectiveOffCenter( nearPlane*(-.5f * screenAspect - headX)/headDist, 
			nearPlane*(.5f * screenAspect - headX)/headDist, 
			nearPlane*(-.5f + headY)/headDist, 
			nearPlane*(.5f + headY)/headDist, 
			nearPlane, 1000);

		return MagicFix (resultM);
	}

	private static Matrix4 MagicFix(Matrix4 matrix){
		float[,] result = Matrix2Float (matrix);
		//result [1, 2] = 0f;
		result[2,3] = 1f;
		result[2,2] = 0.0f;
		result[3,0] = 0.0f;
		result[3,1] = 0.0f;

		result = ScaleMatrix(result, (1.0f / result [0, 0]));
		result[3,2] = nearClipPlane;
		return Float2Matrix (result);
	}

```

---

<div class="post-metadata">

**Author:** ![TyLandercasper\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TyLandercasper\_1](https://forums.unrealengine.com/u/TyLandercasper_1)\
**Post date:** [December 31, 2014, 7:16pm UTC](https://forums.unrealengine.com/t/howto-modify-the-projection-matrix/287457/18 "2014-12-31T19:16:07Z")

</div>

I was able to get it to work with everything except I had to turn anti-aliasing down to 2 or off.

there’s also some wierd lightmap issues, but since its just a prototype I’m not to worried about it. (I mostly just disabled fog)

---

<div class="post-metadata">

**Author:** ![TyLandercasper\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TyLandercasper\_1](https://forums.unrealengine.com/u/TyLandercasper_1)\
**Post date:** [January 1, 2015, 3:39am UTC](https://forums.unrealengine.com/t/howto-modify-the-projection-matrix/287457/19 "2015-01-01T03:39:11Z")

</div>

Also, if you don’t have a problem with modifying the source code, than its worth saying that I was able to fix all of the render issues (minus the anti-aliasing) by modifying the code in localplayer getprojectiondata instead.

void ULocalPlayer::SetOffAxis(const FMatrix& offAxisMatrix){  
EnableOffAxis = true;  
OffAxisProjectionMatrix = offAxisMatrix;  
}

In the function ULocalPlayer::GetProjectionData I added:  
if (EnableOffAxis)  
{  
ProjectionData.ProjectionMatrix = OffAxisProjectionMatrix;//  
// calculate the out rect  
ProjectionData.SetViewRectangle(FIntRect(X, Y, X + SizeX, Y + SizeY));  
}  
else if( !bNeedStereo )

---

<div class="post-metadata">

**Author:** ![kor6en](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kor6en](https://forums.unrealengine.com/u/kor6en)\
**Post date:** [February 26, 2015, 8:38am UTC](https://forums.unrealengine.com/t/howto-modify-the-projection-matrix/287457/20 "2015-02-26T08:38:42Z")

</div>

Can You please describe how to use those files for dummy? I know how to use blueprints, etc. Some little more specific instructions of what to do to achieve projection mapping on material or postprocess.  
Thanks!

---

<div class="post-metadata">

**Author:** ![Dannington](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Dannington](https://forums.unrealengine.com/u/Dannington)\
**Post date:** [March 1, 2015, 10:49pm UTC](https://forums.unrealengine.com/t/howto-modify-the-projection-matrix/287457/21 "2015-03-01T22:49:18Z")

</div>

Hi Hurricane and all.

I’ve downloaded your source but I get compiler errors - namely it says it can’t load the IHeadMountedDisplay.h file. I’ve tried changing this line to include the whole the path to the source but then it just comes up with a whole load of other errors.

Is it because i’m trying to build it in my game’s source folder rather than the engine?

Ah - do I try to build it in the plugin folder maybe?

Any help (for a C++ newb) would be much appreciated.

Thanks for spending the time to publish this source on the answerhub - i’m really interested in using this to create a kind of 3d boardgame effect for a TV show where the projection is calculated from the point of view of the tv studio camera.

[Next page](https://forums.unrealengine.com/t/howto-modify-the-projection-matrix/287457.md?page=2)
