- Home /
DoF based on distance to camera's Y position
The existing DoF(depth of field) effect that comes with unity, works based on the distance to the camera's position. I'm working on a 2.5D game, where the camera is slightly rotated, to have a bit more perspective.
Because the camera is rotated, objects that are near the right of the screen, are much further away from the camera than objects that are near the left side of the screen. Because of this, different parts along the X-axis of the level get blurred differently.(left pic) This is not really the desired result.
What I'm trying to do, is that the DoF only looks at an object's Y position, relative to the camera. (right pic) This way, only things that are closer or further away from a certain Y position will get blurred.
I have tried modifying the existing DoF scripts, but I couldn't make heads or tails of it.
Answer by KoningStoma · Mar 30, 2013 at 11:44 AM
I solved it. Not by modifying the shader, but by using a custom projection matrix.
With a bit of trial and error (I'm not that familliar with matrices etc.), I found that fine tuning m20 and m21 would "skew" the depth of the DoF effect along the x and y axis, without noticeably affecting anything else.
This is how:
var originalProjection : Matrix4x4;
var t1 : float;
var t2 : float;
originalProjection = camera.projectionMatrix;
function Start(){
var p : Matrix4x4 = originalProjection;
t1=-.54;
t2=.14;
}
function Update () {
var p : Matrix4x4 = originalProjection;
p.m20 = t1*.01;
p.m21 = t2*.01;
camera.projectionMatrix = p;
}
Your answer

Follow this Question
Related Questions
How to get Unity's DOF effect with custom shaders? 0 Answers
Depth of Field in Unity Free 0 Answers
Unity 4: problem with DOF on iOS 1 Answer