javascript技巧

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript技巧 > threejs使用JSON加载场景

threejs使用JSON格式保存和加载整个场景分析

作者:gis分享者

本文介绍了如何使用Three.js将三维场景保存为JSON格式,并加载整个场景,通过调用各个对象的.toJSON()方法,可以保存和加载立方体、球体、obj、glb等三维模型的顶点和材质数据,文章详细讲解了实现思路和代码样例,一起看看吧

一、🍀前言

本文详细介绍如何基于threejs在三维场景中使用JSON格式保存和加载整个场景,亲测可用。希望能帮助到您。一起学习,加油!加油!

1.1 ☘️THREE toJSON()方法

通过调用Three.js各个对象的.toJSON()方法可以保存这些对象为json格式,比如你通过Threejs开发一个三维模型编辑系统,想保存Threejs中创建的立方体、球体、obj、glb等三维模型的顶点、材质等数据,就可以通过.toJSON()方法实现。

二、🍀使用JSON格式保存和加载整个场景

1. ☘️实现思路

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>

效果如下:

到此这篇关于学习threejs,使用JSON格式保存和加载整个场景的文章就介绍到这了,更多相关threejs使用JSON加载场景内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文