👨⚕️ 主页: gis分享者
👨⚕️ 感谢各位大佬 点赞👍 收藏⭐ 留言📝 加关注✅!
👨⚕️ 收录于专栏:threejs gis工程师
文章目录
- 一、🍀前言
- 二、🍀绘制任意字体模型
- 1. ☘️实现思路
- 2. ☘️代码样例
一、🍀前言
本文详细介绍如何基于threejs在三维场景中添加环境光和点光源,亲测可用。希望能帮助到您。一起学习,加油!加油!
二、🍀绘制任意字体模型
1. ☘️实现思路
- 1、初始化renderer渲染器
- 2、初始化Scene三维场景
- 3、初始化camera相机,定义相机位置 camera.position.set,设置相机方向camera.lookAt
- 4、初始化THREE.AmbientLight环境光,scene场景加入环境光,初始化THREE.PointLight点光源,设置点光源位置,设置设置投掷阴影为true,scene添加点光源
- 5、初始化gui工具,定义控制灯光和立方体模型位置的函数
- 6、加载几何模型:创建BoxGeometry立方体、PlaneGeometry地面几何体以及AxisHelper辅助工具坐标系,Scene场景加入以上几何体和工具。
- 7、加入controls控制,加入stats监控器,监控帧数信息
2. ☘️代码样例
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>learn12(添加环境光THREE.AmbientLight和点光源THREE.PointLight)</title><script src="lib/threejs/127/three.js-master/build/three.js"></script><script src="lib/threejs/127/three.js-master/examples/js/controls/OrbitControls.js"></script><script src="lib/threejs/127/three.js-master/examples/js/libs/stats.min.js"></script><script src="lib/threejs/127/three.js-master/examples/js/libs/dat.gui.min.js"></script>
</head>
<style>body {margin: 0;}canvas {width: 100%;height: 100%;display: block;}
</style>
<body onload="draw()">
</body>
<script>var renderervar initRender = () => {renderer = new THREE.WebGLRenderer({antialias: true})renderer.setSize(window.innerWidth, window.innerHeight)renderer.shadowMap.enabled = truerenderer.shadowMap.type = THREE.PCFSoftShadowMapdocument.body.appendChild(renderer.domElement)}var cameravar initCamera = () => {camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000)camera.position.set(0, 40, 100)camera.lookAt(new THREE.Vector3(0, 0, 0))}var scenevar initScene = () => {scene = new THREE.Scene()}var ambientLight, pointLightvar initLight = () => {ambientLight = new THREE.AmbientLight('#111111')scene.add(ambientLight)pointLight = new THREE.PointLight('#ffffff')pointLight.position.set(15, 30, 10)pointLight.castShadow = truescene.add(pointLight)}var guivar initGui = () => {gui = {ambientLight: '#111111', //环境光源pointLight: '#ffffff', //点光源lightY: 30, //灯光y轴的位置distance: 0, //点光源距离intensity: 1, //灯光强度visible: true //是否可见}var datGui = new dat.GUI()datGui.addColor(gui, 'ambientLight').onChange(e => {ambientLight.color = new THREE.Color(e)})datGui.addColor(gui, 'pointLight').onChange(e => {pointLight.color = new THREE.Color(e)})datGui.add(gui, "lightY", 0, 100)datGui.add(gui, 'distance', 0, 100).onChange(function (e) {pointLight.distance = e})datGui.add(gui, 'intensity', 0, 5).onChange(function (e) {pointLight.intensity = e})datGui.add(gui, 'visible').onChange(function (e) {pointLight.visible = e})}var cubevar initModel = () => {var helper = new THREE.AxesHelper(10)scene.add(helper)var cubeGeometry = new THREE.BoxGeometry(10, 10, 10)var cubeMaterial = new THREE.MeshLambertMaterial({color: 0x00ffff})cube = new THREE.Mesh(cubeGeometry, cubeMaterial)cube.position.set(5, 5, -5)// cube.position.x = 5// cube.position.y = 5// cube.position.z = -5cube.castShadow = truescene.add(cube)var planeGeometry = new THREE.PlaneGeometry(100, 100)var planeMaterial = new THREE.MeshLambertMaterial({color: 0xaaaaaa})var plane = new THREE.Mesh(planeGeometry, planeMaterial)plane.rotation.x = -0.5 * Math.PIplane.position.y = -0plane.receiveShadow = truescene.add(plane)}var statsvar initStats = () => {stats = new Stats()document.body.appendChild(stats.dom)}var controlsvar initControls = () => {controls = new THREE.OrbitControls(camera, renderer.domElement)// 是否有阻尼或惯性controls.enableDamping = true}// 进行渲染var render = () => {renderer.render(scene, camera)}var onWindowResize = () => {camera.aspect = window.innerWidth / window.innerHeightcamera.updateProjectionMatrix()render()renderer.setSize(window.innerWidth, window.innerHeight)}var animate = () => {render()stats.update()pointLight.position.y = gui.lightYcontrols.update()requestAnimationFrame(animate)}var draw = () => {initGui()initRender()initScene()initCamera()initLight()initModel()initControls()initStats()animate()window.onresize = onWindowResize}
</script>
</html>
效果如下: