- Home /
Camera matrix shifting is moving my skydome
Dear community,
I'm using a script on my camera that moves the projectionMatrix so my perspective changes with the player moving in front of a big screen. I've simplified the script so only the matrix bit is still there instead of boring you with the details.
The problem is that my world reacts normally to the matrix shift, but my skydome moves along with the camera. This results in a weird effect. I've tried to put a skybox on a sphere and that works fine. I've got all my lighting setup and don't feel like making a new scene if any of you know a fix.
I feel like the problem is in the skydome script, but I don't have much knowledge about the shader scripting, apart form culling and lighting.
public var cameraPerspectiveAmount : float = 0.3;
public var range : float;
public var myCamera : Camera;
function Awake () {
}
function Update(){
var xPos : float = Input.GetAxis("Horizontal");
SetVanishingPoint(camera.main, Vector2(xPos * cameraPerspectiveAmount, 0.0));
transform.localPosition.x = xPos * range;
}
function SetVanishingPoint (cam : Camera, perspectiveOffset : Vector2) {
var m = cam.projectionMatrix;
var w = 2*cam.nearClipPlane/m.m00;
var h = 2*cam.nearClipPlane/m.m11;
var left = -w/2 - perspectiveOffset.x;
var right = left+w;
var bottom = -h/2 - perspectiveOffset.y;
var top = bottom+h;
cam.projectionMatrix = PerspectiveOffCenter(left, right, bottom, top, cam.nearClipPlane, cam.farClipPlane);
}
static function PerspectiveOffCenter (
left : float, right : float,
bottom : float, top : float,
near : float, far : float ) : Matrix4x4
{
var x = (2.0 * near) / (right - left);
var y = (2.0 * near) / (top - bottom);
var a = (right + left) / (right - left);
var b = (top + bottom) / (top - bottom);
var c = -(far + near) / (far - near);
var d = -(2.0 * far * near) / (far - near);
var e = -1.0;
var m : Matrix4x4;
m[0,0] = x; m[0,1] = 0.0; m[0,2] = a; m[0,3] = 0.0;
m[1,0] = 0.0; m[1,1] = y; m[1,2] = b; m[1,3] = 0.0;
m[2,0] = 0.0; m[2,1] = 0.0; m[2,2] = c; m[2,3] = d;
m[3,0] = 0.0; m[3,1] = 0.0; m[3,2] = e; m[3,3] = 0.0;
return m;
}
Thank you for helping out!
It seems other people are looking for the same answer:
http://answers.unity3d.com/questions/54090/changing-cameraprojectionmatrix-doesnt-affect-skyb.html
http://answers.unity3d.com/questions/534838/how-can-i-make-a-skybox-respect-oblique-frustum.html
I modified the skybox shader to deal with this, and posted the code in one of the answers you linked above:
http://answers.unity3d.com/questions/534838/how-can-i-make-a-skybox-respect-oblique-frustum.html
For my purposes I only needed the parameters you've labelled a and b above, but the basic idea can be extended to include the full matrix.
Thank you D$$anonymous$$G,
I've also found that changing the near camera clipping planes to a higher value greatly decreases the shifting effect.
However the modified shader eli$$anonymous$$ates the problem altogether.
Cheers!
Your answer
![](https://koobas.hobune.stream/wayback/20220613122132im_/https://answers.unity.com/themes/thub/images/avi.jpg)