rt-sim-training-client/src/jlmap3d/jl3dpassflow/jl3dpassflownew.js
2020-09-17 15:40:55 +08:00

1676 lines
47 KiB
JavaScript

import { Staticmodel } from '@/jlmap3d/jl3dpassflow/config.js';
//loader
import { FBXLoader } from '@/jlmap3d/main/loaders/FBXLoader';
import { OBJLoader } from '@/jlmap3d/main/loaders/OBJLoader';
import { OrbitControls } from '@/jlmap3d/main/control/OrbitControls';
import { ModelManager } from '@/jlmap3d/jl3dpassflow/loader.js';
import { SkeletonUtils } from '@/jlmap3d/main/utils/SkeletonUtils.js';
import { Pathfinding } from '@/jlmap3d/jl3dpassflow/utils/Pathfinding.js';
import { ZoneManager } from '@/jlmap3d/jl3dpassflow/model/zonemanager.js';
import {Materialload} from '@/jlmap3d/main/loaders/Materialload.js';
import { getPublishMapVersion, getPublishMapDetail,getPublish3dMapDetail} from '@/api/jlmap3d/load3ddata';
import { PassflowConnect } from '@/jlmap3d/jl3dpassflow/connect/passflowconnect.js';
import StompClient from '@/utils/sock';
import store from '@/store/index_APP_TARGET';
// import { Loading } from 'element-ui';
let clock = new THREE.Clock();
let delta;
let scene,camerass,renderer;
let rendermode = 0;
let aiswitch = 0;
let mixers = [];
let testmesh1,testmesh2;
let humans = [];
let checkdoor1 = [];
let checkdoor2 = [];
let station;
let zhajiinmodel;
let zhajioutmodel;
let monitor;
let ids = 0;
let humanlist = new THREE.Group();
let originhuman1 = null;
let originhuman2 = null;
let originanima1 = null;
let originanima2 = null;
let toptrain = null;
let downtrain = null;
let zhajiin = [];
let zhajiout = [];
let deviceaction = [];
let passerwebwork = new Worker("../../static/workertest/passsimulation/passer.js");
let stationwebwork = new Worker("../../static/workertest/passsimulation/station.js");
let stationzon = new ZoneManager();
//老版本临时ai控制
let olddataai = false;
for(let i=0;i<5;i++){
checkdoor1[i]={
id:"c1"+i,
status:0,
pos:[],
max:10
};
}
for(let i=0;i<5;i++){
let zhaji = {
id:"in0"+(i+1),
status:0,
waiting:0
}
zhajiin.push(zhaji);
}
for(let i=0;i<5;i++){
let zhaji = {
id:"out0"+(i+1),
status:0,
waiting:0
}
zhajiout.push(zhaji);
}
let stationleft = [];
let stationright = [];
for(let i=0;i<22;i++){
let sl = {
id:"left"+i,
status:0,
waiting:0
}
stationleft.push(sl);
}
for(let i=0;i<22;i++){
let sr = {
id:"right"+i,
status:0,
waiting:0
}
stationright.push(sr);
}
let windowWidth = window.innerWidth ;
let windowHeight = window.innerHeight;
let views4 = [
{
left: 0,
bottom: 0,
width: 0.24,
height: 0.5,
background: new THREE.Color( 0.5, 0.5, 0.7 ),
eye: [ 3.7, 16, 26 ],
up: [3.7, 10 ,16 ],
fov: 30
},
{
left:0.25,
bottom: 0,
width: 0.24,
height: 0.5,
background: new THREE.Color( 0.5, 0.5, 0.7 ),
eye: [ 3.7,17,-4 ],
up: [ 3.7, 10 ,16],
fov: 30
},
{
left: 0.5,
bottom: 0,
width: 0.24,
height: 0.5,
background: new THREE.Color( 0.7, 0.5, 0.5 ),
eye: [ -60, 6,11],
up: [ -59, 5.9,11 ],
fov: 45
},
{
left: 0.75,
bottom: 0,
width: 0.24,
height: 0.5,
background: new THREE.Color( 0.5, 0.7, 0.7 ),
eye: [ -7,17,2],
up: [-7, 10, 8],
fov: 60
}
];
let views2 = [
{
left: 0,
bottom: 0,
width: 0.5,
height: 0.5,
background: new THREE.Color( 0.5, 0.5, 0.7 ),
eye: [ 3.7, 16, 26 ],
up: [3.7, 10 ,16 ],
fov: 60
},
{
left: 0.5,
bottom: 0,
width: 0.5,
height: 0.5,
background: new THREE.Color( 0.7, 0.5, 0.5 ),
eye: [ -60, 6,11],
up: [ -59, 5.9,11 ],
fov: 60
},
];
export function Jl3dpassflow(dom,skinCode,routegroup,viewMap) {
let scope = this;
initView(viewMap);
this.dom = dom;
this.nowcode = null;
this.animateswitch = false;
this.signallights = [];
this.mixers = [];
this.showmodel = null;
//替换材质组(站台的)
this.stationtexture = [];
//初始化webgl渲染
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setClearColor(new THREE.Color(0x000000));
renderer.setSize(dom.offsetWidth, dom.offsetHeight);
windowWidth = dom.offsetWidth ;
windowHeight = dom.offsetHeight;
this.dom.appendChild(renderer.domElement);
//定义相机
camerass = new THREE.PerspectiveCamera(70, dom.offsetWidth / dom.offsetHeight, 0.01, 1000);
camerass.position.set(0, 80, 40);
camerass.aspect = dom.offsetWidth / dom.offsetHeight;
camerass.updateProjectionMatrix();
for ( let ii = 0; ii < views4.length; ++ ii ) {
let view = views4[ ii ];
let camera = new THREE.PerspectiveCamera( view.fov, window.innerWidth / window.innerHeight, 1, 200 );
camera.position.fromArray( view.eye );
camera.lookAt( view.up[0],view.up[1],view.up[2] );
view.camera = camera;
}
for ( let ii = 0; ii < views2.length; ++ ii ) {
let view = views2[ ii ];
let camera = new THREE.PerspectiveCamera( view.fov, window.innerWidth / window.innerHeight, 1, 200 );
camera.position.fromArray( view.eye );
camera.lookAt( view.up[0],view.up[1],view.up[2] );
view.camera = camera;
}
//定义场景(渲染容器)
scene = new THREE.Scene();
scene.background = new THREE.Color(0xa0a0a0);
let mesh = new THREE.Mesh(new THREE.PlaneBufferGeometry(200, 200), new THREE.MeshPhongMaterial({ color: 0x999999, depthWrite: false }));
mesh.rotation.x = - Math.PI / 2;
mesh.receiveShadow = true;
scene.add(mesh);
let grid = new THREE.GridHelper(200, 20, 0x000000, 0x000000);
grid.material.opacity = 0.2;
grid.material.transparent = true;
scene.add(grid);
//定义全局光
let ambientLight = new THREE.AmbientLight(0xffffff, 1.3);
scene.add(ambientLight);
//
// let spotLight = new THREE.SpotLight(0xffffff);
// spotLight.position.set(-50, 60, 15);
// spotLight.castShadow = true;
// spotLight.shadow.mapSize.width = 1024;
// spotLight.shadow.mapSize.height = 1024;
// scene.add(spotLight);
this.controls = new THREE.OrbitControls(camerass, dom);
this.controls.maxPolarAngle = Math.PI / 2;
this.controls.minPolarangle = Math.PI / 5;
this.controls.maxDistance = 800;
this.controls.screenSpacePanning = true;
this.controls.update();
this.selectmodel = null;
const ZONE = 'level';
const SPEED = 10;
const OFFSET = 0.2;
THREE.Pathfinding = Pathfinding;
let level, navmesh;
let groupID, path;
let playerPosition = new THREE.Vector3( -3.5, 0.5, 5.5 );
let targetPosition = new THREE.Vector3();
let pathfinder = new THREE.Pathfinding();
let mouse = new THREE.Vector2();
let raycaster = new THREE.Raycaster();
//上车控制开关
let toppasserin = false;
let downpasserin = false;
//下车控制开关
let toppasseron = false;
let downpasseron = false;
let loader = new THREE.OBJLoader();
// load a resource
loader.load(
// resource URL
'../../static/jl3d/path/path.obj',
// called when resource is loaded
function ( object ) {
// console.time('createZone()');
const zone = THREE.Pathfinding.createZone(object.children[0].geometry);
// console.timeEnd('createZone()');
pathfinder.setZoneData( ZONE, zone );
const navWireframe = new THREE.Mesh(object.children[0].geometry, new THREE.MeshBasicMaterial({
color: 0x808080,
wireframe: true
}));
navWireframe.position.y = OFFSET / 2;
// scene.add(navWireframe);
navmesh = object.children[0];
// scene.add(navmesh);
// Set the player's navigation mesh group
groupID = pathfinder.getGroup( ZONE, playerPosition );
},
// called when loading is in progresses
function ( xhr ) {
console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
},
// called when loading has errors
function ( error ) {
console.log( 'An error happened' );
}
);
// document.addEventListener( 'mouseup', onDocumentMouseUp, false );
function onDocumentMouseUp (event) {
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
camerass.updateMatrixWorld();
raycaster.setFromCamera( mouse, camerass );
const intersects = raycaster.intersectObject( navmesh );
if ( !intersects.length ) return;
console.log(intersects[0].point);
targetPosition.copy( intersects[0].point );
// Teleport on ctrl/cmd click or RMB.
if (event.metaKey || event.ctrlKey || event.button === 2) {
path = null;
originhuman1.position.copy( playerPosition.copy( targetPosition ) );
return;
}
path = pathfinder.findPath( playerPosition, targetPosition, ZONE, groupID );
let points = [];
points.push(new THREE.Vector3(originhuman1.position.x,originhuman1.position.y,originhuman1.position.z));
for(let i=0;i<path.length;i++){
points.push(path[i]);
}
originhuman1.curve = new THREE.CatmullRomCurve3(points);
originhuman1.curve.curvrtype = "centripetal";
originhuman1.progress = 0;
console.log(path);
}
function tick ( dt ) {
if(originhuman1.curve){
if(originhuman1.progress<1){
let point = originhuman1.curve.getPointAt(originhuman1.progress);
//更新模型坐标
originhuman1.position.x = point.x;
originhuman1.position.y = point.y;
originhuman1.position.z = point.z;
if((originhuman1.progress+0.001)<1){
let tangent = originhuman1.curve.getPointAt(originhuman1.progress+0.001);
originhuman1.lookAt(new THREE.Vector3(tangent.x,originhuman1.position.y,tangent.z));
}
originhuman1.progress += 0.05;
}else{
playerPosition.copy( originhuman1.position )
}
}
}
window.onresize = function () {
renderer.setSize(scope.dom.offsetWidth, scope.dom.offsetHeight);
windowWidth = scope.dom.offsetWidth ;
windowHeight = scope.dom.offsetHeight;
camerass.aspect = scope.dom.offsetWidth / scope.dom.offsetHeight;
camerass.updateProjectionMatrix();
}
this.anime = null;
this.modelmanager = new ModelManager();
// let loadingInstance = Loading.service({ fullscreen: true });
this.nowstation = null;
let stationlist = [];
let socktest = null;
getPublish3dMapDetail(skinCode).then(netdata3d => {
this.modelmanager.loadpromise(JSON.parse(netdata3d.data.assets).cctvAssetList, scope.mixers).then(function (data) {
// console.log(scope.modelmanager);
initstationanimation(scope.modelmanager.station.mesh);
initzhajiinaimation(scope.modelmanager.zhajiin.mesh);
initzhajioutanimation(scope.modelmanager.zhajiout.mesh);
inittrain(scope.modelmanager.train.mesh);
scene.add(scope.modelmanager.section.mesh);
// scene.add(scope.modelmanager.train.mesh);
level = scope.modelmanager.station.mesh;
monitor = scope.modelmanager.monitor.mesh;
scene.add(monitor);
Materialload(scope,JSON.parse(netdata3d.data.assets).stationTextureList[0]);
getPublishMapDetail(skinCode).then(netdata => {
scope.switchviews(4);
inithumans();
initstationlist(netdata.data.stationList,netdata.data.stationStandList,netdata.data.psdList);
socktest = new PassflowConnect(scope,deviceaction,toptrain,downtrain,routegroup);
});
// loadingInstance.close();
store.dispatch('app/animationsClose');
animate();
})
});
function initView(viewMap){
if(viewMap == "normal"){
views4 = [
{
left: 0,
bottom: 0,
width: 0.5,
height: 0.5,
background: new THREE.Color( 0.5, 0.5, 0.7 ),
eye: [ 3.7, 16, 26 ],
up: [3.7, 10 ,16 ],
fov: 30
},
{
left: 0,
bottom: 0.5,
width: 0.5,
height: 0.5,
background: new THREE.Color( 0.5, 0.5, 0.7 ),
eye: [ 3.7,17,-4 ],
up: [ 3.7, 10 ,16],
fov: 30
},
{
left: 0.5,
bottom: 0,
width: 0.5,
height: 0.5,
background: new THREE.Color( 0.7, 0.5, 0.5 ),
eye: [ -60, 6,11],
up: [ -59, 5.9,11 ],
fov: 45
},
{
left: 0.5,
bottom: 0.5,
width: 0.5,
height: 0.5,
background: new THREE.Color( 0.5, 0.7, 0.7 ),
eye: [ -7,17,2],
up: [-7, 10, 8],
fov: 60
}
];
views2 = [
{
left: 0,
bottom: 0,
width: 0.5,
height: 1,
background: new THREE.Color( 0.5, 0.5, 0.7 ),
eye: [ 3.7, 16, 26 ],
up: [3.7, 10 ,16 ],
fov: 60
},
{
left: 0.5,
bottom: 0,
width: 0.5,
height: 1,
background: new THREE.Color( 0.7, 0.5, 0.5 ),
eye: [ -60, 6,11],
up: [ -59, 5.9,11 ],
fov: 60
},
];
}else{
views4 = [
{
left: 0,
bottom: 0,
width: 0.24,
height: 0.5,
background: new THREE.Color( 0.5, 0.5, 0.7 ),
eye: [ 3.7, 16, 26 ],
up: [3.7, 10 ,16 ],
fov: 30
},
{
left:0.25,
bottom: 0,
width: 0.24,
height: 0.5,
background: new THREE.Color( 0.5, 0.5, 0.7 ),
eye: [ 3.7,17,-4 ],
up: [ 3.7, 10 ,16],
fov: 30
},
{
left: 0.5,
bottom: 0,
width: 0.24,
height: 0.5,
background: new THREE.Color( 0.7, 0.5, 0.5 ),
eye: [ -60, 6,11],
up: [ -59, 5.9,11 ],
fov: 45
},
{
left: 0.75,
bottom: 0,
width: 0.24,
height: 0.5,
background: new THREE.Color( 0.5, 0.7, 0.7 ),
eye: [ -7,17,2],
up: [-7, 10, 8],
fov: 60
}
];
views2 = [
{
left: 0,
bottom: 0,
width: 0.5,
height: 0.5,
background: new THREE.Color( 0.5, 0.5, 0.7 ),
eye: [ 3.7, 16, 26 ],
up: [3.7, 10 ,16 ],
fov: 60
},
{
left: 0.5,
bottom: 0,
width: 0.5,
height: 0.5,
background: new THREE.Color( 0.7, 0.5, 0.5 ),
eye: [ -60, 6,11],
up: [ -59, 5.9,11 ],
fov: 60
},
];
}
}
function inithumans(){
originhuman1 = scope.modelmanager.man1.mesh;
originhuman1.progress = 1;
// scene.add(originhuman1);
originhuman2 = scope.modelmanager.man2.mesh;
let mixer1 = new THREE.AnimationMixer( originhuman1 );
let mixer2 = new THREE.AnimationMixer( originhuman2 );
originanima1 = originhuman1.animations[ 0 ];
originanima2 = originhuman2.animations[ 0 ];
originhuman1.remove(originhuman1.children[2]);
scene.add(humanlist);
// scene.add(scope.modelmanager.man1.mesh);
}
let checkobject = setInterval(function(){
// console.log(originhuman1);
if(originhuman1){
clearInterval(checkobject);
//进站控制
startWorker();
stationwebwork.postMessage("on");
passerwebwork.postMessage("on");
};
},1000);
this.changestation = function(stationname){
for(let i=0,leni=stationlist.length;i<leni;i++){
if(stationlist[i].name == stationname){
scope.nowstation = stationlist[i];
scope.resetscene();
if(scope.stationtexture["stationlist"]){
scope.modelmanager.station.mesh.getObjectByName("zhantaiming").material.map = scope.stationtexture[scope.nowstation.code];
scope.modelmanager.station.mesh.getObjectByName("zhantaiming").material.map.needsUpdate = true;
}
i=leni;
}
}
}
this.resetscene = function(){
aiswitch = 1;
toppasserin = false;
downpasserin = false;
//下车控制开关
toppasseron = false;
downpasseron = false;
toptrain.nowcode = null;
downtrain.nowcode = null;
// humanlist = new THREE.Group();
for(let j=0; j<humanlist.children.length;j++){
// scope.uncache(humanlist.children[j]);
humanlist.remove(humanlist.children[j]);
j--;
// j--;
}
toptrain.position.y = -20000;
downtrain.position.y = -20000;
deviceaction.down.action.reset();
deviceaction.down.action.time = deviceaction.down.action._clip.duration;
deviceaction.down.action.timeScale = -1;
deviceaction.down.action.play();
deviceaction.top.action.reset();
deviceaction.top.action.time = deviceaction.down.action._clip.duration;
deviceaction.top.action.timeScale = -1;
deviceaction.top.action.play();
for(let an=toptrain.action.top.length-1;an>=0;an--){
toptrain.action.top[an].reset();
toptrain.action.top[an].time = toptrain.action.top[an]._clip.duration;
toptrain.action.top[an].timeScale = -1;
toptrain.action.top[an].play();
}
for(let an=toptrain.action.down.length-1;an>=0;an--){
toptrain.action.down[an].reset();
toptrain.action.down[an].time = toptrain.action.down[an]._clip.duration;
toptrain.action.down[an].timeScale = -1;
toptrain.action.down[an].play();
}
// for(let an=actions.length-1;an>=0;an--){
// actions[an].reset();
// actions[an].time = actions[an]._clip.duration;
// actions[an].timeScale = -1;
// actions[an].play();
// }
setTimeout(function(){
initpasser();
aiswitch = 0;
}, 2000);
}
this.uncache = function(uncachemodel){
if(uncachemodel){
uncachemodel.mixer.uncacheAction();
uncachemodel.traverse( function ( child ) {
if ( child.isMesh ) {
child.geometry.dispose();
child.material.dispose();
}
} );
}
}
this.passerout = function(direct){
// console.log(direct);
if(direct == "top"){
// console.log("toppasser");
// console.log(stationzon.list["standtop"]);
toppasseron = true;
setTimeout(function(){
toppasseron = false
toppasserin = true;
setTimeout(function(){
toppasserin = false;
}, 20000);
}, 5000);
}
if(direct == "down"){
// console.log("downpasser");
// console.log(stationzon.list["standdown"]);
downpasseron = true;
setTimeout(function(){
downpasseron = false;
downpasserin = true;
setTimeout(function(){
downpasserin = false;
}, 20000);
}, 5000);
}
}
this.switchviews = function(viewmode){
if(viewmode == 4){
rendermode = 4;
}
if(viewmode == 2){
rendermode = 2;
}
if(viewmode == 1){
rendermode = 1;
}
if(viewmode == "auto"){
rendermode = 0;
renderer.setViewport( 0, 0, scope.dom.offsetWidth, scope.dom.offsetHeight );
renderer.setScissor( 0, 0, scope.dom.offsetWidth, scope.dom.offsetHeight );
renderer.setScissorTest( false );
renderer.setSize(scope.dom.offsetWidth, scope.dom.offsetHeight);
camerass.aspect = dom.offsetWidth / dom.offsetHeight;
camerass.updateProjectionMatrix();
}
}
function initpasser(){
for(let i=0;i<3;i++){
newhumancreate(stationzon.getinitposition("enter1"),0);
}
for(let i=0;i<3;i++){
newhumancreate(stationzon.getinitposition("enter2"),0);
}
for(let i=0;i<3;i++){
newhumancreate(stationzon.getinitposition("security"),1);
}
// for(let i=0;i<2;i++){
// // newhumancreate(stationzon.getinitposition("entergate"),3);
// }
for(let i=0;i<6;i++){
let standdata = stationzon.getstandposition("standtop");
newhumancreate(standdata.point,4,"top",standdata.door);
}
for(let i=0;i<6;i++){
let standdata = stationzon.getstandposition("standdown");
newhumancreate(standdata.point,4,"down",standdata.door);
}
// for(let i=0;i<5;i++){
// // newhumancreate(stationzon.getinitposition("exitgate"),6);
// }
}
function startWorker(){
initpasser();
passerwebwork.onmessage = function (event) {
if(aiswitch == 0){
moveanimateupdate();
passerai();
delta = clock.getDelta();
// tick(delta);
for(let i=mixers.length-1;i>=0;i--){
if(mixers[i]._actions[0].isRunning()){
mixers[i].update( delta );
}
}
for(let i=humanlist.children.length-1;i>=0;i--){
if(humanlist.children[i].mixer._actions[0].isRunning()){
humanlist.children[i].mixer.update( delta );
}
}
}
};
stationwebwork.onmessage = function (event) {
if(aiswitch == 0){
// console.log(humanlist.children.length);
if(humanlist.children.length < 80){
let direct = Math.floor(Math.random()*(3-1+1))+1;
//1--top
//2-- down
if(direct == 1){
newhumancreate(stationzon.getzoneposition("enter1"),0);
}else{
newhumancreate(stationzon.getzoneposition("enter2"),0);
}
if(toppasseron){
for(let i=0,leni=stationzon.list["standtop"].doorpoints.length;i<leni;i++ ){
setTimeout(function(){
newhumancreate(stationzon.list["standtop"].doorpoints[i],5,"top",i);
}, Math.random()*1000);
}
}
if(downpasseron){
for(let i=0,leni=stationzon.list["standdown"].doorpoints.length;i<leni;i++ ){
setTimeout(function(){
newhumancreate(stationzon.list["standdown"].doorpoints[i],5,"down",i);
}, Math.random()*1000);
}
}
}
}
};
}
//循环渲染函数
function animate() {
// console.log(rendermode);
if(rendermode == 4){
render4view();
}else if(rendermode == 2){
render2view();
}
// else if(rendermode == 1){
// render4view();
// }
else if(rendermode == 0){
renderer.render(scene, camerass);
scope.controls.update();
}
// delta = clock.getDelta();
// tick(delta);
requestAnimationFrame(animate);
}
function passerai(){
for(let i=0;i<humanlist.children.length;i++){
// console.log(humans[i]);
if(humanlist.children[i].status == 0){
//stage = 0 进入车站
if(humanlist.children[i].stage == 0){
let points = [];
points.push(new THREE.Vector3(humanlist.children[i].position.x,humanlist.children[i].position.y,humanlist.children[i].position.z));
groupID = pathfinder.getGroup( ZONE, points[0] );
targetPosition = stationzon.getzoneposition("security");
path = pathfinder.findPath( points[0], targetPosition, ZONE, groupID );
for(let i=0;i<path.length;i++){
points.push(new THREE.Vector3(path[i].x,path[i].y,path[i].z));
}
moveanimateinit(humanlist.children[i],i,points,i);
// console.log(path);
}
//stage = 1 前往进站闸机
if(humanlist.children[i].stage == 1){
let points = [];
points.push(new THREE.Vector3(humanlist.children[i].position.x,humanlist.children[i].position.y,humanlist.children[i].position.z));
groupID = pathfinder.getGroup( ZONE, points[0] );
targetPosition = stationzon.getgateposition("entergate");
path = pathfinder.findPath( points[0], targetPosition.point, ZONE, groupID );
humanlist.children[i].door = targetPosition.door;
for(let i=0;i<path.length;i++){
points.push(new THREE.Vector3(path[i].x,path[i].y,path[i].z));
}
moveanimateinit(humanlist.children[i],i,points,i);
}
//stage = 2 过闸机
if(humanlist.children[i].stage == 2){
let points = [];
points.push(new THREE.Vector3(humanlist.children[i].position.x,humanlist.children[i].position.y,humanlist.children[i].position.z));
points.push(new THREE.Vector3(humanlist.children[i].position.x,humanlist.children[i].position.y,humanlist.children[i].position.z-5));
zhajicontrol("in",humanlist.children[i].door);
moveanimateinit(humanlist.children[i],i,points,i);
}
//stage = 3 前往站台
if(humanlist.children[i].stage == 3){
let direct = Math.floor(Math.random()*(2))+1;
// console.log(direct);
let points = [];
points.push(new THREE.Vector3(humanlist.children[i].position.x,humanlist.children[i].position.y,humanlist.children[i].position.z));
// points.push(new THREE.Vector3(11.81,9.8,13.11));
//1--top
//2-- down
if(direct == 1){
groupID = pathfinder.getGroup( ZONE, points[0] );
targetPosition = stationzon.getstandposition("standtop");
path = pathfinder.findPath( points[0], targetPosition.point, ZONE, groupID );
humanlist.children[i].direct = "top";
}else{
groupID = pathfinder.getGroup( ZONE, points[0] );
targetPosition = stationzon.getstandposition("standdown");
path = pathfinder.findPath( points[0], targetPosition.point, ZONE, groupID );
humanlist.children[i].direct = "down";
}
if(path[0].y<9.84){
points.push(new THREE.Vector3(11.81,9.8,13.11));
points.push(new THREE.Vector3(29.5,1.77,13.16));
}
for(let i=0;i<path.length;i++){
points.push(new THREE.Vector3(path[i].x,path[i].y,path[i].z));
}
humanlist.children[i].door = targetPosition.door;
moveanimateinit(humanlist.children[i],i,points,i);
}
//stage = 4 上车
if(humanlist.children[i].stage == 4){
if(toppasserin && humanlist.children[i].direct == "top"){
let points = [];
points.push(new THREE.Vector3(humanlist.children[i].position.x,humanlist.children[i].position.y,humanlist.children[i].position.z));
points.push(stationzon.list["standtop"].doorpoints[humanlist.children[i].door]);
if(points[1] == undefined){
}else{
moveanimateinit(humanlist.children[i],i,points,i);
}
}
if(downpasserin && humanlist.children[i].direct == "down"){
let points = [];
points.push(new THREE.Vector3(humanlist.children[i].position.x,humanlist.children[i].position.y,humanlist.children[i].position.z));
points.push(stationzon.list["standdown"].doorpoints[humanlist.children[i].door]);
if(points[1] == undefined){
}else{
moveanimateinit(humanlist.children[i],i,points,i);
}
// console.log(points);
}
}
//stage = 5 下车
if(humanlist.children[i].stage == 5){
let points = [];
points.push(new THREE.Vector3(humanlist.children[i].position.x,humanlist.children[i].position.y,humanlist.children[i].position.z));
if(humanlist.children[i].direct == "top"){
points.push(new THREE.Vector3(humanlist.children[i].position.x,humanlist.children[i].position.y,humanlist.children[i].position.z+5));
}
if(humanlist.children[i].direct == "down"){
points.push(new THREE.Vector3(humanlist.children[i].position.x,humanlist.children[i].position.y,humanlist.children[i].position.z-5));
}
groupID = pathfinder.getGroup( ZONE, points[1] );
targetPosition = stationzon.getgateposition("exitgate");
path = pathfinder.findPath( points[1], targetPosition.point, ZONE, groupID );
humanlist.children[i].door = targetPosition.door;
for(let i=0;i<path.length;i++){
points.push(new THREE.Vector3(path[i].x,path[i].y,path[i].z));
}
moveanimateinit(humanlist.children[i],i,points,i);
}
//stage = 6 前往出站闸机
// if(humanlist.children[i].stage == 6){
// let points = [];
// points.push(new THREE.Vector3(humanlist.children[i].position.x,humanlist.children[i].position.y,humanlist.children[i].position.z));
//
// groupID = pathfinder.getGroup( ZONE, points[0] );
// targetPosition = stationzon.getgateposition("exitgate");
// path = pathfinder.findPath( points[0], targetPosition, ZONE, groupID );
//
// for(let i=0;i<path.length;i++){
// points.push(new THREE.Vector3(path[i].x,path[i].y,path[i].z));
// }
// moveanimateinit(humanlist.children[i],i,points,i);
// }
//stage = 7 出闸机
if(humanlist.children[i].stage == 7){
let points = [];
points.push(new THREE.Vector3(humanlist.children[i].position.x,humanlist.children[i].position.y,humanlist.children[i].position.z));
points.push(new THREE.Vector3(humanlist.children[i].position.x+5,humanlist.children[i].position.y,humanlist.children[i].position.z));
zhajicontrol("out",humanlist.children[i].door);
moveanimateinit(humanlist.children[i],i,points,i);
}
//stage = 8 出站
if(humanlist.children[i].stage == 8){
let direct = Math.floor(Math.random()*(3-1+1))+1;
let points = [];
points.push(new THREE.Vector3(humanlist.children[i].position.x,humanlist.children[i].position.y,humanlist.children[i].position.z));
//1--top
//2-- down
if(direct == 1){
groupID = pathfinder.getGroup( ZONE, points[0] );
targetPosition = stationzon.getzoneposition("exit1");
path = pathfinder.findPath( points[0], targetPosition, ZONE, groupID );
}else{
groupID = pathfinder.getGroup( ZONE, points[0] );
targetPosition = stationzon.getzoneposition("exit2");
path = pathfinder.findPath( points[0], targetPosition, ZONE, groupID );
}
for(let i=0;i<path.length;i++){
points.push(new THREE.Vector3(path[i].x,path[i].y,path[i].z));
}
moveanimateinit(humanlist.children[i],i,points,i);
}
}
}
}
function moveanimateinit(model,name,points,index){
model.status = 1;
let curve = new THREE.CatmullRomCurve3(points);
curve.curvrtype = "catmullrom";
// curve.getLength();
// curve动画轨迹
// progress动画进度
// enable当前动画开关
// speed动画速度
// console.log(curve);
model.action.play();
model.progress = 0;
model.runrail = curve;
model.speed = 0.2/curve.getLength();
}
function moveanimateupdate(){
for(let i=0;i<humanlist.children.length;i++){
if(humanlist.children[i].status == 1){
if(humanlist.children[i].progress>=1){
//更新模型坐标
if(humanlist.children[i].status == 1){
humanlist.children[i].progress = 1;
humanlist.children[i].action.stop();
humanlist.children[i].status = 0;
if(humanlist.children[i].stage == 8){
scope.uncache(humanlist.children[i]);
humanlist.remove(humanlist.children[i]);
i--;
}else if(humanlist.children[i].stage == 7){
humanlist.children[i].stage = 8;
}
// else if(humanlist.children[i].stage == 6){
//
// humanlist.children[i].stage = 7;
//
// }
else if(humanlist.children[i].stage == 5){
humanlist.children[i].stage = 7;
}else if(humanlist.children[i].stage == 4){
scope.uncache(humanlist.children[i]);
humanlist.remove(humanlist.children[i]);
i--;
}else if(humanlist.children[i].stage == 3){
humanlist.children[i].stage = 4;
if(olddataai){
humanlist.children[i].stage = 5;
}
}else if(humanlist.children[i].stage == 2){
humanlist.children[i].stage = 3;
}else if(humanlist.children[i].stage == 1){
humanlist.children[i].stage = 2;
}else if(humanlist.children[i].stage == 0){
// // console.log(humans[i].doors);
// zhajiin[humanlist.children[i].doors].waiting = 0;
humanlist.children[i].stage = 1;
}
}
}else{
//根据动画进度获取动画轨迹上点
// console.log(i);
// console.log(humanlist.children[i].runrail);
// console.log(humanlist.children[i].progress);
let point = humanlist.children[i].runrail.getPointAt(humanlist.children[i].progress);
//更新模型坐标
humanlist.children[i].position.x = point.x;
humanlist.children[i].position.y = point.y;
humanlist.children[i].position.z = point.z;
if((humanlist.children[i].progress+0.001)<1){
let tangent = humanlist.children[i].runrail.getPointAt(humanlist.children[i].progress+0.001);
humanlist.children[i].lookAt(new THREE.Vector3(tangent.x,humanlist.children[i].position.y,tangent.z));
}
humanlist.children[i].progress += humanlist.children[i].speed;
point = null;
}
}
}
}
function newhumancreate(position,stage,direct,door){
let mantype = Math.floor(Math.random()*(3-1+1))+1;
let newhuman;
if(mantype == 1){
newhuman = THREE.SkeletonUtils.clone( originhuman1 );
newhuman.animations = [];
newhuman.animations.push(originanima1.clone());
}else{
newhuman = THREE.SkeletonUtils.clone( originhuman2 );
newhuman.animations = [];
newhuman.animations.push(originanima2.clone());
}
let mixer = new THREE.AnimationMixer( newhuman );
newhuman.position.copy(position);
newhuman.status = 0;
newhuman.stage = stage;
if(direct){
if(direct == "top"){
newhuman.rotation.y = Math.PI;
}
newhuman.direct = direct;
}else{
newhuman.direct = null;
}
newhuman.doorstatus = null;
if(door){
newhuman.door = door;
}else{
newhuman.door = null;
}
newhuman.action = mixer.clipAction( newhuman.animations[ 0 ] );
newhuman.mixer = mixer;
newhuman.runrail = null;
newhuman.speed = 0;
newhuman.mixer = mixer;
humanlist.add(newhuman);
// console.log(humanlist.children.length);
}
function initstationlist(stationdata,standdata,psddata){
let list = [];
if(psddata){
for(let i=0,leni = standdata.length;i<leni;i++){
for(let j=0,lenj = psddata.length;j<lenj;j++){
if(standdata[i].code == psddata[j].standCode){
standdata[i].name = psddata[j].code;
}
}
}
for(let i=0,leni = stationdata.length;i<leni;i++){
if(stationdata[i].depot == false){
list[stationdata[i].name] = [];
for(let j=0,lenj = standdata.length;j<lenj;j++){
if(standdata[j].stationCode == stationdata[i].code){
list[stationdata[i].name].push(standdata[j]);
}
}
}
}
for(let k in list){
if(list[k].length>0){
let stationobject = {
code : list[k][0].stationCode,
name : k,
toppsd:null,
downpsd:null,
topsection:null,
downsection:null,
};
if(list[k][0].position.y<list[k][1].position.y){
stationobject.toppsd = list[k][0].name;
stationobject.downpsd = list[k][1].name;
stationobject.topsection = list[k][0].standTrackCode;
stationobject.downsection = list[k][1].standTrackCode;
}else{
stationobject.toppsd = list[k][1].name;
stationobject.downpsd = list[k][0].name;
stationobject.topsection = list[k][1].standTrackCode;
stationobject.downsection = list[k][0].standTrackCode;
}
stationlist.push(stationobject);
}
}
scope.nowstation = stationlist[0];
if(scope.stationtexture["stationlist"]){
scope.modelmanager.station.mesh.getObjectByName("zhantailiebiao").material.map =scope.stationtexture["stationlist"];
scope.modelmanager.station.mesh.getObjectByName("zhantailiebiao").material.map.needsUpdate = true;
if(scope.modelmanager.station.mesh.getObjectByName("menkuangyanse")){
scope.modelmanager.station.mesh.getObjectByName("menkuangyanse").material.map =scope.stationtexture["pingbimen"];
scope.modelmanager.station.mesh.getObjectByName("menkuangyanse").material.map.needsUpdate = true;
}
scope.modelmanager.station.mesh.getObjectByName("zhantaiming").material.map = scope.stationtexture[scope.nowstation.code];
scope.modelmanager.station.mesh.getObjectByName("zhantaiming").material.map.needsUpdate = true;
}
updatestationlist(stationlist);
}else{
olddataai = true;
}
// console.log(scope.nowstation);
}
function inittrain(object){
let ntracks1,ntracks2,tclip,fclip;
ntracks1 = object.animations.slice(16,27);
tclip = new THREE.AnimationClip("three",2,ntracks1);
ntracks2 = object.animations.slice(0,15);
fclip = new THREE.AnimationClip("four",2,ntracks2);
toptrain = object.clone(true);
toptrain.nowcode = null;
toptrain.action = {
top:[],
down:[]
};
let points1 = [];
points1.push(new THREE.Vector3(-71,0.06,-6.3));
points1.push(new THREE.Vector3(60.73,0.06,-6.3));
toptrain.curve = new THREE.CatmullRomCurve3(points1);
downtrain = object.clone(true);
downtrain.nowcode = null;
downtrain.action = {
top:[],
down:[]
};
let points2 = [];
points2.push(new THREE.Vector3(-71,0.06,28.68));
points2.push(new THREE.Vector3(60.73,0.06,28.68));
downtrain.curve = new THREE.CatmullRomCurve3(points2);
inittrainanimation(toptrain,tclip,fclip);
inittrainanimation(downtrain,tclip,fclip);
toptrain.position.z = -20;
toptrain.position.y = -20000;
toptrain.rotation.y = Math.PI;
downtrain.position.z = 40;
downtrain.position.y = -20000;
scene.add(toptrain);
scene.add(downtrain);
}
function inittrainanimation(train,tclip,fclip){
for(let j=0;j<train.children.length;j++){
if(train.children[j].name == "c1" || train.children[j].name == "c6"){
for(let n=0,lenn = train.children[j].children.length;n<lenn;n++){
if(train.children[j].children[n].name == "top"){
train.children[j].children[n].animations = [];
train.children[j].children[n].animations.push(tclip.clone());
let mixer = new THREE.AnimationMixer( train.children[j].children[n] );
mixers.push(mixer);
let action = mixer.clipAction( train.children[j].children[n].animations[ 0 ] );
action.setLoop(THREE.LoopOnce);
action.clampWhenFinished = true;
train.action.top.push(action);
mixers.push(mixer);
}
if(train.children[j].children[n].name == "down"){
train.children[j].children[n].animations = [];
train.children[j].children[n].animations.push(tclip.clone());
let mixer = new THREE.AnimationMixer( train.children[j].children[n] );
mixers.push(mixer);
let action = mixer.clipAction( train.children[j].children[n].animations[ 0 ] );
action.setLoop(THREE.LoopOnce);
action.clampWhenFinished = true;
train.action.down.push(action);
mixers.push(mixer);
}
}
}else{
for(let n=0,lenn = train.children[j].children.length;n<lenn;n++){
if(train.children[j].children[n].name == "top"){
train.children[j].children[n].animations = [];
train.children[j].children[n].animations.push(fclip.clone());
let mixer = new THREE.AnimationMixer( train.children[j].children[n] );
mixers.push(mixer);
let action = mixer.clipAction( train.children[j].children[n].animations[ 0 ] );
action.setLoop(THREE.LoopOnce);
action.clampWhenFinished = true;
train.action.top.push(action);
mixers.push(mixer);
}
if(train.children[j].children[n].name == "down"){
train.children[j].children[n].animations = [];
train.children[j].children[n].animations.push(fclip.clone());
let mixer = new THREE.AnimationMixer( train.children[j].children[n] );
mixers.push(mixer);
let action = mixer.clipAction( train.children[j].children[n].animations[ 0 ] );
action.setLoop(THREE.LoopOnce);
action.clampWhenFinished = true;
train.action.down.push(action);
mixers.push(mixer);
}
}
}
}
}
function initstationanimation(object){
let mixer = new THREE.AnimationMixer( object );
let newclip = object.animations[ 0 ];
for(let j=0;j<object.children.length;j++){
if(object.children[j].name == "top"){
// let geometry = new THREE.BoxBufferGeometry( 50, 50, 50 );
// let material = new THREE.MeshBasicMaterial( {color: 0x00ff00} );
// let cube = new THREE.Mesh( geometry, material );
// cube.position.copy(object.children[j].position);
// scene.add( cube );
object.children[j].animations = [];
object.children[j].animations.push(newclip.clone());
let mixer = new THREE.AnimationMixer( object.children[j] );
let action =mixer.clipAction( object.children[j].animations[0])
action.setLoop(THREE.LoopOnce);
action.clampWhenFinished = true;
// action.play();
mixers.push(mixer);
let device = {
action:action,
mixer:mixer,
};
deviceaction[object.children[j].name] = device;
}
if(object.children[j].name == "down"){
object.children[j].animations = [];
object.children[j].animations.push(newclip.clone());
let mixer = new THREE.AnimationMixer( object.children[j] );
let action =mixer.clipAction( object.children[j].animations[0])
// action.play();
action.setLoop(THREE.LoopOnce);
action.clampWhenFinished = true;
mixers.push(mixer);
let device = {
action:action,
mixer:mixer,
};
deviceaction[object.children[j].name] = device;
}
}
station = object;
// console.log(deviceaction);
scene.add(object);
}
function initzhajiinaimation(object){
let mixer = new THREE.AnimationMixer( object );
let newclip = object.animations[ 0 ];
let newzhaji = object;
for(let i=0;i<newzhaji.children.length;i++){
if(newzhaji.children[i].name == "in01"){
newzhaji.children[i].animations = [];
newzhaji.children[i].animations.push(newclip.clone());
let mixer = new THREE.AnimationMixer( newzhaji.children[i] );
let action =mixer.clipAction( newzhaji.children[i].animations[0]);
action.setLoop(THREE.LoopOnce);
action.clampWhenFinished = true;
mixers.push(mixer);
let device = {
action:action,
mixer:mixer,
};
deviceaction[object.children[i].name] = device;
}
if(newzhaji.children[i].name == "in02"){
newzhaji.children[i].animations = [];
newzhaji.children[i].animations.push(newclip.clone());
let mixer = new THREE.AnimationMixer( newzhaji.children[i] );
let action =mixer.clipAction( newzhaji.children[i].animations[0]);
action.setLoop(THREE.LoopOnce);
action.clampWhenFinished = true;
mixers.push(mixer);
let device = {
action:action,
mixer:mixer,
};
deviceaction[object.children[i].name] = device;
}
if(newzhaji.children[i].name == "in03"){
newzhaji.children[i].animations = [];
newzhaji.children[i].animations.push(newclip.clone());
let mixer = new THREE.AnimationMixer( newzhaji.children[i] );
let action =mixer.clipAction( newzhaji.children[i].animations[0]);
action.setLoop(THREE.LoopOnce);
action.clampWhenFinished = true;
mixers.push(mixer);
let device = {
action:action,
mixer:mixer,
};
deviceaction[object.children[i].name] = device;
}
if(newzhaji.children[i].name == "in04"){
newzhaji.children[i].animations = [];
newzhaji.children[i].animations.push(newclip.clone());
let mixer = new THREE.AnimationMixer( newzhaji.children[i] );
let action =mixer.clipAction( newzhaji.children[i].animations[0]);
action.setLoop(THREE.LoopOnce);
action.clampWhenFinished = true;
mixers.push(mixer);
let device = {
action:action,
mixer:mixer,
};
deviceaction[object.children[i].name] = device;
}
if(newzhaji.children[i].name == "in05"){
newzhaji.children[i].animations = [];
newzhaji.children[i].animations.push(newclip.clone());
let mixer = new THREE.AnimationMixer( newzhaji.children[i] );
let action =mixer.clipAction( newzhaji.children[i].animations[0]);
action.setLoop(THREE.LoopOnce);
action.clampWhenFinished = true;
mixers.push(mixer);
let device = {
action:action,
mixer:mixer,
};
deviceaction[object.children[i].name] = device;
}
}
zhajiinmodel = newzhaji;
scene.add(newzhaji);
}
function initzhajioutanimation(object){
let mixer = new THREE.AnimationMixer( object );
let newclip = object.animations[ 0 ];
let newzhaji = object;
for(let i=0;i<newzhaji.children.length;i++){
if(newzhaji.children[i].name == "out01"){
newzhaji.children[i].animations = [];
newzhaji.children[i].animations.push(newclip.clone());
let mixer = new THREE.AnimationMixer( newzhaji.children[i] );
let action =mixer.clipAction( newzhaji.children[i].animations[0]);
action.setLoop(THREE.LoopOnce);
action.clampWhenFinished = true;
mixers.push(mixer);
let device = {
action:action,
mixer:mixer,
};
deviceaction[object.children[i].name] = device;
}
if(newzhaji.children[i].name == "out02"){
newzhaji.children[i].animations = [];
newzhaji.children[i].animations.push(newclip.clone());
let mixer = new THREE.AnimationMixer( newzhaji.children[i] );
let action =mixer.clipAction( newzhaji.children[i].animations[0]);
action.setLoop(THREE.LoopOnce);
action.clampWhenFinished = true;
mixers.push(mixer);
let device = {
action:action,
mixer:mixer,
};
deviceaction[object.children[i].name] = device;
}
if(newzhaji.children[i].name == "out03"){
newzhaji.children[i].animations = [];
newzhaji.children[i].animations.push(newclip.clone());
let mixer = new THREE.AnimationMixer( newzhaji.children[i] );
let action =mixer.clipAction( newzhaji.children[i].animations[0]);
action.setLoop(THREE.LoopOnce);
action.clampWhenFinished = true;
mixers.push(mixer);
let device = {
action:action,
mixer:mixer,
};
deviceaction[object.children[i].name] = device;
}
if(newzhaji.children[i].name == "out04"){
newzhaji.children[i].animations = [];
newzhaji.children[i].animations.push(newclip.clone());
let mixer = new THREE.AnimationMixer( newzhaji.children[i] );
let action =mixer.clipAction( newzhaji.children[i].animations[0]);
action.setLoop(THREE.LoopOnce);
action.clampWhenFinished = true;
mixers.push(mixer);
let device = {
action:action,
mixer:mixer,
};
deviceaction[object.children[i].name] = device;
}
if(newzhaji.children[i].name == "out05"){
newzhaji.children[i].animations = [];
newzhaji.children[i].animations.push(newclip.clone());
let mixer = new THREE.AnimationMixer( newzhaji.children[i] );
let action =mixer.clipAction( newzhaji.children[i].animations[0]);
action.setLoop(THREE.LoopOnce);
action.clampWhenFinished = true;
mixers.push(mixer);
let device = {
action:action,
mixer:mixer,
};
deviceaction[object.children[i].name] = device;
}
}
zhajioutmodel = newzhaji
scene.add(newzhaji);
}
function zhajicontrol(type,door){
// console.log(door);
// console.log(zhajiin);
let devicenum = door;
if(type == "in"){
deviceaction[zhajiin[devicenum].id].action.reset();
deviceaction[zhajiin[devicenum].id].action.time = 0;
deviceaction[zhajiin[devicenum].id].action.timeScale = 1;
deviceaction[zhajiin[devicenum].id].action.play();
}else if(type == "out"){
deviceaction[zhajiout[devicenum].id].action.reset();
deviceaction[zhajiout[devicenum].id].action.time = 0;
deviceaction[zhajiout[devicenum].id].action.timeScale = 1;
deviceaction[zhajiout[devicenum].id].action.play();
}
}
function render4view() {
// updateSize();
for ( let ii = 0; ii < views4.length; ++ ii ) {
let view = views4[ ii ];
let camera = view.camera;
// view.updateCamera( camera, scene, mouseX, mouseY );
let left = Math.floor( windowWidth * view.left );
let bottom = Math.floor( windowHeight * view.bottom );
let width = Math.floor( windowWidth * view.width );
let height = Math.floor( windowHeight * view.height );
renderer.setViewport( left, bottom, width, height );
renderer.setScissor( left, bottom, width, height );
renderer.setScissorTest( true );
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.render( scene, camera );
}
}
function render2view() {
// updateSize();
for ( let ii = 0; ii < views2.length; ++ ii ) {
let view = views2[ ii ];
let camera = view.camera;
// view.updateCamera( camera, scene, mouseX, mouseY );
let left = Math.floor( windowWidth * view.left );
let bottom = Math.floor( windowHeight * view.bottom );
let width = Math.floor( windowWidth * view.width );
let height = Math.floor( windowHeight * view.height );
renderer.setViewport( left, bottom, width, height );
renderer.setScissor( left, bottom, width, height );
renderer.setScissorTest( true );
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.render( scene, camera );
}
}
}