Skip to content

Commit

Permalink
WIP: Copy-pasta constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
sshirokov committed May 16, 2013
1 parent 1b61424 commit 9eecafd
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 81 deletions.
61 changes: 53 additions & 8 deletions src/ThreeCSG.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,63 @@ SPANNING = 3
# THREE.Mesh or a ThreeBSP.Node
class window.ThreeBSP extends _ThreeBSP
constructor: (treeIsh) ->
return super # XXX: Escape hatch
@matrix = new THREE.Matrix4()
@tree ?= @toTree treeIsh
@tree = @toTree treeIsh

toTree: (treeIsh) =>
return treeIsh if treeIsh instanceof ThreeBSP.Node
if treeIsh instanceof THREE.Mesh
treeIsh.updateMatrix()
treeIsh = treeIsh.geometry
@matrix = treeish.matrix.clone()
# TODO: Get polygon list
# Set @tree = new ThreeBSP.Node (polys)
polygons = []
geometry =
if treeIsh instanceof THREE.Geometry
treeIsh
else if treeIsh instanceof THREE.Mesh
treeIsh.updateMatrix()
@matrix = treeIsh.matrix.clone()
treeIsh.geometry

for face, i in geometry.faces
faceVertexUvs = geometry.faceVertexUvs[0][i]
polygon = new ThreeBSP.Polygon()
do (face, i) =>
if ( face instanceof THREE.Face3 )
vertex = geometry.vertices[ face.a ]
vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z, face.vertexNormals[0], new THREE.Vector2( faceVertexUvs[0].x, faceVertexUvs[0].y ) )
vertex.applyMatrix4(this.matrix)
polygon.vertices.push( vertex )

vertex = geometry.vertices[ face.b ]
vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z, face.vertexNormals[1], new THREE.Vector2( faceVertexUvs[1].x, faceVertexUvs[1].y ) )
vertex.applyMatrix4(this.matrix)
polygon.vertices.push( vertex )

vertex = geometry.vertices[ face.c ]
vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z, face.vertexNormals[2], new THREE.Vector2( faceVertexUvs[2].x, faceVertexUvs[2].y ) )
vertex.applyMatrix4(this.matrix)
polygon.vertices.push( vertex )
else if ( typeof THREE.Face4 )
vertex = geometry.vertices[ face.a ]
vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z, face.vertexNormals[0], new THREE.Vector2( faceVertexUvs[0].x, faceVertexUvs[0].y ) )
vertex.applyMatrix4(this.matrix)
polygon.vertices.push( vertex )

vertex = geometry.vertices[ face.b ]
vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z, face.vertexNormals[1], new THREE.Vector2( faceVertexUvs[1].x, faceVertexUvs[1].y ) )
vertex.applyMatrix4(this.matrix)
polygon.vertices.push( vertex )

vertex = geometry.vertices[ face.c ]
vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z, face.vertexNormals[2], new THREE.Vector2( faceVertexUvs[2].x, faceVertexUvs[2].y ) )
vertex.applyMatrix4(this.matrix)
polygon.vertices.push( vertex )

vertex = geometry.vertices[ face.d ]
vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z, face.vertexNormals[3], new THREE.Vector2( faceVertexUvs[3].x, faceVertexUvs[3].y ) )
vertex.applyMatrix4(this.matrix)
polygon.vertices.push( vertex )
else
throw new Error("Invalid face type at index #{i}")
polygons.push polygon.calculateProperties()
new ThreeBSP.Node polygons

##
## ThreeBSP.Vertex
Expand Down
75 changes: 2 additions & 73 deletions src/ThreeCSG.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,79 +8,8 @@ window._ThreeBSP = (function() {
BACK = 2,
SPANNING = 3;

_ThreeBSP = function( geometry ) {
// Convert THREE.Geometry to ThreeBSP
var i, _length_i,
face, vertex, faceVertexUvs,
polygon,
polygons = [],
tree;

if ( geometry instanceof THREE.Geometry ) {
this.matrix = new THREE.Matrix4;
} else if ( geometry instanceof THREE.Mesh ) {
// #todo: add hierarchy support
geometry.updateMatrix();
this.matrix = geometry.matrix.clone();
geometry = geometry.geometry;
} else if ( geometry instanceof ThreeBSP.Node ) {
this.tree = geometry;
this.matrix = new THREE.Matrix4;
return this;
} else {
throw 'ThreeBSP: Given geometry is unsupported';
}

for ( i = 0, _length_i = geometry.faces.length; i < _length_i; i++ ) {
face = geometry.faces[i];
faceVertexUvs = geometry.faceVertexUvs[0][i];
polygon = new ThreeBSP.Polygon;

if ( face instanceof THREE.Face3 ) {
vertex = geometry.vertices[ face.a ];
vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z, face.vertexNormals[0], new THREE.Vector2( faceVertexUvs[0].x, faceVertexUvs[0].y ) );
vertex.applyMatrix4(this.matrix);
polygon.vertices.push( vertex );

vertex = geometry.vertices[ face.b ];
vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z, face.vertexNormals[1], new THREE.Vector2( faceVertexUvs[1].x, faceVertexUvs[1].y ) );
vertex.applyMatrix4(this.matrix);
polygon.vertices.push( vertex );

vertex = geometry.vertices[ face.c ];
vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z, face.vertexNormals[2], new THREE.Vector2( faceVertexUvs[2].x, faceVertexUvs[2].y ) );
vertex.applyMatrix4(this.matrix);
polygon.vertices.push( vertex );
} else if ( typeof THREE.Face4 ) {
vertex = geometry.vertices[ face.a ];
vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z, face.vertexNormals[0], new THREE.Vector2( faceVertexUvs[0].x, faceVertexUvs[0].y ) );
vertex.applyMatrix4(this.matrix);
polygon.vertices.push( vertex );

vertex = geometry.vertices[ face.b ];
vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z, face.vertexNormals[1], new THREE.Vector2( faceVertexUvs[1].x, faceVertexUvs[1].y ) );
vertex.applyMatrix4(this.matrix);
polygon.vertices.push( vertex );

vertex = geometry.vertices[ face.c ];
vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z, face.vertexNormals[2], new THREE.Vector2( faceVertexUvs[2].x, faceVertexUvs[2].y ) );
vertex.applyMatrix4(this.matrix);
polygon.vertices.push( vertex );

vertex = geometry.vertices[ face.d ];
vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z, face.vertexNormals[3], new THREE.Vector2( faceVertexUvs[3].x, faceVertexUvs[3].y ) );
vertex.applyMatrix4(this.matrix);
polygon.vertices.push( vertex );
} else {
throw 'Invalid face type at index ' + i;
}

polygon.calculateProperties();
polygons.push( polygon );
};

this.tree = new ThreeBSP.Node( polygons );
};
_ThreeBSP = function( geometry ) { return "PLACEHOLDER"; };

_ThreeBSP.prototype.subtract = function( other_tree ) {
var a = this.tree.clone(),
b = other_tree.tree.clone();
Expand Down

0 comments on commit 9eecafd

Please sign in to comment.