Tutorial: Create a Gaussian blur effect

In this tutorial you learn how to apply a Gaussian blur effect and how to apply that effect when a condition is met.

This video shows the result of the tutorial.

../../_images/blur-completed1.gif

This tutorial assumes that you understand the basics of working with Kanzi Studio. The best entry points for getting familiar with Kanzi Studio are:

Assets for the tutorial

The starting point of this tutorial is the <KanziWorkspace>/Tutorials/Blur/Start/Blur.kzproj Kanzi Studio project.

You can find the completed tutorial in the <KanziWorkspace>/Tutorials/Blur/Completed directory.

Create the blur effect

In this section you create the blur effect.

To create the blur effect:

  1. In Kanzi Studio open the <KanziWorkspace>/Tutorials/Blur/Start/Blur.kzproj project.

    The Root Parallel Activity Host contains the CarScreen Activity that shows a 3D car model.

    Tip

    If you cannot see the entire car in the Preview, you can adjust the Preview zoom level in the upper right corner of the Preview.

    ../../_images/adjust-the-preview-zoom-level.png
    ../../_images/start1.png
  2. In the Library press Alt and right-click Effects and select Blur Effect 2D.

    The Blur Effect 2D effect applies a Gaussian blur to the content of a 2D node.

    ../../_images/create-blur-effect-2d.png
  3. From the Library drag the Blur Effect 2D effect to the Prefabs and drop it on the CarScreen prefab.

    This way you set the Effect Prefab property to the Blur Effect 2D.

    The Blur Effect 2D effect by default applies to a 2D node a blur where the radius of the circular area of pixels that blend to each other is 8 pixels.

    ../../_images/drag-blur-effect-to-carscreen.png ../../_images/car-effect-prefab-set.png ../../_images/default-blur-effect-in-preview.png
  4. Control the amount of blur:

    1. In the Prefabs select the CarScreen prefab and in the Properties add the Blur Effect 2D > Blur Radius property.

      You use the Blur Radius property to set the amount of blur in the instance of the Blur Effect 2D effect in the CarScreen prefab.

      The value of the property does not yet affect the amount of blur in the CarScreen prefab because the property is not connected to the instance of the Blur Effect 2D effect in that prefab.

      ../../_images/car-blur-radius-set.png
    2. Click + Add Binding and in the Binding Editor set:

      • Binding Mode to To source

      • Push Target to .Node2D.Effect

        This way you target the instance of the Blur Effect 2D effect in the CarScreen prefab.

      • Property to Blur Radius

      • From the Properties drag the Blur Radius property to the Expression

      Click Save.

      You can now control the amount of blur in the CarScreen prefab by adjusting the value of the Blur Radius property in that prefab.

      ../../_images/to-source-binding-blur-radius.png ../../_images/adjust-blur-radius.gif

Control the blur effect with a state manager

In this section you create a state manager that you use to control the blur effect in the CarScreen prefab. You use the state manager to animate the transitions between the normally rendered and blurred states. In the next sections you create a popup warning and use the state manager to blur the content of the CarScreen prefab when that warning is shown.

