Search Unity

Custom shaders not working in editor and build when switching the platform to mobile

Discussion in 'Shaders' started by IlisanVlad, Feb 7, 2020.

  1. IlisanVlad

    IlisanVlad

    Joined:
    Dec 3, 2017
    Posts:
    30
    After a few days of searching I finally found the tools I need for painting in game on 3D models.

    The problem is that when I change the platform in build settings to either Android or IOS it doesn't work as expected.

    You can see the comparisons in this videos.

    In the first one the build platform is set to PC, Mac & Linux and the painting works fine.
    You can watch this video to see it working.



    And in this one the platform is set to Android and the painting doesn't work anymore. You can just see the cursor as a red sphere instead of circles wrapping around the mesh.


    I tried fixing this but I have almost no idea how to code shaders and I didn't found a solution online.

    You can download the project here https://www.dropbox.com/s/gtuij4qcm1qmcl8/TexturePaint-master.rar?dl=0

    You can find the original post about this tool and more details here https://medium.com/@shahriyarshahrabi/mesh-texture-painting-in-unity-using-shaders-8eb7fc31221c

    Thank you!
     
    Last edited: Feb 9, 2020
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    I'd ask the original author, Shahriar, about it on Twitter. He's on this forum too, but I can't remember his nickname here.
     
  3. IlisanVlad

    IlisanVlad

    Joined:
    Dec 3, 2017
    Posts:
    30
    Thank you for your suggestion. I will ask him and if I find the solution I will post the answer here.
     
  4. Sh-Shahrabi

    Sh-Shahrabi

    Joined:
    Sep 28, 2018
    Posts:
    56
    Someone else tried converting it to Andriod, after the changes mentioned here, he never got back to me, so I assume it worked for him. Here is the discussion on twitter: https://twitter.com/gifgroen/status/1199352325443346438?s=20

    So baisicly he fixed it by enabling Shadow Cascades on andriod. You can change the lines Line 229 in TexturePaint.cs, the command buffer is attached to the AfterDepthTexture event. This is only called if the depth texture is actually rendered, which is called if shadows are rendered. Try changing line 229 and 234 to a different event. There is actually no reason why this needs to be specifically afterDepthTexture, any event would do, worst case scenario your calculations will be one frame behind, try changing that and if everything works, either do a pull request or let me know I will change it to a more sensible event later when I am on the PC.
    https://github.com/IRCSS/TexturePaint/blob/master/Assets/Scripts/TexturePaint.cs
     
  5. IlisanVlad

    IlisanVlad

    Joined:
    Dec 3, 2017
    Posts:
    30
    Hello Shahrabi -

    I've tried what you have suggested. I've enabled the Shadow Cascades which solved the whole problem in the editor...but not in the build. Here is what it looks like in the build


    After that, I've tried changing the CameraEvent. from the lines 229 & 234 to some random events that Visual Studio recommanded me but it didn't solve the problem. Most of them broke the painting and it stopped working even in the editor, only CameraEvent.AfterEverything sort of worked, but it did nothing. It was acting the same way as AfterDepthTexture.

    Sorry for taking your time :)
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    Sounds like you're missing a script on the camera to enable the depth texture, which is the main thing shadow cascades forces on. Most android platforms will ignore the shadow cascade setting, so you won't get the depth texture.
     
    Sh-Shahrabi likes this.
  7. IlisanVlad

    IlisanVlad

    Joined:
    Dec 3, 2017
    Posts:
    30
    By setting Camera.depthTextureMode = DepthTextureMode.Depth; removes the need to enable Shadow Cascades but it still has the problem I have posted above(the glitchy thing). Do you know how to solve that?
     
  8. Prolantis

    Prolantis

    Joined:
    Jan 17, 2016
    Posts:
    5
    Uhm, I ended up more or less rewriting this in the Universal Render Pipeline since I wanted learn how that (and shader graph) worked. But graph has not been that much of a succes, so I wrote the shader in HLSL as the URP expects it all. So all that is left of the original project by Sharirar is the (slightly modified) shaders.

    If there are any questions, feel free to ask :)

    Edit: Me is the guy from the Twitterverse, far anybody questioning the random answers here :D
     
    Last edited: Feb 11, 2020
  9. IlisanVlad

    IlisanVlad

    Joined:
    Dec 3, 2017
    Posts:
    30
    First of all thank you for taking you time and answering to this thread :)

    Initially I was using URP as well but had to remove it because the Shahriar's shaders were not compatible with it.
    I am glad it is possible to do it for URP but I don't know how. My knowledge for HLSL and shader graph is almost 0.

    Are there any good sources to guide a shader noob through the process of making this tool? If not will you be able make a guide or if I am not pushing too much, can you post the projects or scripts?

    Thank you!
     
    Prolantis likes this.
  10. Prolantis

    Prolantis

    Joined:
    Jan 17, 2016
    Posts:
    5
    I am willing to contribute here. I am going to think about how, because this does require some knowledge on shader(s) and how Unity renders. But I think we can somehow clarify how you would tackle the challenge of shader to Render pipeline and the CommandBuffer usage with Render Pipeline. :)

    What's your background in coding and more specifically how CommandBuffers and Camera(s) work in Unity?

    Edit: regarding the glitching you have you; there is a line in the Paint shader somewhere that does OneMinus on uvRemapped.y. I believe wrapping that in this if statement fixes it. You can try this by commenting out this OneMinus line.
    Code (CSharp):
    1. #if UNITY_UV_STARTS_AT_TOP
    2.     uvRemapped.y = 1. - uvRemapped.y;
    3. #endif
     
  11. IlisanVlad

    IlisanVlad

    Joined:
    Dec 3, 2017
    Posts:
    30
    I tried to wrap that line of code and it worked !! :) so many thanks! I have tested it on Android and I am exporting the project right now to test it on IOS too. What I have noticed so far is that it has really bad performance. My game has a lot of vertices, around 20k on avearge, on the screen. While painting it drops to 10-20 fps and I think even when I am not painting the fps suffers.

    Hopefully the one I will do with you will work better.
     
    Last edited: Feb 20, 2020
  12. IlisanVlad

    IlisanVlad

    Joined:
    Dec 3, 2017
    Posts:
    30
    Hi @Prolantis -

    I finished testing the IOS build and sadly it doesn't work. It works in the editor but not in the build. It has a similar effect to the initial problem this thread is about.
    It doesn't paint, the cursor sphere is just a white circle, the material starts as a black color instead of white and sometimes I can see it trying to paint...just a red(the default color) circle appears over the mesh where I touched to paint.
     
    Last edited: Feb 11, 2020
  13. IlisanVlad

    IlisanVlad

    Joined:
    Dec 3, 2017
    Posts:
    30
    Sorry because I keep pushing @Prolantis . I am sure that you are busy but I still have that iOS problem using the Metal APIs. I managed to simplify the scripts, solve other problems I had but I don't know how to solve this. The material turns black when I start painting and sometimes the paint stays on the object but disappears after a few seconds. I was hoping if you knew an easy fix like the one with the if statement you have sent me before. Thank you.
     
  14. suleymanbucuk

    suleymanbucuk

    Joined:
    Mar 13, 2019
    Posts:
    11
    Conversations above saved my life. Thanks a lot all kind souls.

    @Sh-Shahrabi a very big thanks to you especially. Thanks for sharing your knowledge.