3d-interface/js/ProgressiveSphere.js

90 lines
3.5 KiB
JavaScript
Raw Normal View History

var ProgessiveSphere = function(loader, res) {
2015-04-07 16:05:43 +02:00
Displayable.call(this);
this.started = false;
this.finished = false;
this.wasFinished = false;
this.begin = false;
this.addedToScene = false;
if (res === undefined)
res = 5;
2015-04-07 16:05:43 +02:00
(function(self) {
loader.load('/static/data/spheres/' + res + '.obj', function(object) {
2015-04-07 16:05:43 +02:00
object.traverse(function(child) {
if (child instanceof THREE.Mesh) {
child.up = new THREE.Vector3(0,0,1);
self.totalMesh = child;
self.geometry = new THREE.Geometry();
self.material = new THREE.MeshLambertMaterial();
self.material.color.setRGB(1,0,0);
self.material.side = THREE.DoubleSide;
self.mesh = new THREE.Mesh(self.geometry, self.material);
self.current_face = 0;
self.started = true;
self.begin = true;
self.addToScene(scene);
}
});
});
})(this);
}
ProgessiveSphere.prototype = Object.create(Displayable.prototype);
ProgessiveSphere.prototype.constructor = ProgessiveSphere;
ProgessiveSphere.prototype.addFace = function() {
if (this.started && this.begin && this.addedToScene) {
if (this.current_face < this.totalMesh.geometry.attributes.position.array.length / 3) {
// Add the 3 new vertices
this.geometry.vertices.push(new THREE.Vector3(
this.totalMesh.geometry.attributes.position.array[3*this.current_face],
this.totalMesh.geometry.attributes.position.array[3*this.current_face+1],
this.totalMesh.geometry.attributes.position.array[3*this.current_face+2]
));
this.geometry.vertices.push(new THREE.Vector3(
this.totalMesh.geometry.attributes.position.array[3*this.current_face+3],
this.totalMesh.geometry.attributes.position.array[3*this.current_face+4],
this.totalMesh.geometry.attributes.position.array[3*this.current_face+5]
));
this.geometry.vertices.push(new THREE.Vector3(
this.totalMesh.geometry.attributes.position.array[3*this.current_face+6],
this.totalMesh.geometry.attributes.position.array[3*this.current_face+7],
this.totalMesh.geometry.attributes.position.array[3*this.current_face+8]
));
// Add the new face
this.geometry.faces.push(new THREE.Face3(
this.geometry.vertices.length-3,
this.geometry.vertices.length-2,
this.geometry.vertices.length-1
));
2015-04-07 16:05:43 +02:00
// Update the stuff
this.geometry.mergeVertices();
2015-04-07 16:05:43 +02:00
this.geometry.computeFaceNormals();
this.geometry.computeVertexNormals();
2015-04-07 16:05:43 +02:00
this.current_face += 3;
this.geometry.elementsNeedUpdate = true;
this.geometry.normalsNeedUpdate = true;
this.geometry.verticesNeedUpdate = true;
this.geometry.groupsNeedUpdate = true;
} else {
this.finished = true;
}
}
if (!this.wasFinished && this.finished) {
this.wasFinished = true;
2015-04-08 16:00:11 +02:00
console.log("Finished reconstructing the mesh !");
2015-04-07 16:05:43 +02:00
}
}
ProgessiveSphere.prototype.addToScene = function(scene) {
Displayable.prototype.addToScene.call(this, scene);
this.addedToScene = true;
}