Search Unity

Hey guys, need some help with camera alpha mask and smoothing it out

Discussion in 'Shaders' started by dlantjs, Feb 19, 2015.

  1. dlantjs

    dlantjs

    Joined:
    Jan 13, 2014
    Posts:
    8
    hello, this is my first post, and I am very very weak in terms of shaderlab and Cg, I've looked into some quiet helpful tutorial on youtube and unity wiki and such but I still dont seem to have an idea how to solve my issue

    http://forum.unity3d.com/threads/camera-mask-alpha-falloff.145313/#post-994707

    my problem is pretty much the same as this post's, from the youtube tutorials I've seen I feel like I am supposed to use Lerp to solve this, but I just cant understand how to blend the main camera view with this secondary camera view which is looking at a texture renderer.

    I've tried contacting the OP for help, but it seems the last time he was seen on this forum was December 2014.

    Any help will be very much appreciated, thank you!
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    The secondary camera view isn't looking at a texture renderer. It's rendering into a render texture instead of the main target. You then have to draw this render texture onto the screen. You might as well just use GUI.DrawTexture for this.
     
  3. dlantjs

    dlantjs

    Joined:
    Jan 13, 2014
    Posts:
    8
    Umm, I heard that GUI draw calls are quiet expensive, (perhaps it doesnt matter here since it is a camera?), but I still dont see how this will make the edges any smoother...
     
  4. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Ah, I though you were unable to display the results of the secondary camera in the primary view.

    Maybe you can post some shader/script code to show what you are doing now.
     
  5. dlantjs

    dlantjs

    Joined:
    Jan 13, 2014
    Posts:
    8
    The shader is the exact same shader im using on the link I put up, and there really isnt a script code that accompanies the shader.
     
  6. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    So, something like:
    Code (csharp):
    1. Shader "TextureMask"
    2. {
    3.   Properties
    4.   {
    5.   _Mask ("Culling Mask", 2D) = "white" {}
    6.   }
    7.   SubShader
    8.   {
    9.   Tags {"Queue" = "Overlay-1"}
    10.   Blend SrcAlpha OneMinusSrcAlpha
    11.   Lighting Off
    12.   ZWrite Off
    13.   ZTest Always
    14.   Pass
    15.   {
    16.   SetTexture [_Mask] {combine texture}
    17.   }
    18.   }
    19. }
     
  7. dlantjs

    dlantjs

    Joined:
    Jan 13, 2014
    Posts:
    8
    well yeah,


    1. Shader "TextureMask"
    2. {
    3. Properties
    4. {
    5. _Mask ("Culling Mask", 2D) = "white" {}
    6. }
    7. SubShader
    8. {
    9. Tags {"Queue" = "Background"}
    10. Blend SrcAlpha OneMinusSrcAlpha
    11. Lighting Off
    12. ZWrite On
    13. ZTest Always
    14. Alphatest LEqual 0
    15. Pass
    16. {
    17. SetTexture [_Mask] {combine texture}
    18. }
    19. }
    20. }
    this to be exact but they are pretty much identical right?
     
  8. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Yes, I know, but what about my version?
     
  9. dlantjs

    dlantjs

    Joined:
    Jan 13, 2014
    Posts:
    8
    uhh your code doesnt actually make the 2nd cam a circle, as the ZWrite needs to be On for it to do that, and on some occasions I get an error stating that Queue = Overlay - 1 is unknown or something
     
  10. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    The original code seems to render the overlay at the start of the frame and uses the z-buffer to mask it. You can't do any blending with this approach.

    My code should render the overlay at the end of the frame. ZWrite is meaningless at that point. Queue = Overlay -1 should work, but you can also try just Queue = Overlay.
     
  11. dlantjs

    dlantjs

    Joined:
    Jan 13, 2014
    Posts:
    8
    hmm thanks, it seems the 'Queue = xx' determines at what point of the frame it works, so at what time can I blend anything? Anything with Queue = Overlay doesnt actually make the circle it seems.
     
  12. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Anything between Transparent and Overlay I'd say. You want this map to be on top of other transparent objects. There are numbers assigned to these Tags, so that's why Overlay-1 is supposed to work. Or Transparent+10.

    How does it look now and how does the mask texture look?
     
  13. dlantjs

    dlantjs

    Joined:
    Jan 13, 2014
    Posts:
    8
    it looks like the camera only becomes a circle when the tag is on background (well I tried overlay, overlay+10, transparent, transparent+10, and then background). I think the camera alpha mask thing only works when the tag is on background.

    Can I then perhaps then do tag = background and then with the circle view port camera renderTexture it to an object and make a camera stare at that object to do blending?
     
  14. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Background simply isn't the right time to do blending if you want to blend it over your scene.

    How does it look now and how does the mask texture look?
     
  15. dlantjs

    dlantjs

    Joined:
    Jan 13, 2014
    Posts:
    8
    Uhh i told you, the camera viewport stays rectangular, when the mask texture is circle here I uploaded the required set up so you can see it for yourself.

    The clipcam has to be set as Orthographic btw, and I've also uploaded the mask im using.
     

    Attached Files:

    v_christf likes this.
  16. lran

    lran

    Joined:
    Feb 20, 2014
    Posts:
    1
    I also have the same problem. can`t find the answer anywhere