I’m trying to use the built-in (Pro) DOF to selectively blur objects “in the background” while keeping the foreground crisp. But since the 2 dof filters in Unity will end up blurring everything that has alpha and all my objects (background and foreground) have alpha, I had to recur to two cameras and a foreground and background layers. So far so good.
But since I can’t cause objects to blur or not based on their distance (since I’m doing the fixed layers), I thought the only way around it would be to instead modify the dof focal distance via a script at runtime. That is, when I want to focus on the foreground, I’d grab the background camera (with the dof script attached), and get the DOF script off it and modify it’s focal distance.
I tried this (assume a MonoBehaviour, etc…):
Unity 3.4 DOF
// ...
private DepthOfField34 DOF34;
void Start() {
DOF34 = GameObject.Find("BackgroundCamera").GetComponent();
// ...
}
void Update() {
// ...
if (shouldFocusOnForeground) {
if (DOF34.focalZDistance > 1.0f) {
DOF34.focalZDistance -= 10f;
}
}
else {
if (DOF34.focalZDistance < 1000.0f) {
DOF34.focalZDistance += 10f;
}
}
}
But the script property “Focal Distance” doesn’t move at all.
Then I tried the other DOF:
Unity 4 DOF Scatter / DX11
// ...
private DepthOfFieldScatter DOFx;
void Start() {
DOFx = GameObject.Find("BackgroundCamera").GetComponent();
// ...
}
void Update() {
// ...
if (shouldFocusOnForeground) {
if (DOFx.focalLength > 1.0f) {
DOFx.focalLength -= 10f;
}
}
else {
if (DOFx.focalLength < 1000.0f) {
DOFx.focalLength += 10f;
}
}
}
With this one, instead, only one time does 10 get subtracted or added to the focalLength. It just looks like a bug to me. But hopefully I’m just doing something wrong. Any ideas? (btw, I also tried in LateUpdate())