To control the blur effect with a state manager:

  1. In the Prefabs select the CarScreen prefab and in the State Tools click Create State Manager.

    Kanzi Studio creates a state manager and assigns it to the CarScreen prefab.

    ../../_images/car-screen-in-prefabs.png ../../_images/create-state-manager.png
  2. Click Create State twice to create two states and name them Default and Blur.

    You use the Default state to define the state of your application when the CarScreen prefab is rendered without applying blur, the Blur state when the prefab is blurred.

    ../../_images/state-manager-states.png
  3. Click <No Controller Property>, select + Create Property Type and set:

    • Name to Blur.BlurScreen

    • Type to Boolean

    In a state manager the value of the property that you select as the Controller Property defines the conditions when each state in a state group is active.

    ../../_images/create-controller-property.png ../../_images/blur-screen-controller-property.png
  4. In the Blur state set the value of the BlurScreen controller property to True.

    ../../_images/set-blur-controller-property.png
  5. In the State Tools select the Default state, in the Properties set the Blur Radius property to 0, and in the State Tools in the Default state click Set to save the value of the Blur Radius property to that state.

    This way you set the Default state to render the CarScreen without applying the blur.

    ../../_images/blur-radius-0-default.png ../../_images/set-default-state.png
  6. In the Properties set the Blur Radius property to 100 and in the State Tools in the Blur state click Set.

    This way you set the Blur state to apply the blur to the CarScreen.

    ../../_images/blur-radius-100.png ../../_images/set-blur-state.png
  7. In the State Tools click Any -> Any, in the State Transition Settings set Duration to 500, and click Save.

    This way you set the length of the transition between the Default and Blur states to 500 milliseconds.

    ../../_images/click-any-any.png ../../_images/blur-state-transition-settings.png
  8. Click Edit State Manager to deactivate the state tools.

    ../../_images/deactivate-state-tools.png
  9. In the Prefabs select the CarScreen prefab and in the Properties remove the Blur Radius property.

    You remove this property because you use a state manager to control the value of this property.

    ../../_images/carscreen-remove-blur-radius.png
  10. In the Properties add and disable the Controller Properties > Blur.BlurScreen property.

    This way you set Kanzi to render the CarScreen prefab without applying the blur. In the next sections you create a popup warning and use the Blur.BlurScreen property to blur the CarScreen when that warning is shown.

    ../../_images/car-screen-in-prefabs.png ../../_images/blurscreen-property-disabled.png

Create a popup warning

In this section you create a popup warning that you show on top of the CarScreen.

To create a popup warning:

  1. In the Activity Browser below the Root Activity Host click add-button, select Create Activity, and name the Activity and its prefab DoorWarning.

    You use this Activity to show a door-open warning on top of the CarScreen Activity.

    ../../_images/create-doorwarning-activity.png ../../_images/name-doorwarning-activity.png
  2. In the Activity Browser in the DoorWarning Activity click activate.

    This way you simulate the activation of the DoorWarning Activity so that you can see its content in the Preview.

    ../../_images/doorwarning-activate.png
  3. In the Prefabs drag the DoorWarningImage prefab to the DoorWarning prefab.

    ../../_images/drag-door-warning-image.gif ../../_images/door-warning-initial-preview.png
  4. In the Prefabs select the DoorWarning prefab, in the Preview select the Node tool node-tool, and in the align tool align click the center square.

    This way you align the DoorWarning Activity to the center of the Root Activity Host.

    ../../_images/doorwarning-in-prefabs.png ../../_images/doorwarning-centered-preview.png
  5. In the Activity Browser right-click the DoorWarning Activity and select Add Transition > Fade Out Fade In.

    Kanzi Studio creates in the Library > State Managers a state manager, which defines the fade-out and fade-in transitions, and sets the DoorWarning Activity to use it.

    The state DoorWarning State Manager > ActivityState state group contains the states that map to the different statuses of Activity nodes. In the Active state the DoorWarning is opaque and in all other states it is fully transparent.

    ../../_images/doorwarning-add-transition.png
  6. In the Library > State Managers select the DoorWarning State Manager > ActivityState state group and in the Properties remove these state transitions:

    • Inactive --> Activating

    • Deactivating --> Inactive

    You keep the state transitions that define the transitions to and from the Active state, where the DoorWarning Activity is opaque.

    ../../_images/activitystate-state-group.png ../../_images/remove-state-transition.png ../../_images/activity-state-transitions.png

Now when you simulate the activation and deactivation of the DoorWarning Activity, the door-open warning takes 500 milliseconds to fade in and fade out.

../../_images/doorwarning-simulate.gif

Control the blur effect and the popup warning

In this section you set the blur effect and the popup warning so that when the warning is shown, the content of the CarScreen behind the warning is blurred.

