- Home /
DoF effect from Code
Hello.
I'm having a bit of trouble with the DoF effect. I'm trying to add the class as a component to my main camera at run time and set some of its settings.
private DepthOfField mDOF;
...{other code here}...
//Set up the DoF effect for the camera
mDOF = mMyCamera.gameObject.AddComponent<DepthOfField>();
mDOF.aperture = 0.5f;
mDOF.focalSize = 0.0f;
mDOF.focalLength = Vector3.Distance(mMyCamera.transform.position, mPlayer.CurrentPosition);
mDOF.blurSampleCount = DepthOfField.BlurSampleCount.Medium;
mDOF.enabled = true;
the problem is that I keep getting a null reference exception with the following; UnityStandardAssets.ImageEffects.DepthOfField.OnRenderImage
Am I missing something obvious or is it not possible to add the DoF component at run time?
thanks David
Could you please post the exact text of error you are getting?
Did you import the image effects package into your project?
Answer by Naphier · Apr 19, 2016 at 10:33 PM
Apparently it needs shaders assigned to it. To determine what shaders I added the following line to DepthOfField.CheckResources() and I added the DOF effect. (Don't leave this in or you'll get spammed when playing the scene):
Debug.LogFormat("dofHdrShader: {0}\ndx11BokehShader: {1}\ndofHdrMaterial: {2}\ndx11bokehMaterial: {3}", dofHdrShader, dx11BokehShader, dofHdrMaterial, dx11bokehMaterial);
That told me I needed to do this to instantiate this kind of component:
UnityStandardAssets.ImageEffects.DepthOfField dof = Camera.main.gameObject.AddComponent<UnityStandardAssets.ImageEffects.DepthOfField>();
dof.dofHdrShader = Shader.Find("Hidden/Dof/DepthOfFieldHdr");
dof.dx11BokehShader = Shader.Find("Hidden/Dof/DX11Dof");
dof.enabled = true;
And all seems to be just fine with it.
Caveat: If you want this to run in a build you will need to add those two shaders to the Graphic Settings "Always Included Shaders" section. The can be found in the "Standard Assets/Effects/Shaders/_DepthOfField" folder. The DepthOfFieldHdr is apparently names DepthOfFieldScatter.
Thanks, that seems like the correct answer in that it makes the problem go away.
Still, I just wound up adding the DoF effect to the camera object in the inspector, disabling it, then getting a local instance and enabling it at run time.
I am really not a fan of adding components to game objects when I am only going to access them for a few seconds out of several $$anonymous$$utes of play. I'm from the old school of "only create an object when you need to use it, and destroy it when you're done".
But adding at desgin time seems to be the best way to make this work.
thanks for looking into it though. I thought I was completely forgetting how to use AddComponent()
$$anonymous$$
Yeah. If you're looking for easy then just add it in the editor and enable/disable when you don't need it. You could even have multiple disabled cameras to do all sorts of fancy stuff. Lots easier than trying to figure out what the heck Unity folks or doing under the hood.
Your answer
Follow this Question
Related Questions
Post Processing Depth of Field in 2D 0 Answers
Depth of Field Problem 0 Answers
How to make camera position relative to a specific target. 1 Answer
Depth of field not working 0 Answers
Referencing DOF in a script? (Unity 5) 2 Answers