Code edit (5 edits merged)
Please save this source code
User prompt
Make a translateTriangle function and centre the cube with the translateTriangle function.
Code edit (2 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading '0')' in or related to this line: 'var p1 = project(triangle[0], triangle[1], triangle[2]);' Line Number: 144
User prompt
In the projectTriangle function, use perspective project on the 3 points array in the triangle parameter, and return a 2 point array to draw.
Code edit (1 edits merged)
Please save this source code
Code edit (5 edits merged)
Please save this source code
User prompt
make an arg for wireframes on the drawTri function, default false.
Code edit (2 edits merged)
Please save this source code
User prompt
In the drawTri function, fill in the triangles using the scanline rendering algorithm.
User prompt
Fill in the triangles using the scanline rendering algorithm.
Code edit (2 edits merged)
Please save this source code
User prompt
Create a class "Cube" and store the triangles for now.
Code edit (1 edits merged)
Please save this source code
User prompt
optimise the code for speed
Code edit (2 edits merged)
Please save this source code
User prompt
Draw a 1000 random lines in a loop in between the start valuable and the console log
Code edit (1 edits merged)
Please save this source code
User prompt
Please fix the bug: 'toString is not a function' in or related to this line: 'console.log("Time: " + toString(Date.now() - start));' Line Number: 82
Code edit (1 edits merged)
Please save this source code
User prompt
Draw the triangles in the tris.forEach function
Code edit (3 edits merged)
Please save this source code
User prompt
Make a line drawing function using the putPixel function
Code edit (1 edits merged)
Please save this source code
User prompt
Please fix the bug: 'tween.to is not a function' in or related to this line: 'tween.to(self, {' Line Number: 36 ↪💡 Consider importing and using the following plugins: @upit/tween.v1
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0
});
/****
* Game Code
****/
// Game constants for the grid
var GRID_SIZE = 64;
var PIXEL_SIZE = 2048 / GRID_SIZE;
// Cache children array reference
var screen = game.children;
function putPixel(x, y, color) {
if (x >= 0 && x < GRID_SIZE && y >= 0 && y < GRID_SIZE) {
screen[x + y * GRID_SIZE].tint = color;
}
}
// Line drawing function using Bresenham's line algorithm
function drawLine(x0, y0, x1, y1, color) {
var dx = x1 - x0;
var dy = y1 - y0;
var adx = dx < 0 ? -dx : dx;
var ady = dy < 0 ? -dy : dy;
var sx = dx < 0 ? -1 : 1;
var sy = dy < 0 ? -1 : 1;
var err = adx - ady;
var x = x0;
var y = y0;
while (true) {
putPixel(x, y, color);
if (x === x1 && y === y1) {
break;
}
var e2 = 2 * err;
if (e2 > -ady) {
err -= ady;
x += sx;
}
if (e2 < adx) {
err += adx;
y += sy;
}
}
}
function drawTriangle(triangle, color, wireframe) {
if (wireframe === undefined) {
wireframe = false;
}
// Extract triangle vertices
var x1 = triangle[0];
var y1 = triangle[1];
var x2 = triangle[2];
var y2 = triangle[3];
var x3 = triangle[4];
var y3 = triangle[5];
// Draw the three sides of the triangle only if wireframe is true
if (wireframe) {
drawLine(x1, y1, x2, y2, color); // Side 1-2
drawLine(x2, y2, x3, y3, color); // Side 2-3
drawLine(x3, y3, x1, y1, color); // Side 3-1
} else {
// Helper to interpolate x between two points at a given y
var interpX = function interpX(y, x0, y0, x1, y1) {
if (y1 === y0) {
return x0;
}
return x0 + (x1 - x0) * (y - y0) / (y1 - y0);
}; // Fill bottom flat triangle (v0, v1, v2) if v1.y != v0.y
// Sort vertices by y (ascending)
var verts = [{
x: x1,
y: y1
}, {
x: x2,
y: y2
}, {
x: x3,
y: y3
}];
verts.sort(function (a, b) {
return a.y - b.y;
});
var v0 = verts[0];
var v1 = verts[1];
var v2 = verts[2];
if (v1.y !== v0.y) {
for (var y = Math.ceil(v0.y); y < Math.ceil(v1.y); y++) {
var xA = interpX(y, v0.x, v0.y, v2.x, v2.y);
var xB = interpX(y, v0.x, v0.y, v1.x, v1.y);
var xStart = Math.ceil(Math.min(xA, xB));
var xEnd = Math.floor(Math.max(xA, xB));
for (var x = xStart; x <= xEnd; x++) {
putPixel(x, y, color);
}
}
}
// Fill top flat triangle (v1, v2, v0) if v2.y != v1.y
if (v2.y !== v1.y) {
for (var y = Math.ceil(v1.y); y < Math.ceil(v2.y); y++) {
var xA = interpX(y, v0.x, v0.y, v2.x, v2.y);
var xB = interpX(y, v1.x, v1.y, v2.x, v2.y);
var xStart = Math.ceil(Math.min(xA, xB));
var xEnd = Math.floor(Math.max(xA, xB));
for (var x = xStart; x <= xEnd; x++) {
putPixel(x, y, color);
}
}
}
}
}
var colorIndex = 0;
var offset = 2732 / 2 - GRID_SIZE * PIXEL_SIZE / 2;
// Create the grid of pixels
for (var y = 0; y < GRID_SIZE; y++) {
for (var x = 0; x < GRID_SIZE; x++) {
// Get an instance of our pixel asset
var pixel = LK.getAsset('pixel', {
x: x * PIXEL_SIZE,
y: y * PIXEL_SIZE + offset,
tint: 0
});
game.addChild(pixel);
}
}
function projectTriangle(triangle) {
// triangle: [x1, y1, z1, x2, y2, z2, x3, y3, z3]
// Perspective projection parameters
var fov = 900; // Focal length (distance from camera to projection plane)
var cx = GRID_SIZE / 2;
var cy = GRID_SIZE / 2;
function project(x, y, z) {
// Avoid division by zero
var zz = z === 0 ? 0.0001 : z;
return [Math.round(cx + (x - cx) * (fov / (fov + zz))), Math.round(cy + (y - cy) * (fov / (fov + zz)))];
}
var p1 = project(triangle[0], triangle[1], triangle[2]);
var p2 = project(triangle[3], triangle[4], triangle[5]);
var p3 = project(triangle[6], triangle[7], triangle[8]);
// Return as flat array for drawTriangle: [x1, y1, x2, y2, x3, y3]
return [p1[0], p1[1], p2[0], p2[1], p3[0], p3[1]];
}
;
//Start of the 3d renderer
// 3 points
var triangles = [[0, 0, 0, 32, 0, 0, 0, 32, 0], [0, 32, 0, 32, 0, 0, 32, 32, 0], [0, 0, 32, 32, 0, 32, 0, 32, 32], [0, 32, 32, 32, 0, 32, 32, 32, 32]];
var start = Date.now();
triangles.forEach(function (triangle, i) {
drawTriangle(projectTriangle(triangle), 0xFFFFFF, true);
});
console.log("Time: " + String(Date.now() - start));
; ===================================================================
--- original.js
+++ change.js
@@ -127,9 +127,9 @@
}
function projectTriangle(triangle) {
// triangle: [x1, y1, z1, x2, y2, z2, x3, y3, z3]
// Perspective projection parameters
- var fov = 800; // Focal length (distance from camera to projection plane)
+ var fov = 900; // Focal length (distance from camera to projection plane)
var cx = GRID_SIZE / 2;
var cy = GRID_SIZE / 2;
function project(x, y, z) {
// Avoid division by zero