Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用Three.js获取纹理尺寸_Javascript_Three.js_Textures - Fatal编程技术网

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

Javascript 如何使用Three.js获取纹理尺寸,javascript,three.js,textures,Javascript,Three.js,Textures,我想知道是否有可能获得纹理的尺寸? 我用这条线来设置我的纹理: const texture = THREE.ImageUtils.loadTexture(src) 也许有必要加载图像以获取其维度(但是否只可能使用javascript,而不使用html及其div?) 我希望以后创建的材质适合纹理尺寸。首先,THREE.ImageUtils.loadTexture在最新的THREE.js(r90)中被弃用。请看一看 也就是说,您可以从加载的纹理获取图像及其属性 texture.image 根据图

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

const texture = THREE.ImageUtils.loadTexture(src)
也许有必要加载图像以获取其维度(但是否只可能使用javascript,而不使用html及其div?)


我希望以后创建的材质适合纹理尺寸。

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

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

texture.image
根据图像格式,您应该能够访问
宽度
/
高度
属性,这将是纹理的尺寸

请注意:加载纹理是异步的,因此需要定义
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 );
} );

我已经尝试过使用TextureLoader,但它对我不起作用。似乎图像没有被读取,所以我最终得到了一个白色的对象。这就是我决定使用ImageUtils的原因,对于它,属性图像也是空的,但显示纹理。字段图像“未定义”,但有字段“源文件”的信息。能否发布一个片段?最好检查
.naturalWidth
.naturalHeight
而不是
.height
.width
,因为自然高度/宽度保证包含实际图像大小和宽度
.width
/
.height
不要(至少在某些情况下,最好是安全的):值得注意的是,调用
TextureLoader.load()
返回的纹理可能还不包含图像。而在回调函数中,它是这样做的。这是因为
load()
函数的异步性质。您正在加载什么格式的图像?我使用的是映射web服务,所以我不知道该格式(它不在我用于加载图像的URL中),您应该能够通过映射服务的文档从映射服务获取该信息,或者手动下载图像并自己检查其格式。