• 【前端知识】Three 学习日志(三)—— 光源对物体表面的影响


    Three 学习日志(三)—— 光源对物体表面的影响

    一、设置材质为受光照影响
    //MeshLambertMaterial受光照影响
    const material = new THREE.MeshLambertMaterial();
    
    • 1
    • 2

    在这里插入图片描述
    此时,场景中一片漆黑,无法看到原来的物体,需要设置光源来照亮物体。

    二、设置点光源
    //点光源:两个参数分别表示光源颜色和光照强度
    // 参数1:0xffffff是纯白光,表示光源颜色
    // 参数2:1.0,表示光照强度,可以根据需要调整
    const pointLight = new THREE.PointLight(0xffffff, 1.0);
    //点光源位置
    pointLight.position.set(200, 200, 200); //物体坐标为(100,100,100),设置光源在(200,200,200)从斜上方照射向物体
    scene.add(pointLight); //点光源添加到场景中
    
    • 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>
    head>
    
    <body>
        <script>
            // 任意调用API,为了检测three.js是否引入成功
            console.log(THREE.Scene);
            // 创建3D场景对象Scene
            const scene = new THREE.Scene();
            // 创建一个长方体几何对象Geometry
            const geometry = new THREE.BoxGeometry(100, 100, 100);
            // 创建一个材质对象Material(MeshBasicMaterial 为网格基础材质)
            // const material = new THREE.MeshBasicMaterial({
            //     color: 0xff0000,//0xff0000设置材质颜色为红色
            // });
    
            // MeshLambertMaterial受光照影响
            const material = new THREE.MeshLambertMaterial();
            // 点光源:两个参数分别表示光源颜色和光照强度
            // 参数1:0xffffff是纯白光,表示光源颜色
            // 参数2:1.0,表示光照强度,可以根据需要调整
            const pointLight = new THREE.PointLight(0xffffff, 1.0);
            //点光源位置
            pointLight.position.set(200, 200, 200);
            scene.add(pointLight); //点光源添加到场景中
    
            // 创建物体:网格模型(Mesh为网格模型)
            // 两个参数分别为上面创建的几何体geometry、材质material
            const mesh = new THREE.Mesh(geometry, material); //网格模型对象Mesh
            // 设置网格模型在三维空间中的位置坐标,默认是坐标原点
            mesh.position.set(0, 10, 0);
            // 构建好物体后,将物体添加到场景中
            scene.add(mesh);
            // 至此,我们的物体已经添加到场景中,但我们并不能立即看到构建的物体,还需要通过虚拟相机进行渲染
            // 实例化一个透视投影相机对象
            const camera = new THREE.PerspectiveCamera();
            // 30:视场角度, width / height:Canvas画布宽高比, 1:近裁截面, 3000:远裁截面
            // const camera = new THREE.PerspectiveCamera(30, width / height, 1, 3000);
            // 根据需要设置相机位置具体值
            camera.position.set(200, 200, 200);
            //相机观察目标指向Threejs 3D空间中某个位置
            camera.lookAt(0, 0, 0); //坐标原点
            // camera.lookAt(mesh.position);//指向mesh对应的位置
            // 定义相机输出画布的尺寸(单位:像素px)
            const width = 800; //宽度
            const height = 500; //高度
            // 创建渲染器对象
            const renderer = new THREE.WebGLRenderer();
            // 设置渲染器宽高
            renderer.setSize(width, height);
            // 通过渲染器,将场景和相机进行结合、渲染,得到一个Canvas元素
            renderer.render(scene, camera); //执行渲染操作
            // 将渲染结果加入页面中
            document.body.appendChild(renderer.domElement);
    
        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
  • 相关阅读:
    不可变集合的详细概述
    NC14700 追债之旅 (拆点+最短路)
    RAMAN 中 OPTIMIZATION 优化选项的作用
    【Servlet】1:踏入JavaWeb的第一把钥匙
    2022-09-09 Unity InputSystem4——输入配置文件
    Linux 系统适用范围
    VMWare和CentOS 7 的超级详细的安装步骤!!
    [docker] 网络连接
    造芯:车企们的新赛场
    C++ Reference: Standard C++ Library reference: C Library: cmath: rint
  • 原文地址:https://blog.csdn.net/weixin_42919342/article/details/132982649