chatgpt辅助修改Three.js电器柜拆分代码

chatgpt辅助修改Three.js电器柜拆分代码

记录一次chatgpt辅助修改代码的经历

简单讲解一下我的需求,我在建模软件blender之中给我的电器柜做了一个拆分动画,将电器柜里面的零件拆出,分布在空间之中,用于可视化查询一些零件信息,但是该电器柜模型中的每一个零件都有独立动画,而我的原始代码只能播放其中的一个零件的动画。

简单来说,我需要让我原来只能播放一个零件动画的代码,变成能播放全部零件动画的代码

这是我原来的代码,由于用于demo演示,所以写的很屎。

<template>
  <div id="container">
    <div>
      <button class="btn" @click="split">拆分</button>
    </div>
    <div>
      <button class="btn" @click="combine">复原</button>
    </div>
  </div>
</template>

<script>
/* eslint-disable */
import * as THREE from 'three'
import {OrbitControls} from 'three/addons/controls/OrbitControls.js'
import {GLTFLoader} from 'three/addons/loaders/GLTFLoader.js'

let mixer
let clock
let ani
let lock = 0
export default {
  name: 'App',
  methods:{
    split(){
      if(lock==1){
        return
      }
      let action = mixer.clipAction(ani)
      action.timeScale = 1;
      action.paused=false;
      action.play()
      lock=1
      setTimeout(function() {
        // 暂停动画
        action.paused = true;
        lock=0
      }, 4000); // 等待1000毫秒(1秒)
    },
    combine(){
      if(lock==1){
        return
      }
      let action = mixer.clipAction(ani)
      action.timeScale = -1;
      action.paused=false;
      action.play()
      lock=1
      setTimeout(function() {
        // 暂停动画
        action.paused = true;
        lock=0
      }, 3990); // 等待1000毫秒(1秒)
    },
    init(){
      // 创建时钟
      clock = new THREE.Clock()
      // 创建场景
      const scene = new THREE.Scene();
      
      const url = '电器柜拆分.glb';

      // 创建相机
      const camera = new THREE.PerspectiveCamera();
      camera.position.set(-26,30,50)
    

      // 添加环境光
      const ambientLight = new THREE.AmbientLight(0xffffff, 1);
      const pointLight1 = new THREE.PointLight(0xffffff,1000,0);
      const pointLight2 = new THREE.PointLight(0xffffff,1000,0);
      const pointLight3 = new THREE.PointLight(0xffffff,1000,0);
      const pointLight4 = new THREE.PointLight(0xffffff,1000,0);
      const pointLight5 = new THREE.PointLight(0xffffff,2000,40);

      pointLight1.position.set(-25,5,25)
      pointLight2.position.set(-25,5,-25)
      pointLight3.position.set(25,5,25)
      pointLight4.position.set(25,5,-25)
      pointLight5.position.set(0,20,20)
      scene.add(pointLight1);
      scene.add(pointLight2);
      scene.add(pointLight3);
      scene.add(pointLight4);
      scene.add(pointLight5);
      scene.add(ambientLight);

      var gridHelper = new THREE.GridHelper(200, 100, 0x888888, 0x888888);
      gridHelper.position.x=0;
      gridHelper.position.z=-10;
      scene.add(gridHelper);

      // 创建渲染器
      const renderer = new THREE.WebGLRenderer({
          alpha: true, // 开启透明度
          antialias: true // 抗锯齿
      });
      var container = document.getElementById('container');
      container.appendChild(renderer.domElement);
      renderer.setSize(container.clientWidth,container.clientHeight)

      // 加载 glb 格式的 3D 模型
      const loader = new GLTFLoader();
      loader.load(
      url,
      (gltf) => {
          gltf.scene.scale.set(0.2, 0.33, 0.2); // 缩小模型
          gltf.scene.position.set(5,3,0); // 将模型位置移到原点处
          mixer = new THREE.AnimationMixer(gltf.scene)
          ani = gltf.animations[0]
          //mixer.clipAction(gltf.animations[0]).play()
          scene.add(gltf.scene);
      },
      (xhr) => {
          // 加载过程中的回调函数
          console.log((xhr.loaded / xhr.total) * 100 + "% loaded");
      },
      (error) => {
          // 加载失败的回调函数
          console.error("Failed to load model", error);
      }
      );

      const controls = new OrbitControls(camera,renderer.domElement)
      controls.target = new THREE.Vector3(0, 10, 0)
      // 渲染场景
      const animate = () => {
      controls.update();
          requestAnimationFrame(animate);
          if (mixer) mixer.update(clock.getDelta())
          renderer.render(scene, camera);
      };

      animate();
    }
  },
  mounted(){
    this.init();
  }
}
</script>

