Arvtesh / UnityFx.Outline

Screen-space outlines for Unity3d.
MIT License
1.26k stars 90 forks source link

Wrong outline with OpenGLES2 graphic API (Also WebGL 1.0) #49

Closed AGM-GR closed 2 years ago

AGM-GR commented 2 years ago

Setting the Graphic API to OpenGLES2 (tested in Unity 2020.3 and 2021.2 using URP) gets wrong outline/ not outlined objects. It depends on object position in camera space.

imagen

UnityFX.Outline: Current repository version (05f9833)

AGM-GR commented 2 years ago

I found that the problem is in the Blit() function when drawing the full screen triangle with a graphic shader level < 35:

_commandBuffer.DrawMesh(_resources.FullscreenTriangleMesh, Matrix4x4.identity, mat, 0, shaderPass, props);

This Matrix4x4.identity draw the triangle in world space, so it needs a referente to the current camera to use:

_commandBuffer.DrawMesh(_resources.FullscreenTriangleMesh, camera.transform.localToWorldMatrix, mat, 0, shaderPass, props);

So you need a reference to the camera, I don't know if you prefer a parameter in the function or maybe a static/class variable. I could do a pull request with the change you prefer, just let me know.

AGM-GR commented 2 years ago

Also I realised if the camera is orthographic, is needed to scale the fullscreen rectangle to fill the camera orthographic size. And in my last solution the matrix was using the camera transform scale too, so this will do the trick:

Matrix4x4 cameraMatrix = camera.cameraToWorldMatrix;
if (camera.orthographic)
    cameraMatrix = cameraMatrix * Matrix4x4.Scale(Vector3.one * camera.orthographicSize * 2f);
_commandBuffer.DrawMesh(_resources.FullscreenTriangleMesh, cameraMatrix, mat, 0, shaderPass, props);

Now it seems to works nice.

Arvtesh commented 2 years ago

That's weird. I cannot reproduce it on my side using OpenGLES2/WebGL 1.0.

The purpose of Blit is to copy content of one render texture into another using specific shader. It does not use any matrix transforms as it operates in screen space. The actual model/view transform is applied when object is being rendered (see DrawRenderer).

Could you send me a repro project where you are able to reproduce the issue please?

AGM-GR commented 2 years ago

There is a test repo, with Unity 2020.3.16. I know this should be in screen space but with OpenGLES2, if I visualize the FragmentV you can see the triangle in world space and that is so strange to me.

imagen imagen

Test Project: UnityFx.Outline.Bug.zip

Arvtesh commented 2 years ago

There is a bug in shader. The fix is in develop and will be published with the next update. Thanks for pointing this out.