[英]Three.js create texture from image file
I'm having some trouble creating a texture from an image file in Three.js. 我在Three.js中从图像文件创建纹理时遇到了一些麻烦。 I have a function to create a mesh that looks like this:
我有一个函数来创建一个看起来像这样的网格:
var image = document.createElement('img');
image.src = imageFile;
var texture = new THREE.Texture(image);
var mat = new THREE.MeshPhongMaterial();
mat.map = texture;
var mesh = new THREE.Mesh(new THREE.PlaneGeometry(20, 20), mat);
var plane = mesh;
plane.position.x = 0;
scene.add(plane);
However, my texture renders black and the image doesn't seem to render properly. 但是,我的纹理呈现黑色,并且图像似乎无法正确呈现。
The easiest way to use an image as a texture is to load it using THREE.ImageUtils.loadTexture(url, mapping, onLoad, onError)
, here is an example: 将图像用作纹理的最简单方法是使用
THREE.ImageUtils.loadTexture(url, mapping, onLoad, onError)
加载图像,下面是一个示例:
var mesh = new THREE.Mesh(
new THREE.PlaneGeometry( 1, 1, 1, 1 ),
new THREE.MeshBasicMaterial({ map: THREE.ImageUtils.loadTexture('/my-image.jpg') })
);
The THREE.Texture
constructor does accept an image as its first parameter, I think the problem you are encountering is because you aren't waiting for the image to complete loading. THREE.Texture
构造函数确实接受图像作为它的第一个参数,我认为您遇到的问题是因为您没有等待图像完成加载。 Using ImageUtils
you can avoid having to dealing with callbacks. 使用
ImageUtils
可以避免处理回调。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.