• 【前端知识】Three 学习日志(六)—— 环境光与平行光


    Three 学习日志(六)—— 环境光与平行光

    一、设置环境光
    //环境光:没有特定方向,整体改变场景的光照明暗
    const ambient = new THREE.AmbientLight(0xffffff, 0.4);
    scene.add(ambient);
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    二、调整环境光强度
    //环境光强度调整为0.8
    const ambient = new THREE.AmbientLight(0xffffff, 0.8);
    scene.add(ambient);
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    三、设置平行光与平行光辅助
    // 平行光
    const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
    // 设置光源的方向:通过光源position属性和目标指向对象的position属性计算
    directionalLight.position.set(80, 100, 50);
    // 方向光指向对象网格模型mesh,可以不设置,默认的位置是0,0,0
    directionalLight.target = mesh;
    scene.add(directionalLight);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    四、完整代码
    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.MeshLambertMaterial();
            const mesh = new THREE.Mesh(geometry, material); //网格模型对象Mesh
            mesh.position.set(0, 0, 0);
            scene.add(mesh);
    
            // //环境光强度调整为0.8
            // const ambient = new THREE.AmbientLight(0xffffff, 0.8);
            // scene.add(ambient);
    
            // 平行光
            const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
            // 设置光源的方向:通过光源position属性和目标指向对象的position属性计算
            directionalLight.position.set(80, 100, 50);
            // 方向光指向对象网格模型mesh,可以不设置,默认的位置是0,0,0
            directionalLight.target = mesh;
            scene.add(directionalLight);
    
            // DirectionalLightHelper:可视化平行光
            const dirLightHelper = new THREE.DirectionalLightHelper(directionalLight, 5,0xff0000);
            scene.add(dirLightHelper);
    
            const camera = new THREE.PerspectiveCamera();
            camera.position.set(200, 200, 200);
            camera.lookAt(0, 0, 0);
            const width = 800; //宽度
            const height = 500; //高度
            const renderer = new THREE.WebGLRenderer();
            renderer.setSize(width, height);
            renderer.render(scene, camera); //执行渲染操作
            document.body.appendChild(renderer.domElement);
            // 设置相机控件轨道控制器
            const controls = new OrbitControls(camera, renderer.domElement);
            // 如果OrbitControls改变了相机参数,重新调用渲染器渲染三维场景
            controls.addEventListener('change', function () {
                renderer.render(scene, camera); //执行渲染操作
            });//监听鼠标、键盘事件
    
        script>
    body>
    
    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
  • 相关阅读:
    MySQL学习系列(1)-每天学习10个知识
    【数据结构】——顺序表(增删查改)
    MALUNet:一种多关注,轻量级的皮肤病变分割UNet
    如何在springboot项目中引入knife4j接口文档
    mssql调用外部接口
    lv5 嵌入式开发-5 线程的创建和参数传递
    设计模式——中介者模式
    pandas之数据的合并与分组
    Spring Authorization Server优化篇:Redis值序列化器添加Jackson Mixin,解决Redis反序列化失败问题
    招投标系统简介 企业电子招投标采购系统源码之电子招投标系统 —降低企业采购成本
  • 原文地址:https://blog.csdn.net/weixin_42919342/article/details/133013511