-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.js
129 lines (104 loc) · 6.34 KB
/
render.js
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
// render.js
// Contains functions for Game World Creation and Rendering
import { calculateDistance} from './utils.js';
import {updateFloatingPlatformPosition, getHeightAtPosition} from './groundMechanics.js';
import * as renderUtils from './renderUtils.js';
import { groundPolygons, drawGroundSegments, drawFloatingPlatform, placeObj} from './levelBuilder.js';
export function renderScene(canvas, fovSlider, base, grid, cube, bullets, gun, ego, pitch, yaw, dy, keysPressed, platform, platform_grid, playerHitbox, groundY, mainCurveSegments, leftRailSegments, rightRailSegments, absGround) {
const context = canvas.getContext('2d');
if (!context) {
console.error('Failed to get canvas context!');
return;
}
if (ego.z < 0) {
groundY = getHeightAtPosition(ego.x, ego.z, ego.y+1900, absGround);
console.log(`groundY: ${groundY}`);
if (isNaN(groundY)) {
} else {
ego.y = groundY - 1900;
}
}
// console.warn(`groundY: ${groundY}`)
if (groundY - (ego.y + 1900) > 0) {
ego.onGround = false;
console.log(`Player above ground : ${groundY - (ego.y + 1900)} mm`);
}
else if (groundY - (ego.y + 1900) <= 0) {
ego.onGround = true;
}
if (ego.velocityY >= 0) {
// console.log("might be in free fall");
ego.isJumping = false;
ego.isFreeFalling = true;
// console.log(`onGround: ${ego.onGround}`);
}
groundPolygons.length = 0;
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillStyle = 'black';
context.fillRect(0, 0, canvas.width, canvas.height);
const cam2scrn = calculateDistance(fovSlider.value);
drawGroundSegments(base, grid, ego, canvas, fovSlider, pitch, yaw, dy, 0 ,-19000, 0);
drawGroundSegments(base, grid, ego, canvas, fovSlider, pitch, yaw, dy, 0 ,-19000, -4000, 0, {angle: 0, axis:[0, 0, 0]});
drawGroundSegments(base, grid, ego, canvas, fovSlider, pitch, yaw, dy, -38000 ,-56000, 18000);
const segments = 100;
context.save();
leftRailSegments.forEach(segment => renderUtils.drawHermiteCurve(segment, segments, ego, fovSlider, canvas, pitch, yaw, context));
rightRailSegments.forEach(segment => renderUtils.drawHermiteCurve(segment, segments, ego, fovSlider, canvas, pitch, yaw, context));
context.restore();
const platformInfo = updateFloatingPlatformPosition(mainCurveSegments);
drawFloatingPlatform(platform, platform_grid, ego, canvas, fovSlider, pitch, yaw, dy, platformInfo);
if (keysPressed['v']) {
mainCurveSegments.forEach(segment => renderUtils.drawHermiteCurve(segment, segments, ego, fovSlider, canvas, pitch, yaw, context, '4', 'yellow'));
}
placeObj(cube, {angle: 0, axis:[0, 1, 0]}, {x: 18000, y: -2000, z: -53000}, ego, fovSlider, canvas, pitch, yaw, "cyan", false, 1, 0);
placeObj(cube, {angle: 0, axis:[0, 1, 0]}, {x: 0, y: -2000, z: 0}, ego, fovSlider, canvas, pitch, yaw, "red", false, 1, 0);
drawGroundSegments(base, grid, ego, canvas, fovSlider, pitch, yaw, dy, -15000, -19000, 4000, 0);
drawGroundSegments(base, grid, ego, canvas, fovSlider, pitch, yaw, dy, 0, -2000, 4000, 0);
drawGroundSegments(base, grid, ego, canvas, fovSlider, pitch, yaw, dy, -5000, -5900, 4000, 1000, {angle: 3.14/2, axis:[1, 0, 0]});
drawGroundSegments(base, grid, ego, canvas, fovSlider, pitch, yaw, dy, -3000, -5000, 4000, -1000);
drawGroundSegments(base, grid, ego, canvas, fovSlider, pitch, yaw, dy, -8000, -8900, 4000, 0, {angle: 3.14/2, axis:[1, 0, 0]});
drawGroundSegments(base, grid, ego, canvas, fovSlider, pitch, yaw, dy, -6000, -8000, 4000, -2000);
drawGroundSegments(base, grid, ego, canvas, fovSlider, pitch, yaw, dy, -12000, -14000, 4000, -4000);
drawGroundSegments(base, grid, ego, canvas, fovSlider, pitch, yaw, dy, -14000, -14900, 4000, -2000, {angle: 3.14/2, axis:[1, 0, 0]});
drawGroundSegments(base, grid, ego, canvas, fovSlider, pitch, yaw, dy, -9000, -11000, 4000, -3000);
drawGroundSegments(base, grid, ego, canvas, fovSlider, pitch, yaw, dy, -11000, -11900, 4000, -1000, {angle: 3.14/2, axis:[1, 0, 0]});
bullets.forEach((bullet) => {
const translatedBullet = bullet.shape.map(point => {
let transformedPoint = vec3.fromValues(point.x, point.y, point.z);
vec3.add(transformedPoint, transformedPoint, [bullet.position.x, bullet.position.y, bullet.position.z]);
return {
x: transformedPoint[0],
y: transformedPoint[1],
z: transformedPoint[2]
};
});
const projectedBullet = translatedBullet.map(corner => renderUtils.projectPoint(corner, ego, fovSlider, canvas, pitch, yaw)).filter(point => point !== null);
if (projectedBullet.length > 0) {
renderUtils.drawObj(projectedBullet, "white", canvas, false, 2);
}
});
const transformedGun = gun.map(point => {
let transformedPoint = vec4.fromValues(point.x, point.y, point.z, 1);
let translationToPlayer = mat4.create();
mat4.translate(translationToPlayer, translationToPlayer, [ego.x + 10, ego.y + 300, ego.z - 600]);
vec4.transformMat4(transformedPoint, transformedPoint, translationToPlayer);
let translateToOrigin = mat4.create();
mat4.translate(translateToOrigin, translateToOrigin, [-ego.x, -ego.y, -ego.z]);
vec4.transformMat4(transformedPoint, transformedPoint, translateToOrigin);
let combinedRotationMatrix = mat4.create();
mat4.rotateY(combinedRotationMatrix, combinedRotationMatrix, -yaw);
mat4.rotateX(combinedRotationMatrix, combinedRotationMatrix, -pitch);
vec4.transformMat4(transformedPoint, transformedPoint, combinedRotationMatrix);
let translateBack = mat4.create();
mat4.translate(translateBack, translateBack, [ego.x, ego.y, ego.z]);
vec4.transformMat4(transformedPoint, transformedPoint, translateBack);
return {
x: transformedPoint[0],
y: transformedPoint[1],
z: transformedPoint[2]
};
});
const projectedGun = transformedGun.map(corner => renderUtils.projectPoint(corner, ego, fovSlider, canvas, pitch, yaw)).filter(point => point !== null);
renderUtils.drawObj(projectedGun, "green", canvas, false, 2);
renderUtils.drawAimPoint(canvas);
}