Skip to content

Commit 4c58766

Browse files
shader lighting example
1 parent 8eac9f4 commit 4c58766

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+3769
-0
lines changed
218 KB
Loading
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#version 100
2+
3+
precision mediump float;
4+
5+
// Input vertex attributes (from vertex shader)
6+
varying vec2 fragTexCoord;
7+
varying vec4 fragColor;
8+
9+
// Input uniform values
10+
uniform sampler2D texture0;
11+
uniform vec4 colDiffuse;
12+
13+
// NOTE: Add here your custom variables
14+
uniform vec2 resolution = vec2(800, 450);
15+
16+
void main()
17+
{
18+
// Texel color fetching from texture sampler
19+
vec4 texelColor = texture2D(texture0, fragTexCoord);
20+
21+
// NOTE: Implement here your fragment shader code
22+
23+
gl_FragColor = texelColor*colDiffuse;
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#version 100
2+
3+
// Input vertex attributes
4+
attribute vec3 vertexPosition;
5+
attribute vec2 vertexTexCoord;
6+
attribute vec3 vertexNormal;
7+
attribute vec4 vertexColor;
8+
9+
// Input uniform values
10+
uniform mat4 mvp;
11+
12+
// Output vertex attributes (to fragment shader)
13+
varying vec2 fragTexCoord;
14+
varying vec4 fragColor;
15+
16+
// NOTE: Add here your custom variables
17+
18+
void main()
19+
{
20+
// Send vertex attributes to fragment shader
21+
fragTexCoord = vertexTexCoord;
22+
fragColor = vertexColor;
23+
24+
// Calculate final vertex position
25+
gl_Position = mvp*vec4(vertexPosition, 1.0);
26+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#version 100
2+
3+
precision mediump float;
4+
5+
// Input vertex attributes (from vertex shader)
6+
varying vec2 fragTexCoord;
7+
varying vec4 fragColor;
8+
9+
// Input uniform values
10+
uniform sampler2D texture0;
11+
uniform vec4 colDiffuse;
12+
13+
// NOTE: Add here your custom variables
14+
15+
const vec2 size = vec2(800, 450); // render size
16+
const float samples = 5.0; // pixels per axis; higher = bigger glow, worse performance
17+
const float quality = 2.5; // lower = smaller glow, better quality
18+
19+
void main()
20+
{
21+
vec4 sum = vec4(0);
22+
vec2 sizeFactor = vec2(1)/size*quality;
23+
24+
// Texel color fetching from texture sampler
25+
vec4 source = texture2D(texture0, fragTexCoord);
26+
27+
const int range = 2; // should be = (samples - 1)/2;
28+
29+
for (int x = -range; x <= range; x++)
30+
{
31+
for (int y = -range; y <= range; y++)
32+
{
33+
sum += texture2D(texture0, fragTexCoord + vec2(x, y)*sizeFactor);
34+
}
35+
}
36+
37+
// Calculate final fragment color
38+
gl_FragColor = ((sum/(samples*samples)) + source)*colDiffuse;
39+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#version 100
2+
3+
precision mediump float;
4+
5+
// Input vertex attributes (from vertex shader)
6+
varying vec2 fragTexCoord;
7+
varying vec4 fragColor;
8+
9+
// Input uniform values
10+
uniform sampler2D texture0;
11+
uniform vec4 colDiffuse;
12+
13+
// NOTE: Add here your custom variables
14+
15+
// NOTE: Render size values must be passed from code
16+
const float renderWidth = 800.0;
17+
const float renderHeight = 450.0;
18+
19+
vec3 offset = vec3(0.0, 1.3846153846, 3.2307692308);
20+
vec3 weight = vec3(0.2270270270, 0.3162162162, 0.0702702703);
21+
22+
void main()
23+
{
24+
// Texel color fetching from texture sampler
25+
vec3 tc = texture2D(texture0, fragTexCoord).rgb*weight.x;
26+
27+
tc += texture2D(texture0, fragTexCoord + vec2(offset.y)/renderWidth, 0.0).rgb*weight.y;
28+
tc += texture2D(texture0, fragTexCoord - vec2(offset.y)/renderWidth, 0.0).rgb*weight.y;
29+
30+
tc += texture2D(texture0, fragTexCoord + vec2(offset.z)/renderWidth, 0.0).rgb*weight.z;
31+
tc += texture2D(texture0, fragTexCoord - vec2(offset.z)/renderWidth, 0.0).rgb*weight.z;
32+
33+
gl_FragColor = vec4(tc, 1.0);
34+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# version 100
2+
3+
precision mediump float;
4+
5+
// Input vertex attributes (from vertex shader)
6+
varying vec2 fragTexCoord;
7+
varying vec4 fragColor;
8+
9+
// Input uniform values
10+
uniform sampler2D texture0;
11+
uniform vec4 colDiffuse;
12+
13+
// NOTE: Add here your custom variables
14+
15+
float hatchOffsetY = 5.0;
16+
float lumThreshold01 = 0.9;
17+
float lumThreshold02 = 0.7;
18+
float lumThreshold03 = 0.5;
19+
float lumThreshold04 = 0.3;
20+
21+
void main()
22+
{
23+
vec3 tc = vec3(1.0, 1.0, 1.0);
24+
float lum = length(texture2D(texture0, fragTexCoord).rgb);
25+
26+
if (lum < lumThreshold01)
27+
{
28+
if (mod(gl_FragCoord.x + gl_FragCoord.y, 10.0) == 0.0) tc = vec3(0.0, 0.0, 0.0);
29+
}
30+
31+
if (lum < lumThreshold02)
32+
{
33+
if (mod(gl_FragCoord .x - gl_FragCoord .y, 10.0) == 0.0) tc = vec3(0.0, 0.0, 0.0);
34+
}
35+
36+
if (lum < lumThreshold03)
37+
{
38+
if (mod(gl_FragCoord .x + gl_FragCoord .y - hatchOffsetY, 10.0) == 0.0) tc = vec3(0.0, 0.0, 0.0);
39+
}
40+
41+
if (lum < lumThreshold04)
42+
{
43+
if (mod(gl_FragCoord .x - gl_FragCoord .y - hatchOffsetY, 10.0) == 0.0) tc = vec3(0.0, 0.0, 0.0);
44+
}
45+
46+
gl_FragColor = vec4(tc, 1.0);
47+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# version 100
2+
3+
precision mediump float;
4+
5+
// Input vertex attributes (from vertex shader)
6+
varying vec2 fragTexCoord;
7+
varying vec4 fragColor;
8+
9+
// Input uniform values
10+
uniform sampler2D texture0;
11+
uniform vec4 colDiffuse;
12+
13+
// NOTE: Add here your custom variables
14+
15+
// NOTE: Render size values must be passed from code
16+
const float renderWidth = 800.0;
17+
const float renderHeight = 450.0;
18+
19+
float stitchingSize = 6.0;
20+
int invert = 0;
21+
22+
vec4 PostFX(sampler2D tex, vec2 uv)
23+
{
24+
vec4 c = vec4(0.0);
25+
float size = stitchingSize;
26+
vec2 cPos = uv * vec2(renderWidth, renderHeight);
27+
vec2 tlPos = floor(cPos / vec2(size, size));
28+
tlPos *= size;
29+
30+
int remX = int(mod(cPos.x, size));
31+
int remY = int(mod(cPos.y, size));
32+
33+
if (remX == 0 && remY == 0) tlPos = cPos;
34+
35+
vec2 blPos = tlPos;
36+
blPos.y += (size - 1.0);
37+
38+
if ((remX == remY) || (((int(cPos.x) - int(blPos.x)) == (int(blPos.y) - int(cPos.y)))))
39+
{
40+
if (invert == 1) c = vec4(0.2, 0.15, 0.05, 1.0);
41+
else c = texture2D(tex, tlPos * vec2(1.0/renderWidth, 1.0/renderHeight)) * 1.4;
42+
}
43+
else
44+
{
45+
if (invert == 1) c = texture2D(tex, tlPos * vec2(1.0/renderWidth, 1.0/renderHeight)) * 1.4;
46+
else c = vec4(0.0, 0.0, 0.0, 1.0);
47+
}
48+
49+
return c;
50+
}
51+
52+
void main()
53+
{
54+
vec3 tc = PostFX(texture0, fragTexCoord).rgb;
55+
56+
gl_FragColor = vec4(tc, 1.0);
57+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#version 100
2+
3+
precision mediump float;
4+
5+
// Input vertex attributes (from vertex shader)
6+
varying vec2 fragTexCoord;
7+
varying vec4 fragColor;
8+
9+
// Custom variables
10+
#define PI 3.14159265358979323846
11+
uniform float uTime = 0.0;
12+
13+
float divisions = 5.0;
14+
float angle = 0.0;
15+
16+
vec2 VectorRotateTime(vec2 v, float speed)
17+
{
18+
float time = uTime*speed;
19+
float localTime = fract(time); // The time domain this works on is 1 sec.
20+
21+
if ((localTime >= 0.0) && (localTime < 0.25)) angle = 0.0;
22+
else if ((localTime >= 0.25) && (localTime < 0.50)) angle = PI/4*sin(2*PI*localTime - PI/2);
23+
else if ((localTime >= 0.50) && (localTime < 0.75)) angle = PI*0.25;
24+
else if ((localTime >= 0.75) && (localTime < 1.00)) angle = PI/4*sin(2*PI*localTime);
25+
26+
// Rotate vector by angle
27+
v -= 0.5;
28+
v = mat2(cos(angle), -sin(angle), sin(angle), cos(angle))*v;
29+
v += 0.5;
30+
31+
return v;
32+
}
33+
34+
float Rectangle(in vec2 st, in float size, in float fill)
35+
{
36+
float roundSize = 0.5 - size/2.0;
37+
float left = step(roundSize, st.x);
38+
float top = step(roundSize, st.y);
39+
float bottom = step(roundSize, 1.0 - st.y);
40+
float right = step(roundSize, 1.0 - st.x);
41+
42+
return (left*bottom*right*top)*fill;
43+
}
44+
45+
void main()
46+
{
47+
vec2 fragPos = fragTexCoord;
48+
fragPos.xy += uTime/9.0;
49+
50+
fragPos *= divisions;
51+
vec2 ipos = floor(fragPos); // Get the integer coords
52+
vec2 fpos = fract(fragPos); // Get the fractional coords
53+
54+
fpos = VectorRotateTime(fpos, 0.2);
55+
56+
float alpha = Rectangle(fpos, 0.216, 1.0);
57+
vec3 color = vec3(0.3, 0.3, 0.3);
58+
59+
gl_FragColor = vec4(color, alpha);
60+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#version 100
2+
3+
precision mediump float;
4+
5+
// Input vertex attributes (from vertex shader)
6+
varying vec2 fragTexCoord;
7+
varying vec4 fragColor;
8+
9+
// Input uniform values
10+
uniform sampler2D texture0; // Depth texture
11+
uniform vec4 colDiffuse;
12+
13+
// NOTE: Add here your custom variables
14+
15+
void main()
16+
{
17+
float zNear = 0.01; // camera z near
18+
float zFar = 10.0; // camera z far
19+
float z = texture2D(texture0, fragTexCoord).x;
20+
21+
// Linearize depth value
22+
float depth = (2.0*zNear)/(zFar + zNear - z*(zFar - zNear));
23+
24+
// Calculate final fragment color
25+
gl_FragColor = vec4(depth, depth, depth, 1.0f);
26+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#version 100
2+
3+
precision mediump float;
4+
5+
// Input vertex attributes (from vertex shader)
6+
varying vec2 fragTexCoord;
7+
8+
// Input uniform values
9+
uniform sampler2D texture0;
10+
11+
// NOTE: Default parameters for Oculus Rift DK2 device
12+
const vec2 LeftLensCenter = vec2(0.2863248, 0.5);
13+
const vec2 RightLensCenter = vec2(0.7136753, 0.5);
14+
const vec2 LeftScreenCenter = vec2(0.25, 0.5);
15+
const vec2 RightScreenCenter = vec2(0.75, 0.5);
16+
const vec2 Scale = vec2(0.25, 0.45);
17+
const vec2 ScaleIn = vec2(4.0, 2.5);
18+
const vec4 HmdWarpParam = vec4(1.0, 0.22, 0.24, 0.0);
19+
const vec4 ChromaAbParam = vec4(0.996, -0.004, 1.014, 0.0);
20+
21+
void main()
22+
{
23+
// The following two variables need to be set per eye
24+
vec2 LensCenter = fragTexCoord.x < 0.5 ? LeftLensCenter : RightLensCenter;
25+
vec2 ScreenCenter = fragTexCoord.x < 0.5 ? LeftScreenCenter : RightScreenCenter;
26+
27+
// Scales input texture coordinates for distortion: vec2 HmdWarp(vec2 fragTexCoord, vec2 LensCenter)
28+
vec2 theta = (fragTexCoord - LensCenter)*ScaleIn; // Scales to [-1, 1]
29+
float rSq = theta.x*theta.x + theta.y*theta.y;
30+
vec2 theta1 = theta*(HmdWarpParam.x + HmdWarpParam.y*rSq + HmdWarpParam.z*rSq*rSq + HmdWarpParam.w*rSq*rSq*rSq);
31+
//vec2 tc = LensCenter + Scale*theta1;
32+
33+
// Detect whether blue texture coordinates are out of range since these will scaled out the furthest
34+
vec2 thetaBlue = theta1*(ChromaAbParam.z + ChromaAbParam.w*rSq);
35+
vec2 tcBlue = LensCenter + Scale*thetaBlue;
36+
37+
if (any(bvec2(clamp(tcBlue, ScreenCenter - vec2(0.25, 0.5), ScreenCenter + vec2(0.25, 0.5)) - tcBlue))) gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
38+
else
39+
{
40+
// Do blue texture lookup
41+
float blue = texture2D(texture0, tcBlue).b;
42+
43+
// Do green lookup (no scaling)
44+
vec2 tcGreen = LensCenter + Scale*theta1;
45+
float green = texture2D(texture0, tcGreen).g;
46+
47+
// Do red scale and lookup
48+
vec2 thetaRed = theta1*(ChromaAbParam.x + ChromaAbParam.y*rSq);
49+
vec2 tcRed = LensCenter + Scale*thetaRed;
50+
float red = texture2D(texture0, tcRed).r;
51+
52+
gl_FragColor = vec4(red, green, blue, 1.0);
53+
}
54+
}

0 commit comments

Comments
 (0)