• 【前端知识】Three 学习日志(十二)—— WebGL渲染器设置(锯齿模糊)


    Three 学习日志(十二)—— WebGL渲染器设置(锯齿模糊)

    一、设置抗锯齿
    const renderer = new THREE.WebGLRenderer({
      antialias:true,
    });
    
    • 1
    • 2
    • 3
    二、效果对比

    设置前:
    在这里插入图片描述
    设置后:
    在这里插入图片描述

    三、查看并设置设备像素比
    // 不同硬件设备的屏幕的设备像素比window.devicePixelRatio值可能不同
    console.log('查看当前屏幕设备像素比', window.devicePixelRatio);
    // 获取你屏幕对应的设备像素比.devicePixelRatio告诉threejs,以免渲染模糊问题
    renderer.setPixelRatio(window.devicePixelRatio);
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    四、设置背景颜色
    renderer.setClearColor(0x444444, 1); //设置背景颜色
    
    • 1

    在这里插入图片描述

    五、完整代码
    DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Learn Threetitle>
        
        <script src="../build/three.js">script>
        
        <script type="importmap">
            {
                "imports": {
                    "three": "../build/three.module.js",
                    "three/addons/": "../examples/jsm/"
                }
            }
        script>
    head>
    
    <body>
        <script type="module">
            // 引入轨道控制器扩展库OrbitControls.js
            import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
            // 创建3D场景对象Scene
            const scene = new THREE.Scene();
            const axesHelper = new THREE.AxesHelper(150);
            scene.add(axesHelper);
            const geometry = new THREE.BoxGeometry(100, 100, 100);
            const material = new THREE.MeshBasicMaterial({
                color: 0xff0000, //设置材质颜色
            });
    
            const pointLight = new THREE.PointLight(0xffffff, 1.0);
            pointLight.position.set(200, 200, 200);
            scene.add(pointLight); //点光源添加到场景中
    
            const mesh = new THREE.Mesh(geometry, material); //网格模型对象Mesh
            mesh.position.set(0, 0, 0);
            scene.add(mesh);
            const camera = new THREE.PerspectiveCamera();
            camera.position.set(600, 600, 600);
            camera.lookAt(0, 0, 0);
            const width = window.innerWidth; // 窗口宽度
            const height = window.innerHeight; // 窗口高度
            const renderer = new THREE.WebGLRenderer({
                antialias: true, // 抗锯齿
            });
            renderer.setSize(width, height);
    
            // 不同硬件设备的屏幕的设备像素比window.devicePixelRatio值可能不同
            console.log('查看当前屏幕设备像素比', window.devicePixelRatio);
            // 获取你屏幕对应的设备像素比.devicePixelRatio告诉threejs,以免渲染模糊问题
            renderer.setPixelRatio(window.devicePixelRatio);
            renderer.setClearColor(0x444444, 1); //设置背景颜色
    
            renderer.render(scene, camera); //执行渲染操作
            document.body.appendChild(renderer.domElement);
            const controls = new OrbitControls(camera, renderer.domElement);
            controls.addEventListener('change', function () {
                renderer.render(scene, camera); //执行渲染操作
            });//监听鼠标、键盘事件
    
        script>
    body>
    <style>
        body {
            overflow: hidden;
            margin: 0px;
        }
    style>
    
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
  • 相关阅读:
    JavaScript:实现Binary Search Tree二叉搜索树算法(附完整源码)
    OAuth2协议
    Java - JWT的简单介绍和使用
    Conmi的正确答案——Maven项目的pom.xml配置阿里云
    k8s 容忍和污点
    【一起学Rust · 项目实战】命令行IO项目minigrep——接收命令行参数与读取文件内容
    C++11右值引用的价值体现
    springboot和spring对比
    一张VR图像帧的生命周期
    机器学习文献|基于循环细胞因子特征,通过机器学习算法预测NSCLC免疫治疗结局
  • 原文地址:https://blog.csdn.net/weixin_42919342/article/details/133027795