Added streaming simulation

This commit is contained in:
Thomas FORGIONE 2015-04-07 16:05:43 +02:00
parent 96db6d177f
commit f1a7d1c033
4 changed files with 207 additions and 0 deletions

View File

@ -32,6 +32,16 @@
camera that you want.
</p>
</li>
<li>
<p>
<a href="/stream/">Streaming simulation</a>
</p>
<p>
A mesh of a sphere is fully loaded, and displayed
progressively. This test is here to prove that we can
dynamically add vertices and faces to a mesh.
</p>
</li>
</ul>
</body>
</html>

80
js/ProgressiveSphere.js Normal file
View File

@ -0,0 +1,80 @@
var ProgessiveSphere = function(loader) {
Displayable.call(this);
this.started = false;
this.finished = false;
this.wasFinished = false;
this.begin = false;
this.addedToScene = false;
(function(self) {
loader.load('/data/spheres/4.obj', function(object) {
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.current_face, this.current_face+1, this.current_face+2));
// Update the stuff
this.geometry.computeFaceNormals();
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;
alert("Finished reconstructing the mesh !");
}
}
ProgessiveSphere.prototype.addToScene = function(scene) {
Displayable.prototype.addToScene.call(this, scene);
this.addedToScene = true;
}

27
stream/index.html Normal file
View File

@ -0,0 +1,27 @@
<!doctype html>
<html>
<head>
<title>The begining</title>
<meta charset="utf-8">
</head>
<body>
<h1>Here is stuff</h1>
<div style="border-width:1px; border-style: solid;" id="container"></div>
<script src="/js/three/three.min.js"></script>
<script src="/js/three/MTLLoader.js"></script>
<script src="/js/three/OBJLoader.js"></script>
<script src="/js/three/OBJMTLLoader.js"></script>
<script src="/js/three/OrbitControls.js"></script>
<script src="/js/three/PointerLockControls.js"></script>
<script src="/js/Cube.js"></script>
<script src="/js/BouncingCube.js"></script>
<script src="/js/Camera.js"></script>
<script src="/js/PointerCamera.js"></script>
<script src="/js/FixedCamera.js"></script>
<script src="/js/CameraContainer.js"></script>
<script src="/js/Tools.js"></script>
<script src="/js/ProgressiveSphere.js"></script>
<script src="js/main.js"></script>
</body>
</html>

90
stream/js/main.js Normal file
View File

@ -0,0 +1,90 @@
var mesh_number = 25;
var renderer, scene, controls, cube, container, plane, mouse= {x:0, y:0}, sphere;
var bigmesh;
var raycaster;
var objects = [];
var spheres = new Array(mesh_number);
var visible = 0;
var loader;
var container_size = {width: 1067, height: 600};
init();
animate();
function init() {
// on initialise le moteur de rendu
container = document.getElementById('container');
container.style.height = container_size.height + 'px';
container.style.width = container_size.width + 'px';
renderer = new THREE.WebGLRenderer({alpha:"true"});
renderer.setSize(container_size.width, container_size.height);
renderer.shadowMapEnabled = true;
// renderer.setClearColor(0x000000);
document.getElementById('container').appendChild(renderer.domElement);
// on initialise la scène
scene = new THREE.Scene();
raycaster = new THREE.Raycaster();
// init light
var directional_light = new THREE.DirectionalLight(0x999999);
directional_light.position.set(1, 0.5, 1).normalize();
directional_light.castShadow = true;
scene.add(directional_light);
var ambient_light = new THREE.AmbientLight(0x333333);
scene.add(ambient_light);
// on initialise la camera que lon place ensuite sur la scène
camera = new Camera(50, container_size.width / container_size.height, 1, 100000);
scene.add(camera);
window.addEventListener('resize', onWindowResize, false);
container.addEventListener('mousedown', function() { } , false);
// Load the scene
loader = new THREE.OBJLoader();
sphere = new ProgessiveSphere(loader);
// sphere.addToScene(scene);
// sphere.addToScene(scene);
plane = new Plane(1000,1000);
plane.translate(0,0,-100);
plane.addToScene(scene);
}
function animate() {
// on appelle la fonction animate() récursivement à chaque frame
requestAnimationFrame(animate);
camera.update();
camera.look();
sphere.addFace();
renderer.render(scene, camera);
}
function onWindowResize() {
camera.aspect = container.offsetWidth / container.offsetHeight;
camera.updateProjectionMatrix();
renderer.setSize(container.offsetWidth, container.offsetHeight);
renderer.render(scene, camera);
}
function hide(object) {
object.traverse(function(object) {object.visible = false;});
}
function show(object) {
object.traverse(function(object) {object.visible = true;});
}
function click(event) {
// Nope
}