Three.js 光线教程
光线 ·Light· ▶ 在线运行案例案例合集三维可视化功能案例threehub.cn开源仓库github地址https://github.com/z2586300277/three-cesium-examples400个案例代码:网盘链接你将学到什么Scene / Camera / Renderer 标准渲染管线搭建案例完整源码结构与可复用初始化模板效果说明一个受光照影响的立方体能看到明暗面场景中还有平行光辅助线红色线段指示光源位置与照射方向。核心概念材质与光照| 材质 | 是否受光 | 典型用途 | |------|---------|---------| |MeshBasicMaterial| 否 | UI、线框、不受光纯色 | |MeshLambertMaterial| 是漫反射 | 哑光表面 | |MeshPhongMaterial| 是漫反射高光 | 塑料、金属感 | |MeshStandardMaterial| 是PBR 物理 | 现代项目默认 |本案例用MeshLambertMaterial无参数 默认白色必须加光源才能看见。光源类型| 类型 | 特点 | 衰减 | |------|------|------| |AmbientLight| 全局均匀照明无方向 | 无 | |DirectionalLight| 平行光模拟太阳 | 无 | |PointLight| 点光源向四周发射 | 有 distance/decay | |SpotLight| 锥形聚光 | 有 angle/penumbra |// 平行光 颜色 强度 位置方向由 position 指向原点决定const light new THREE.DirectionalLight(0xff00ff, 1.0); light.position.set(100, 0, 200); scene.add(light);DirectionalLightHelper(light, size, color)在场景中画出光源位置与目标方向的参考线便于调试。实现步骤创建 Scene MeshLambertMaterial的 Mesh受光材质添加DirectionalLight并设置 position添加DirectionalLightHelper可视化AxesHelper Camera Renderer一次性渲染代码要点import * as THREE from three;// 1、影响哪些材质 // 不影响MeshBasicMaterial基础 // 影像 MeshLambertMaterial漫反射、MeshPhongMaterial高光、MeshPhysicalMaterial物理、MeshStandardMaterial物理// 2、光源分类、颜色、强度、衰减、位置 // PointLight点光源、SpotLight聚光灯、DirectionalLight平行光、AmbientLight环境光// 场景 const scene new THREE.Scene();// 创建场景 const geometry new THREE.BoxGeometry(100, 100, 100); //几何体 const material new THREE.MeshLambertMaterial(); //材质 const mesh new THREE.Mesh(geometry, material); //网格模型 mesh.position.set(0, 10, 0); //网格模型位置 scene.add(mesh); //场景添加网格模型// AxesHelper const axesHelper new THREE.AxesHelper(150); scene.add(axesHelper);// ---------光源----------- const directionalLight new THREE.DirectionalLight(0xff00ff, 1.0); //颜色、强度 directionalLight.position.set(100, 0, 200); //位置 scene.add(directionalLight); //点光源添加到场景中// -----------光源参考线----------- 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, 10, 0); //相机观察位置// 渲染器 const renderer new THREE.WebGLRenderer(); // 创建渲染器 const box document.getElementById(box); renderer.setSize(box.clientWidth, box.clientHeight); //渲染区域 renderer.render(scene, camera); //执行渲染 box.appendChild(renderer.domElement);完整源码GitHub小结本文提供光线完整 Three.js 源码与在线 Demo建议先运行案例再改 uniform/参数做二次实验更多 Three.js 实战案例见 three-cesium-examples 合集 与 GitHub 开源仓库