-
Notifications
You must be signed in to change notification settings - Fork 1
/
shaders.xml
183 lines (146 loc) · 5.38 KB
/
shaders.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<shaders>
<script id="depth-shader-vs" type="x-shader/x-vertex">
precision mediump float;
uniform mat4 modelViewProjectionMatrix;
attribute vec3 vertexPosition;
void main(void) {
gl_Position = modelViewProjectionMatrix * vec4(vertexPosition, 1.0);
}
</script>
<script id="depth-shader-fs" type="x-shader/x-fragment">
precision highp float;
void main(void) {
//float w = gl_FragCoord.w;
float z = gl_FragCoord.z;
float z1 = fract(z*255.0);
float z2 = fract(z1*255.0);
float z3 = fract(z2*255.0);
//this biasing is necessary for shadow mapping to work correctly
//source: http://forum.devmaster.net/t/shader-effects-shadow-mapping/3002
// this might be due to the GPU *rounding* the float values to the nearest uint8_t instead of the expeected *truncating*
z -= 1.0/255.0*z1;
z1 -= 1.0/255.0*z2;
z2 -= 1.0/255.0*z3;
gl_FragColor = vec4(z, z1, z2, z3);//vec4(vec3(depth), 1.0);
//gl_FragColor = vec4((z-232.0/256.0)*25.0, z1, 0, 1.0);
}
</script>
<script id="building-shader-vs" type="x-shader/x-vertex">
attribute vec3 vertexPosition;
attribute vec2 vertexTexCoords;
attribute vec3 vertexNormal;
attribute vec3 vertexColorIn;
uniform mat4 modelViewProjectionMatrix;
uniform vec3 cameraPos;
varying vec2 texCoordV;
varying vec3 normal;
varying vec3 vertexColor;
void main(void) {
gl_Position = modelViewProjectionMatrix * vec4(vertexPosition, 1.0);
texCoordV = vertexTexCoords;
vec3 vView = normalize( cameraPos - vertexPosition);
float diffuse = abs( 0.5 * dot(vView, vertexNormal) );
vertexColor = vertexColorIn * (diffuse + 0.4);
}
</script>
<script id="building-shader-fs" type="x-shader/x-fragment">
<![CDATA[
precision mediump float;
varying vec2 texCoordV;
varying vec3 vertexColor;
uniform sampler2D tex;
void main(void) {
vec3 brightness = texture2D(tex, texCoordV).rgb * 2.0;
gl_FragColor = vec4( vertexColor*brightness, 1.0);
}
]]>
</script>
<script id="texture-shader-fs" type="x-shader/x-fragment">
<![CDATA[
precision mediump float;
varying vec2 texCoordV;
uniform sampler2D tex;
void main(void) {
gl_FragColor = texture2D(tex, texCoordV.st);
}
]]>
</script>
<script id="texture-shader-vs" type="x-shader/x-vertex"><![CDATA[
attribute vec3 vertexPosition;
attribute vec2 vertexTexCoords;
uniform mat4 modelViewProjectionMatrix;
varying vec2 texCoordV;
void main(void) {
gl_Position = modelViewProjectionMatrix * vec4(vertexPosition, 1.0);
texCoordV = vertexTexCoords;
}
]]>
</script>
<script id="shadowed-shader-vs" type="x-shader/x-vertex">
attribute vec3 vertexPosition;
attribute vec2 vertexTexCoords;
attribute vec3 normalIn;
uniform mat4 modelViewProjectionMatrix;
uniform mat4 shadowMatrix;
varying vec2 texCoordV;
varying vec4 shadowTexPosition;
varying vec3 normalOut;
void main(void) {
gl_Position = modelViewProjectionMatrix * vec4(vertexPosition, 1.0);
shadowTexPosition = shadowMatrix * vec4(vertexPosition, 1.0);
texCoordV = vertexTexCoords;
normalOut = normalIn;
}
</script>
<script id="shadowed-texture-shader-fs" type="x-shader/x-fragment">
<![CDATA[
precision mediump float;
varying vec2 texCoordV;
varying vec4 shadowTexPosition;
varying vec3 normalOut;
uniform sampler2D tex;
uniform sampler2D shadowTex; //light source depth texture
uniform vec3 sunDir;
void main(void) {
vec4 col1 = texture2D(tex, texCoordV.st);
//shadow position in normalized device coordinates
vec3 shadowNDC = (shadowTexPosition.xyz / shadowTexPosition.w) * 0.5 + 0.5;
float depth = shadowNDC.z;
int inLight = 0;
const float delta = 0.5/2048.0;
vec4 col = texture2D(shadowTex, vec2(shadowNDC.s, shadowNDC.t ) );
float lightDepth = col.r + col.g/255.0 + col.b/(255.0*255.0) + col.a/(255.0*255.0*255.0);
if (lightDepth + 0.00002 > depth)
inLight++;
//if (texture2D(shadowTex, vec2(shadowNDC.s, shadowNDC.t ) ).r + 0.00002 >= depth) inLight++;
/*if (texture2D(shadowTex, vec2(shadowNDC.s, shadowNDC.t + delta) ).r + 0.00001 >= depth) inLight++;
if (texture2D(shadowTex, vec2(shadowNDC.s + delta, shadowNDC.t ) ).r + 0.00001 >= depth) inLight++;
if (texture2D(shadowTex, vec2(shadowNDC.s + delta, shadowNDC.t + delta) ).r + 0.00001 >= depth) inLight++;*/
//float normDepth = a + b /
float sunDiff = dot(sunDir, normalOut);
if (( sunDiff < 0.00001) || (inLight == 0))
gl_FragColor = col1;
else
{
vec4 col_tmp = vec4(col1.rgb*(1.0 + float(inLight)/2.5 * sunDiff), 1.0);
//col_tmp.b = abs( depth - lightDepth);
gl_FragColor = col_tmp;
}
}
]]>
</script>
<script id="flat-shader-vs" type="x-shader/x-vertex">
attribute vec3 vertexPosition;
uniform mat4 modelViewProjectionMatrix;
void main(void) {
gl_Position = modelViewProjectionMatrix * vec4(vertexPosition, 1.0);
}
</script>
<script id="flat-shader-fs" type="x-shader/x-fragment">
precision mediump float;
uniform vec4 color;
void main(void) {
gl_FragColor = color;
}
</script>
</shaders>