mob-sakai / SoftMaskForUGUI

Enhance Unity UI (uGUI) with advanced soft-masking features to create more visually appealing effects!
https://github.com/mob-sakai/SoftMaskForUGUI
MIT License
1.97k stars 261 forks source link

Softmask VR shows different result each eye resulting in stereoscopic effect #79

Closed maskedmouse closed 4 years ago

maskedmouse commented 4 years ago

Describe the bug When using VR the left eye seems to show a different result of the masking than the right eye. Which is causing a sort of stereoscopic unwanted effect.

To Reproduce Steps to reproduce the behavior:

  1. Open Sample Scene
  2. Build for Android Oculus GO
  3. Observe Left & Right eye. Left eye is displaying the mask differently than the right eye. It is cut off from the left or right depending on the eye.

Environment (please complete the following information):

Additional context Here's a screenshot taken with the Android Logcat package from Unity. Screenshot Oculus GO

With a PC build, it seems that the mask isn't even drawn at the right place but rather stuck on 1 position. Screenshot PC Oculus

Link to the project: https://maskedmous.stackstorage.com/s/cJpOTbzpPvtrHVn

I think it has to do with the SoftMask shader implementation not adjusting for SinglePassStereo rendering. But I'm not a shader programmer so I wouldn't know what to change. https://docs.unity3d.com/Manual/SinglePassStereoRendering.html

mob-sakai commented 4 years ago

@maskedmouse I don't know much about VR and don't have a VR device...

maskedmouse commented 4 years ago

From my understanding is, is that for VR to work in Single Pass Stereo, the shader needs to use the helper methods provided by Unity. From what I've read through the Softmask UGUI shader you get the alpha from the SoftMask.cginc implemented method by passing in a IN.vertex and IN.worldPosition. The IN values are already SinglePassStereo corrected by unity due to the UNITY_VERTEX_INPUT_INSTANCE_ID & UNITY_VERTEX_OUTPUT_STEREO

But when I go through your code of the SoftMask.cginc.. I suspect that values there aren't adapted for SingleStereoPass usage. Thus giving the effect I described in my original post.

I suspect the view variable is in need of adjustment for SinglePassStereo as per described in https://docs.unity3d.com/Manual/SinglePassStereoRendering.html half2 view = clipPos.xy/_ScreenParams.xy; Since clipPos.xy is already adjusted, all that's left is _ScreenParams.xy, but i'm uncertain where it comes from.

I'll try and fiddle around when I have some time to spare. If I have the solution I'll let you know.

mob-sakai commented 4 years ago

_ScreenParams is a built-in shader variable. https://docs.unity3d.com/Manual/SL-UnityShaderVariables.html

mob-sakai commented 4 years ago

The generator of the soft mask buffer does not support stereo. https://github.com/mob-sakai/SoftMaskForUGUI/blob/upm/Scripts/SoftMask.cs#L533-L611

We need to use these APIs to generate soft mask buffers for the right and left eyes.