前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >three.js中场景模糊、纹理失真的问题

three.js中场景模糊、纹理失真的问题

作者头像
charlee44
发布2020-06-16 15:44:44
6.4K0
发布2020-06-16 15:44:44
举报
文章被收录于专栏:代码编写世界代码编写世界

目录

  • 1. 概述
  • 2. 方案
    • 2.1. 开启反走样
    • 2.2. 开启HiDPI设置
  • 3. 结果
  • 4. 参考

1. 概述

在three.js场景中,有时会遇到场景模糊,纹理失真的现象,似乎three.js并没有用到纹理图片应有的分辨率。可以通过相关设置来解决这个问题。

2. 方案

2.1. 开启反走样

three.js创建的WebGLRenderer对象有抗锯齿选项的支持:

代码语言:javascript
复制
var renderer = new THREE.WebGLRenderer({
    antialias: true,     //抗锯齿
});

这个选项默认是关闭的,所以需要显式开启一下。

2.2. 开启HiDPI设置

如果开启抗锯齿后仍然显示比较模糊,那么可能就是使用的是HiDPI (High Dots Per Inch) 设备显示造成的,HiDPI设备能在较小尺寸下显示出较高分辨率,也就是每一个屏幕上的物理像素其实是由多个像素显示出来的,所以需要设置一下设备像素比率:

代码语言:javascript
复制
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);

其实关于HiDPI的讨论还是挺多的,比如说有个缩放与布局设置:

imglink1
imglink1

这个设置会更改window.devicePixelRatio的值,如果程序不做相关的设置,那么程序的UI显示出来就会是模糊的。现代程序组件一般都会自动做出相关的调整,在WebGL中则需要显式设置一下。

3. 结果

测试代码:

代码语言:javascript
复制
'use strict';

function init() {
    //console.log("Using Three.js version: " + THREE.REVISION);   

    // create a scene, that will hold all our elements such as objects, cameras and lights.
    var scene = new THREE.Scene();

    // create a camera, which defines where we're looking at.
    var camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1);

    // create a render and set the size
    var renderer = new THREE.WebGLRenderer({
        antialias: true,     //抗锯齿
    });
    renderer.setClearColor(new THREE.Color(0x000000));    
    renderer.setPixelRatio(window.devicePixelRatio);
    renderer.setSize(window.innerWidth, window.innerHeight);

    // add the output of the renderer to the html element
    document.getElementById("webgl-output").appendChild(renderer.domElement);


    var loader = new THREE.TextureLoader();
    loader.setCrossOrigin('Anonymous');
    var basePath = "1.jpg";
    loader.load(basePath, function (texture) {
        // create the ground plane
        var planeGeometry = new THREE.PlaneGeometry(2, 2);
        // var planeMaterial = new THREE.MeshBasicMaterial({
        //     color: 0xAAAAAA
        // });
        var planeMaterial = new THREE.MeshBasicMaterial({
            map: texture
        });

        var plane = new THREE.Mesh(planeGeometry, planeMaterial);

        // add the plane to the scene
        scene.add(plane);

        renderer.render(scene, camera);
    });
}

关闭反走样以及HiDPI:

imglink2
imglink2

开启反走样以及HiDPI之后显示效果有所改善:

imglink3
imglink3

4. 参考

  1. 关于ThreeJS场景失真的问题
  2. 关于three.js 抗锯齿
  3. HiDPI (简体中文)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-06-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 概述
  • 2. 方案
    • 2.1. 开启反走样
      • 2.2. 开启HiDPI设置
      • 3. 结果
      • 4. 参考
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档