-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrolling_log.gdshader.bak
85 lines (79 loc) · 2.29 KB
/
rolling_log.gdshader.bak
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
/* TODOs:
- Player as a Global uniform
- Allow "importing" of spatial shaders
*/
shader_type spatial;
// player_pos updated in Ground.gd, used to calculate distance from player
// uniform float PI = 3.14159265358979323846264;
uniform vec3 player_pos = vec3(0.0, 0.0, 0.0);
// Set "active" to false to turn off the displacement shader.
uniform bool active = true;
// Set "remap_normals" to true if you want lighting to take place after displacement
uniform bool remap_normals = true;
// Use RADIUS to determine how warped the world should be.
uniform float RADIUS = 10.0;
uniform bool hang = false;
/*void fragment() {
// Add color
ALBEDO = vec3(0.1, 0.3, 0.05);
}*/
void vertex() {
// Vertex displacement math, as a function of dist_z, dist_y
float dist_z = VERTEX.z - player_pos.z;
float dist_y = VERTEX.y;
// Dz and theta is calculated from RADIUS for use in the transform.
float Dz = PI*RADIUS/2.0;
float theta = dist_z / RADIUS;
// Calculate which "side" we're on
// 1 and -1 represent "hanging towel"
// 0 represents the "rolling log" part
int side;
if (active) {
if (dist_z > Dz) {
side = 1;
} else if (dist_z < -Dz) {
side = -1;
} else {
side = 0;
}
// set side = 0 if you want a log without "hanging sides"
if (!hang) { side = 0; }
if (side == 1) {
// positive vertical side
VERTEX.y = -(dist_z - Dz) - RADIUS;
VERTEX.z = dist_y + RADIUS;
}
if (side == -1) {
// negative vertical side
VERTEX.y = (dist_z + Dz) - RADIUS;
VERTEX.z = - (dist_y + RADIUS);
}
if (side == 0) {
// rolling log
VERTEX.y = (dist_y + RADIUS)*cos(theta) - RADIUS;
VERTEX.z = (dist_y + RADIUS)*sin(theta) ;
}
// reposition world vertices
VERTEX.z += player_pos.z;
}
// todo - recalculate normals
if (remap_normals) {
if (side == 1) {
//vec3 normal = normalize(vec3(NORMAL.x, -NORMAL.z, NORMAL.y));
vec3 normal = normalize(vec3(0.0, 1.0, 0.0));
NORMAL = normal;
} else if (side == -1) {
//vec3 normal = normalize(vec3(NORMAL.x, NORMAL.z, -NORMAL.y));
vec3 normal = normalize(vec3(0.0, 1.0, 0.0));
NORMAL = normal;
} else if (side == 0) {
// todo - check this math!
vec3 normal = normalize(vec3(
NORMAL.x,
NORMAL.y * cos(theta) - NORMAL.z * sin(theta),
NORMAL.z * cos(theta) + NORMAL.z * sin(theta)
));
NORMAL = normal;
}
}
}