webgl-obj-loader
Advanced tools
Comparing version 2.0.2 to 2.0.3
{ | ||
"name": "webgl-obj-loader", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "A simple OBJ model loader to help facilitate the learning of WebGL", | ||
@@ -15,3 +15,3 @@ "main": "dist/webgl-obj-loader.min.js", | ||
"prettify": "prettier --write src/**/*.ts", | ||
"publish": "npm publish" | ||
"publish": "yarn release && npm publish" | ||
}, | ||
@@ -18,0 +18,0 @@ "prettier": { |
@@ -246,2 +246,7 @@ # webgl-obj-loader | ||
## ChangeLog | ||
**2.0.3** | ||
* Add simple support for N-Gons. | ||
* This uses a very elementary algorithm to triangulate N-gons, but should still produce a full mesh. | ||
* Any help to create a better triangulation algorithm would be greatly appreciated! Please create a [pull request](https://github.com/frenchtoast747/webgl-obj-loader/pulls). | ||
**2.0.0** | ||
@@ -248,0 +253,0 @@ * Updated to TypeScript |
@@ -22,3 +22,3 @@ import Mesh, { | ||
const version = "1.1.3"; | ||
const version = "2.0.3"; | ||
@@ -25,0 +25,0 @@ /** |
103
src/mesh.ts
@@ -248,20 +248,11 @@ import { Layout } from "./layout"; | ||
*/ | ||
let quad = false; | ||
for (let j = 0, eleLen = elements.length; j < eleLen; j++) { | ||
// Triangulating quads | ||
// quad: 'f v0/t0/vn0 v1/t1/vn1 v2/t2/vn2 v3/t3/vn3/' | ||
// corresponding triangles: | ||
// 'f v0/t0/vn0 v1/t1/vn1 v2/t2/vn2' | ||
// 'f v2/t2/vn2 v3/t3/vn3 v0/t0/vn0' | ||
if (j === 3 && !quad) { | ||
// add v2/t2/vn2 in again before continuing to 3 | ||
j = 2; | ||
quad = true; | ||
} | ||
const hash0 = elements[0] + "," + currentMaterialIndex; | ||
const hash = elements[j] + "," + currentMaterialIndex; | ||
if (hash in unpacked.hashindices) { | ||
unpacked.indices[currentObjectByMaterialIndex].push(unpacked.hashindices[hash]); | ||
} else { | ||
/* | ||
const triangles = triangulate(elements); | ||
for (const triangle of triangles) { | ||
for (let j = 0, eleLen = triangle.length; j < eleLen; j++) { | ||
const hash = triangle[j] + "," + currentMaterialIndex; | ||
if (hash in unpacked.hashindices) { | ||
unpacked.indices[currentObjectByMaterialIndex].push(unpacked.hashindices[hash]); | ||
} else { | ||
/* | ||
Each element of the face line array is a Vertex which has its | ||
@@ -280,9 +271,9 @@ attributes delimited by a forward slash. This will separate | ||
*/ | ||
const vertex = elements[j].split("/"); | ||
// it's possible for faces to only specify the vertex | ||
// and the normal. In this case, vertex will only have | ||
// a length of 2 and not 3 and the normal will be the | ||
// second item in the list with an index of 1. | ||
const normalIndex = vertex.length - 1; | ||
/* | ||
const vertex = elements[j].split("/"); | ||
// it's possible for faces to only specify the vertex | ||
// and the normal. In this case, vertex will only have | ||
// a length of 2 and not 3 and the normal will be the | ||
// second item in the list with an index of 1. | ||
const normalIndex = vertex.length - 1; | ||
/* | ||
The verts, textures, and vertNormals arrays each contain a | ||
@@ -306,31 +297,28 @@ flattend array of coordinates. | ||
*/ | ||
// Vertex position | ||
unpacked.verts.push(+verts[(+vertex[0] - 1) * 3 + 0]); | ||
unpacked.verts.push(+verts[(+vertex[0] - 1) * 3 + 1]); | ||
unpacked.verts.push(+verts[(+vertex[0] - 1) * 3 + 2]); | ||
// Vertex textures | ||
if (textures.length) { | ||
const stride = options.enableWTextureCoord ? 3 : 2; | ||
unpacked.textures.push(+textures[(+vertex[1] - 1) * stride + 0]); | ||
unpacked.textures.push(+textures[(+vertex[1] - 1) * stride + 1]); | ||
if (options.enableWTextureCoord) { | ||
unpacked.textures.push(+textures[(+vertex[1] - 1) * stride + 2]); | ||
// Vertex position | ||
unpacked.verts.push(+verts[(+vertex[0] - 1) * 3 + 0]); | ||
unpacked.verts.push(+verts[(+vertex[0] - 1) * 3 + 1]); | ||
unpacked.verts.push(+verts[(+vertex[0] - 1) * 3 + 2]); | ||
// Vertex textures | ||
if (textures.length) { | ||
const stride = options.enableWTextureCoord ? 3 : 2; | ||
unpacked.textures.push(+textures[(+vertex[1] - 1) * stride + 0]); | ||
unpacked.textures.push(+textures[(+vertex[1] - 1) * stride + 1]); | ||
if (options.enableWTextureCoord) { | ||
unpacked.textures.push(+textures[(+vertex[1] - 1) * stride + 2]); | ||
} | ||
} | ||
// Vertex normals | ||
unpacked.norms.push(+vertNormals[(+vertex[normalIndex] - 1) * 3 + 0]); | ||
unpacked.norms.push(+vertNormals[(+vertex[normalIndex] - 1) * 3 + 1]); | ||
unpacked.norms.push(+vertNormals[(+vertex[normalIndex] - 1) * 3 + 2]); | ||
// Vertex material indices | ||
unpacked.materialIndices.push(currentMaterialIndex); | ||
// add the newly created Vertex to the list of indices | ||
unpacked.hashindices[hash] = unpacked.index; | ||
unpacked.indices[currentObjectByMaterialIndex].push(unpacked.hashindices[hash]); | ||
// increment the counter | ||
unpacked.index += 1; | ||
} | ||
// Vertex normals | ||
unpacked.norms.push(+vertNormals[(+vertex[normalIndex] - 1) * 3 + 0]); | ||
unpacked.norms.push(+vertNormals[(+vertex[normalIndex] - 1) * 3 + 1]); | ||
unpacked.norms.push(+vertNormals[(+vertex[normalIndex] - 1) * 3 + 2]); | ||
// Vertex material indices | ||
unpacked.materialIndices.push(currentMaterialIndex); | ||
// add the newly created Vertex to the list of indices | ||
unpacked.hashindices[hash] = unpacked.index; | ||
unpacked.indices[currentObjectByMaterialIndex].push(unpacked.hashindices[hash]); | ||
// increment the counter | ||
unpacked.index += 1; | ||
} | ||
if (j === 3 && quad) { | ||
// add v0/t0/vn0 onto the second triangle | ||
unpacked.indices[currentObjectByMaterialIndex].push(unpacked.hashindices[hash0]); | ||
} | ||
} | ||
@@ -774,1 +762,14 @@ } | ||
} | ||
function* triangulate(elements: string[]) { | ||
if (elements.length <= 3) { | ||
yield elements; | ||
} else if (elements.length === 4) { | ||
yield [elements[0], elements[1], elements[2]]; | ||
yield [elements[2], elements[3], elements[0]]; | ||
} else { | ||
for (let i = 1; i < elements.length - 1; i++) { | ||
yield [elements[0], elements[i], elements[i + 1]]; | ||
} | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
500761
303
5245