-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathPSXRendering.shader
334 lines (280 loc) · 9.13 KB
/
PSXRendering.shader
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
"PSX rendering" by Alexander Alekseev aka TDM - 2016
License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
*/
#define PSX_MODE
#ifdef PSX_MODE
#define INT_VERTICES
vec2 RESOLUTION = vec2(320.0, 240.0);
#else
#define BILINEAR
vec2 RESOLUTION = iResolution.xy;
#endif
// math
float cross(vec2 a, vec2 b) {
return a.x * b.y - a.y * b.x;
}
vec3 barycentric(vec2 a, vec2 b, vec2 c, vec2 p) {
vec2 ab = b - a, ac = c - a, ap = p - a;
vec2 vw = vec2(cross(ap,ac),cross(ab,ap)) / cross(ab,ac);
return vec3(1.0-vw.x-vw.y, vw);
}
float quantization(float a, float b) {
return floor(a * b) / b;
}
vec2 quantization(vec2 a, float b) {
return floor(a * b) / b;
}
vec2 quantization(vec2 a, vec2 b) {
return floor(a * b) / b;
}
vec3 quantization(vec3 a, float b) {
return floor(a * b) / b;
}
float hash( vec2 p ) {
float h = dot(p,vec2(127.1,311.7));
return fract(sin(h)*43758.5453123);
}
float noise1(vec2 p) {
#ifndef BILINEAR
return hash(floor(p));
#else
vec2 i = floor(p);
vec2 f = fract(p);
vec2 tx = mix(vec2(hash(i),hash(i+vec2(0.,1.))) ,
vec2(hash(i+vec2(1.,0.)),hash(i+vec2(1.))),f.x);
return mix(tx.x,tx.y,f.y);
#endif
}
mat4 getRotMatrix(vec3 a) {
vec3 s = sin(a);
vec3 c = cos(a);
mat4 ret;
ret[0] = vec4(c.y*c.z,c.y*s.z,-s.y,0.0);
ret[1] = vec4(s.x*s.y*c.z-c.x*s.z,s.x*s.y*s.z+c.x*c.z,s.x*c.y,0.0);
ret[2] = vec4(c.x*s.y*c.z+s.x*s.z, c.x*s.y*s.z-s.x*c.z, c.x*c.y,0.0);
ret[3] = vec4(0.0,0.0,0.0,1.0);
return ret;
}
mat4 getPosMatrix(vec3 p) {
mat4 ret;
ret[0] = vec4(1.0,0.0,0.0,p.x);
ret[1] = vec4(0.0,1.0,0.0,p.y);
ret[2] = vec4(0.0,0.0,1.0,p.z);
ret[3] = vec4(0.0,0.0,0.0,1.0);
return ret;
}
// textures
vec4 textureGround(vec2 uv) {
const vec2 RES = vec2(8.0, 8.0);
float n = noise1(uv * RES);
n = n * 0.2 + 0.5;
return vec4(n*0.9,n*0.6,n*0.4,1.0);
}
vec4 textureWall(vec2 uv) {
const vec2 RES = vec2(32.0, 16.0);
vec2 iuv = floor(uv * RES);
float n = noise1(uv * RES);
n = n * 0.5 + 0.25;
float nc = n * (smoothstep(1.0,0.4, iuv.x / RES.x) * 0.5 + 0.5);
return vec4(nc * 0.4, nc * 1.0, nc * 0.5, n + uv.x - abs(uv.y-0.5) );
}
vec4 textureBox(vec2 uv) {
const vec2 RES = vec2(8.0, 8.0);
vec2 iuv = (floor(uv * RES) + 0.5) / RES;
float n = noise1(uv * RES);
n = max(abs(iuv.x - 0.5), abs(iuv.y - 0.5)) * 2.0;
n = n * n;
n = 0.5 + n * 0.4 + noise1(uv * RES) * 0.1;
return vec4(n, n*0.8, n*0.5, 1.0);
}
vec4 textureSky(vec2 uv) {
const vec2 RES = vec2(8.0, 32.0);
float n = noise1(uv * RES);
n = n * 0.05 + 0.8;
return vec4(0.5,n*1.0,n*1.1,1.0);
}
// rasterization
void triangle(vec2 p,
vec2 v0, vec2 v1, vec2 v2,
vec2 uv0, vec2 uv1, vec2 uv2,
in int tex, inout vec3 color) {
if(cross(v2-v0,v1-v0) <= 1e-4) return;
vec3 bary = abs(barycentric(v0,v1,v2, p));
if(bary.x + bary.y + bary.z <= 1.0001) {
vec2 uv = uv0 * bary.x + uv1 * bary.y + uv2 * bary.z;
vec4 frag;
if(tex == 0) {
frag = textureGround(uv);
} else if(tex == 1) {
frag = textureWall(uv);
} else {
frag = textureBox(uv);
}
if(frag.w > 0.5) color = frag.xyz;
}
}
void quad(vec2 p,
vec2 v0, vec2 v1, vec2 v2, vec2 v3,
vec2 uv0, vec2 uv1, vec2 uv2, vec2 uv3,
in int tex, inout vec3 color) {
triangle(p, v0,v1,v2, uv0,uv1,uv2, tex,color);
triangle(p, v2,v3,v0, uv2,uv3,uv0, tex,color);
}
// geometry transformation engine
void gte(inout vec3 v, mat4 mat) {
// perspective
v = (vec4(v,1.0) * mat).xyz;
v.xy /= max(v.z, 1.0);
v *= 2.0;
// quantization to simulate int
#ifdef INT_VERTICES
const vec2 QUANT = vec2(320.0, 240.0) * 0.25;
v.xy = quantization(v.xy, QUANT);
#endif
}
// renderer
void gpu(vec2 p,
vec2 v0, vec2 v1, vec2 v2, vec2 v3,
vec2 uv0, vec2 uv1, vec2 uv2, vec2 uv3,
in int tex, inout vec3 color) {
quad(p,
v0,v1,v2,v3,
uv0,uv1,uv2,uv3,
tex,color);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord ) {
vec2 uv = fragCoord.xy / iResolution.xy;
uv = uv * 2.0 - 1.0;
uv.x *= iResolution.x / iResolution.y;
uv = quantization(uv, RESOLUTION * 0.5);
const float WIDTH = 1.3;
const float HEIGHT = 6.0;
const float DEPTH = 4.0;
const float LOD = 4.0;
float time = iGlobalTime * 2.;
vec3 posv = vec3(-WIDTH*3.0,-2.0, -time+5.0);
vec3 rotv = vec3(sin(time)*0.05 + 0.1,
sin(time*0.9)*0.05,
sin(time*0.7)*0.05);
// int-like position
#ifdef INT_VERTICES
posv = quantization(posv, 64.0);
rotv = quantization(rotv, 256.0);
#endif
mat4 cam = getPosMatrix(posv);
mat4 rot = getRotMatrix(rotv);
cam = cam * rot;
vec3 c = textureSky(uv + vec2(rotv.y,-rotv.x) * 3.0).xyz;
// ground
float z_offset = -floor((posv.z + DEPTH * 1.5) / DEPTH) * DEPTH * 0.5;
float poly_depth = DEPTH;
for(int dip = 0; dip < 32; dip++) {
// kinda LOD
z_offset += step(mod(float(dip),4.0), 0.5) * poly_depth * 0.5;
#ifdef PSX_MODE
if(dip > 11) poly_depth = DEPTH * LOD;
#endif
vec3 vert[4];
vert[0] = vec3(-WIDTH,0.0, poly_depth);
vert[1] = vec3(-WIDTH,0.0, 0.0);
vert[2] = vec3( WIDTH,0.0, 0.0);
vert[3] = vec3( WIDTH,0.0, poly_depth);
vec3 posv = vec3(mod(float(dip),4.0) * WIDTH,
0.0,
z_offset);
mat4 pos = getPosMatrix(posv * 2.0);
mat4 mat = pos * cam;
for(int i = 0; i < 4; i++) gte(vert[i], mat);
gpu(uv,
vert[3].xy,vert[2].xy,vert[1].xy,vert[0].xy,
vec2(0.0),vec2(0.0,1.0),vec2(1.0),vec2(1.0,0.0),
0, c);
}
// walls
z_offset = -floor((posv.z + DEPTH ) / DEPTH) * DEPTH * 0.5;
poly_depth = DEPTH;
for(int dip = 0; dip < 8; dip++) {
// kinda LOD
z_offset += poly_depth * 0.5;
#ifdef PSX_MODE
if(dip > 2) poly_depth = DEPTH * LOD;
#endif
vec3 vert[4];
vert[0] = vec3(0.0,HEIGHT, poly_depth);
vert[1] = vec3(0.0,HEIGHT, 0.0);
vert[2] = vec3(0.0,0.0, 0.0);
vert[3] = vec3(0.0,0.0, poly_depth);
vec3 posv = vec3(WIDTH * 3.5,
0.0,
z_offset);
//posv.z -= z_offset;
mat4 posm = getPosMatrix(posv * 2.0);
mat4 mat = posm * cam;
for(int i = 0; i < 4; i++) gte(vert[i], mat);
gpu(uv,
vert[0].xy,vert[1].xy,vert[2].xy,vert[3].xy,
vec2(0.0),vec2(0.0,1.0),vec2(1.0),vec2(1.0,0.0),
1, c);
vert[0] = vec3(0.0,HEIGHT, poly_depth);
vert[1] = vec3(0.0,HEIGHT, 0.0);
vert[2] = vec3( 0.0,0.0, 0.0);
vert[3] = vec3( 0.0,0.0, poly_depth);
posv = vec3(-WIDTH * 0.5,
0.0,
z_offset);
posm = getPosMatrix(posv * 2.0);
mat = posm * cam;
for(int i = 0; i < 4; i++) gte(vert[i], mat);
gpu(uv,
vert[3].xy,vert[2].xy,vert[1].xy,vert[0].xy,
vec2(1.0,0.0), vec2(1.0), vec2(0.0,1.0),vec2(0.0),
1, c);
}
// box
vec3 vert[8];
vert[0] = vec3(-1.0,-1.0, 1.0);
vert[1] = vec3(-1.0, 1.0, 1.0);
vert[2] = vec3( 1.0, 1.0, 1.0);
vert[3] = vec3( 1.0,-1.0, 1.0);
vert[4] = vec3(-1.0,-1.0,-1.0);
vert[5] = vec3(-1.0, 1.0,-1.0);
vert[6] = vec3( 1.0, 1.0,-1.0);
vert[7] = vec3( 1.0,-1.0,-1.0);
vec3 box_posv = vec3(-posv.x,
2.0,
-posv.z + 15.0);
rotv = vec3(time * 0.5, time * 0.6, time * 0.7);
mat4 posm = getRotMatrix(rotv) * getPosMatrix(box_posv);
mat4 mat = posm * cam;
for(int i = 0; i < 8; i++) {
vert[i].y *= 0.65;
gte(vert[i], mat);
}
gpu(uv,
vert[3].xy,vert[2].xy,vert[1].xy,vert[0].xy,
vec2(0.0),vec2(0.0,1.0),vec2(1.0),vec2(1.0,0.0),
2, c);
gpu(uv,
vert[4].xy,vert[5].xy,vert[6].xy,vert[7].xy,
vec2(0.0),vec2(0.0,1.0),vec2(1.0),vec2(1.0,0.0),
2, c);
gpu(uv,
vert[7].xy,vert[6].xy,vert[2].xy,vert[3].xy,
vec2(0.0),vec2(0.0,1.0),vec2(1.0),vec2(1.0,0.0),
2, c);
gpu(uv,
vert[0].xy,vert[1].xy,vert[5].xy,vert[4].xy,
vec2(0.0),vec2(0.0,1.0),vec2(1.0),vec2(1.0,0.0),
2, c);
gpu(uv,
vert[2].xy,vert[6].xy,vert[5].xy,vert[1].xy,
vec2(0.0),vec2(0.0,1.0),vec2(1.0),vec2(1.0,0.0),
2, c);
gpu(uv,
vert[0].xy,vert[4].xy,vert[7].xy,vert[3].xy,
vec2(0.0),vec2(0.0,1.0),vec2(1.0),vec2(1.0,0.0),
2, c);
// fragment
fragColor = vec4(c,1.0);
}