Search Unity

In shader, modify texture for use in another shader

Discussion in 'Shaders' started by sewy, Aug 1, 2019.

  1. sewy

    sewy

    Joined:
    Oct 11, 2015
    Posts:
    150
    Hello,
    I am up to an effect with water like this:

    For that, i need to get a mask like this one:


    but without usage of second camera (water plane is not cheep (flat shaded and modular) so I dont want to render it twice).

    I am thinking of this solution:
    1. Create black texture with screen height/width and pass it to the water shader.
    2. When redering the water, for each pixel of the water plane in the screen space, modify texture pixel (and every pixel below) with white color.

    Is it a good approach? Is there other, easier (cheaper)?

    Is it possible to modify texture in shader? - I dont need to save the texture to CPU, because i would use it in different shader for the underwater as a mask.

    Thanks
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    So, there's a magic trick in that above image.

    The magic trick is they're just clipping through the water plane with the camera's near clip, then hiding the edge with a black strip, and nothing else special.
    upload_2019-8-1_15-41-10.png

    Note the black area is as wide as the maximum variation in the ripple height. All they're doing is placing a black quad at the near plane with a wavy patterned shader on it, likely rippled by some noise function or just another texture.
     
  3. sewy

    sewy

    Joined:
    Oct 11, 2015
    Posts:
    150
    That is realy good catch - how do you know?

    As i've implemented it, it works for some water planes (depending on the wave function), but what if we have different waters with different waves? -> Leading back to the original Q.

    Is there a way to get the mask from within the water shader pass - eg. can I inject data from transparent shader into some texture?
     
    Last edited: Aug 8, 2019
  4. sewy

    sewy

    Joined:
    Oct 11, 2015
    Posts:
    150
    RWStructuredBuffer was the solution for me, didnt have success with RWTexture2D.