Search Unity

how can i make this shader work on mobile(Android)?

Discussion in 'Shaders' started by Fog0, Mar 27, 2015.

  1. Fog0

    Fog0

    Joined:
    Mar 27, 2015
    Posts:
    3
    I believe that the problem in this frag(line 82),I tried several times , but without result.

    The goal is that the effect on mobile becomes similar to the PC(this shader works on PC).

    It is the shader "Toony -Basic" with splatmap , needs no light at all .

    If you try use it on android, it will be pink.

    phone model: Huawei Ascend G610.

    I made this changers on "Toony -Basic":

    -Add some Properties, Lines 7-14

    -Add iniform sampler2D, Lines 47-48

    -Add struct Input, line 73-80

    -Change "float4 frag (v2f i) : COLOR" to "float4 frag (v2f i,Input IN) : COLOR, line 82", add Input IN

    -Add splat efect, lines 87 - 91

    -change "return float4(2.0f cube.rgb col.rgb, col.a);" to "return float4(2.0f cube.rgb col2.rgb col.rgb, col2.a col.a);", just by add col2, line 91.

    Code (CSharp):
    1. Shader "Custom/myToonLightfake"
    2. {
    3.     Properties
    4.     {
    5.  
    6.         // Control Texture ("Splat Map")
    7.          _Control ("Control (RGBA)", 2D) = "red" {}
    8.      
    9.         // Terrain textures - each weighted according to the corresponding colour
    10.         // channel in the control texture
    11.          _Splat3 ("Layer 3 (A)", 2D) = "black" {}
    12.          _Splat2 ("Layer 2 (B)", 2D) = "gray" {}
    13.          _Splat1 ("Layer 1 (G)", 2D) = "gray" {}
    14.          _Splat0 ("Layer 0 (R)", 2D) = "gray" {}
    15.      
    16.         // Used in fallback on old cards & also for distant base map
    17.          _MainTex ("BaseMap (RGB)", 2D) = "white" {}
    18.          _Color ("Main Color", Color) = (1,1,1,1)
    19.      
    20.         // Colour of toon outline
    21.         _OutlineColor ("Outline Color", Color) = (0,0,0,1)
    22.         _Outline ("Outline width", Range (.002, 0.05)) = .05
    23.      
    24.         _ToonShade ("ToonShader Cubemap(RGB)", CUBE) = "" { Texgen CubeNormal }
    25.     }
    26.     SubShader
    27.     {
    28.  
    29.         Tags { "RenderType"="Opaque" }
    30.      
    31.         Pass
    32.         {
    33.             Cull Off
    34.             Name "myToonLightfake"
    35.             CGPROGRAM
    36.             #pragma vertex vert
    37.             #pragma fragment frag
    38.          
    39.             #pragma fragmentoption ARB_precision_hint_fastest
    40.             #include "UnityCG.cginc"
    41.          
    42.             sampler2D _MainTex;
    43.             samplerCUBE _ToonShade;
    44.             float4 _MainTex_ST;
    45.             float4 _Color;
    46.          
    47.             uniform sampler2D _Control;
    48.             uniform sampler2D _Splat0,_Splat1,_Splat2,_Splat3;
    49.          
    50.             struct appdata
    51.             {
    52.                 float4 vertex : POSITION;
    53.                 float2 texcoord : TEXCOORD0;
    54.                 float3 normal : NORMAL;
    55.             };
    56.          
    57.             struct v2f
    58.             {
    59.                 float4 pos : POSITION;
    60.                 float2 texcoord : TEXCOORD0;
    61.                 float3 cubenormal : TEXCOORD1;              
    62.             };
    63.                 v2f vert (appdata v)
    64.             {
    65.                 v2f o;
    66.                 o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    67.                 o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    68.                 o.cubenormal = mul (UNITY_MATRIX_MV, float4(v.normal,0));
    69.                 return o;
    70.             }
    71.          
    72.             struct Input
    73.             {
    74.                 float2 uv_Control : TEXCOORD0;
    75.                 float2 uv_Splat0 : TEXCOORD1;
    76.                 float2 uv_Splat1 : TEXCOORD2;
    77.                 float2 uv_Splat2 : TEXCOORD3;
    78.                 float2 uv_Splat3 : TEXCOORD4;
    79.             };
    80.          
    81.             float4 frag (v2f i,Input IN) : COLOR
    82.             {
    83.                 float4 col2 = _Color * tex2D(_MainTex, i.texcoord);            
    84.                 float4 cube = texCUBE(_ToonShade, i.cubenormal);
    85.                 fixed4 splat_control = tex2D (_Control, IN.uv_Control);            
    86.                 fixed4 col;
    87.                 col  = _Color *splat_control.r * tex2D (_Splat0, IN.uv_Splat0);
    88.                 col += _Color *splat_control.g * tex2D (_Splat1, IN.uv_Splat1);
    89.                 col += _Color *splat_control.b * tex2D (_Splat2, IN.uv_Splat2);
    90.                 col += _Color *splat_control.a * tex2D (_Splat3, IN.uv_Splat3);
    91.                 return float4(2.0f * cube.rgb * col2.rgb * col.rgb, col2.a * col.a);            
    92.             }
    93.  
    94.             ENDCG
    95.              
    96.         }
    97.         //UsePass "Custom/myOutline/OUTLINE"            
    98.     }
    99. //Fallback "Toon/Basic"
    100. } // Ehd Shader
     
    Last edited: Mar 31, 2015
  2. echo4papa

    echo4papa

    Joined:
    Mar 26, 2015
    Posts:
    158
    Have you tried adding this:


    Code (CSharp):
    1. #pragma vertex vert
    2. #pragma fragment frag
    3. #pragma target 3.0
    4.  
    The default target model is 2.0(still) and it limits the amount instructions you can have. Using target 3.0 should give you enough, and most modern Android devices will support SM3.

    http://docs.unity3d.com/Manual/SL-ShaderPrograms.html
     
  3. Fog0

    Fog0

    Joined:
    Mar 27, 2015
    Posts:
    3
    i tried now ,no result,here my phone model: Huawei Ascend G610.
     
  4. Fog0

    Fog0

    Joined:
    Mar 27, 2015
    Posts:
    3
    Now I have another point of view ... trying to merge with frag frag2
    - code Update

    Code (CSharp):
    1. Shader "Custom/myToonLightfake2"
    2. {
    3.     Properties
    4.     {    
    5.         // Control Texture ("Splat Map")
    6.          _Control ("Control (RGBA)", 2D) = "red" {}
    7.        
    8.         // Terrain textures - each weighted according to the corresponding colour
    9.         // channel in the control texture
    10.          _Splat3 ("Layer 3 (A)", 2D) = "black" {}
    11.          _Splat2 ("Layer 2 (B)", 2D) = "gray" {}
    12.          _Splat1 ("Layer 1 (G)", 2D) = "gray" {}
    13.          _Splat0 ("Layer 0 (R)", 2D) = "gray" {}
    14.        
    15.         // Used in fallback on old cards & also for distant base map
    16.          _MainTex ("BaseMap (RGB)", 2D) = "white" {}
    17.          _Color ("Main Color", Color) = (1,1,1,1)
    18.        
    19.         // Colour of toon outline
    20.         _OutlineColor ("Outline Color", Color) = (0,0,0,1)
    21.         _Outline ("Outline width", Range (.002, 0.05)) = .05
    22.        
    23.         _ToonShade ("ToonShader Cubemap(RGB)", CUBE) = "" { Texgen CubeNormal }
    24.     }
    25.    
    26.     CGINCLUDE
    27.     #include "UnityCG.cginc"
    28.     sampler2D _MainTex;
    29.     samplerCUBE _ToonShade;
    30.     float4 _MainTex_ST;
    31.     float4 _Color;
    32.    
    33.     uniform sampler2D _Control;
    34.     uniform sampler2D _Splat0,_Splat1,_Splat2,_Splat3;  
    35.            
    36.     struct appdata
    37.     {
    38.         float4 vertex : POSITION;
    39.         float2 texcoord : TEXCOORD0;
    40.         float3 normal : NORMAL;
    41.     };          
    42.     struct v2f
    43.     {
    44.         float4 pos : POSITION;
    45.         float2 texcoord : TEXCOORD0;
    46.         float3 cubenormal : TEXCOORD1;        
    47.     };  
    48.     v2f vert (appdata v)
    49.     {
    50.         v2f o;
    51.         o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    52.         o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    53.         o.cubenormal = mul (UNITY_MATRIX_MV, float4(v.normal,0));
    54.         return o;
    55.     }
    56.            
    57.     struct Input
    58.     {
    59.         float2 uv_Control : TEXCOORD0;
    60.         float2 uv_Splat0 : TEXCOORD1;
    61.         float2 uv_Splat1 : TEXCOORD2;
    62.         float2 uv_Splat2 : TEXCOORD3;
    63.         float2 uv_Splat3 : TEXCOORD4;
    64.     };
    65.    
    66.     uniform float4 col;
    67.    
    68.     float4 frag2 (Input IN) : COLOR
    69.     {
    70.         fixed4 splat_control = tex2D (_Control, IN.uv_Control);              
    71.         //fixed4 col;
    72.         col  = _Color *splat_control.r * tex2D (_Splat0, IN.uv_Splat0);
    73.         col += _Color *splat_control.g * tex2D (_Splat1, IN.uv_Splat1);
    74.         col += _Color *splat_control.b * tex2D (_Splat2, IN.uv_Splat2);
    75.         col += _Color *splat_control.a * tex2D (_Splat3, IN.uv_Splat3);
    76.         //#pragma fragment frag
    77.         return float4(2.0f * col.rgb, col.a);
    78.                        
    79.     }
    80.     float4 frag (v2f i) : COLOR
    81.     {
    82.         float4 col2 = _Color * tex2D(_MainTex, i.texcoord);              
    83.         float4 cube = texCUBE(_ToonShade, i.cubenormal);      
    84.         return float4(2.0f * cube.rgb * col2.rgb,  col2.a);  
    85.     }
    86.    
    87.    
    88.     ENDCG
    89.    
    90.     SubShader
    91.     {
    92.         Tags { "RenderType"="Opaque" }
    93.         Pass
    94.         {
    95.             Cull Off
    96.             Name "myToonLightfake2"
    97.             CGPROGRAM
    98.            
    99.             #pragma vertex vert
    100.             #pragma fragment frag2
    101.             //#pragma fragment frag      
    102.             #pragma target 3.0
    103.  
    104.             ENDCG
    105.                
    106.         }
    107.         //UsePass "Custom/myOutline/OUTLINE"
    108.     }
    109. } // Ehd Shader