2015-04-02 11:38:06 +01:00
var mesh _number = 25 ;
var renderer , scene , camera , controls , cube , container , plane , mouse = { x : 0 , y : 0 } ;
var raycaster ;
var objects = [ ] ;
var spheres = new Array ( mesh _number ) ;
var visible = 0 ;
2015-06-15 07:45:42 +01:00
var loader , previousTime ;
2015-04-02 11:38:06 +01:00
2015-07-01 09:14:15 +01:00
var container _size = { } ;
2015-04-02 11:38:06 +01:00
container _size . width = 1067 ;
container _size . height = 600 ;
init ( ) ;
animate ( ) ;
2015-07-01 09:14:15 +01:00
function loadSphere ( i ) {
loader . load ( '/static/data/spheres/' + ( i + 1 ) + '.obj' , function ( object ) {
object . traverse ( function ( child ) {
if ( child instanceof THREE . Mesh ) {
child . material . color . setHex ( 0xff0000 ) ;
child . up = new THREE . Vector3 ( 0 , 0 , 1 ) ;
child . geometry . computeFaceNormals ( ) ;
child . geometry . computeVertexNormals ( ) ;
}
} ) ;
spheres [ i ] = object ;
scene . add ( object ) ;
if ( i !== 0 )
hide ( object ) ;
} ) ;
}
2015-04-02 11:38:06 +01:00
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 ;
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 ( 0xffffff ) ;
directional _light . position . set ( 1 , 0.5 , 1 ) . normalize ( ) ;
directional _light . castShadow = true ;
scene . add ( directional _light ) ;
var ambient _light = new THREE . AmbientLight ( 0x444444 ) ;
scene . add ( ambient _light ) ;
// on initialise la camera que l’ on place ensuite sur la scène
2015-07-01 15:31:43 +01:00
camera = new L3D . Camera ( 50 , container _size . width / container _size . height , 1 , 10000 ) ;
2015-04-02 11:38:06 +01:00
scene . add ( camera ) ;
window . addEventListener ( 'resize' , onWindowResize , false ) ;
container . addEventListener ( 'mousedown' , click , false ) ;
// Création d'un objloader
loader = new THREE . OBJLoader ( ) ;
for ( var i = 0 ; i < mesh _number ; i ++ ) {
2015-07-01 09:14:15 +01:00
loadSphere ( i ) ;
2015-04-02 11:38:06 +01:00
}
2015-07-01 15:31:43 +01:00
plane = new L3D . Plane ( 1000 , 1000 ) ;
2015-04-02 11:38:06 +01:00
plane . translate ( 0 , 0 , - 100 ) ;
plane . addToScene ( scene ) ;
}
function animate ( ) {
// on appelle la fonction animate() récursivement à chaque frame
requestAnimationFrame ( animate ) ;
2015-06-15 07:45:42 +01:00
var currentTime = Date . now ( ) - previousTime ;
camera . update ( isNaN ( currentTime ) ? 20 : currentTime ) ;
previousTime = Date . now ( ) ;
2015-04-02 11:38:06 +01:00
camera . look ( ) ;
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 click ( event ) {
++ visible ;
visible %= spheres . length ;
console . log ( 'Mesh ' , visible + 1 , ' out of ' , spheres . length , ' : ' , spheres [ visible ] . children [ 0 ] . geometry . attributes . position . array . length , ' vertices (with duplication...)' ) ;
// hide everything except visible
for ( var i in spheres )
{
hide ( spheres [ i ] ) ;
}
show ( spheres [ visible ] ) ;
}
function hide ( object ) {
object . traverse ( function ( object ) { object . visible = false ; } ) ;
}
function show ( object ) {
object . traverse ( function ( object ) { object . visible = true ; } ) ;
}