Search Unity

image effect shader causes black screen on android but working on pc.

Discussion in 'Image Effects' started by Abdullah9Salih, Oct 25, 2020.

  1. Abdullah9Salih

    Abdullah9Salih

    Joined:
    Oct 28, 2018
    Posts:
    10
    i'm using an image effect shader that adds edge detection to all objects.
    the shader works fine on pc and in editor. also in some android phones. but not all off them .
    when the scene starts . the screen freezes and doesn't render anything .
    when i change Blit type in android player setting from always to auto/never .
    it works fine on all devices but the frame rate drops by 20 fps.

    can anyone help me with this?


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. [ExecuteAlways]
    5. public class O_CustomImageEffect : MonoBehaviour {
    6.     public Material imageEffect;
    7.     RenderTexture myRenderTexture;
    8.     private void OnRenderImage (RenderTexture source, RenderTexture destination) {
    9.         if (imageEffect != null) {
    10.             Graphics.Blit (source, destination, imageEffect);
    11.         } else
    12.             destination = source;
    13.     }
    14. }



    Code (CSharp):
    1. Shader "The Developer/SS Texture Based Outline"
    2. {
    3.     Properties
    4.     {
    5.         _OutlineColor("Outline Color", Color) = (1,1,1,1)
    6.         _OutlineThreshold("Outline Threshold", Float) = 3
    7.         _OutlineWidth("Outline Width", Float) = 2
    8.         _EdgeStrengthen ("Edge Strengthen", Float) = 2
    9.         _MainTex ("Texture", 2D) = "white" {}
    10.     }
    11.     SubShader
    12.     {
    13.         // No culling or depth
    14.         // Cull Off ZWrite Off ZTest Always
    15.         Pass
    16.         {
    17.             CGPROGRAM
    18.             #pragma vertex vert
    19.             #pragma fragment frag
    20.             #include "UnityCG.cginc"
    21.             struct appdata
    22.             {
    23.                 float4 vertex : POSITION;
    24.                 float3 normal : NORMAL;
    25.             };
    26.             struct v2f
    27.             {
    28.                 float4 position : POSITION;
    29.                 float4 screenPos : TEXCOORD0;
    30.             };
    31.             fixed4 _OutlineColor;
    32.             half _OutlineThreshold;
    33.             half _OutlineWidth;
    34.             half _EdgeStrengthen;
    35.             sampler2D _CameraDepthTexture;
    36.             sampler2D _MainTex;
    37.             v2f vert(appdata input)
    38.             {
    39.                 v2f output;
    40.                 output.position = UnityObjectToClipPos(input.vertex);
    41.                 output.screenPos = output.position;
    42.                 return output;
    43.             }
    44.             #define PSL01UVC(tex, uv1, uv2) pow(\
    45.                 (tex2D(tex, uv1) - tex2D(tex, uv2)) * _EdgeStrengthen, \
    46.                 2)
    47.             float4 outline;
    48.             half4 pixel;
    49.             half2 uv;
    50.             half onePixelW, onePixelH;
    51.             fixed i;
    52.             void OutlinePixel(sampler2D tex){
    53.                 outline = 0;
    54.                 for(i = 1 ; i <= _OutlineWidth ; ++i)
    55.                     outline +=
    56.                             PSL01UVC(tex, half2(uv.x - i * onePixelW, uv.y),
    57.                                           half2(uv.x + i * onePixelW, uv.y)) +
    58.                             PSL01UVC(tex, half2(uv.x, uv.y + i * onePixelH),
    59.                                           half2(uv.x, uv.y - i * onePixelH)) +
    60.                             PSL01UVC(tex, half2(uv.x - i * onePixelW, uv.y - i * onePixelH),
    61.                                           half2(uv.x + i * onePixelW, uv.y + i * onePixelH)) +
    62.                             PSL01UVC(tex, half2(uv.x - i * onePixelW, uv.y + i * onePixelH),
    63.                                           half2(uv.x + i * onePixelW, uv.y - i * onePixelH));
    64.             }
    65.             half4 frag(v2f input) : SV_Target
    66.             {
    67.                 uv = input.screenPos.xy / input.screenPos.w;
    68.                 uv.x = (uv.x + 1) * .5;
    69.                 uv.y = (uv.y + 1) * .5;
    70.                 pixel = tex2D(_MainTex, uv);
    71.                 onePixelW = 1.0 / _ScreenParams.x;
    72.                 onePixelH = 1.0 / _ScreenParams.y;
    73.                 OutlinePixel(_MainTex);
    74.                 return lerp(pixel, _OutlineColor, (outline.r + outline.g + outline.b) >= _OutlineThreshold ? 1 : 0);
    75.             }
    76.             ENDCG
    77.         }
    78.     }
    79. }
     
  2. Abdullah9Salih

    Abdullah9Salih

    Joined:
    Oct 28, 2018
    Posts:
    10