Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make parse quad indices. #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 27 additions & 19 deletions src/ObjLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,26 +83,34 @@ export default class ObjLoader {
const cache: Record<string, number> = {};
let i = 0;
for (const faces of cachedFaces) {
for (const faceString of faces) {
// If we already saw this, add to indices list.
if (cache[faceString] !== undefined) {
finalIndices.push(cache[faceString]);
continue;
// calculate triangle count in faces
const triangleCount = faces.length - 2;
for(var j = 0; j < triangleCount; j++) {
const triangleFace : string[] = [faces[0]];
triangleFace.push(faces[1 + j]);
triangleFace.push(faces[2 + j]);

for (const faceString of triangleFace) {
// If we already saw this, add to indices list.
if (cache[faceString] !== undefined) {
finalIndices.push(cache[faceString]);
continue;
}

cache[faceString] = i;
finalIndices.push(i);

// Need to convert strings to integers, and subtract by 1 to get to zero index.
const [vI, uvI, nI] = faceString
.split("/")
.map((s: string) => Number(s) - 1);

vI > -1 && finalPosition.push(...cachedVertices[vI]);
uvI > -1 && finalUvs.push(...cachedUvs[uvI]);
nI > -1 && finalNormals.push(...cachedNormals[nI]);

i += 1;
}

cache[faceString] = i;
finalIndices.push(i);

// Need to convert strings to integers, and subtract by 1 to get to zero index.
const [vI, uvI, nI] = faceString
.split("/")
.map((s: string) => Number(s) - 1);

vI > -1 && finalPosition.push(...cachedVertices[vI]);
uvI > -1 && finalUvs.push(...cachedUvs[uvI]);
nI > -1 && finalNormals.push(...cachedNormals[nI]);

i += 1;
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Cloth from "./Cloth";
// For the simulation to work with collisions,
// it is wise to use equal spacing between all the particles.
// This is possible to do in Blender even if the cloth as a whole is a rectangle.
const OBJECT_URL: string = "cloth_30_45_l.obj";
const OBJECT_URL: string = "cloth_20_30_l.obj";
const VERTEX_SPACING = 0.05;

(async () => {
Expand All @@ -23,7 +23,8 @@ const VERTEX_SPACING = 0.05;
const mesh = objLoader.parse(objFile);

const modelTransformation = new Transformation();
modelTransformation.scale = [1.0, 1.0, 1.0];
modelTransformation.translation = [0,0,0];
modelTransformation.scale = [1, 1, 1];
modelTransformation.rotationXYZ = [0, 1, 0];

// Create Buffers and Bind Groups
Expand All @@ -35,14 +36,14 @@ const VERTEX_SPACING = 0.05;

// Initalize Scene objects
const lightModel = new Transformation();
lightModel.translation = [5.0, 0.0, 0.0];
lightModel.translation = [5.0, 0.0, 100.0];
lightModel.rotationXYZ = [0, 0, 0];

const perspectiveCamera = new Camera(
(2 * Math.PI) / 5,
gpuCanvas.aspectRatio,
0.1,
100
0.01,
10000
);

perspectiveCamera.translation = [0, 0.0, 2.1];
Expand Down