3DSelfie  Hansjoerg Hofer (1026632), Sebastian Metzler (0927550)
threshold.shader
Go to the documentation of this file.
1 Shader "Custom/threshold" {
2  Properties
3  {
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
16 
17  }
18  SubShader
19  {
20  //Tags { "Queue" = "Geometry" }
21  //Tags { "RenderType" = "Transparent" }
22  Pass
23  {
24  //Blend SrcAlpha OneMinusSrcAlpha
25  GLSLPROGRAM
26 // #pragma only_renderers gles
27 
28 
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;
36 
37  varying vec4 textureCoordinates;
38 
39  #ifdef VERTEX
40 
41  void main()
42  {
43 
44 
45  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
46 
47 
48  //desktop
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;
54 
55 
56  //mobile
57 
58 // textureCoordinates = (gl_Position + 1.0) * 0.5;
59 // textureCoordinates = textureCoordinates.yxzw;
60  }
61  #endif
62 
63  #ifdef FRAGMENT
64 
65  vec3 rgb2hsv(vec3 c){
66 
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));
70 
71  float d = q.x - min(q.w, q.y);
72  float e = 1.0e-10;
73  return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
74  }
75 
76  vec3 hsv2rgb(vec3 c){
77 
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);
81  }
82 
83 
84  void setColor(vec4 c1, vec4 c2){
85  if(_BinaryMode == 1.0){
86  gl_FragColor = c2;
87  }else{
88 
89  gl_FragColor = c1;
90  }
91  }
92 
93  void main()
94  {
95 
96 
97  vec4 c = texture2D(_MainTex, vec2(textureCoordinates));
98 
99  vec3 c1 = rgb2hsv(c);
100  c1.g *= 1.7;
101  c = vec4(hsv2rgb(c1), 1.0);
102 
103  if(_ThresholdMode == 1.0){
104 
105 
106 // http://link.springer.com/chapter/10.1007%2F978-3-540-74472-6_85?LI=true#page-1
107 
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;
112 
113  if((_Thresholds.z < cr && cr < _Thresholds.w && _Thresholds.x < cb && cb < _Thresholds.y) || ( _Thresholds2.x < y && y < _Thresholds2.y)){
114 
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){
116 
117 
118  setColor(c, vec4(1.0, 1.0, 1.0, 1.0));
119 
120 // if(_BinaryMode == 1)
121 // gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
122 // else
123 // gl_FragColor = c;
124  }else{
125 
126  setColor(c * 0.5, vec4(0.0, 0.0, 0.0, 1.0));
127 
128  }
129 
130  }
131  else{
132 
133  gl_FragColor = c;
134 
135  }
136 
137 
138  }
139  #endif
140  ENDGLSL
141  }
142  }
143 
144 }