- Home /
Oblique Frustrum on Multiple Cameras
I need to create an oblique frustrum on 3 scene cameras. I'm using Unity's canned SetObliqueness function, which works fine for a single camera. For some reason, when I add additional cameras, the frustrum will only go oblique on the newest camera. If I deactivate a camera, the most recently added camera will return properly oblique, but adding back the deactivated camera removes the obliqueness. I've tried renaming variables and functions and using Camera.current vs. Camera.main but at most, I can only get one camera oblique. I really need to get this functioning properly. Anyone have any ideas?
This is a rather huge project, but I can post the Set Obliqueness subroutine:
public void SetObliqueness2(float horizObl, float vertObl) {
$$anonymous$$atrix4x4 mat = Camera.main.projection$$anonymous$$atrix;
mat[0, 2] = horizObl;
mat[1, 2] = vertObl;
Camera.main.projection$$anonymous$$atrix = mat;
}
It's called inside Start of the camera's primary script as such: SetObliqueness2(0,1f);
I've also tried to call the function from inside Update.
I have two copies of the primary camera, which I have edited to be side cameras. I've looked at the camera matrix values during runtime, and I know my scripts are assigning the values (I assigned slightly different numbers for each camera) correctly. I do have some water objects, which I seem to recall play with the camera frustrum, so maybe some conflict there I can't find? @supernat, maybe I can answer a specific inquiry about the scripts?
Answer by Bunny83 · Jun 15, 2015 at 09:05 AM
Well, your method only works on the main camera. You have to change the method so it can work on a different camera.
You could create a seperate script which contains this:
//ObliqueFrustum.cs
using UnityEngine;
public class ObliqueFrustum : MonoBehaviour
{
private Camera cam;
public Vector2 offset;
void Start()
{
cam = GetComponent<Camera>();
SetMatrix(cam, offset);
}
public void SetObliqueness(float horizObl, float vertObl)
{
offset = new Vector2(horizObl, vertObl);
SetMatrix(cam, offset);
}
public static void SetMatrix(Camera cam, Vector2 offset)
{
Matrix4x4 mat = cam.projectionMatrix;
mat[0, 2] = offset.x;
mat[1, 2] = offset.y;
cam.projectionMatrix = mat;
}
}
Just attach this script to each camera object and set the offset in the inspector. If you want to change the obliquness later at runtime you can call "SetObliqueness()" on the appropriate instance of your script.
Maybe a bit more flexible would be:
//ObliqueFrustum.cs
using UnityEngine;
public class ObliqueFrustum : MonoBehaviour
{
public Vector2 offset;
void Start()
{
GetComponent<Camera>().SetObliqueProjection(offset)
}
}
public static class CameraFrustumExtensions
{
// extension method of the Camera class, needs to be in a static class
public static void SetObliqueProjection(this Camera cam, Vector2 offset)
{
Matrix4x4 mat = cam.projectionMatrix;
mat[0, 2] = offset.x;
mat[1, 2] = offset.y;
cam.projectionMatrix = mat;
}
public static void SetObliqueProjection(this Camera cam, float horizObl, float vertObl)
{
SetObliqueProjection(cam, new Vector2(horizObl, vertObl));
}
}
This way you can simply call "SetObliqueProjection" on any camera reference. So you don't need to attach the "ObliqueFrustum" script to the camera as long as you reference the correct camera. For example:
// some other manager script
// ...
public Camera cam1; // assigned in the inspector
public Camera cam2;
public Camera cam3;
void Start()
{
// using the extension method we defined above.
cam1.SetObliqueProjection(x1, y1);
cam2.SetObliqueProjection(x2, y2);
cam3.SetObliqueProjection(x3, y3);
}
@Bunny83 -- THAN$$anonymous$$S! Your ObliqueFrustrum method works great! (It just didn't compile as is -- for anyone else who faces this issue, this script, with a few $$anonymous$$or tweaks, worked for me):
using UnityEngine;
using System.Collections;
public class ObliqueFrustrum : $$anonymous$$onoBehaviour {
private Camera cam;
public Vector2 offset;
void Start()
{
cam = GetComponent<Camera>();
SetObliqueness(offset);
}
public void SetObliqueness(Vector2 offset)
{
offset = new Vector2(offset.x, offset.y);
Set$$anonymous$$atrix(cam, offset);
}
public static void Set$$anonymous$$atrix(Camera cam, Vector2 offset)
{
$$anonymous$$atrix4x4 mat = cam.projection$$anonymous$$atrix;
mat[0, 2] = offset.x;
mat[1, 2] = offset.y;
cam.projection$$anonymous$$atrix = mat;
}
}
$$anonymous$$any, many thanks!