学习threejs,使用JSON格式保存和加载整个场景
👨⚕️ 主页: gis分享者
👨⚕️ 感谢各位大佬 点赞👍 收藏⭐ 留言📝 加关注✅!
👨⚕️ 收录于专栏:threejs gis工程师
文章目录
- 一、🍀前言
- 1.1 ☘️THREE toJSON()方法
- 二、🍀使用JSON格式保存和加载整个场景
- 1. ☘️实现思路
- 2. ☘️代码样例
一、🍀前言
本文详细介绍如何基于threejs在三维场景中使用JSON格式保存和加载整个场景,亲测可用。希望能帮助到您。一起学习,加油!加油!
1.1 ☘️THREE toJSON()方法
通过调用Three.js各个对象的.toJSON()方法可以保存这些对象为json格式,比如你通过Threejs开发一个三维模型编辑系统,想保存Threejs中创建的立方体、球体、obj、glb等三维模型的顶点、材质等数据,就可以通过.toJSON()方法实现。
二、🍀使用JSON格式保存和加载整个场景
1. ☘️实现思路
- 1、初始化renderer渲染器
- 2、初始化Scene三维场景
- 3、初始化camera相机,定义相机位置 camera.position.set,定义相机方向lookAt。
- 4、初始化THREE.AmbientLight环境光源,scene场景加入环境光源,初始化THREE.PointLight点光源,设置点光源位置,设置点光源投影,scene添加点光源。
- 5、加载几何模型:创建THREE.AxesHelper坐标辅助工具、THREE.SphereGeometry球体几何体、THREE.BoxGeometry立方体几何体、THREE.PlaneGeometry平面几何体以及THREE.Object3D对象group,group添加球体几何体和立方体几何体为组。scene场景加入THREE.AxesHelper坐标辅助工具、group组以及平面几何体对象(实现细节具体看代码样例,有不懂的地方评论区探讨)。
- 6、加入gui控制,定义保存场景方法exportScene、清理场景方法clearScene、导入场景方法importScene,实现细节具体看代码样例。加入controls,加入stats监控器,监控帧数信息。
2. ☘️代码样例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>learn41(使用JSON格式保存和加载整个场景)</title>
<script src="lib/threejs/127/three.js-master/build/three.js"></script>
<script src="lib/threejs/127/three.js-master/examples/js/utils/SceneUtils.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>
html, body {
margin: 0;
height: 100%;
}
canvas {
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
renderer.shadowMap.type = THREE.PCFSoftShadowMap
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, 50)
camera.lookAt(new THREE.Vector3(0, 0, 0))
}
var light
var initLight = () => {
scene.add(new THREE.AmbientLight(0x444444))
light = new THREE.PointLight(0xffffff)
light.position.set(15, 50, 10)
light.castShadow = true
scene.add(light)
}
var sphere, cube, group
var initModel = () => {
group = new THREE.Object3D()
scene.add(group)
var shereGeometry = new THREE.SphereGeometry(5, 200, 200)
var sphereMaterial = new THREE.MeshLambertMaterial({color:0xaaaaaa})
sphere = new THREE.Mesh(shereGeometry, sphereMaterial)
sphere.position.set(-5, 5, 0)
sphere.castShadow = true
group.add(sphere)
var helper = new THREE.AxesHelper(50)
scene.add(helper)
var cubeGeometry = new THREE.BoxGeometry(10, 10, 8)
var cubeMaterial = new THREE.MeshLambertMaterial({color:0x00ffff})
cube = new THREE.Mesh(cubeGeometry, cubeMaterial)
cube.position.set(15, 5, -5)
cube.castShadow = true
group.add(cube)
var planeGeometry = new THREE.PlaneGeometry(100, 100)
var planeMaterial = new THREE.MeshStandardMaterial({color:0xaaaaaa})
plane = new THREE.Mesh(planeGeometry, planeMaterial)
plane.rotation.x = -0.5 * Math.PI
plane.receiveShadow = true
scene.add(plane)
}
var gui
var initGui = () => {
gui = {
exportScene: () => {
var sceneJson = JSON.stringify(scene.toJSON())
localStorage.setItem('scene', sceneJson)
},
clearScene: () => {
scene = new THREE.Scene()
},
importScene: () => {
var json = localStorage.getItem('scene')
if (json) {
var loadedGeometry = JSON.parse(json)
var loader = new THREE.ObjectLoader()
scene = loader.parse(loadedGeometry)
}
}
}
var datGui = new dat.GUI()
//将设置属性添加到gui当中,gui.add(对象,属性,最小值,最大值)
datGui.add(gui, "exportScene")
datGui.add(gui, "clearScene")
datGui.add(gui, "importScene")
}
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 render = () => {
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()
initGui()
initStats()
initControls()
animate()
window.onresize = onWindowResize
}
</script>
</html>
效果如下: