
var preloader_texturenames = [];
var preloader_i = 0;
var preloader_hello = 0;

var preloader_camera = new THREE.PerspectiveCamera(45, 16/9, 0.1, 20000); // view angle, aspect, near, far
var preloader_scene = new THREE.Scene();
preloader_camera.position.set(0,0,-15);
preloader_camera.lookAt(preloader_scene.position);

function prepare_texture_preloader()
{
	loading_console_write('Megatexture preloader is prepearing to rape the GPU.');

	for (t in tex)
	{
		preloader_texturenames.push(t);
	}

}

function preload_texture()
{
	if (loadable_elements>loaded_elements)
	{
		console.log("Renderwait...");
		
		setTimeout(function(){ preload_texture() },100);
		return false;
	}

	if (preloader_hello == 0)
	{
		loading_console_write('Megatexture preloading loading textures into GPU memory:');
		preloader_hello = 1;
	}

	if (preloader_i < preloader_texturenames.length)
	{
		loading_console_write('Loading '+preloader_texturenames[preloader_i]+' ('+(preloader_i+1)+'/'+preloader_texturenames.length+')');

		var geometry = new THREE.BoxGeometry(0.5,0.5,0.5,1);
		var material = new THREE.MeshLambertMaterial({ map: tex[preloader_texturenames[preloader_i]] });
		var cube = new THREE.Mesh(geometry, material);
		var x = 2.5-(preloader_i/preloader_texturenames.length)*5;
		cube.position.set(x, -5, 0);
		preloader_scene.add(cube);

		renderer.render(preloader_scene, preloader_camera);
		preloader_i++;

		requestAnimationFrame(preload_texture);
	} else
	{
		preload_scenes();
		//prepareSync();
		//startDemo();
	}

}