Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to blur specific layers only?

Discussion in '2D' started by MadPropz101, Sep 14, 2018.

  1. MadPropz101

    MadPropz101

    Joined:
    Aug 29, 2018
    Posts:
    13
    I'm trying to figure out how to blur only the background and foreground layers, so that only my main layer is in focus. I used the post processing addon from the asset store and applies DoF through different cameras that all see different layers, but the problem with this is that the cameras in front still affect the stuff that's in the back.
    It doesn't have to be done with DoF, i just need a way to blur only the layers that i want during runtime, and at different blur intensities. I know it can be done by already preparing the sprites to look blurry but that's not very flexible, especially for my game where i use a lot of moving parts and Spine animations.
     
  2. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    You go to your project, click create, select post processing profile. It will appear in your assets (Project). You then drag and drop it in the "profile" slot on your camera. It'll be in the inspector at the bottom. Profile with an empty field. Drop it in there.

    Then you have the options, many options, to do everything from fog, blur, etc... I'm not sure if you have to download this asset or if it comes stock. However, Unity free standard assets should have it if not. I honestly don't know.
     
  3. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
  4. MadPropz101

    MadPropz101

    Joined:
    Aug 29, 2018
    Posts:
    13
    Yeah that's exactly what i did but as i said the problem is that the cameras in the front add the blur even to the layers which they are not set up to see. This is the video i used:


    This is the effect i want to achieve, as you can see the squirrel is sharp but the stuff in the foreground and background is blurred:
     
  5. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Go to Edit>>Build Settings>>Project Settings>>Physics

    This should bring up the layer matrix in the inspector window. You can UNCHECK the stuff you don't want the other cameras to see by the layer name. maybe this will help you?
     
  6. MadPropz101

    MadPropz101

    Joined:
    Aug 29, 2018
    Posts:
    13
    I just tried messing around with the layer matrix, but if i understand correctly this is only used for collisions?
    The way my game is set up is that i have multiple cameras all following the player, and each camera's culling mask is set to show specific layers.
    I thought that when i added depth of field/blur to a camera that i would only apply the effect to the layers set in the culling mask, but for some reason the effect also applies to all the objects/layers in the scene that are behind, even though they are shown through completely different cameras.
     
  7. roykoma

    roykoma

    Joined:
    Dec 9, 2016
    Posts:
    176
    You could create two Cameras.

    • First create a new Layer "Focus" (or whatever you like)
    • Set one Camera (your "Focus" camera) to "Depth only" for Clear Flags and its Culling mask to "Focus" only.
    • Set the other Cameras (your "Blur" camera) culling Mask to everything except "Focus"
    • Child the other Camera to you "Focus" camera and look out that the values are equal, so that they always see the same.
    • Create a new Post processing Volume with "Depth of Field" and set its values to whatever you like. Set this object on a Layer "PostProcessing" (or whatever you like)
    • Add a Post Processing Layer with the Layer set to "PostProcessing" to the seconds (blur) camera
    • enjoy
    Result and Images of the editor (so you can see how it is set up):
    https://imgur.com/a/umbnxaP

     
    Last edited: Sep 14, 2018
  8. MadPropz101

    MadPropz101

    Joined:
    Aug 29, 2018
    Posts:
    13
    How did you create a post processing volume and get the DoF settings shown?
    Is this the asset i need to use to have that option?
    https://assetstore.unity.com/packages/3d/post-processing-volume-89782
     
  9. roykoma

    roykoma

    Joined:
    Dec 9, 2016
    Posts:
    176
    Sorry, I forgot they didn't update the asset in the store yet.

    What I used is the Version 2 of the unity post processing stack that can be downloaded via https://github.com/Unity-Technologies/PostProcessing
    You can see the documentation for it here

    Can also be downloaded via the package manager in Unity 2018.
    I only changed the "Max Blur Size" to "Very Large" in this case.

    I can not tell you how to do this with V1 PostProcessing as I never used it.
     
  10. munkbusiness

    munkbusiness

    Joined:
    Aug 22, 2017
    Posts:
    55
    I have been struggling with this. And I have easily been able to replicate the effect roykoma posted, with the sharp object being closest to the camera, but I cannot do the opposite having only the closest object blurred and the things behind it sharp. Which is also what madpropz101 requested (blurred front, sharp middle, blurred back).
     
    MadPropz101 likes this.
  11. MadPropz101

    MadPropz101

    Joined:
    Aug 29, 2018
    Posts:
    13
    @roykoma showed me a viable solution so i'll repost it here.

    • Put the "SpriteShaders" Folder from https://github.com/traggett/UnitySpriteShaders into your assets
    • Use only one camera
    • Put a Post Process Layer on that Camera and match the Layers again with your PostProcessing Volume
    • Create a new material and select the "Sprite (Unlit)" Shader
    • Check the "Write to Depth" checkbox on the material
    • Put this material on your sprites
    • Add the Depth Of Blur to your PostProcessing Volume and play around with the values till it is good. For me maxing out Focal Length and Aperture and then adjusting the Focus Distance did the trick.

    This method however no longer just works with layers but instead takes the real depth.
     
  12. munkbusiness

    munkbusiness

    Joined:
    Aug 22, 2017
    Posts:
    55
    If it no longer works with layers it might be a problem for me, as I do parallax effects with multiple cameras already.

    EDIT: Actually this also works with layers, at least with blurring the foreground which is all i needed.
     
    Last edited: Sep 19, 2018
  13. MadPropz101

    MadPropz101

    Joined:
    Aug 29, 2018
    Posts:
    13
    Yeah it does work.
     
  14. ThreeRing

    ThreeRing

    Joined:
    Aug 24, 2016
    Posts:
    15
    Thank you for your explanation!
    I can't seem to get it working on my side, though :/
    Is it possible to ask for a video showing the steps?
     
  15. ad48hp

    ad48hp

    Joined:
    Sep 18, 2013
    Posts:
    12
    Didn't got this one, sadly..
    Can you post screenshot of your settings ?
    I tried to use only one camera, but then, how exactly is the shader supposed to prevent the DoF from being used on the object, that shares the material the shader is applied to ?
     
    ThreeRing likes this.
  16. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    972
    I guess you could put the red box duplicate on the "PostProcessing" layer, but with the shadows only option. So you would get a blurry shadow on the white plane.
    Is there a way to setup it the other way round, i.e. get the red box blurred and sharp background environment?