4

我在玩 webGL 和 ThreeJS,然后我遇到了以下问题:

从远处看时,带有大图像的纹理会被像素化。

检查示例:http: //jsfiddle.net/4qTR3/1/

下面是代码:


    var scene = new THREE.Scene();
    var camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 10, 7000);

    var light = new THREE.PointLight(0xffffff);
    light.position.set(0, 150, 100);
    scene.add(light);

    var light2 = new THREE.AmbientLight(0x444444);
    scene.add(light2);

    var renderer = new THREE.WebGLRenderer({
        antialias: true
    });
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);

    var geometry = new THREE.PlaneGeometry(500, 500, 10, 10);

    //I use different textures in my project
    var texture = new THREE.ImageUtils.loadTexture(TEST_IMAGE);
    var textureBack = new THREE.ImageUtils.loadTexture(TEST_IMAGE);

    textureBack.anisotropy = renderer.getMaxAnisotropy();
    texture.anisotropy = renderer.getMaxAnisotropy();

    //Filters
    texture.magFilter = THREE.NearestFilter;
    texture.minFilter = THREE.LinearMipMapLinearFilter;
    textureBack.magFilter = THREE.NearestFilter;
    textureBack.minFilter = THREE.LinearMipMapLinearFilter;

    var materials = [
    new THREE.MeshLambertMaterial({
        transparent: true,
        map: texture,
        side: THREE.FrontSide
    }),
    new THREE.MeshLambertMaterial({
        transparent: true,
        map: textureBack,
        side: THREE.BackSide
    })];

    for (var i = 0, len = geometry.faces.length; i < len; i++) {
        var face = geometry.faces[i].clone();
        face.materialIndex = 1;
        geometry.faces.push(face);
        geometry.faceVertexUvs[0].push(geometry.faceVertexUvs[0][i].slice(0));
    }

    planeObject = new THREE.Mesh(geometry, new THREE.MeshFaceMaterial(materials));

    planeObject.overdraw = true;
    planeObject.position.z = -5000;

    scene.add(planeObject);

    camera.position.z = 1000;

    (function render() {
        requestAnimationFrame(render);
        planeObject.rotation.y += 0.02;
        renderer.render(scene, camera);
    })();

如果纹理图像中有文字,文字会变得非常像素化,质量很差。

我怎样才能解决这个问题?

4

1 回答 1

15

为了不被像素化,您需要使用 mips,但 WebGL 不能为非 2 次幂纹理生成 mips。您的纹理是 800x533,它们都不是 2 的幂。

几个选项

1) 将图片离线缩放到 2 的幂,例如 512x512 或 1024x512

2)在制作纹理之前在运行时缩放图片。

自己加载图像,加载后制作一个 2 次幂的画布。调用 drawImage(img, 0, 0, canvas.width, canvas.height) 将图像缩放到画布中。然后将画布加载到纹理中。

您可能还想将您的 mag 过滤器从 更改NearestFilterLinearFilter

注:(1)是更好的选择。(2) 在用户机器上花费时间,使用更多内存,并且您无法保证缩放的质量。

这里的例子

于 2013-10-15T20:31:16.913 回答