如何使用Three.js获取纹理尺寸

问题描述 投票:0回答:1

我想知道是否有可能获得纹理的尺寸?我用这行来设置我的纹理:

const texture = THREE.ImageUtils.loadTexture(src)

也许有必要加载图像来获得它的尺寸(但是只有javascript,而不是html和它的div?)?

我想我之后创建的材质适合纹理尺寸。

javascript three.js textures
1个回答
1
投票

首先,THREE.ImageUtils.loadTexture在最新的THREE.js(r90)中被弃用。请看看THREE.TextureLoader

也就是说,您可以从加载的纹理中获取图像及其属性。

texture.image

根据图像格式,您应该能够访问width / height属性,这将是纹理的尺寸。

请注意:加载纹理是异步的,因此您需要定义onLoad回调。

var loader = new THREE.TextureLoader();
var texture = loader.load( "./img.png", function ( tex ) {
    // tex and texture are the same in this example, but that might not always be the case
    console.log( tex.image.width, tex.image.height );
    console.log( texture.image.width, texture.image.height );
} );
© www.soinside.com 2019 - 2024. All rights reserved.