【前端知识】Three 学习日志(五)—— 点光源辅助观察

Three 学习日志(五)------ 点光源辅助观察

一、引入点光源辅助观察

javascript 复制代码
// 光源辅助观察
const pointLightHelper = new THREE.PointLightHelper(pointLight, 10);
scene.add(pointLightHelper);

二、改变点光源位置

javascript 复制代码
// 点光源位置
pointLight.position.set(200, 200, 200);
// 改变点光源位置
pointLight.position.set(-100, -100, -100);
scene.add(pointLight); //点光源添加到场景中

三、完整代码

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Learn Three</title>
    <!-- 引入three,下载地址参考:http://www.webgl3d.cn/pages/aac9ab/#%E7%89%B9%E5%AE%9A%E7%89%88%E6%9C%ACthree-js%E6%96%87%E4%BB%B6%E5%8C%85%E4%B8%8B%E8%BD%BD -->
    <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 pointLight = new THREE.PointLight(0xffffff, 1.0);

        // 点光源位置
        pointLight.position.set(200, 200, 200);
        // 改变点光源位置
        pointLight.position.set(-100, -100, -100);
        scene.add(pointLight); //点光源添加到场景中
        // 光源辅助观察
        const pointLightHelper = new THREE.PointLightHelper(pointLight, 10);
        scene.add(pointLightHelper);

        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(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>
相关推荐
How_doyou_do5 小时前
模态框的两种管理思路
java·服务器·前端
snow@li6 小时前
前端:前端/浏览器 可以录屏吗 / 实践 / 录制 Microsoft Edge 标签页、应用窗口、整个屏幕
前端·浏览器录屏·前端录屏·web录屏
李贺梖梖6 小时前
CSS学习
前端·css
蚂小蚁6 小时前
一文吃透:宏任务、微任务、事件循环、浏览器渲染、Vue 批处理与 Node 差异(含性能优化)
前端·面试·架构
狼性书生6 小时前
uniapp实现的Tab 选项卡组件模板
前端·uni-app·vue·组件·插件
吃饺子不吃馅6 小时前
前端画布类型编辑器项目,历史记录技术方案调研
前端·架构·github
拜晨7 小时前
使用motion实现小宇宙贴纸墙效果
前端·交互设计
拜晨7 小时前
使用motion实现小宇宙节目广场的效果
前端·交互设计
知花实央l7 小时前
【Web应用实战】 文件上传漏洞实战:Low/Medium/High三级绕过(一句话木马拿webshell全流程)
前端·学习·网络安全·安全架构