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
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值