如何在 Threejs (r65) 中使用纹理图集? [英] How to use texture atlas with Threejs (r65)?

查看:24
本文介绍了如何在 Threejs (r65) 中使用纹理图集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于新的threejs版本(r65)发布,uvOffset和uvScale被移动到texture.offset和texture.repeate.

Since the new threejs revision (r65) is released, the uvOffset and uvScale is moved to texture.offset and texture.repeate.

不幸的是,texture.offset 不适合我想要完成的任务.我想在一个场景中显示多个精灵,它们都使用相同的纹理.纹理是具有不同瓷砖的纹理图集.如果我更改 texture.offset,则场景中所有精灵的纹理都会更改.是否有任何解决方案可以单独更改每个精灵的偏移量?

Unfortunately, the texture.offset is not working for me what I am trying to accomplish. I want to display multiple sprites in a scene that all use the same texture. The texture is a texture atlas with different tiles. If I change the texture.offset, the texture is changed for all sprites within the scene. Is there any solution to just change the offset individually for each sprite?

我猜 uvOffset 把这个工作做得很好,我不明白为什么它被移动了.

I guess uvOffset did this job very well and I don't understand why it is moved.

这里有一些代码来看看我想要做什么:

Here is some code to see what I am trying to do:

var gui_texture = THREE.ImageUtils.loadTexture('images/button.png');

var btn_material1 = new THREE.SpriteMaterial( { map:gui_texture } );
btn_material1.map.offset.set(0.5,0);
var button = new THREE.Sprite(btn_material1);
button1.position.set(-screen_half_x+50, screen_half_y-25, 1);
button1.scale.set(100, 50, 1);
gui_node.add(button1);

var btn_material2 = new THREE.SpriteMaterial( { map:gui_texture } );
btn_material2.map.offset.set(-0.5,0);
var button2 = new THREE.Sprite(btn_material2);
button2.position.set(-screen_half_x+50, screen_half_y-150, 1);
button2.scale.set(100, 50, 1.0);
gui_node.add(button2);

真诚的

马库斯

推荐答案

我意识到这不是最漂亮的...但试试这个:

I realise that is not the prettiest... but try with this:

var texture1 = new THREE.Texture();
texture1.offset.set( 0.5, 0 );

var texture2 = new THREE.Texture();
texture2.offset.set( - 0.5, 0 );

var loader = new THREE.ImageLoader();
loader.load( 'images/button.png', function ( image ) {

    texture1.image = image;
    texture1.needsUpdate = true;

    texture2.image = image;
    texture2.needsUpdate = true;

} );

var button1 = new THREE.Sprite( new THREE.SpriteMaterial( { map:texture1 } ) );
button1.position.set( - screen_half_x + 50, screen_half_y - 25, 1 );
button1.scale.set( 100, 50, 1 );
gui_node.add( button1 );

var button2 = new THREE.Sprite( new THREE.SpriteMaterial( { map:texture2 } ) );
button2.position.set( - screen_half_x + 50, screen_half_y - 150, 1 );
button2.scale.set( 100, 50, 1.0 );
gui_node.add( button2 );

这篇关于如何在 Threejs (r65) 中使用纹理图集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