Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Riderfan · Apr 19, 2016 at 09:50 PM · cameradepth of field

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image incorrect · Apr 19, 2016 at 10:17 PM 0
Share

Could you please post the exact text of error you are getting?

avatar image miahmiah · Apr 19, 2016 at 11:28 PM 0
Share

Did you import the image effects package into your project?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Riderfan · Apr 19, 2016 at 10:53 PM 0
Share

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$$

avatar image Naphier Riderfan · Apr 19, 2016 at 11:03 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

52 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges