-
Notifications
You must be signed in to change notification settings - Fork 0
/
copying shader stuff from old demo.txt
120 lines (70 loc) · 2.78 KB
/
copying shader stuff from old demo.txt
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
__FLOOR SHADER__
VERTEX
//pass the x1 in local sprite coordinates.
//the sprite center (small red cross) is 0,0
VAR1.xy=SRC_VERTEX;
FRAGMENT
// just pass the normal
uniform texture normal;
vec2 ywnormal=tex( normal,UV).wy * vec2(2.0,2.0) - vec2(1.0,1.0);
NORMAL=vec3(ywnormal,sqrt(1 - (ywnormal.x * ywnormal.x) - (ywnormal.y * ywnormal.y) ));
LIGHTING
//the tile half size in pixels
vec2 tile_vec=vec2(140,70);
float z_scale = 1.0;
//compute a 3D postion fot thelight
vec3 light_3d = vec3(-LIGHT_VEC.x,LIGHT_HEIGHT,LIGHT_VEC.y);
//rotate the normal map to the same coordinates as the pseudo 3d ponit
//this could be optimized
float r = asin(tile_vec.y/tile_vec.x);
mat3 rot_mat = mat3( vec3(1,0,0), vec3(0,cos(r),-sin(r)), vec3(0,sin(r),cos(r)));
vec3 n = rot_mat * NORMAL;
n.y=-n.y;
//compute diffuse light
float dp = max(dot(normalize(n),-normalize(light_3d)),0);
LIGHT=vec4(vec3(dp),1.0)*COLOR*LIGHT_COLOR;
__WALL SHADER__
VERTEX
//assign the local vertex coordinates to a varying
//the center coordinates (0,0) matches the sprite center
// (shows as a cross when the sprite is selected)
VAR1.xy=SRC_VERTEX;
FRAGMENT
uniform texture normal;
vec2 ywnormal=tex( normal,UV).wy * vec2(2.0,2.0) - vec2(1.0,1.0);
NORMAL=vec3(ywnormal,sqrt(1 - (ywnormal.x * ywnormal.x) - (ywnormal.y * ywnormal.y) ));
LIGHTING
// this is the half size of the tile in pixels
vec2 tile_vec=vec2(47,22);
// this mask is used to see which part of the tile is the background (^)
// or the foregronud (v)
uniform texture bg_mask;
bool behind = tex(bg_mask,UV).a>0.5;
float y_ofs;
if (behind) {
// ^ part
y_ofs = tile_vec.y*2 - abs(VAR1.x * tile_vec.y / tile_vec.x);
} else {
// v part
y_ofs = abs(VAR1.x * tile_vec.y / tile_vec.x);
}
vec2 point_2d = vec2(VAR1.x,(tile_vec.y - VAR1.y));
// compute a pseudo 3D point for both the light and the texel
// this makes the normalmap calculation work properly
vec3 point_3d = vec3( point_2d.x, point_2d.y - y_ofs, y_ofs );
vec3 light_3d = vec3(point_2d.x-LIGHT_VEC.x,LIGHT_HEIGHT,(-(VAR1.y - (LIGHT_VEC.y))+tile_vec.y));
//this could be optimized
//a rotation matrix is created to convert the normalmap vector
//to the same coordinate space as our pseudo 3d point and light
float r = asin(tile_vec.y / tile_vec.x);
mat3 rot_mat = mat3( vec3(1,0,0), vec3(0,cos(r),-sin(r)), vec3(0,sin(r),cos(r)));
vec3 n = rot_mat * NORMAL;
n.y=-n.y;
//finally compute the dot product. Simple diffuse is computed here,
//but specular could be added and it will look prettier
float dp = max(dot(normalize(n),-normalize(light_3d-point_3d)),0);
LIGHT=vec4(vec3(dp),1.0)*COLOR*LIGHT_COLOR*3.0;
//finally, after light was cmoputed, make the light "flat"
//this makes the shadows work properly
//the +2 is added to avoid z-fighting between caster and pseudo 3d coords.
LIGHT_VEC.y+=point_3d.y+2;