👨⚕️ 主页: gis分享者
👨⚕️ 感谢各位大佬 点赞👍 收藏⭐ 留言📝 加关注✅!
👨⚕️ 收录于专栏:threejs gis工程师
文章目录
- 一、🍀前言
- 二、🍀绘制任意字体模型
- 1. ☘️实现思路
- 2. ☘️代码样例
一、🍀前言
本文详细介绍如何基于threejs在三维场景中模拟窗户光源,亲测可用。希望能帮助到您。一起学习,加油!加油!
二、🍀绘制任意字体模型
1. ☘️实现思路
- 1、初始化renderer渲染器
- 2、初始化Scene三维场景
- 3、初始化camera相机,定义相机位置 camera.position.set,设置相机方向camera.lookAt
- 4、初始化THREE.AmbientLight环境光源,scene场景加入环境光源,初始化THREE.RectAreaLight窗户光源,设置窗户光源位置,设置窗户光源方向,scene添加窗户光源
- 5、初始化gui工具,定义控制灯光和投影的函数
- 6、加载几何模型:创建SphereGeometry球体、BoxGeometry立方体、PlaneGeometry地面几何体以及AxisHelper辅助工具坐标系,Scene场景加入以上几何体和工具。
- 7、加入controls、gui控制,加入stats监控器,监控帧数信息
2. ☘️代码样例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>learn16(模拟窗户光源)</title>
<script src="lib/threejs/127/three.js-master/build/three.js"></script>
<script src="lib/threejs/127/three.js-master/examples/js/lights/RectAreaLightUniformsLib.js"></script>
<script src="lib/threejs/127/three.js-master/examples/jsm/helpers/RectAreaLightHelper.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 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 rectLight, rectLightHelper
var initLight = () => {
var ambientLight = new THREE.AmbientLight(0x111111)
scene.add(ambientLight)
rectLight = new THREE.RectAreaLight(0xffffff, 500, 10, 10)
rectLight.position.set(5, 10, -20)
rectLight.lookAt( 0, 0, 0 )
scene.add(rectLight)
// rectLightHelper = new THREE.RectAreaLightHelper(rectLight)
// scene.add(rectLightHelper)
}
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: 0xff00ff})
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})
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 gui
var initGui = () => {
gui = {
motion: true,
width: rectLight.width,
height: rectLight.height,
color: rectLight.color.getHex(),
intensity: rectLight.intensity
}
var datGui = new dat.GUI()
datGui.add(gui, 'width', 0.1, 100).onChange(e => {
rectLight.width = e
})
datGui.add(gui, 'height', 0.1, 100).onChange(e => {
rectLight.height = e
})
datGui.addColor(gui, 'color').onChange(e => {
rectLight.color.setHex(e)
})
datGui.add(gui, 'intensity', 0, 1000).onChange(e => {
rectLight.intensity = e
})
datGui.open()
}
var render = () => {
// rectLightHelper.update()
renderer.render(scene, camera)
}
var onWindowResize = () => {
camera.aspect = window.innerWidth/window.innerHeight
camera.updateProjectionMatrix()
render()
renderer.setSize(window.innerWidth, window.innerHeight)
}
var animate = () => {
render()
stats.update()
controls.update()
requestAnimationFrame(animate)
}
var draw = () => {
initRender()
initScene()
initCamera()
initLight()
initModel()
initControls()
initStats()
initGui()
animate()
window.onresize = onWindowResize
}
</script>
</html>
效果如下: