• Postprocess

Pinhole depth of field parameters

By: SKY ENGINE AI
scroll down ↓to find out morepinhole-depth-of-field-parameters_2_resourcesTutorial

Pinhole Depth of Field Postprocess step parameters

In this case we will present you how to parametrize Pinhole Depth of Field step.

Agenda:

  • PinholeDepthOfFieldPostprocess parameters
  • Focus distance manipulation
  • Focal length manipulation

PinholeDepthOfFieldPostprocess parameters

We can modify the PinholeDepthOfFieldPostprocess step with different parameters:

  • aperture_f - aperture f-number, it's a ratio of focal length to apertures diameter
  • focal_length - focal length in mm
  • focus_distance - distance from the lens to the focus plane in mm. Objects in this distance will be the sharpest
  • specific_heat - numeric value of the specific heat used by the diffusion simulation.
  • alpha_threshold - threshold for calculating blurring contribution in the diffuse
    simulation. Pixels with a circle of confusion above this value would not be additionally blurred.

Scene setup

Let's use custom scene composer to set up the scene.
Scene consists of:

  • 3 shaderballs - locations: (0, 0, 0), (2, 0, 4), (4, 0, 8),
  • camera - location: (2, 1, 12)
  • direction light - direction: (1, -1, -1)
    from skyrenderer.cases.utils import DepthOfFieldSceneComposer
    scene_composer = DepthOfFieldSceneComposer()
    scene_composer.setup_scene()
2025-02-04 12:57:47,829 | skyrenderer.scene.renderer_context |  INFO: Root paths:
- root path: /dli/skyenvironment/skyrenderer/skyrenderer
- assets path: /dli/mount/assets
- config path: /dli/skyenvironment/skyrenderer/skyrenderer/config
- gpu sources path: /dli/skyenvironment/skyrenderer/skyrenderer/optix_sources/sources
- cache path: /dli/mount/cache
- ptx cache path: compiled_ptx/ptx
- ocio path: ocio_configs/aces_1.2/config.ocio


top_node
|-- shaderball_GEO
|-- camera_target_NUL
+-- camera_origin_NUL

Background distance setup

To be able to use PinholeDepthOfFieldPostprocess we have to define the background distance, to have proper values
in the depth map which is the input for the algorithm of this specific DOF postprocess.
In order to do that, we have to set up the background with desired bg_distance passed to parameter provider.

    from skyrenderer.basic_types.item_component import Background
    from skyrenderer.basic_types.procedure import ConstantColorMiss
    scene_composer.renderer_context.define_env(Background(
        scene_composer.renderer_context,
        ConstantColorMiss(scene_composer.renderer_context),
        parameter_set=ConstantColorMiss.create_parameter_provider(scene_composer.renderer_context, bg_distance=5.0),
    ))

Focus distance manipulation

We will generate two renders using different focus_distance parameter.
First we need to specify pinhole lens parameters.
Two renders will show furthest and closest shaderball as the sharpest.

    from skyrenderer.render_chain.camera_steps.Lens.pinhole_lens import PinholeLens
    from skyrenderer.render_chain.postprocess_steps.pinhole_depth_of_field import PinholeDepthOfFieldPostprocess
    from skyrenderer.render_chain import RenderChain, VisibleLightRenderStep
    width = height = 800
    scene_composer.renderer_context.antialiasing_level = 128
    lens = PinholeLens(
        scene_composer.renderer_context,
        PinholeLens.create_parameter_provider(
            scene_composer.renderer_context,
            fx=width,
            fy=height,
        ),
    )
    visible_light_step = VisibleLightRenderStep(
        scene_composer.renderer_context,
        lens=lens,
        origin_name="camera_origin_NUL",
        target_name="camera_target_NUL",
    )

Create first render - focus on the furthest shaderball

    distance_to_furthest = 12201
    dof_params = PinholeDepthOfFieldPostprocess.create_parameter_provider(
        scene_composer.renderer_context, focus_distance=distance_to_furthest, aperture_f=5, focal_length=20
    )
    pinhole_dof_step = PinholeDepthOfFieldPostprocess(scene_composer.renderer_context, parameter_provider=dof_params)
    scene_composer.renderer_context.define_render_chain(
        RenderChain(
            render_steps=[visible_light_step, pinhole_dof_step],
            width=800,
            height=800,
        ),
    )
    furthest_render = scene_composer.get_render()
2025-02-04 12:57:48,288 | skyrenderer.utils.time_measurement |  INFO: Setup time: 393 ms

