当前位置: 首页 > article >正文

three.js 实现模型模型 ,拆解,爆炸,还原的动画效果

three.js 实现模型模型 ,拆解,爆炸,还原的动画效果

在线链接:https://threehub.cn/#/codeMirror?navigation=ThreeJS&classify=basic&id=modelUnpack

国内站点预览:http://threehub.cn

github地址: https://github.com/z2586300277/three-cesium-examples
在这里插入图片描述

import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader.js'
import * as dat from 'dat.gui'
import gsap from 'gsap'

const box = document.getElementById('box')

const scene = new THREE.Scene()

const camera = new THREE.PerspectiveCamera(75, box.clientWidth / box.clientHeight, 0.1, 1000)

camera.position.set(5, 5, 5)

const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true, logarithmicDepthBuffer: true })

renderer.setSize(box.clientWidth, box.clientHeight)

box.appendChild(renderer.domElement)

new OrbitControls(camera, renderer.domElement)

window.onresize = () => {

    renderer.setSize(box.clientWidth, box.clientHeight)

    camera.aspect = box.clientWidth / box.clientHeight

    camera.updateProjectionMatrix()

}

animate()

function animate() {

    requestAnimationFrame(animate)

    renderer.render(scene, camera)

}

scene.add(new THREE.AmbientLight(0xffffff, 1))

const pointLight = new THREE.PointLight(0xffffff, 1.5, 0, 2)

pointLight.position.set(5, 5, 5)

scene.add(pointLight)

const directionalLight = new THREE.DirectionalLight(0xffffff, 2)

directionalLight.position.set(-5, 5, -5)

scene.add(directionalLight)

scene.add(new THREE.AxesHelper(1000))

let group

// 加载模型 gltf/ glb  draco解码器
const loader = new GLTFLoader()

loader.setDRACOLoader(new DRACOLoader().setDecoderPath(`https://file.threehub.cn/` + 'js/three/draco/'))

loader.load(

    `https://threehub.cn/` + '/files/model/car.glb',

    gltf => {

        group = gltf.scene

        const box = new THREE.Box3().setFromObject(group);

        const center = new THREE.Vector3();

        box.getCenter(center);

        group.center = center

        group.traverse((child) => {

            if (child.isMesh) {

                child.localToWorld(child.position)   //转为世界坐标

                child.startPoisition = child.position.clone()

            }

        })

        scene.add(group)

    }

)

// 拆解
const mergeHandle = (model) => {

    const distance = () => Math.random() > 0.5 ? 1.5 : -1.5

    model.traverse((child) => {

        if (child.startPoisition) {

            const v1 = distance()

            const v2 = distance()

            const v3 = distance()

            gsap.to(child.position, { x: child.startPoisition.x + v1, y: child.startPoisition.y + v2, z: child.startPoisition.z + v3, duration: 1, ease: 'power2.inOut' })

        }

    });
};

const gui = new dat.GUI()

gui.add({

    '拆解动画': () => {

        mergeHandle(group)
    }

}, '拆解动画')

gui.add({

    '还原动画': () => {

        group.traverse((child) => {

            if (child.startPoisition) {

                gsap.to(child.position, { x: child.startPoisition.x, y: child.startPoisition.y, z: child.startPoisition.z, duration: 1, ease: 'power2.inOut' })

            }

        });

    }

}, '还原动画')

/**
 * 名称: 模型拆解动画
 * 作者: 优雅永不过时 https://github.com/z2586300277
*/


http://www.kler.cn/news/357895.html

相关文章:

  • 薪资管理系统原型PC端+移动端 Axure原型 交互设计 Axure实战项目
  • 时间复杂度记法(大O记法)相关知识简记
  • MySQL-12.DQL-条件查询
  • 【Python知行篇】代码的曼妙乐章:探索数据与逻辑的和谐之舞
  • git分支操作简记
  • Go语言中的错误处理:使用自定义错误类型和panic/recover机制
  • mysql 主从安装
  • CTE 与存储过程:SQL 查询简化与复杂业务逻辑处理的最佳选择
  • 线程同步之双摄
  • 【ios】在 SwiftUI 中实现可随时调用的加载框
  • React.createRef(),React.forwardRef(),forwardRef()结合next.js的link进行路由跳转
  • MySQL【知识改变命运】06
  • SQL第18课——使用视图
  • 【电子通识】方形贴片固定电阻器制造流程中激光切割的必要性
  • 穷举vs暴搜vs深搜vs回溯vs剪枝(三)
  • rest 参数
  • vue入门四-pinia
  • 4、CSS3笔记
  • (44)MATLAB读取语音信号进行频谱分析
  • 鸿蒙HarmonyOS之跳转页面并传递参数的方法