1 Shader "Custom/threshold" {
4 //_webcamTex ("Base (RGB)", 2D) = "" {}
5 _MainTex ("Texture Image", 2D) = "white" {}
6 // _SobelKernel ("_SobelKernel", 2D) = "white" {}
7 // _GaussKernel ("_GaussKernel", 2D) = "white" {}
8 // _PrevTex ("Previous Frame", 2D) = "white" {}
9 //// _TexSize ("Texture Size", Vector) = (0,0,0,0)
10 // _TexSizeX ("Texture Width", Float) = 0.0
11 // _TexSizeY ("Texture Height", Float) = 0.0
12 _Thresholds ("_Thresholds", Vector) = (0.0,0.0,0.0,0.0)
13 _Thresholds2 ("_Thresholds2", Vector) = (0.0,0.0,0.0,0.0)
14 _BinaryMode ("_BinaryMode", Float) = 0
15 _ThresholdMode ("_ThresholdMode", Float) = 1
20 //Tags { "Queue" = "Geometry" }
21 //Tags { "RenderType" = "Transparent" }
24 //Blend SrcAlpha OneMinusSrcAlpha
26 // #pragma only_renderers gles
29 uniform sampler2D _MainTex;
30 // uniform sampler2D _SobelKernel;
31 // uniform sampler2D _GaussKernel;
32 uniform vec4 _Thresholds;
33 uniform vec4 _Thresholds2;
34 uniform int _BinaryMode;
35 uniform int _ThresholdMode;
37 varying vec4 textureCoordinates;
45 gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
49 // textureCoordinates = gl_Position;
50 textureCoordinates = vec4( gl_Vertex.y * 2.0, gl_Vertex.x * 2.0, gl_Vertex.z, gl_Vertex.w);
51 // textureCoordinates = vec4(gl_Position.x * -1.0, gl_Position.y, gl_Position.z, gl_Position.w);
52 textureCoordinates = (textureCoordinates + 1.0) * 0.5;
53 // textureCoordinates.x = textureCoordinates.x * 0.5 + 0.25;
58 // textureCoordinates = (gl_Position + 1.0) * 0.5;
59 // textureCoordinates = textureCoordinates.yxzw;
67 vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
68 vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
69 vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
71 float d = q.x - min(q.w, q.y);
73 return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
78 vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
79 vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
80 return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
84 void setColor(vec4 c1, vec4 c2){
85 if(_BinaryMode == 1.0){
97 vec4 c = texture2D(_MainTex, vec2(textureCoordinates));
101 c = vec4(hsv2rgb(c1), 1.0);
103 if(_ThresholdMode == 1.0){
106 // http://link.springer.com/chapter/10.1007%2F978-3-540-74472-6_85?LI=true#page-1
108 float y = 16.0 + 65.481 * c.r + 128.553 * c.g + 24.966 * c.b;
109 float cb = 128.0 - 37.797 * c.r - 74.203 * c.g + 112.0 * c.b;
110 // float cr = 128.0 + 112.0 * c.r - 93.786 * c.g - 18.214 * c.b;
111 float cr = 128.0 + 112.0 * c.r - 93.786 * c.g - 18.214 * c.b;
113 if((_Thresholds.z < cr && cr < _Thresholds.w && _Thresholds.x < cb && cb < _Thresholds.y) || ( _Thresholds2.x < y && y < _Thresholds2.y)){
115 // if(_Thresholds.x < c.r && c.r < _Thresholds.y && _Thresholds.z < c.g && c.g < _Thresholds.w && _Thresholds2.x < c.b && c.b < _Thresholds2.y){
118 setColor(c, vec4(1.0, 1.0, 1.0, 1.0));
120 // if(_BinaryMode == 1)
121 // gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
126 setColor(c * 0.5, vec4(0.0, 0.0, 0.0, 1.0));