2025-02-04 12:57:51,207 | skyrenderer.utils.time_measurement |  INFO: Context update time: 2.92 seconds

2025-02-04 12:57:52,706 | skyrenderer.utils.time_measurement |  INFO: Key points calculation time: 0 ms

2025-02-04 12:57:52,707 | skyrenderer.utils.time_measurement |  INFO: Render time: 1.50 seconds

Create second render - focus on the closest shaderball

    distance_to_closest = 4583
    dof_params = PinholeDepthOfFieldPostprocess.create_parameter_provider(
        scene_composer.renderer_context, focus_distance=distance_to_closest, aperture_f=5, focal_length=20
    )
    pinhole_dof_step = PinholeDepthOfFieldPostprocess(scene_composer.renderer_context, parameter_provider=dof_params)
    scene_composer.renderer_context.define_render_chain(
        RenderChain(
            render_steps=[visible_light_step, pinhole_dof_step],
            width=800,
            height=800,
        ),
    )
    closest_render = scene_composer.get_render()
    render_dict = {"Focus on furthest": furthest_render, "Focus on closest": closest_render}
    scene_composer.visualize_grid_desc(render_dict, shape=(800, 1600), n_cols=2)
pinhole-depth-of-field-parameters_1_resourcesTutorial
2025-02-04 12:57:52,792 | skyrenderer.utils.time_measurement |  INFO: Setup time: 59 ms

2025-02-04 12:57:55,717 | skyrenderer.utils.time_measurement |  INFO: Context update time: 2.92 seconds

2025-02-04 12:57:56,285 | skyrenderer.utils.time_measurement |  INFO: Key points calculation time: 0 ms

2025-02-04 12:57:56,286 | skyrenderer.utils.time_measurement |  INFO: Render time: 569 ms

Focal length manipulation

The next two renders will show the effect of manipulation of focal length parameter.
On the second render the objects will be more blurred.

Create first render - the furthest shaderball will be just slightly blurred

    dof_params = PinholeDepthOfFieldPostprocess.create_parameter_provider(
        scene_composer.renderer_context, focus_distance=distance_to_closest, aperture_f=5, focal_length=10
    )
    pinhole_dof_step = PinholeDepthOfFieldPostprocess(scene_composer.renderer_context, parameter_provider=dof_params)
    scene_composer.renderer_context.define_render_chain(
        RenderChain(
            render_steps=[visible_light_step, pinhole_dof_step],
            width=800,
            height=800,
        ),
    )
    furthest_render = scene_composer.get_render()
2025-02-04 12:57:56,512 | skyrenderer.utils.time_measurement |  INFO: Setup time: 53 ms

2025-02-04 12:57:59,455 | skyrenderer.utils.time_measurement |  INFO: Context update time: 2.94 seconds

2025-02-04 12:58:00,898 | skyrenderer.utils.time_measurement |  INFO: Key points calculation time: 0 ms

2025-02-04 12:58:00,899 | skyrenderer.utils.time_measurement |  INFO: Render time: 1.44 seconds

Create second render - focus on the closest shaderball

    dof_params = PinholeDepthOfFieldPostprocess.create_parameter_provider(
        scene_composer.renderer_context, focus_distance=distance_to_closest, aperture_f=5, focal_length=30
    )
    pinhole_dof_step = PinholeDepthOfFieldPostprocess(scene_composer.renderer_context, parameter_provider=dof_params)
    scene_composer.renderer_context.define_render_chain(
        RenderChain(
            render_steps=[visible_light_step, pinhole_dof_step],
            width=800,
            height=800,
        ),
    )
    closest_render = scene_composer.get_render()
    render_dict = {"Focal length: 10": furthest_render, "Focal length: 30": closest_render}
    scene_composer.visualize_grid_desc(render_dict, shape=(800, 1600), n_cols=2)
pinhole-depth-of-field-parameters_2_resourcesTutorial
2025-02-04 12:58:00,966 | skyrenderer.utils.time_measurement |  INFO: Setup time: 52 ms

2025-02-04 12:58:03,926 | skyrenderer.utils.time_measurement |  INFO: Context update time: 2.96 seconds

2025-02-04 12:58:04,521 | skyrenderer.utils.time_measurement |  INFO: Key points calculation time: 0 ms

2025-02-04 12:58:04,522 | skyrenderer.utils.time_measurement |  INFO: Render time: 595 ms

Summary

In this section you have learnt:

  • You can set focus on an object or objects you choose using focus_distance parameter.
  • You can modify the blurriness of objects closer or further from focus_distance using focal_length parameter.