👨⚕️ 主页: gis分享者
👨⚕️ 感谢各位大佬 点赞👍 收藏⭐ 留言📝 加关注✅!
👨⚕️ 收录于专栏:threejs gis工程师
文章目录
- 一、🍀前言
- 二、🍀绘制任意字体模型
- 1. ☘️实现思路
- 2. ☘️代码样例
一、🍀前言
本文详细介绍如何基于threejs在三维场景中添加户外光照光源,亲测可用。希望能帮助到您。一起学习,加油!加油!
二、🍀绘制任意字体模型
1. ☘️实现思路
- 1、初始化renderer渲染器
- 2、初始化Scene三维场景
- 3、初始化camera相机,定义相机位置 camera.position.set,设置相机方向camera.lookAt
- 4、初始化THREE.DirectionalLight平行光源,scene场景加入平行光,初始化THREE.HemisphereLight户外光照光源,设置户外光照光源位置,设置设置投掷阴影为true,scene添加平行光源
- 5、初始化gui工具,定义控制灯光和投影的函数
- 6、加载几何模型:创建SphereGeometry球体、BoxGeometry立方体、PlaneGeometry地面几何体以及AxisHelper辅助工具坐标系,Scene场景加入以上几何体和工具。
- 7、加入controls控制,加入stats监控器,监控帧数信息
2. ☘️代码样例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>learn15(户外光照光源)</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 renderer
var initRender = () => {
renderer = new THREE.WebGLRenderer({antialias: true})
renderer.shadowMap.enabled = true
renderer.shadowMap.type = THREE.PCFSoftShadowMap
renderer.setSize(window.innerWidth, window.innerHeight)
document.body.appendChild(renderer.domElement)
}
var scene
var initScene = () => {
scene = new THREE.Scene()
}
var camera
var 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 hemiLight, directionalLight
var initLight = () => {
// 天空颜色、地面颜色、光强, 户外光源
hemiLight = new THREE.HemisphereLight(0x0000ff, 0x00ff00, 0.3)
hemiLight.position.set(0, 500, 0)
scene.add(hemiLight)
directionalLight = new THREE.DirectionalLight('#ffffff')
directionalLight.position.set(-40, 60 - 10)
directionalLight.shadow.camera.near = 20
directionalLight.shadow.camera.far = 200
directionalLight.shadow.camera.left = -50
directionalLight.shadow.camera.right = 50
directionalLight.shadow.camera.top = 50
directionalLight.shadow.camera.bottom = -50
directionalLight.shadow.mapSize.width = 1024
directionalLight.shadow.mapSize.height = 1024
directionalLight.castShadow = true
scene.add(directionalLight)
}
var gui
var initGui = () => {
gui = {
directionalLight: "#ffffff", //点光源
directionalLightIntensity: 1, //灯光强度
visible: true, //是否可见
castShadow: true,
exponent: 30,
target: "plane",
debug: false,
groundColor: "#00ff00",
skyColor: "#0000ff",
hemiLightIntensity: 0.3
}
var datGui = new dat.GUI()
datGui.addColor(gui, 'skyColor').onChange(e => {
hemiLight.color = new THREE.Color(e)
})
datGui.addColor(gui, 'groundColor').onChange(e => {
hemiLight.groundColor = new THREE.Color(e)
})
datGui.add(gui, 'hemiLightIntensity', 0, 1).onChange(e => {
hemiLight.intensity = e
})
datGui.addColor(gui, 'directionalLight').onChange(e => {
directionalLight.color = new THREE.Color(e)
})
datGui.add(gui, 'visible').onChange(e => {
directionalLight.visible = e
})
datGui.add(gui, 'castShadow').onChange(e => {
directionalLight.castShadow = e
})
datGui.add(gui, 'debug').onChange(e => {
if (e) {
var debug = new THREE.CameraHelper(directionalLight.shadow.camera)
debug.name = 'debug'
scene.add(debug)
} else {
var debug = scene.getObjectByName('debug')
scene.remove(debug)
}
})
}
var stats
var initStats = () => {
stats = new Stats()
document.body.appendChild(stats.dom)
}
var controls
var initControls = () => {
controls = new THREE.OrbitControls(camera, renderer.domElement)
controls.enableDamping = true
}
var cube, sphere, plane
var initModel = () => {
var helper = new THREE.AxisHelper(10)
scene.add(helper)
var sphereGeometry = new THREE.SphereGeometry(10, 30, 30)
var sphereMaterial = new THREE.MeshStandardMaterial({color: 0xeeeeee})
var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial)
sphere.position.set(-20, 20, 0)
sphere.castShadow = true
scene.add(sphere)
var cubeGeometry = new THREE.BoxGeometry(10, 10, 10)
var cubeMaterial = new THREE.MeshLambertMaterial({color: 0x00ffff})
var cube = new THREE.Mesh(cubeGeometry, cubeMaterial)
cube.position.set(30, 5, -5)
cube.castShadow = true
scene.add(cube)
var planeGeometry = new THREE.PlaneGeometry(5000, 5000, 20, 20)
var planeMaterial = new THREE.MeshLambertMaterial({color: 0xaaaaaa})
plane = new THREE.Mesh(planeGeometry, planeMaterial)
plane.rotation.x = -0.5 * Math.PI
plane.position.y = -0
plane.receiveShadow = true
scene.add(plane)
}
var render = () => {
renderer.render(scene, camera)
}
var onWindowResize = () => {
camera.aspect = window.innerWidth / window.inner
camera.updateProjectionMatrix()
render()
renderer.setSize(window.innerWidth, window.innerHeight)
}
var animate = () => {
render()
stats.update()
controls.update()
requestAnimationFrame(animate)
}
var draw = () => {
initGui()
initRender()
initScene()
initCamera()
initLight()
initModel()
initStats()
initControls()
animate()
window.onresize = onWindowResize
}
</script>
</html>
效果如下: