简体   繁体   English

three.js textureloader多个图像

[英]three.js textureloader multiple images

So I have a website that does a few things in webgl w/ three.js, and I noticed that loadTexture is going away soon, and that I should probably switch to using textureloaders. 所以我有一个网站在webgl w / three.js中做了一些事情,我注意到loadTexture很快就会消失,我应该转而使用纹理加载器。 Basically, i'd like to preload all my textures before any code executes at the start of my code. 基本上,我想在我的代码开始执行任何代码之前预先加载所有纹理。 I was thinking about doing something like below, which will allow me to load any number of images, and assign them to a texture object that can be called when needed. 我正在考虑做类似下面的事情,这将允许我加载任意数量的图像,并将它们分配给可在需要时调用的纹理对象。

var loadedtex = {}
var to_load = ['images/a.png','images/b.png','images/c.png']
var texture_loader = new THREE.TextureLoader();
for(var i=0;i<to_load.length;i++){
    texture_loader.load(to_load[i],function(tex){
        loadedtex[to_load[i]] = tex
    })
}

I'm probably missing something obvious, but I can't quite get it to work correctly. 我可能错过了一些明显的东西,但我无法让它正常工作。 When I use the code above, loadedtex will fill up loadedtex['images/c.png'] every time, instead of properly looping and remember which "i" value it should be using. 当我使用上面的代码时,loadedtex每次都会填满loadedtex ['images / c.png'],而不是正确循环并记住它应该使用哪个“i”值。

Proper result should be something like: 适当的结果应该是这样的:

loadedtex['images/a.png'] = Three.Texture{}...
loadedtex['images/b.png'] = Three.Texture{}...
loadedtex['images/c.png'] = Three.Texture{}...

but what i really get is: 但我真正得到的是:

loadedtex['images/c.png'] = Three.Texture{}

I know its because the "i" variable will always be max value by the time anything loads, but i'm not sure how to accomplish what i want. 我知道它,因为“i”变量在任何加载时总是最大值,但我不知道如何实现我想要的。

Thanks. 谢谢。

---edit--- - -编辑 - -

This is what I ended up with. 这就是我最终的结果。 Seems to work well. 似乎运作良好。 Thanks for the advice. 谢谢你的建议。

var loadedtex = {}
var textureloaded = 0
var to_load = ['images/a.png','images/b.png','images/c.png']
var texture_loader = new THREE.TextureLoader();
load_textures = function(){
    if (textureloaded == to_load.length){return}
    var texture = to_load[textureloaded]
    texture_loader.load(texture,function(tex){
        loadedtex[texture] = tex
        textureloaded += 1
        load_textures()
    })  
}
load_textures()

If you're using ES6: 如果您使用的是ES6:

let assets = [ 
    { name:'img1', url:'img/img1.png' },
    { name:'img2', url:'img/img2.png' },
    { name:'img3', url:'img/img3.png' },
];
this.textures = {};
for ( let img of assets ) {
     this.loader.load(img.url, ( texture ) => {
        this.textures[img.name] = texture;
        assets.splice( assets.indexOf(img), 1);
        if ( !assets.length ) {
            this.init()
        }
        console.log('[TextureLoader] Loaded %o', img.name);
    });  
}

Try to make a closure function for it like, 尝试为它做一个闭包函数,比如

for(var i=0;i<to_load.length;i++){
    (function(tl){
       texture_loader.load(tl,function(tex){
           loadedtex[tl] = tex
       })
    })(to_load[i]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM