How to set autofocus in DepthOfField in Default Rendering Pipeline.
For Example, Once I go near to any objects in the scene, its focus needs to be changed automatically and make the object clear. Is It Possible?
Did you try to walk through this example? - https://playground.babylonjs.com/#8F5HYV#9
2 Likes
Hey this is 100% possible. In addition to following the guide above I’d recommend adding a function to scene.onAfterRenderObservable() to get the updated distance value after every frame.
In my case below, I have a mesh I wish to target and I use the built it meshDistanceToCamera function to calculate the distance. It’s calculated in meters and babylon’s default render pipeline DOF expects millimeters, so we have to multiply it by 1000.
I hope this helps!
-
note SceneManager.Meshes is just a dictionary of meshes.
const DOFFactor = 1000; defaultPipeline.depthOfFieldEnabled = true; // false by default if ( defaultPipeline.depthOfFieldEnabled && defaultPipeline.depthOfField.isSupported ) { defaultPipeline.depthOfFieldBlurLevel = 0; defaultPipeline.depthOfField.fStop = 1.4; defaultPipeline.depthOfField.focalLength = 35; scene.onAfterRenderObservable.add(() => { const topLevelMsh = SceneManager.Meshes[CONSTANTS.MYMESH] .loadedMeshes[0]; let distancefromCamera = DOFFactor * topLevelMsh.getDistanceToCamera(scene.activeCamera); if (defaultPipeline) { defaultPipeline.depthOfField.focusDistance = distancefromCamera; } }); defaultPipeline.depthOfField.lensSize = 100; }
4 Likes