var Hexa = function(scene) { var self=this; self.scene = scene; var options = { box_width: 30, box_height: 30, box_depth: 2 }; var facettes = []; var material; this.load= function() { var self=this; this.depth = (Math.sqrt(3)/2) * options.box_width; this.box_depth = options.box_depth; var texloader = new THREE.TextureLoader(); this.cubeTexture=texloader.load("textures/test.jpg"); return new Promise(function(ok, reject) { var loader = new THREE.JSONLoader(); loader.load( "js/door.js", function(geometry, mat) { self.door_geo = geometry; self.door_mat = mat; ok(); }); }); }; this.build= function() { var self=this; this.load().then(function() { var hexas=[]; for(var i=0; i<5; i++) { var pair = i%2===0 ? 0 : 1; hexas.push(self.create({ x:i*2*self.depth - i*2*self.box_depth, z:(pair+0)*self.depth, })); hexas.push(self.create({ x:i*2*self.depth - i*2*self.box_depth, z:(pair+2)*self.depth, })); hexas.push(self.create({ x:i*2*self.depth - i*2*self.box_depth, z:(pair+4)*self.depth, })); if(!pair) { hexas.push(self.create(scene, { x:i*2*self.depth - i*2*self.box_depth, z:(pair*6)*self.depth, })); } } }); }; this.create = function(params) { var self=this; var pivots=[]; this.fulldepth = options.box_depth + options.box_depth*2; var cell = new THREE.Object3D(); cell.position.x=params.x; cell.position.y=0; cell.position.z=params.z; self.scene.add(cell); for(var i=0; i<6;i++) { pivots[i] = new THREE.Object3D(); cell.add(pivots[i]); var materials = []; materials.push(new THREE.MeshLambertMaterial({ map: this.cubeTexture, color: 0xffffff })); materials.push(new THREE.MeshLambertMaterial({ map: this.cubeTexture, color: 0xffffff })); materials.push(new THREE.MeshLambertMaterial({ map: this.cubeTexture, color: 0xffffff })); materials.push(new THREE.MeshLambertMaterial({ map: this.cubeTexture, color: 0xffffff })); materials.push(new THREE.MeshLambertMaterial({ map: this.cubeTexture, color: 0xffffff })); materials.push(new THREE.MeshLambertMaterial({ map: this.cubeTexture, color: 0xffffff })); materials.push(new THREE.MeshLambertMaterial({ map: this.cubeTexture, color: 0xffffff })); facettes[i] = new THREE.Mesh( self.door_geo, new THREE.MeshFaceMaterial(materials)); facettes[i].receiveShadow = true; facettes[i].castShadow = true; pivots[i].add( facettes[i] ); facettes[i].position.set(0, 0, self.depth); switch(i) { case 0: pivots[i].rotation.y= Math.radians(0*60); break; case 1: pivots[i].rotation.y= Math.radians(1*60); break; case 2: pivots[i].rotation.y= Math.radians(2*60); break; case 3: pivots[i].rotation.y= Math.radians(3*60); break; case 4: pivots[i].rotation.y= Math.radians(4*60); break; case 5: pivots[i].rotation.y= Math.radians(5*60); break; default: facettes[i].position.set(10000,10000,10000); break; } } self.facettes = facettes; self.pivots = pivots; }; }