Page 1 of 1

Local Vertex Offset different behaviour scene/game [SOLVED]

PostPosted: Mon Feb 05, 2018 2:12 pm
by KennyGuillaume
Hi!

Our team is using Amplify Shader Editor in Unity to make the shaders for the game we're currently working on. Our first experience with Amplify has been great, except for this small issue we're now facing.

The setup
The idea is that we want to create a slight deformation to our meshes with the shader (in the examples the effect is exaggerated). We're doing this by feeding Simplex2D noise to the Local Vertex Offset.
Here the setup we have right now:
Image

The problem
Problem is that this is effect is not consistent in the game view compared to the scene view. The goal is to do everything in local space, so rotating, positioning,... shouldn't have any influence on the effect. In the scene view it actually does it like expected.
In game view however, the mesh, with two materials(which have the exact same shader) are not aligned anymore. Also, when rotating or moving the mesh, the Simplex2D noise, doesn't seem to work in local/object space anymore. It looks more like a projection.
Image
Every mesh has 2 materials - one main material and one for the rim. Here you can see that in game view the deformation is not aligned anymore.

I tried switching between forward and deferred rendering. But that didn't seem to have any effect. Am I missing something? Am I using a wrong setup. Or do you guys have any suggestions? I'd like to hear.

Have a great day!
Kenny

Re: Local Vertex Offset different behaviour scene/game

PostPosted: Mon Feb 05, 2018 4:03 pm
by Amplify_Borba
Hello Kenny, thank you for getting in touch and for your support!

Regarding the issue you're having, since you're using the Vertex Position data for generating noise and the same material for two objects, its values are inconsistent from the scene to the game view due to Unity's Dynamic Batching, which causes the local values of the vertex position to be altered, resulting in the difference you're observing.

You may test it by disabling Batching through adding the Custom SubShader Tag as per the image below.

DisableBatching.jpg
DisableBatching.jpg (13.3 KiB) Viewed 6355 times


We wouldn't recommend disabling dynamic batching on these objects, as it could drastically increase draw calls, the better solution would be to calculate the noise through other means that do not rely on vertex data.

Please let me know if you have any further questions, I'll be glad to assist!

Re: Local Vertex Offset different behaviour scene/game

PostPosted: Tue Feb 06, 2018 9:40 am
by KennyGuillaume
Thank you for your reply. This was indeed the problem. We decided on using the 2nd UV channel for mapping the noise. This seems to work fine.

Thanks again.

Re: Local Vertex Offset different behaviour scene/game

PostPosted: Tue Feb 06, 2018 10:11 am
by Amplify_Borba
Glad to know that you've found a viable workaround, thank you for taking the time to share it with the community!

Have you had the chance to rate and review Amplify Shader Editor? It would be awesome if you could share your experience with the Unity community, the Unity Asset Store thrives on user interaction and direct feedback.Every bit helps, your feedback is extremely valuable to us!

Feel free to get back in touch if you have further issues or questions, thanks!

Re: Local Vertex Offset different behaviour scene/game

PostPosted: Wed Feb 07, 2018 9:01 am
by KennyGuillaume
Sad to say this problem isn't resolved. As a test I just plugged in a constant value to the Local Vertex Offset:

Image

Now, when in game view, I start rotating one of the boards, some vertices start snapping to another offset:

Image

It's definitely an issue with batching, because when I turn it off in the shader, the issue is not present.
Can't I use the local vertex offset at all when using Unity's batching?

Thanks!

Re: Local Vertex Offset different behaviour scene/game

PostPosted: Wed Feb 07, 2018 11:08 am
by Amplify_Borba
KennyGuillaume wrote:Sad to say this problem isn't resolved. As a test I just plugged in a constant value to the Local Vertex Offset:

Image

Now, when in game view, I start rotating one of the boards, some vertices start snapping to another offset:

Image

It's definitely an issue with batching, because when I turn it off in the shader, the issue is not present.
Can't I use the local vertex offset at all when using Unity's batching?

Thanks!


We're unable to replicate this on our side, could you provide us with a simple sample with the issue present, as it would greatly speed up our resolve in debugging this?

Looking forward to your reply, thanks!

Re: Local Vertex Offset different behaviour scene/game

PostPosted: Wed Feb 07, 2018 11:47 am
by KennyGuillaume
Made a unitypackage demonstrating the problem in a sample scene:
https://drive.google.com/open?id=19yxyj ... GexL1GP8l5

Problem is very clear if you start moving the camera. (in game view of course)

Hope this helps.

Re: Local Vertex Offset different behaviour scene/game

PostPosted: Wed Feb 07, 2018 4:23 pm
by Amplify_Borba
Thank you for taking the time to provide us with a sample, it was extremely helpful!

It's really tricky to be manipulating vertex position on shaders whose game objects will later be dynamically batched. As we previously said, local vertex positions will be altered to all objects contained on a batch as they will have a new overall pivot point, so any kind of operations done over them will result in different results than expected.

There's a good workaround which is to use GPU Instancing. This is a more performant way on reducing drawcalls which doesn't suffer from the issues you are currently having. Unity always prioritizes instancing over Dynamic Batching so, if it can instance a Mesh, it will disable dynamic batching on that Mesh.

We noticed, via your sample that you are already using instanced variables on the shader graph but your materials have the Enable GPU Instancing turned off. For you to correctly use GPU Instancing you'll just need to toggle that option on.

One quick note, on our previous post we have said that for you to disable Dynamic Batching, you would need to add the DisableBatching Custom SubShader Tag and set a value to true.
There is actually an easier way on achieving this, which is by going to the Output node Rendering Options and setting the Disable Batching option to true. This will internally to the same as what was suggested.

DisableBatching2.jpg
DisableBatching2.jpg (57.87 KiB) Viewed 6305 times


Please let me know if this helps, thanks!

Re: Local Vertex Offset different behaviour scene/game

PostPosted: Thu Feb 08, 2018 12:17 pm
by KennyGuillaume
Amplify_Borba wrote:Thank you for taking the time to provide us with a sample, it was extremely helpful!

It's really tricky to be manipulating vertex position on shaders whose game objects will later be dynamically batched. As we previously said, local vertex positions will be altered to all objects contained on a batch as they will have a new overall pivot point, so any kind of operations done over them will result in different results than expected.

There's a good workaround which is to use GPU Instancing. This is a more performant way on reducing drawcalls which doesn't suffer from the issues you are currently having. Unity always prioritizes instancing over Dynamic Batching so, if it can instance a Mesh, it will disable dynamic batching on that Mesh.

We noticed, via your sample that you are already using instanced variables on the shader graph but your materials have the Enable GPU Instancing turned off. For you to correctly use GPU Instancing you'll just need to toggle that option on.

One quick note, on our previous post we have said that for you to disable Dynamic Batching, you would need to add the DisableBatching Custom SubShader Tag and set a value to true.
There is actually an easier way on achieving this, which is by going to the Output node Rendering Options and setting the Disable Batching option to true. This will internally to the same as what was suggested.

DisableBatching2.jpg


Please let me know if this helps, thanks!


Enabling GPU instancing does indeed fix the problem. Thanks for the support!

Re: Local Vertex Offset different behaviour scene/game [SOLV

PostPosted: Thu Feb 08, 2018 12:32 pm
by Amplify_Borba
No problem, always happy to help, don't hesitate to get back in touch if you come across any further issues, feedback is also welcome!