1. The space forums use a separate registration system from the main virtual world. To comment on these forums, you need to create a separate forum account.
    Dismiss Notice
  2. Hey there, we have two Skype groups for space early users - if you're creating content, or need help, feel free to join in either the the beginners or advanced users groups.
    Dismiss Notice

Water refraction bug

Discussion in 'General' started by Xulain, Nov 16, 2017.

Share This Page

  1. Xulain

    By:XulainNov 16, 2017
    New Member

    Joined:
    Oct 8, 2017
    Messages:
    19
    Likes Received:
    1
    When you use refraction in a water shader it effects everything in front of the water as well as behind which can look kinda rubbish. I have been doing some research and have found out if you change the rendering queue order for all objects in front of the water it fixes the problem. The problem with this fix is the sinespace avatar is internal.

    So how would I change the rendering queue order of the sinespace avatar?

    There are other ways to fix this problem but they are a lot more complicated and slightly above my level. I found this doc from nvidia on the subject if anyone can help me implement it (preferably in shader forge as that's what I used to make my water shader) .

    https://developer.nvidia.com/gpugems/GPUGems2/gpugems2_chapter19.html

    I believe suimono uses the masking solution in this doc to fix this problem.
     
    Last edited: Nov 16, 2017
  2. Adam

    By:AdamNov 16, 2017
    Developer
    Staff Member Moderator

    Joined:
    Aug 21, 2014
    Messages:
    630
    Likes Received:
    113
    Hey - avatars actually render in three+ passes already; we render all opaque geometry first, then all alpha-tested (cutout) geometry next, and finally all transparent geometry. You could use the Unity's "Material Pass Override" to change when the water renders; but the problem is avatars do render with other scene geometry to prevent glitches.

    Now, theoretically, I might be able to move avatars slightly - e.g. have avatar geometry render at 2050 instead of 2000, 2500 instead of 2450 and 3050 instead of 3000; which'd let you run it on e.g. 2025 to capture just solid geometry without avatars; but that could be a disruptive change (albeit one we can make relatively easily).

    The problem is the avatars render "in the scene" which means they do need to be rendering roughly the same time as everything else, otherwise you end up with very weird glitches.
     
  3. Xulain

    By:XulainNov 16, 2017
    New Member

    Joined:
    Oct 8, 2017
    Messages:
    19
    Likes Received:
    1
    Maybe the masking way would be a better way then. I looked at the nvidia gems code in that doc and I think that's the answer I'm sure suimono do it that way because I've seen that mask around my avatar at scenes with suimono. The problem for me is it isn't as simple as cut and paste and I don't know how to port it.
     
  4. Adam

    By:AdamNov 16, 2017
    Developer
    Staff Member Moderator

    Joined:
    Aug 21, 2014
    Messages:
    630
    Likes Received:
    113
    That would be stencil buffers - the problem there is that all the avatar shaders need to write to stencils, which means everyone who makes custom shaders for clothing also need to write to the stencil buffer in the same way. Now technically, I guess, you/we might be able to do refraction in post, where we do have a texture containing object IDs (used for our highlight picker to get the glow highlights working), although it might be a bit messy.
     
  5. Xulain

    By:XulainNov 17, 2017
    New Member

    Joined:
    Oct 8, 2017
    Messages:
    19
    Likes Received:
    1
    You kinda lost me there. The nvidia gpugem is not a lot of code the problem is even thou it's shader code a lot of the names in the code are different to the names unity uses. Why does unity do that why couldn't they keep names the same? (it's like someone changing all the words in the English language what's the point? ) . So you have to try work out what the unity name is for everything. Which is hard when you don't even know what all of the the names mean and do. Maybe I'll come back to this later when have been working with shaders a bit longer as it's not at the top of my list of things I want to do in sinespace.
     
  6. Adam

    By:AdamNov 18, 2017
    Developer
    Staff Member Moderator

    Joined:
    Aug 21, 2014
    Messages:
    630
    Likes Received:
    113
  7. Xulain

    By:XulainNov 19, 2017
    New Member

    Joined:
    Oct 8, 2017
    Messages:
    19
    Likes Received:
    1
    I had a look at stencils and it looks over complicated. I am not sure if you looked at the link of the nvidia gpu gem but here is the code
    Code:
    half4 main(float2 bumpUV : TEXCOORD0,
    
      float4 screenPos : TEXCOORD1
    
      uniform sampler2D tex0,
    
      uniform sampler2D tex1,
    
      uniform float4 vScale) : COLOR
    
    
    
    {
    
      // fetch bump texture
    
      half4 bumpTex=2.0 * tex2D(tex0, bumpUV.xy) - 1.0;
    
    
    
      // compute projected texture coordinates
    
      half2 vProj = (screenPos.xy/screenPos.w);
    
    
    
      // fetch refraction map
    
    
    
      half4 vRefrA = tex2D(tex1, vProj.xy + bumpTex.xy * vScale.xy);
    
      half4 vRefrB = tex2D(tex1, vProj.xy);
    
    
    
      return vRefrB * vRefrA.w + vRefrA * (1 - vRefrA.w);
    
    }
    Now that doesn't look as complicated as what you are saying the only problem is porting it to unity for a novice like me because terms and syntax will be different although even that looks similar. Is this code using a stencil buffer?.
     
    Last edited: Nov 19, 2017
  8. Adam

    By:AdamNov 19, 2017
    Developer
    Staff Member Moderator

    Joined:
    Aug 21, 2014
    Messages:
    630
    Likes Received:
    113
    That wont be using the stencil buffer - but will be subject to the same issue (stuff 'refracting' that shouldn't.) - they key to fixing the issue will be either tweaking draw order (possibly messy), sampling our object ID map (possibly also messy), or using stencils (also possibly messy.) - admittedly not great choices.
     
  9. Xulain

    By:XulainNov 21, 2017
    New Member

    Joined:
    Oct 8, 2017
    Messages:
    19
    Likes Received:
    1
    I actually have managed to find a node setup example that is similar to the nvidia gpu gem code basically simulating refraction and using a mask and it works. I do still get a little bit of a refraction bug sometimes but it is 90% better than before. I am also looking at amplify shader as that has features built in for stencils.