<style>
#container{
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
.btn{
  width: 100px;
  height: 50px;
  margin-bottom: 20px;
}
</style>

该代码运行效果如下

在这里插入图片描述

随后我利用chatgpt修改我的代码

我先把我的代码告诉他让他记住

在这里插入图片描述

为了让他记得深刻一点,并看看他理解的对不对,我让他讲解一下

在这里插入图片描述

随后我将我的需求告诉他

在这里插入图片描述

随后chatgpt为我生成修改后的代码

<template>
  <div id="container">
    <div>
      <button class="btn" @click="split">拆分</button>
    </div>
    <div>
      <button class="btn" @click="combine">复原</button>
    </div>
  </div>
</template>

<script>
import * as THREE from 'three'
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'

let mixer
let clock
let animations = []
let lock = 0

export default {
  name: 'App',
  methods: {
    split() {
      if (lock == 1) {
        return
      }
      lock = 1
      animations.forEach((ani) => {
        let action = mixer.clipAction(ani)
        action.timeScale = 1
        action.paused = false
        action.play()
      })
      setTimeout(function () {
        animations.forEach((ani) => {
          let action = mixer.clipAction(ani)
          action.paused = true
        })
        lock = 0
      }, 4000)
    },
    combine() {
      if (lock == 1) {
        return
      }
      lock = 1
      animations.forEach((ani) => {
        let action = mixer.clipAction(ani)
        action.timeScale = -1
        action.paused = false
        action.play()
      })
      setTimeout(function () {
        animations.forEach((ani) => {
          let action = mixer.clipAction(ani)
          action.paused = true
        })
        lock = 0
      }, 3990)
    },
    init() {
      clock = new THREE.Clock()
      const scene = new THREE.Scene()
      const camera = new THREE.PerspectiveCamera()
      camera.position.set(-26, 30, 50)

      const ambientLight = new THREE.AmbientLight(0xffffff, 1)
      const pointLight1 = new THREE.PointLight(0xffffff, 1000, 0)
      const pointLight2 = new THREE.PointLight(0xffffff, 1000, 0)
      const pointLight3 = new THREE.PointLight(0xffffff, 1000, 0)
      const pointLight4 = new THREE.PointLight(0xffffff, 1000, 0)
      const pointLight5 = new THREE.PointLight(0xffffff, 2000, 40)

      pointLight1.position.set(-25, 5, 25)
      pointLight2.position.set(-25, 5, -25)
      pointLight3.position.set(25, 5, 25)
      pointLight4.position.set(25, 5, -25)
      pointLight5.position.set(0, 20, 20)
      scene.add(pointLight1)
      scene.add(pointLight2)
      scene.add(pointLight3)
      scene.add(pointLight4)
      scene.add(pointLight5)
      scene.add(ambientLight)

      var gridHelper = new THREE.GridHelper(200, 100, 0x888888, 0x888888)
      gridHelper.position.x = 0
      gridHelper.position.z = -10
      scene.add(gridHelper)

      const renderer = new THREE.WebGLRenderer({
        alpha: true,
        antialias: true
      })
      var container = document.getElementById('container')
      container.appendChild(renderer.domElement)
      renderer.setSize(container.clientWidth, container.clientHeight)

      const loader = new GLTFLoader()
      loader.load(
        '电器柜拆分.glb',
        (gltf) => {
          gltf.scene.scale.set(0.2, 0.33, 0.2)
          gltf.scene.position.set(5, 3, 0)
          mixer = new THREE.AnimationMixer(gltf.scene)
          gltf.animations.forEach((ani) => {
            animations.push(ani)
          })
          scene.add(gltf.scene)
        },
        (xhr) => {
          console.log((xhr.loaded / xhr.total) * 100 + '% loaded')
        },
        (error) => {
          console.error('Failed to load model', error)
        }
      )

      const controls = new OrbitControls(camera, renderer.domElement)
      controls.target = new THREE.Vector3(0, 10, 0)

      const animate = () => {
        controls.update()
        requestAnimationFrame(animate)
        if (mixer) mixer.update(clock.getDelta())
        renderer.render(scene, camera)
      }

      animate()
    }
  },
  mounted() {
    this.init()
  }
}
</script>

<style>
#container {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
.btn {
  width: 100px;
  height: 50px;
  margin-bottom: 20px;
}
</style>

直接将代码粘贴进去源文件中运行

Boom!!直接就完成了,还帮我解决了我头疼的一些小bug!!太爽!~~

下面是修改完的效果

在这里插入图片描述

  • 34
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值