Tfe

Ongi etorri tfe-ren webgunera...

Old stuff/old_sites/threejs/15_die/js/assets.js

(Deskargatu)
var Assets = function()
{
    this.jsons = [];
    this.load_textures = [];
    this.textures = [];

    this.add=function(file, name)
    {
        this.jsons.push( { file: file, name: name});
    };

    this.load = function()
    {
        this.add('js/meshes/wall.js','wall');
        this.add('js/meshes/wall1.js','wall1');
        this.add('js/meshes/door.js','door');
        this.add('js/meshes/door1.js','door1');
        this.add('js/meshes/dooropen.js','dooropen');
        this.add('js/meshes/dooropen1.js','dooropen1');

        this.add('blender/pingu_with_armature.json','perso');
        this.add('blender/ennemy.json','ennemy');
        this.add('blender/key.json','key');

        this.add_texture('textures/path_wall.jpg','path_wall');
        this.add_texture('textures/path_wall_bump.jpg','path_wall_bump');

        this.add_texture('textures/wall.jpg','cell_wall');
        this.add_texture('textures/path_wall_bump.jpg','cell_wall_bump');

        this.add_texture('textures/path_floor.jpg','path_floor');
        this.add_texture('textures/path_floor_bump.jpg','path_floor_bump');
        this.add_texture('textures/path_floor.jpg','maze_floor');
        this.add_texture('textures/path_floor_bump.jpg','maze_floor_bump');

        return this._load();
    };

    this.add_texture=function(file, name)
    {
        this.load_textures.push( { file: file, name: name});
    };

    this._load = function()
    {
        var self=this;
        var promises=[];

        this.jsons.forEach(function(json)
        {
            promises.push(new Promise(function(ok, reject)
            {
                var loader = new THREE.JSONLoader();
                loader.load(json.file, function(geometry, mat)
                {
                    self[json.name+'_geo'] = geometry;
                    self[json.name+'_mat'] = mat;
                    ok();
                });
            }));
        });
        this.load_textures.forEach(function(json)
        {
            promises.push(new Promise(function(ok, reject)
            {
                var textureLoader = new THREE.TextureLoader();
                textureLoader.load(json.file, function(tex)
                {
                    self[json.name+'_texture']= tex;
                    self[json.name+'_texture'].wrapS = self[json.name+'_texture'].wrapT = THREE.RepeatWrapping;
                    ok();
                });
            }));
        });

        promises.push(new Promise(function(ok, reject)
        {
            self.step_sound = new Audio('sounds/step.mp3');
            self.step_sound.loop=true;
            self.step_sound.load();
            ok();
        }));
        return Promise.all(promises);
    }

    this.getTexture = function(texture, bumpTexture)
    {
        if(!this.textures[texture+'_'+bumpTexture])
        {
            console.log('creating texture ',texture+' / '+bumpTexture);
            this.textures[texture+'_'+bumpTexture] = new THREE.MeshPhongMaterial({
                bumpScale:1,
                map: this[texture+'_texture'],
                bumpMap: this[bumpTexture+'_texture']
            });
        }
        return this.textures[texture+'_'+bumpTexture];
    }
};