-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.lua
More file actions
596 lines (506 loc) · 18.7 KB
/
engine.lua
File metadata and controls
596 lines (506 loc) · 18.7 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
-- Super Simple 3D Engine v1
-- groverburger 2019
-- https://raw.githubusercontent.com/groverburger/ss3d/master/engine.lua
cpml = require "cpml"
local engine = {}
-- create a new Model object
-- given a table of verts for example: { {0,0,0}, {0,1,0}, {0,0,1} }
-- each vert is its own table that contains three coordinate numbers, and may contain 2 extra numbers as uv coordinates
-- another example, this with uvs: { {0,0,0, 0,0}, {0,1,0, 1,0}, {0,0,1, 0,1} }
-- polygons are automatically created with three consecutive verts
function engine.newModel(verts, texture, coords, color, format, scale)
local m = {}
-- default values if no arguments are given
if coords == nil then
coords = {0,0,0}
end
if scale == nil then
scale = 1.0
end
if color == nil then
color = {1,1,1}
end
if format == nil then
format = {
{"VertexPosition", "float", 3},
{"VertexTexCoord", "float", 2},
{"VertexNormal", "float", 3},
}
end
if texture == nil then
texture = love.graphics.newCanvas(1,1)
love.graphics.setCanvas(texture)
love.graphics.clear(unpack(color))
love.graphics.setCanvas()
end
if verts == nil then
verts = {}
end
-- translate verts by given coords
for i=1, #verts do
if coords[1] ~= 0.0 or coords[2] ~= 0.0 or coords[3] ~= 0.0 or scale ~= 1.0 then
local newVert = {}
newVert[1] = (verts[i][1] + coords[1]) * scale
newVert[2] = (verts[i][2] + coords[2]) * scale
newVert[3] = (verts[i][3] + coords[3]) * scale
newVert[4] = verts[i][4]
newVert[5] = verts[i][5]
verts[i] = newVert
end
-- if not given uv coordinates, put in random ones
if #verts[i] < 5 then
verts[i][4] = love.math.random()
verts[i][5] = love.math.random()
end
-- if not given normals, figure it out
if #verts[i] < 8 then
local polyindex = math.floor((i-1)/3)
local polyfirst = polyindex*3 +1
local polysecond = polyindex*3 +2
local polythird = polyindex*3 +3
local sn1 = {}
sn1[1] = verts[polythird][1] - verts[polysecond][1]
sn1[2] = verts[polythird][2] - verts[polysecond][2]
sn1[3] = verts[polythird][3] - verts[polysecond][3]
local sn2 = {}
sn2[1] = verts[polysecond][1] - verts[polyfirst][1]
sn2[2] = verts[polysecond][2] - verts[polyfirst][2]
sn2[3] = verts[polysecond][3] - verts[polyfirst][3]
local cross = UnitVectorOf(CrossProduct(sn1,sn2))
verts[i][6] = cross[1]
verts[i][7] = cross[2]
verts[i][8] = cross[3]
end
end
-- define the Model object's properties
m.mesh = nil
if #verts > 0 then
m.mesh = love.graphics.newMesh(format, verts, "triangles")
m.mesh:setTexture(texture)
end
m.texture = texture
m.format = format
m.verts = verts
m.transform = TransposeMatrix(cpml.mat4.identity())
m.color = color
m.visible = true
m.dead = false
m.wireframe = false
m.culling = false
m.timeOffset = math.random(0, 10)
m.setVerts = function (self, verts)
if #verts > 0 then
self.mesh = love.graphics.newMesh(self.format, verts, "triangles")
self.mesh:setTexture(self.texture)
end
self.verts = verts
end
-- translate and rotate the Model
m.setTransform = function (self, coords, rotations)
if angle == nil then
angle = 0
axis = cpml.vec3.unit_y
end
self.transform = cpml.mat4.identity()
self.transform:translate(self.transform, cpml.vec3(unpack(coords)))
if rotations ~= nil then
for i=1, #rotations, 2 do
self.transform:rotate(self.transform, rotations[i],rotations[i+1])
end
end
self.transform = TransposeMatrix(self.transform)
end
-- returns a list of the verts this Model contains
m.getVerts = function (self)
local ret = {}
for i=1, #self.verts do
ret[#ret+1] = {self.verts[i][1], self.verts[i][2], self.verts[i][3]}
end
return ret
end
-- prints a list of the verts this Model contains
m.printVerts = function (self)
local verts = self:getVerts()
for i=1, #verts do
print(verts[i][1], verts[i][2], verts[i][3])
if i%3 == 0 then
print("---")
end
end
end
-- set a texture to this Model
m.setTexture = function (self, tex)
self.mesh:setTexture(tex)
end
-- check if this Model must be destroyed
-- (called by the parent Scene model's update function automatically)
m.deathQuery = function (self)
return not self.dead
end
return m
end
-- create a new Scene object with given canvas output size
function engine.newScene(renderWidth, renderHeight)
love.graphics.setDepthMode("lequal", true)
local scene = {}
local particleVerts = {}
for i = 0, 100000 do
table.insert(particleVerts, {
math.random() * 100 - 50,
math.random() * 1 - 100,
math.random() * 100 - 50,
})
end
scene.particles = love.graphics.newMesh({
{"VertexPosition", "float", 3},
}, particleVerts, "points")
local explosionParticleVerts = {}
for i = 0, 100000 do
table.insert(explosionParticleVerts, {
0.0,
-10.0,
0.0,
0.0,
-10.0,
0.0,
0.0,
})
end
scene.explosionParticles = love.graphics.newMesh({
{"VertexPosition", "float", 3},
{"endPosition", "float", 3},
{"startTime", "float", 1},
{"explosionSize", "float", 1}
}, explosionParticleVerts, "points")
scene.explosionParticles:setTexture(love.graphics.newImage("assets/particle.png"))
scene.currentExplosionIdx = 1
SPREAD = 0.05
scene.shoot = function (self, x, y, z, targetX, targetY, targetZ)
for i = 1, 10 do
local rx = (math.random() - 0.5)
local ry = (math.random() - 0.5)
local rz = (math.random() - 0.5)
explosionParticleVerts[scene.currentExplosionIdx] = {
x + rx * SPREAD,
y + ry * SPREAD,
z + rz * SPREAD,
targetX + rx * SPREAD,
targetY + ry * SPREAD,
targetZ + rz * SPREAD,
TimeElapsed + math.random() * 0.2,
1.0,
}
scene.currentExplosionIdx = scene.currentExplosionIdx + 1
if scene.currentExplosionIdx >= 100000 then
scene.currentExplosionIdx = 1
end
end
scene.explosionParticles:setVertices(explosionParticleVerts)
end
-- define the shaders used in rendering the scene
scene.threeShader = love.graphics.newShader[[
uniform highp mat4 view;
uniform highp mat4 model_matrix;
uniform highp mat4 model_matrix_inverse;
uniform highp vec3 light_pos;
uniform highp vec3 view_pos;
uniform highp float time_elapsed;
varying highp vec3 frag_pos;
varying highp vec3 normal;
#ifdef VERTEX
attribute highp vec4 VertexNormal;
vec4 position(mat4 transform_projection, vec4 vertex_position) {
normal = vec3(model_matrix_inverse * vec4(VertexNormal));
vec4 p = vertex_position;
vec4 result = view * model_matrix * p;
frag_pos = vec3(model_matrix * p);
return result;
}
#endif
#ifdef PIXEL
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords) {
vec2 coords = texture_coords;
// if ghost
if (length(normal) > 9.0) {
coords.y *= 1.2;
coords.y += cos(coords.y + time_elapsed * 3.0) * 0.1 - 0.1;
}
vec4 texturecolor = Texel(texture, coords);
// if the alpha here is close to zero just don't draw anything here
if (texturecolor.a == 0.0)
{
discard;
}
if (length(normal) > 0.0 && length(normal) < 10.0) {
float ambientStrength = 1.2;
float diffuseStrength = 2.0;
float specularStrength = 1.8;
// the ground
if (normal.y > 0.8) {
ambientStrength = 0.3;
}
vec3 norm = normalize(normal);
vec3 lightDir = normalize(light_pos - frag_pos);
float diffuse = max(dot(norm, lightDir), 0.0);
vec3 viewDir = normalize(view_pos - frag_pos);
vec3 reflectDir = reflect(-lightDir, normal);
float specular = 0.5 * pow(max(dot(viewDir, reflectDir), 0.0), 32.0);
texturecolor.rgb *= ambientStrength + diffuse * diffuseStrength + specular * specularStrength;
} else if (length(normal) < 10.0) {
texturecolor.rgb *= vec3(1.8, 1.0, 1.0);
}
// if ghost
float alpha = 1.0;
if (length(normal) > 9.0) {
alpha = (cos(time_elapsed) + 1.0) * 0.2 + 0.4;
}
float fogDist = length(frag_pos - view_pos);
float fogAmount = 0.0;
if (fogDist > 0.5) {
fogAmount = (fogDist - 0.5) / 3.0;
}
if (fogAmount > 0.8) {
fogAmount = 0.8;
}
if (length(normal) == 0.0) {
fogAmount = 0.0;
}
vec4 result = (1.0 - fogAmount) * (color * texturecolor) + fogAmount * vec4(0.0, 0.0, 0.0, 1.0);
result.a = alpha;
return result;
}
#endif
]]
scene.explosionShader = love.graphics.newShader[[
uniform highp mat4 view;
uniform highp float time;
uniform highp vec3 cameraPos;
varying highp float dist;
varying highp float percentDone;
varying highp float explosionSizeV;
#ifdef VERTEX
attribute highp vec3 endPosition;
attribute highp float startTime;
attribute highp float explosionSize;
vec4 position(mat4 transform_projection, vec4 vertex_position) {
if (time - startTime > 0.5) {
return vec4(0.0, -1000.0, 0.0, 1.0);
}
float percent = (time - startTime) / 0.5;
vec4 vec = vec4(endPosition.x, endPosition.y, endPosition.z, 1.0) - vertex_position;
vec4 pos = vertex_position + vec * percent;
dist = length(vec3(pos.x, pos.y, pos.z) - cameraPos);
percentDone = percent;
explosionSizeV = explosionSize;
vec4 result = view * pos;
return result;
}
#endif
#ifdef PIXEL
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords) {
vec2 coord = gl_PointCoord - vec2(0.5);
float radius = 0.5;
radius = explosionSizeV * radius / dist;
if (radius > 0.5) {
radius = 0.5;
}
if(length(coord) > radius)
discard;
float percentX = ((coord.x / radius) + 1.0) / 2.0;
float percentY = ((coord.y / radius) + 1.0) / 2.0;
vec4 result = Texel(texture, vec2(percentX, percentY));
if (result.a < 0.01 || result.r + result.g + result.b < 0.3) {
discard;
}
result.a = 0.5;
return result;
}
#endif
]]
scene.renderWidth = renderWidth
scene.renderHeight = renderHeight
-- create a canvas that will store the rendered 3d scene
scene.threeCanvas = love.graphics.newCanvas(renderWidth, renderHeight)
-- create a canvas that will store a 2d layer that can be drawn on top of the 3d scene
scene.twoCanvas = love.graphics.newCanvas(renderWidth, renderHeight)
scene.modelList = {}
engine.camera = {
pos = cpml.vec3(2.5, 0.3, -3.5),
angle = cpml.vec3(0, 0, 0),
perspective = TransposeMatrix(cpml.mat4.from_perspective(60, renderWidth/renderHeight, 0.1, 10000)),
transform = cpml.mat4(),
}
-- camera.perspective = TransposeMatrix(cpml.mat4.from_perspective(90, love.graphics.getWidth()/love.graphics.getHeight(), 0.001, 10000))
scene.lightPos = {100,10,20}
-- should be called in love.update every frame
scene.update = function (self)
local i = 1
while i<=#(self.modelList) do
local thing = self.modelList[i]
if thing:deathQuery() then
i=i+1
else
table.remove(self.modelList, i)
end
end
end
-- renders the models in the scene to the threeCanvas
-- will draw threeCanvas if drawArg is not given or is true (use if you want to scale the game canvas to window)
scene.render = function (self, drawArg, timeElapsed)
love.graphics.clear(0,0,0,0)
love.graphics.setColor(1,1,1)
love.graphics.setCanvas({self.threeCanvas, depth=true})
love.graphics.clear(0,0,0,0)
love.graphics.setShader(self.threeShader)
local Camera = engine.camera
Camera.transform = cpml.mat4()
local t, a = Camera.transform, Camera.angle
local p = {}
p.x = Camera.pos.x
p.y = Camera.pos.y
p.z = Camera.pos.z
p.x = p.x * -1
p.y = p.y * -1
p.z = p.z * -1
t:rotate(t, a.y, cpml.vec3.unit_x)
t:rotate(t, a.x, cpml.vec3.unit_y)
t:rotate(t, a.z, cpml.vec3.unit_z)
t:translate(t, p)
self.threeShader:send("view", Camera.perspective * TransposeMatrix(Camera.transform))
self.threeShader:send("light_pos", self.lightPos)
self.threeShader:send("view_pos", {Camera.pos.x, Camera.pos.y, Camera.pos.z})
for i=1, #self.modelList do
local model = self.modelList[i]
if model ~= nil and model.visible and #model.verts > 0 then
self.threeShader:send("model_matrix", model.transform)
self.threeShader:send("model_matrix_inverse", TransposeMatrix(InvertMatrix(model.transform)))
local time = TimeElapsed
if model.timeOffset then
time = time + model.timeOffset
end
self.threeShader:send("time_elapsed", time)
love.graphics.setWireframe(model.wireframe)
if model.culling then
love.graphics.setMeshCullMode("back")
end
love.graphics.draw(model.mesh, -self.renderWidth/2, -self.renderHeight/2)
love.graphics.setMeshCullMode("none")
love.graphics.setWireframe(false)
end
end
love.graphics.setShader(self.explosionShader)
self.explosionShader:send("time", TimeElapsed)
self.explosionShader:send("view", Camera.perspective * TransposeMatrix(Camera.transform))
self.explosionShader:send("cameraPos", {Camera.pos.x, Camera.pos.y, Camera.pos.z})
love.graphics.setPointSize(200)
love.graphics.draw(self.explosionParticles, -self.renderWidth/2, -self.renderHeight/2)
love.graphics.setCanvas()
love.graphics.setShader()
if drawArg == nil or drawArg == true then
love.graphics.draw(self.threeCanvas, self.renderWidth/2,self.renderHeight/2, 0, 1, -1, self.renderWidth/2-OffsetX, self.renderHeight/2 - OffsetY)
end
end
-- renders the given func to the twoCanvas
-- this is useful for drawing 2d HUDS and information on the screen in front of the 3d scene
-- will draw threeCanvas if drawArg is not given or is true (use if you want to scale the game canvas to window)
scene.renderFunction = function (self, func, drawArg)
love.graphics.setColor(1,1,1)
love.graphics.setCanvas(Scene.twoCanvas)
love.graphics.clear(0,0,0,0)
func()
love.graphics.setCanvas()
local flip = 1
--if shouldSwitchScreen() then
-- flip = -1
--
if drawArg == nil or drawArg == true then
love.graphics.draw(Scene.twoCanvas, self.renderWidth/2,self.renderHeight/2, 0, 1,flip, self.renderWidth/2 - OffsetX, self.renderHeight/2 - OffsetY)
end
end
-- useful if mouse relativeMode is enabled
-- useful to call from love.mousemoved
-- a simple first person mouse look function
scene.mouseLook = function (self, x, y, dx, dy)
local Camera = engine.camera
Camera.angle.x = Camera.angle.x + math.rad(dx * 0.5)
Camera.angle.y = math.max(math.min(Camera.angle.y + math.rad(dy * 0.5), math.pi/2), -1*math.pi/2)
end
return scene
end
-- useful functions
function TransposeMatrix(mat)
local m = cpml.mat4.new()
return cpml.mat4.transpose(m, mat)
end
function InvertMatrix(mat)
local m = cpml.mat4.new()
return cpml.mat4.invert(m, mat)
end
function CopyTable(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[CopyTable(orig_key)] = CopyTable(orig_value)
end
setmetatable(copy, CopyTable(getmetatable(orig)))
else
copy = orig
end
return copy
end
function GetSign(n)
if n > 0 then return 1 end
if n < 0 then return -1 end
return 0
end
function CrossProduct(v1,v2)
local a = {x = v1[1], y = v1[2], z = v1[3]}
local b = {x = v2[1], y = v2[2], z = v2[3]}
local x, y, z
x = a.y * (b.z or 0) - (a.z or 0) * b.y
y = (a.z or 0) * b.x - a.x * (b.z or 0)
z = a.x * b.y - a.y * b.x
return { x, y, z }
end
function UnitVectorOf(vector)
local ab1 = math.abs(vector[1])
local ab2 = math.abs(vector[2])
local ab3 = math.abs(vector[3])
local max = VectorLength(ab1, ab2, ab3)
if max == 0 then max = 1 end
local ret = {vector[1]/max, vector[2]/max, vector[3]/max}
return ret
end
function VectorLength(x2,y2,z2)
local x1,y1,z1 = 0,0,0
return ((x2-x1)^2+(y2-y1)^2+(z2-z1)^2)^0.5
end
function ScaleVerts(verts, sx,sy,sz)
if sy == nil then
sy = sx
sz = sx
end
for i=1, #verts do
local this = verts[i]
this[1] = this[1]*sx
this[2] = this[2]*sy
this[3] = this[3]*sz
end
return verts
end
function MoveVerts(verts, sx,sy,sz)
if sy == nil then
sy = sx
sz = sx
end
for i=1, #verts do
local this = verts[i]
this[1] = this[1]+sx
this[2] = this[2]+sy
this[3] = this[3]+sz
end
return verts
end
return engine