Skip to content

Commit

Permalink
fix(csg): Check faceVertexUvs exists (#3)
Browse files Browse the repository at this point in the history
* Check faceVertexUvs exists
  • Loading branch information
samalexander authored Aug 8, 2019
1 parent 252930c commit 5ae8a48
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
10 changes: 9 additions & 1 deletion src/__tests__/csg.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CSG } from '../index';
import { Mesh, BoxGeometry, Vector3 } from 'three';
import { Mesh, BoxGeometry, Vector3, Geometry } from 'three';

describe('CSG instance methods', () => {

Expand Down Expand Up @@ -55,4 +55,12 @@ describe('CSG instance methods', () => {
expect(meshC).toBeTruthy();
});

test('custom geometry', () => {
const box = new BoxGeometry(1, 1, 1);
const g = new Geometry();
g.vertices = box.vertices;
g.faces = box.faces;
CSG.fromGeometry(g);
});

});
12 changes: 7 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export class CSG {
const f = fs[i];
const vertices = [];
for (let j = 0; j < 3; j++) {
vertices.push(new Vertex(vs[f[fm[j]]], f.vertexNormals[j], geom.faceVertexUvs[0][i][j]));
const uvs = geom.faceVertexUvs[0][i] !== undefined && geom.faceVertexUvs[0][i][j] !== undefined ?
geom.faceVertexUvs[0][i][j] : undefined;
vertices.push(new Vertex(vs[f[fm[j]]], f.vertexNormals[j], uvs));
}

polys.push(new Polygon(vertices));
Expand Down Expand Up @@ -256,14 +258,14 @@ class Vertex {
normal: Vector;
uv: Vector;

constructor(pos: IVector, normal: IVector, uv: IVector) {
constructor(pos: IVector, normal: IVector, uv?: IVector) {
this.pos = new Vector(pos.x, pos.y, pos.z);
this.normal = new Vector(normal.x, normal.y, normal.z);
this.uv = new Vector(uv.x, uv.y, uv.z);
if (uv) this.uv = new Vector(uv.x, uv.y, uv.z);
}

clone() {
return new Vertex(this.pos.clone(), this.normal.clone(), this.uv.clone());
return new Vertex(this.pos.clone(), this.normal.clone(), this.uv ? this.uv.clone() : undefined);
}

// Invert all orientation-specific data (e.g. vertex normal). Called when the
Expand All @@ -276,7 +278,7 @@ class Vertex {
// interpolating all properties using a parameter of `t`. Subclasses should
// override this to interpolate additional properties.
interpolate(other: Vertex, t: number) {
return new Vertex(this.pos.lerp(other.pos, t), this.normal.lerp(other.normal, t), this.uv.lerp(other.uv, t));
return new Vertex(this.pos.lerp(other.pos, t), this.normal.lerp(other.normal, t), this.uv ? this.uv.lerp(other.uv, t) : undefined);
}
}

Expand Down

0 comments on commit 5ae8a48

Please sign in to comment.