You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
6.2 KiB
JavaScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import { OBJLoader } from 'three/examples/jsm/loaders/OBJLoader'
import { MTLLoader } from 'three/examples/jsm/loaders/MTLLoader'
import {
isLoading,
tuopanLocation,
tishengjiLocation, keyidongModel, storeyHeight, tuopanModel, animationLine, storeyWidth
} from './setThree'
import * as TWEEN from '@tweenjs/tween.js'
import { agvData } from '@/views/board/model/agv'
let tuopanInishengji = true
let s = 1
const tishengjiAnimation = (model, floor, newLocation = 0, time = 2000) => {
return new Promise((resolve, reject) => {
tishengjiLocation.floor = floor
if (!model || tishengjiLocation.y === newLocation) {
resolve()
}
let distance = newLocation - tishengjiLocation.y
let tween = new TWEEN.Tween(model.position)
.to({ y: newLocation }, Math.abs(distance) / s * 16) // 移动到(1, 1, 1)持续1000毫秒
.onComplete(() => {
model.position.y = newLocation
tishengjiLocation.y = newLocation
resolve()
tween.stop()
tween = null
})
.start()
// let time1 = setInterval(() => {
// if ((distance > 0 && model.position.y >= newLocation) || (distance < 0 && model.position.y <= newLocation)) {
// model.position.y = newLocation
// tishengjiLocation.y = newLocation
// resolve()
// clearInterval(time1)
// clearTimeout(time3)
// } else {
// model.position.y += distance / (s / 16)
// tishengjiLocation.y = model.position.y
// }
// }, 16)
// let time3 = setTimeout(() => {
// model.position.y = newLocation
// tishengjiLocation.y = newLocation
// resolve()
// clearInterval(time1)
// clearTimeout(time3)
// }, time + 1000)
})
}
const tuopanXAnimation = (model, newLocation = 0, time = 2000) => {
return new Promise((resolve, reject) => {
if (!model || newLocation === tuopanLocation.x || tishengjiLocation.floor !== tuopanLocation.floor) {
resolve()
} else {
tuopanInishengji = newLocation === 0
let distance = newLocation - tuopanLocation.x
let tween = new TWEEN.Tween(model.position)
.to({ x: newLocation }, Math.abs(distance) / s * 16) // 移动到(1, 1, 1)持续1000毫秒
.onComplete(() => {
model.position.x = newLocation
tuopanLocation.x = newLocation
resolve()
tween.stop()
tween = null
})
.start()
// let time1 = setInterval(() => {
// if ((distance > 0 && model.position.x >= newLocation) || (distance < 0 && model.position.x <= newLocation)) {
// model.position.x = newLocation
// tuopanLocation.x = newLocation
// resolve()
// clearInterval(time1)
// clearTimeout(time3)
// } else {
// model.position.x += distance / (time / 16)
// tuopanLocation.x = model.position.x
// }
// }, 16)
// let time3 = setTimeout(() => {
// model.position.x = newLocation
// tuopanLocation.x = newLocation
// resolve(0)
// clearInterval(time1)
// clearTimeout(time3)
// }, time + 1000)
}
})
}
const tuopanYAnimation = (model, floor, newLocation = 0, time = 2000) => {
return new Promise((resolve, reject) => {
tuopanLocation.floor = floor
if (!model || tuopanLocation.y === newLocation) {
resolve()
}
let distance = newLocation - tuopanLocation.y
let tween = new TWEEN.Tween(model.position)
.to({ y: newLocation }, Math.abs(distance) / s * 16) // 移动到(1, 1, 1)持续1000毫秒
.onComplete(() => {
model.position.y = newLocation
tuopanLocation.y = newLocation
resolve()
tween.stop()
tween = null
})
.start()
// let time1 = setInterval(() => {
// if ((distance > 0 && model.position.y >= newLocation) || (distance < 0 && model.position.y <= newLocation)) {
// model.position.y = newLocation
// tuopanLocation.y = newLocation
// resolve()
// clearInterval(time1)
// clearTimeout(time3)
// } else {
// model.position.y += distance / (time / 16)
// tuopanLocation.y = model.position.y
// }
// }, 16)
// let time3 = setTimeout(() => {
// model.position.y = newLocation
// tuopanLocation.y = newLocation
// resolve()
// clearInterval(time1)
// clearTimeout(time3)
// }, time + 1000)
})
}
const addtuopan = (params) => {
return new Promise((resolve, reject) => {
try {
let objLoader1 = new OBJLoader()
let mtlLoader1 = new MTLLoader()
let objLoader2 = new OBJLoader()
let mtlLoader2 = new MTLLoader()
mtlLoader1.load('/model/jiaodongjichang.mtl', function(materials) {
materials.preload()
objLoader1.setMaterials(materials)
objLoader1.load('/model/jiaodongjichang.obj.obj', function(object1) {
// 将模型添加到场景
params.scene.add(object1)
// mtlLoader2.load('/model/tishengji/tishengji.mtl', function(materials2) {
// materials2.preload()
// objLoader2.setMaterials(materials2)
// objLoader2.load('/model/tishengji/tishengji.obj', function(object2) {
// 将模型添加到场景
// object1.add(object2)
resolve(object1)
// })
// })
})
})
} catch (e) {
resolve()
}
})
}
setInterval(() => {
if (isLoading) {
if (animationLine.length < 5) {
let floor = Math.ceil(Math.random() * 5)
animationLine.push(() => {
if (tuopanInishengji) {
return Promise.all([tishengjiAnimation(keyidongModel, floor, storeyHeight[floor]), tuopanYAnimation(tuopanModel, floor, storeyHeight[floor])])
} else {
animationLine.push(() => tishengjiAnimation(keyidongModel, floor, storeyHeight[floor]))
}
})
if (Math.random() > 0.5) {
animationLine.push(() => tuopanXAnimation(tuopanModel, storeyWidth[floor]))
animationLine.push(() => tuopanXAnimation(tuopanModel, 0))
}
}
}
}, 1800)
export {
tuopanInishengji,
tishengjiAnimation,
tuopanXAnimation,
tuopanYAnimation,
addtuopan
}