본문 바로가기
SHADER

유니티와 노트4의 전쟁... 특정 셰이더 코드와 충돌일으킴(vert_img)

by tartist 2016. 10. 29.

 아래 코드는 라디알 후처리를 처리하는 셰이더 코드입니다...

근데 이 코드가 노트4를 계속 죽이는 문제를 일으켰습니다...




             #pragma vertex vert_img 

             #pragma fragment frag 

             #pragma fragmentoption ARB_precision_hint_fastest 

   

             #include "UnityCG.cginc" 

   

             uniform sampler2D _MainTex; 

             uniform half4 _MainTex_TexelSize; 

             uniform half _BlurStrength; 

             uniform half _BlurWidth; 

             uniform half _imgWidth; 

             uniform half _imgHeight; 

   

             half4 frag (v2f_img i) : COLOR  

             { 

                 half4 color = tex2D(_MainTex, i.uv); 

         

                 // some sample positions 

                 half samples[10]; 

                 samples[0] = -0.08; 

                 samples[1] = -0.05; 

                 samples[2] = -0.03; 

                 samples[3] = -0.02; 

                 samples[4] = -0.01; 

                 samples[5] =  0.01; 

                 samples[6] =  0.02; 

                 samples[7] =  0.03; 

                 samples[8] =  0.05; 

                 samples[9] =  0.08; 

         

                 //vector to the middle of the screen 

                 half2 dir = 0.5 * half2(_imgHeight,_imgWidth) - i.uv; 

         

                 //distance to center 

                 half dist = sqrt(dir.x*dir.x + dir.y*dir.y); 

         

                 //normalize direction 

                 dir = dir/dist; 

         

                 //additional samples towards center of screen 

                 half4 sum = color; 

                 for(int n = 0; n < 10; n++) 

                 { 

                     sum += tex2D(_MainTex, i.uv + dir * samples[n] * _BlurWidth * _imgWidth); 

                 } 

         

                 //eleven samples... 

                 sum *= 1.0/11.0; 

         

                 //weighten blur depending on distance to screen center 

                 /* 

                 half t = dist * _BlurStrength / _imgWidth; 

                 t = clamp(t, 0.0, 1.0); 

                 */  

                 half t = saturate(dist * _BlurStrength); 

         

                 //blend original with blur 

                 return lerp(color, sum, t); 

             } 

             ENDCG 





어느 부분에서 크래쉬를 일으켰을까...

바로 vert_img  ~~~!!!



이 녀석이 노트4에서 앱을 죽이는 역할을 담당했네요...


이 코드를 아래와 같이 바꾸자 노트4 크래쉬가 해결됐습니다~~



             #pragma vertex vert

             #pragma fragment frag 

             #pragma fragmentoption ARB_precision_hint_fastest 

             #pragma exclude_renderers gles3 d3d11_9x xbox360 xboxone ps3 ps4 psp2

   

             #include "UnityCG.cginc" 

   

             uniform sampler2D _MainTex; 


             float4 _MainTex_ST;


             uniform half4 _MainTex_TexelSize; 

             uniform half _BlurStrength; 

             uniform half _BlurWidth; 

             uniform half _imgWidth; 

             uniform half _imgHeight; 



  struct appdata_t {

float4 vertex : POSITION;

float2 texcoord : TEXCOORD;

};

struct v2f {

float4 vertex : SV_POSITION;

float2 texcoord : TEXCOORD;

};


v2f vert (appdata_t v)

{

v2f o;

o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);

o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);

return o;

}




             half4 frag (v2f i) : COLOR  

             { 

                 half4 color = tex2D(_MainTex, i.texcoord); 

         

                 // some sample positions 

                 half samples[10]; 

                 samples[0] = -0.08; 

                 samples[1] = -0.05; 

                 samples[2] = -0.03; 

                 samples[3] = -0.02; 

                 samples[4] = -0.01; 

                 samples[5] =  0.01; 

                 samples[6] =  0.02; 

                 samples[7] =  0.03; 

                 samples[8] =  0.05; 

                 samples[9] =  0.08; 

         

                 //vector to the middle of the screen 

                 half2 dir = 0.5 * half2(_imgHeight,_imgWidth) - i.texcoord; 

         

                 //distance to center 

                 half dist = sqrt(dir.x*dir.x + dir.y*dir.y); 

         

                 //normalize direction 

                 dir = dir/dist; 

         

                 //additional samples towards center of screen 

                 half4 sum = color; 

                 for(int n = 0; n < 10; n++) 

                 { 

                     sum += tex2D(_MainTex, i.texcoord + dir * samples[n] * _BlurWidth * _imgWidth); 

                 } 

         

                 //eleven samples... 

                 sum *= 1.0/11.0; 

         

                 //weighten blur depending on distance to screen center 

                 /* 

                 half t = dist * _BlurStrength / _imgWidth; 

                 t = clamp(t, 0.0, 1.0); 

                 */  

                 half t = saturate(dist * _BlurStrength); 

         

                 //blend original with blur 

                 return lerp(color, sum, t); 

             } 

             ENDCG




아.. 이 문제 때문에 정말 힘든 하루였음..ㅠㅠ


노트4에서는 절대 vert_img 가 들어간 셰이더를 사용하면 안됩니다!