👨⚕️ 主页: gis分享者
👨⚕️ 感谢各位大佬 点赞👍 收藏⭐ 留言📝 加关注✅!
👨⚕️ 收录于专栏:threejs gis工程师
文章目录
- 一、🍀前言
- 二、🍀绘制任意字体模型
- 1. ☘️实现思路
- 2. ☘️代码样例
一、🍀前言
本文详细介绍基于threejs如何绘制任意文字模型,亲测可用。希望能帮助到您。一起学习,加油!加油!
二、🍀绘制任意字体模型
1. ☘️实现思路
- 1、初始化renderer渲染器
- 2、初始化Scene三维场景
- 3、初始化camera相机,定义相机位置 camera.position.set
- 4、初始化light灯光,定义灯光位置,开启light.castShadow灯光阴影,Scene场景加入灯光
- 5、加载几何模型:创建SphereGeometry球体、BoxGeometry立方体、PlaneGeometry地面几何体以及AxisHelper辅助工具坐标系,Scene场景加入以上几何体和工具。
- 6、加入controls控制,加入stats监控器,监控帧数信息
2. ☘️代码样例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>learn9(实现阴影效果)</title>
<script src="lib/threejs/127/three.js-master/build/three.js"></script>
<script src="lib/threejs/127/three.js-master/examples/js/controls/TrackballControls.js"></script>
<script src="lib/threejs/127/three.js-master/examples/js/libs/stats.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.setSize(window.innerWidth, window.innerHeight)
renderer.shadowMap.enabled = true
// 默认的是,没有设置的这个清晰 THREE.PCFShadowMap
renderer.shadowMap.type = THREE.PCFSoftShadowMap
document.body.appendChild(renderer.domElement)
}
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 scene
var initScene = () => {
scene = new THREE.Scene()
// scene.fog = new THREE.FogExp2(0xffffff,0.02)
scene.fog = new THREE.Fog(0xffffff, 100, 120)
}
var light
var initLight = () => {
scene.add(new THREE.AmbientLight(0x444444))
light = new THREE.SpotLight(0xffffff)
light.position.set(60, 30, 0)
//告诉平行光需要开启阴影投射
light.castShadow = true
scene.add(light)
}
var initModel = () => {
// 球
var sphereGemometry = new THREE.SphereGeometry(5, 20, 20)
var sphereMatarial = new THREE.MeshStandardMaterial({color: 0x7777ff})
var sphere = new THREE.Mesh(sphereGemometry, sphereMatarial)
sphere.position.set(0, 5, 0)
sphere.castShadow = true
scene.add(sphere)
//辅助工具
var helper = new THREE.AxisHelper(10)
scene.add(helper)
//立方体
var cubeGeometry = new THREE.BoxGeometry(10, 10, 8)
var cubeMaterial = new THREE.MeshLambertMaterial({color: 0x00ffff})
var cube = new THREE.Mesh(cubeGeometry, cubeMaterial)
cube.position.x = 25
cube.position.y = 5
cube.position.z = -5
//告诉立方体需要投射阴影
cube.castShadow = true
scene.add(cube)
// 底面
var planeGeometry = new THREE.PlaneGeometry(100, 100)
var planeMatarial = new THREE.MeshStandardMaterial({color: 0xaaaaaa})
var plane = new THREE.Mesh(planeGeometry, planeMatarial)
plane.rotation.x = -0.5 * Math.PI
plane.position.y = -0
plane.receiveShadow = true
scene.add(plane)
}
var stats
var initStats = () => {
stats = new Stats()
document.body.appendChild(stats.dom)
}
var controls
var initControle = () => {
controls = new THREE.TrackballControls(camera, renderer.domElement)
controls.addEventListener('change', render)
}
var render = () => {
renderer.render(scene, camera)
}
var onWindwosResize = () => {
camera.aspect = window.innerWidth / window.innerHeight
camera.updateProjectionMatrix()
controls.handleResize()
render()
renderer.setSize(window.innerWidth, window.innerHeight)
}
var animate = () => {
render()
stats.update()
controls.update()
requestAnimationFrame(animate)
}
var draw = () => {
initRender()
initScene()
initCamera()
initLight()
initModel()
initControle()
initStats()
animate()
window.onresize = onWindwosResize
}
</script>
</html>
效果如下: