vue-three 日常记录 人员定位/模型动态实时移动/前后端分离/websocket/前端vue-three/解决内存占用大的问题

11 篇文章 1 订阅
本文介绍了一个使用Django后端、Vue前端和Websocket通信的人员定位系统。前端通过Three.js展示三维模型,并利用watch监听坐标变化,动态更新模型位置。在模型移动时,通过删除并重新添加模型组内的mesh来实现平滑移动,关键代码包括加载模型、监听坐标变化以及模型的删除与添加操作。
摘要由CSDN通过智能技术生成

由于项目需要做一个人员定位的系统,最后采取的Django的后端框架,前端的VUE框架,前后端分离,通讯用的websocket。前端三维模型的展示用的three

注:以下是我的思路,现在可以实现,如有问题或者改进,欢迎提问。
以下内容只是我前端模型动态移动的代码和思路

定义一个函数为加载模型的函数。记住一定要mesh放置在group中,这是为了后面移除的时候方便
加载模型函数

loaddog(){
        this.group1=new THREE.Group()//添加一个组,group为全局变量
        var loader = new GLTFLoader();
        loader.load("/static/model/Duck/dog/dog.gltf", (gltf) => {
         this.group1.add(gltf.scene)
         this.group1.scale.set(20,20,20)
         this.group1.position.set(0,0,0)
         this.scene.add(this.group1) 
        /* 
        this.meshc = gltf.scene;
        this.meshc.position.set(this.positions[0],0,this.positions[1])//模型的坐标位置
        this.scene.add(this.meshc);
        this.meshc.scale.set(20, 20, 20);//模型的拉伸 
       */
       }); 
    },

监听坐标的变化

watch:{
       positions: {
      handler(newVal, oldVal) {
        console.log('1',newVal);
        console.log(oldVal);
        //console.log('1',this.renderer.info)
        this.scene.remove(this.group1)
        // 删除掉所有的模型组内的mesh
        this.group1.traverse(function (item) {
            if (item instanceof THREE.Mesh) {
                item.geometry.dispose(); // 删除几何体
                item.material.dispose(); // 删除材质
                item=null
            }
        });
        this.scene.add(this.group1)       
        this.group1.position.set(oldVal[0],0,oldVal[1])
        this.group1.lookAt(newVal[0],0,newVal[1])    
      }
    },
  },

其中positions为后端发到前端的坐标,形式为[‘x’,‘y’]
其中最重要的是为删除掉所有的模型组内的mesh。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北漂研究生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值