-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.glsl
146 lines (117 loc) · 4.28 KB
/
transform.glsl
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
#if !defined CORE_TRANSFORM_2
#define CORE_TRANSFORM_2
uniform vec3 cameraPosition;
uniform vec3 previousCameraPosition;
uniform mat4 gbufferModelView;
uniform mat4 gbufferModelViewInverse;
uniform mat4 gbufferPreviousModelView;
uniform mat4 gbufferProjection;
uniform mat4 gbufferProjectionInverse;
uniform mat4 gbufferPreviousProjection;
float fovScale = gbufferProjection[1][1] * 0.7299270073;
vec3 toView(vec3 clippos) { // Clippos to viewpos
return unprojectPerspectiveMAD(clippos, gbufferProjectionInverse);
}
vec3 toPlayer(vec3 viewspace) { // Viewpos to Playerfeetpos
return mat3(gbufferModelViewInverse) * viewspace + gbufferModelViewInverse[3].xyz;
}
vec3 toPlayerEye(vec3 viewspace) { // Viewpos to Playereyepos
return mat3(gbufferModelViewInverse) * viewspace;
}
vec3 playerEyeToFeet(vec3 playereye) {
return playereye + gbufferModelViewInverse[3].xyz;
}
vec3 toWorld(vec3 playerpos) { // Playerfeetpos to worldpos
return playerpos + cameraPosition;
}
vec3 backToPlayer(vec3 worldpos) { // Worldpos to playerfeetpos
return worldpos - cameraPosition;
}
vec3 backToView(vec3 playerpos) { // playerfeetpos to viewpos
return mat3(gbufferModelView) * (playerpos - gbufferModelViewInverse[3].xyz);
}
vec3 eyeToView(vec3 playereye) {
return mat3(gbufferModelView) * playereye;
}
vec3 backToClip(vec3 viewpos) { // viewpos to clip pos
return projectPerspectiveMAD(viewpos, gbufferProjection);
}
vec4 backToClipW(vec3 viewpos) { // viewpos to clip pos
vec4 tmp = projectHomogeneousMAD(viewpos, gbufferProjection);
return vec4(tmp.xyz / tmp.w, tmp.w);
}
vec3 backToScreen(vec3 viewpos) { // viewpos to screen pos
return backToClip(viewpos) * 0.5 + 0.5;
}
vec3 toPrevPlayer(vec3 worldpos) { // Worldpos to previous playerfeetpos
return worldpos - previousCameraPosition;
}
vec3 toPrevView(vec3 prevplayerpos) { // previous playerfeetpos to previous viewpos
return mat3(gbufferPreviousModelView) * prevplayerpos + gbufferPreviousModelView[3].xyz;
}
vec3 eyeToPrevView(vec3 prevplayereye) { // previous playereyepos to previous viewpos
return mat3(gbufferPreviousModelView) * prevplayereye;
}
vec3 toPrevClip(vec3 prevviewpos) { // previous viewpos to previous screen pos
return projectPerspectiveMAD(prevviewpos, gbufferPreviousProjection);
}
vec4 toPrevClipW(vec3 prevviewpos) { // previous viewpos to previous screen pos
vec4 tmp = projectHomogeneousMAD(prevviewpos, gbufferPreviousProjection);
return vec4(tmp.xyz / tmp.w, tmp.w);
}
vec3 toPrevScreen(vec3 prevviewpos) { // previous viewpos to previous screen pos
return projectPerspectiveMAD(prevviewpos, gbufferPreviousProjection) * 0.5 + 0.5;
}
vec4 reprojectScreen(vec3 screenPos) {
// Project to World Space
vec3 pos = toView(screenPos * 2. - 1.);
pos = toPlayer(pos);
pos = toWorld(pos);
// Project to previous Screen Space
pos = toPrevPlayer(pos);
pos = toPrevView(pos);
vec4 clip = toPrevClipW(pos);
return vec4(clip.xyz * .5 + .5, clip.w);
}
vec3 previousReproject(vec3 clipPos) {
// Project to World Space
vec3 pos = toView(clipPos);
pos = toPlayer(pos);
pos = toWorld(pos);
// Project to previous Screen Space
pos = toPrevPlayer(pos);
pos = toPrevView(pos);
return toPrevScreen(pos);
}
vec3 previousReprojectClip(vec3 clipPos) {
// Project to World Space
vec3 pos = toView(clipPos);
pos = toPlayer(pos);
pos = toWorld(pos);
// Project to previous Screen Space
pos = toPrevPlayer(pos);
pos = toPrevView(pos);
return toPrevClip(pos);
}
vec3 reprojectTAA(vec3 screenPos) {
if (screenPos.z < 0.56) return screenPos;
// Project to World Space
vec3 pos = toView(screenPos * 2. - 1.);
pos = toPlayer(pos);
pos = toWorld(pos);
// Project to previous Screen Space
pos = toPrevPlayer(pos);
pos = toPrevView(pos);
return toPrevScreen(pos);
}
vec3 screenSpaceMovement(vec3 clipPos, vec3 weight) {
// Project to Player Space
vec3 pos = toView(clipPos);
pos = toPlayer(pos);
// Calculate World Space
pos += (cameraPosition - previousCameraPosition) * 1.;
// Project to previous Screen Space
pos = backToView(pos);
return backToScreen(pos);
}
#endif