template模板中代码如下:
<template>
<div >
</div>
<div class="control">
<button @click="split(true)">分解</button>
<button @click="split(false)">组合</button>
</div>
</template>
<style scoped>
.control{
display: flex;
position: fixed;
top:50px;
right: 50px;
}
.control button {
width: 100px;
height: 30px;
margin: 10px;
}
</style>
import * as THREE from 'three'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import { gsap } from 'gsap'
import { onMounted } from 'vue'
const init = () => {}
let scene, camera, loader, renderer, controls, model
// 初始化场景
const initScene = () => {
scene = new ()
= new (0xcccccc)
= new (0xcccccc)
}
// 添加相机
const initCamera = () => {
camera = new (75, / , 0.01, 1)
(0.08, 0.08, 0.15)
}
// 添加灯光
const initLight = () => {
// 设置环境光
(new (0xffffff, 0.5))
// 添加球光源
const hesLight = new (0xffffff, 0x444444)
= 0.6
(hesLight)
// 自然光
const dirLight = new ()
(0, 0, 15)
(dirLight)
const dirLight2 = new ()
(0, 0, -15)
(dirLight2)
const dirLight3 = new ()
(15, 0, 0)
(dirLight3)
const dirLight4 = new ()
(-15, 0, 0)
(dirLight4)
const dirLight5 = new ()
(0, 15, 0)
(dirLight5)
const dirLight6 = new ()
(0, -15, 0)
(dirLight6)
const dirLight7 = new ()
(5, 15, 5)
(dirLight7)
const dirLight8 = new ()
(-5, -15, -5)
(dirLight8)
// 聚光灯
const sportLight = new (0xffffff, 0.8)
= / 8; //散射角度,跟水平线的夹角
= 0.1; // 聚光锥的半影衰减百分比
= 2; // 纵向:沿着光照距离的衰减量。
= 10;
= 10;
// 阴影映射宽度,阴影映射高度
(512, 512);
(0, 15, 0);
// 光照射的方向
(0, 0, 0);
= true;
(sportLight);
}
// 加载GLTF模型
const initModel = () => {
loader = new GLTFLoader()
('/model/', gltf => {
model =
(0, -0.08, 0)
// 添加模型到场景
(model)
})
}
// 创建渲染器
const initRenderer = () => {
renderer = new ({ antialias: true })
(, )
('scene').appendChild()
}
// 添加控制器
const initControl = () => {
controls = new OrbitControls(camera, )
= true
= 0.25
= true
}
// 渲染循环
const playAnimate = () => {
const animate = function () {
requestAnimationFrame(animate)
(scene, camera)
}
animate()
}
const init = () => {
initScene()
initCamera()
initLight()
initModel()
initRenderer()
initControl()
playAnimate()
}
onMounted(() => {
init()
})
const split = (val) => {
const myModel = [0].children
if(val) {
(item => {
if( === "Obj3d66-5355088-2-382") {
// = 0.03
(,{
duration:1,//动画持续时间
y:0.03,//目标位置
ease:''
})
} else if ( === "Obj3d66-5355088-2-382_1"){
// = -0.05
(,{
duration:1,//动画持续时间
y:-0.05,//目标位置
ease:''
})
}
});
} else {
(item => {
if( === "Obj3d66-5355088-2-382") {
// = 0.03
(,{
duration:1,//动画持续时间
y:0,//目标位置
ease:'line'
})
} else if ( === "Obj3d66-5355088-2-382_1"){
// = -0.05
(,{
duration:1,//动画持续时间
y:0,//目标位置
ease:'line'
})
}
});
}
}