From 05fca4e2e100cd24571427809ba4953cf79f3769 Mon Sep 17 00:00:00 2001 From: alan Date: Tue, 5 Dec 2023 18:58:43 -0500 Subject: [PATCH] Fix networking, mesh import --- buildwin.cmd | 4 ++++ package.json | 3 ++- src/client/client.ts | 9 +++++---- src/client/math/triangle.ts | 2 +- src/client/mesh/import.ts | 13 +++++++++++-- src/server/server.ts | 13 ++++++++----- 6 files changed, 31 insertions(+), 13 deletions(-) create mode 100644 buildwin.cmd diff --git a/buildwin.cmd b/buildwin.cmd new file mode 100644 index 0000000..630919a --- /dev/null +++ b/buildwin.cmd @@ -0,0 +1,4 @@ +@REM rmdir dist\ +@REM mkdir dist\public +@REM xcopy .\assets .\dist\public /e /h /y /f +tsc && webpack --config .\webpack\development.js diff --git a/package.json b/package.json index e0a683e..2c21cdd 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,8 @@ "build": "tsc && webpack --config ./webpack/development.js", "rebuild": "npm run clean && npm run build", "server": "node ./build/server/server.js", - "debug": "node ./build/server/server.js -debug" + "debug": "node ./build/server/server.js -debug", + "winserver": "node .\\build\\server\\server.js" }, "repository": { "type": "git", diff --git a/src/client/client.ts b/src/client/client.ts index 51660fe..fc03f99 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -19,10 +19,10 @@ const request_mesh = (filename: string) => { request_mesh_listeners(request); request.onreadystatechange = function() { if (request.readyState == XMLHttpRequest.DONE) { - alert(request.responseText); + console.log(request.responseText); const mesh = MeshImport.parse(request.responseText); console.log(`Got mesh array at ${mesh.name}`); - console.log(mesh.shells); + console.log(mesh); } } request.open('GET', `http://localhost:${3000}/mesh/${filename}`, true); @@ -30,7 +30,8 @@ const request_mesh = (filename: string) => { return request; } -document.onload = function() { +// Wait for page load before executing code +window.addEventListener('load', function() { console.log('PAGE LOADED'); const req = request_mesh('clover.obj'); -} +}) diff --git a/src/client/math/triangle.ts b/src/client/math/triangle.ts index b1a1017..71dacc6 100644 --- a/src/client/math/triangle.ts +++ b/src/client/math/triangle.ts @@ -1,6 +1,6 @@ import Vector2 from './vector'; -const EPSILON = 0.0001; +const EPSILON = 0.000001; class Triangle { public uvs: Vector2[]; diff --git a/src/client/mesh/import.ts b/src/client/mesh/import.ts index ad0af8e..7b8f7a5 100644 --- a/src/client/mesh/import.ts +++ b/src/client/mesh/import.ts @@ -22,7 +22,10 @@ const parse_tri = (line: string) => { let nums: number[] = []; for (let i = 1; i < 4 && i < vals.length; i++) { - nums[i-1] = parseFloat(vals[i]); + const idx = (i-1) * 2; + const face_corner = vals[i].split('/'); + nums[idx] = parseInt(face_corner[0], 10); + nums[idx+1] = parseInt(face_corner[1], 10); } return nums; } @@ -50,6 +53,8 @@ class MeshImport { } }); + console.log(`# VERTs: ${verts.length / 3}\t# UVs ${uvs.length}\t# TRIs: ${tris.length / 6}`); + // Construct triangles let t: Triangle[] = []; for (let i = 0; i < tris.length; i += 6) { @@ -65,6 +70,8 @@ class MeshImport { t.push(new Triangle(v, uv, uvi)); } + console.log('IMPORT: Total triangles found', t.length); + // Create an initial shell let shells: Shell[] = []; // Initialize shells by sorting triangles roughly int othem @@ -85,6 +92,8 @@ class MeshImport { } } + console.log('IMPORT: Total shells initial', shells.length); + // Now, combine shells for (let i = 0; i < shells.length; i++) { const a = shells[i]; @@ -93,7 +102,7 @@ class MeshImport { if (a.connected_shell(b)) { a.merge(b); - shells = shells.splice(j, 1); + shells.splice(j, 1); j--; } } diff --git a/src/server/server.ts b/src/server/server.ts index 44d76e4..7752e96 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -8,19 +8,22 @@ dotenv.config(); const app: Express = express(); const port = process.env.PORT; +const dir = `${process.cwd()}/dist/public`; + +app.use('/', express.static(dir)); + app.get('/mesh/*', (req: Request, res: Response) => { console.log('Got MESH request ', req.url); res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "X-Requested-With"); - app.use(express.static('dist/public/objects/hard')); + res.sendFile(`${dir}/objects/hard/clover.obj`); }); app.get('/', (req: Request, res: Response) => { - console.log('Got MAIN request ', req.url); - - app.use(express.static('dist' + '/public')); + console.log('Got MAIN request ', req.url.length); + res.sendFile(`${dir}/index.html`); }); app.listen(port, () => { - console.log(`Server is running on localhost:${port}`); + console.log(`Server is running on localhost:${port}, working in ${dir}`); });