To control the blur effect and the popup warning:

  1. In the Activity Browser, right-click Root, select Create Activity Property > Bool, and name the property Root.DoorOpen.

    Kanzi Studio creates an Activity Property property type and adds it to the Root node.

    You use this property type to control the activation state of the DoorWarning Activity.

    ../../_images/create-activity-property.png ../../_images/name-activity-property.png ../../_images/dooropen-property-in-root.png
  2. In the Node Tree select the Root node, in the Node Components press Alt and right-click Triggers, select Data Trigger, and name it Door Warning.

    A Data Trigger keeps the actions in that Data Trigger applied for as long as the condition expression in that Data Trigger is met.

    ../../_images/root-in-node-tree.png ../../_images/create-data-trigger.png ../../_images/door-warning-data-trigger.png
  3. In the Door Warning Data Trigger in the Expression click expression-open, select the Properties window, drag the Blur.ActivityProperty.Root.DoorOpen property to the Expression field, and edit the expression to

    {@./Blur.ActivityProperty.Root.DoorOpen} == true
    

    A condition expression in a Data Trigger must evaluate to either True or False.

    Click Save.

    This way you set the Data Trigger to keep an Action that you add to that Data Trigger applied only when the Blur.ActivityProperty.Root.DoorOpen is enabled (Boolean value True). In the next step you add an Apply Activation Action that activates and deactivates the DoorWarning Activity.

    ../../_images/pop-out-data-trigger-expression.png ../../_images/data-trigger-condition-expression.png
  4. In the Node Components press Alt and right-click the Door Warning Data Trigger, select Apply Activation Action, and in the Apply Activation Action set the Activity Activation Path to DoorWarning.

    The path to the Activity that you set in the Activity Activation Path property is relative to the Activity Host node that you set in the Activity Host Path property.

    The Data Trigger and Apply Activation Action keep this Activity active for as long as the condition in the Data Trigger is met.

    ../../_images/create-apply-activation-action.png ../../_images/apply-activation-action-properties.png
  5. In the Preview click restart-preview to restart the Preview.

    You can now use the Blur.ActivityProperty.Root.DoorOpen property in the Root Activity Host to activate and deactivate the DoorWarning Activity.

    ../../_images/dooropen-activate-doorwarning.gif
  6. In the Prefabs select the CarScreen Activity, in the Node Components create a Data Trigger, name it Door Open Blur, and set the Expression to

    {../Blur.ActivityProperty.Root.DoorOpen} == true
    

    Click Apply.

    This way you set the Data Trigger to keep an Action in that Data Trigger applied only when the Blur.ActivityProperty.Root.DoorOpen in the Root Activity Host is enabled.

    ../../_images/car-screen-in-prefabs.png ../../_images/door-open-blur-data-trigger.png
  7. In the Node Components press Alt and right-click the Door Open Blur Data Trigger, select Apply Property Action, and in the Apply Property Action set:

    • Target Property to Controller Properties > Blur.BlurScreen

    • Fixed Value to enabled

    This way you set the Door Open Blur Data Trigger to keep the Blur.BlurScreen property in the CarScreen Activity enabled when the Blur.ActivityProperty.Root.DoorOpen in the Root Activity Host is enabled.

    ../../_images/create-apply-property-action.png ../../_images/apply-property-action-properties.png

Now when you in the Root Activity Host:

  • Disable the Blur.ActivityProperty.Root.DoorOpen property, Kanzi renders the CarScreen Activity without applying the blur and deactivates the DoorWarning Activity.

  • Enable the Blur.ActivityProperty.Root.DoorOpen property, Kanzi applies blur to the CarScreen Activity and activates the DoorWarning Activity.

To simulate this using the Activity Browser, select the Root Activity Host and in the side panel toggle the DoorOpen property.

../../_images/blur-completed1.gif

See also

To learn more about 2D effects, see Effects for 2D nodes.

To learn more about the Activity system, see Activities.

To learn more about using bindings, see Using bindings and Bindings expressions reference.

To learn more about the Kanzi state manager, see State manager.

To learn more about how to apply anti-aliasing, see Applying anti-aliasing.