Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Shader works in Editor but not on iPhone

Discussion in 'Shaders' started by IlisanVlad, Mar 12, 2020.

  1. IlisanVlad

    IlisanVlad

    Joined:
    Dec 3, 2017
    Posts:
    30
    I am using the next script for a painting asset found on this website https://medium.com/@shahriyarshahrabi/mesh-texture-painting-in-unity-using-shaders-8eb7fc31221c .

    As you render the faces of your mesh in the uv space of the mesh, you are reconstruction the islands one triangle at the time. On the edges of the island, it can be that due to underestimation the rasterizer doesn’t consider a pixel which is actually in the island. For these pixels, no pixel shader will be executed and you will be left over with a crease. (text and image taken form the website)

    1_PQqglCLcR-bkkA7AGifNog.jpeg

    The basic idea is that every frame, after the paint texture has been updated, I run a shader over the entire texture, and using a filter and a pre baked mask of the uv islands, extend the islands outwards.

    The problem is that it does not work on iPhone. There is no error, no weird coloring, it is like that script does not get applied. I am not sure where the problem is coming from..maybe the for loops or the command buffers used in c#.

    Code (CSharp):
    1. Shader "Unlit/FixIlsandEdges"
    2. {
    3.  
    4.     SubShader
    5.     {
    6.         // =====================================================================================================================
    7.         // TAGS AND SETUP ------------------------------------------
    8.         Tags { "RenderType"="Opaque" }
    9.         LOD 100
    10.  
    11.         Pass
    12.         {
    13.  
    14.          
    15.         // =====================================================================================================================
    16.         // DEFINE AND INCLUDE ----------------------------------
    17.             CGPROGRAM
    18.             #pragma  vertex   vert
    19.             #pragma  fragment frag
    20.          
    21.             #include "UnityCG.cginc"
    22.  
    23.             // =====================================================================================================================
    24.             // DECLERANTIONS ----------------------------------
    25.             struct appdata
    26.             {
    27.                 float4 vertex : POSITION;
    28.                 float2 uv      : TEXCOORD0;
    29.             };
    30.  
    31.             struct v2f
    32.             {
    33.                 float2 uv      : TEXCOORD0;
    34.                 float4 vertex : SV_POSITION;
    35.             };
    36.  
    37.                     sampler2D    _MainTex;
    38.             uniform    float4        _MainTex_TexelSize;
    39.                     sampler2D   _IlsandMap;
    40.          
    41.             // =====================================================================================================================
    42.             // VERTEX FRAGMENT ----------------------------------
    43.             v2f vert (appdata v)
    44.             {
    45.                 v2f o;
    46.                 o.vertex = UnityObjectToClipPos(v.vertex);
    47.                 o.uv     = v.uv;
    48.                 return o;
    49.             }
    50.          
    51.             fixed4 frag (v2f i) : SV_Target
    52.             {
    53.  
    54.             fixed4 col = tex2D(_MainTex, i.uv);
    55.             float map  = tex2D(_IlsandMap,i.uv);
    56.          
    57.             float3 average = col;
    58.  
    59.             if (map.x < 0.2) {                                    // only take an average if it is not in a uv ilsand
    60.                 int n = 0;
    61.                 average = float3(0., 0., 0.);
    62.  
    63.                 for (float x = -1.5; x <= 1.5; x++) {
    64.                     for (float y = -1.5; y <= 1.5; y++) {
    65.  
    66.                         float3 c =  tex2D(_MainTex, i.uv + _MainTex_TexelSize.xy*float2(x, y));
    67.                         float  m =  tex2D(_IlsandMap, i.uv + _MainTex_TexelSize.xy*float2(x, y));
    68.  
    69.                                n += step(0.1, m);
    70.                          average += c * step(0.1, m);
    71.  
    72.                     }
    73.                 }
    74.  
    75.                 average /= n;
    76.             }
    77.      
    78.             col.xyz = average;
    79.      
    80.                 return col;
    81.             }
    82.             ENDCG
    83.         }
    84.     }
    85. }
    I've read somewhere that using tex2D in for loops is a big no no, so I changed that to a tex2Dlod but the same thing happens.

    You can check out the Git project here https://github.com/IRCSS/TexturePaint/blob/master/Assets/Scripts/FixIlsandEdges.shader

    I am completely lost on this one and google isn't very helpful. Thank you for any comments.
     
    Last edited: Mar 14, 2020
  2. BattleAngelAlita

    BattleAngelAlita

    Joined:
    Nov 20, 2016
    Posts:
    400
    Try to remove step. Step is basically shortcut for if(a < b)... and iphones has some strange buggy behaviour with stacked branches.
     
  3. IlisanVlad

    IlisanVlad

    Joined:
    Dec 3, 2017
    Posts:
    30
    Last edited: Mar 16, 2020
  4. IlisanVlad

    IlisanVlad

    Joined:
    Dec 3, 2017
    Posts:
    30
    Anyone? I am still trying to figure this out