function Store3D() {
|
this.scene = null;//场景
|
this.camera = null;//相机
|
this.renderer = null;//渲染器
|
this.objects = [];//场景中所有对象的集合
|
this.firstTime = 1;
|
this.storeIsShow=1;//是否显示库房
|
this.groupIsShow=1;//是否显示排
|
this.shelfIsShow=1;//是否显示架子
|
this.goodTypes=[];
|
this.objectLockPointer=[];//存储所有的sprite对象
|
}
|
|
/**
|
* 初始化仓库所有插件
|
*/
|
Store3D.prototype.initMain = function () {
|
this.initScene();
|
this.initCamera();
|
this.initRenderer();
|
this.initOrbitControl();
|
this.initStats();
|
this.initAxisHelper();
|
this.initBuilding();
|
this.initLight();
|
this.initObjectSelect();
|
this.initAgvCar();
|
},
|
/**
|
* 仓库整体开始运行
|
*/
|
Store3D.prototype.start = function () {
|
this.initMain();
|
this.animate();
|
},
|
|
/**
|
初始化场景,仅仅需要有句话就可以生命一个场景,非常简单
|
**/
|
Store3D.prototype.initScene = function () {
|
this.scene = new THREE.Scene();
|
},
|
/**
|
初始化场景,因为我们做的工厂模型,尽可能的接近于真实情景,采用透视相机
|
**/
|
Store3D.prototype.initCamera = function () {
|
//声明一个透视相机,
|
// 视角:60,
|
// 纵横比aspect:全屏,使用的是浏览器的宽度/高度
|
//近平面near:0.1
|
//远平面视角far:10000
|
this.camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 10000);
|
/*
|
设置相机位置,注意threejs中的坐标系采用的是右手坐标系
|
*/
|
this.camera.position.x = 0;
|
this.camera.position.y = 1600;
|
this.camera.position.z = 1000;
|
//相机的朝向
|
this.camera.lookAt(0, 0, 0);
|
//将相机放到场景中
|
this.scene.add(this.camera);
|
},
|
/**
|
* 显示或者隐温度控件
|
*/
|
Store3D.prototype.changeTemperatureShow=function(){
|
if (this.tmpIsShow == 1) {
|
for (let i = 0; i < this.objectLockPointer.length; i++) {
|
let tmpObject = this.objectLockPointer[i];
|
if (tmpObject.type == "Temperature") {
|
this.removeObject(tmpObject.id);
|
}
|
else if(tmpObject.type == "StoreSign")
|
{
|
this.addObject(tmpObject);
|
}
|
}
|
this.tmpIsShow =0;
|
}
|
else {
|
for (let i = 0; i < this.objectLockPointer.length; i++) {
|
let tmpObject = this.objectLockPointer[i];
|
if (tmpObject.type == "Temperature") {
|
this.addObject(tmpObject);
|
}
|
else if(tmpObject.type == "StoreSign")
|
{
|
this.removeObject(tmpObject.id);
|
}
|
}
|
this.tmpIsShow =1;
|
}
|
},
|
/**
|
* 根据ID或者名称移除对象
|
*/
|
Store3D.prototype.removeObject = function (nameorid) {
|
for (let i = 0; i < this.objects.length; i++) {
|
let tmpObject = this.objects[i];
|
if (tmpObject.name == nameorid || tmpObject.id == nameorid) {
|
this.objects.splice(i, 1);
|
this.scene.remove(tmpObject);
|
}
|
|
}
|
},
|
|
/**
|
声名渲染器
|
**/
|
Store3D.prototype.initRenderer = function () {
|
this.renderer = new THREE.WebGLRenderer(
|
{
|
antialias: true,//是否开启反锯齿,设置为true开启反锯齿。
|
alpha: true,//是否可以设置背景色透明。
|
logarithmicDepthBuffer: true//模型的重叠部位便不停的闪烁起来。这便是Z-Fighting问题,为解决这个问题,我们可以采用该种方法
|
}
|
);
|
this.renderer.setSize(window.innerWidth, window.innerHeight);//渲染器的尺寸与windows的尺寸相同
|
this.renderer.setClearColor(0x39609B);//设置渲染的背景颜色
|
this.renderer.setPixelRatio(window.devicePixelRatio);//设置渲染器的分辨率与浏览器电脑本身的分辨率相同
|
//将渲染器添加到我们的网页中,可以将渲染的内容在网页中显示出来
|
let container = document.getElementById("container");
|
container.appendChild(this.renderer.domElement);
|
},
|
/**
|
* 初始化物体选中控件
|
*/
|
Store3D.prototype.initObjectSelect=function(){
|
new ObjectSelect(this.scene, this.camera);
|
},
|
/**
|
* 初始化灯光
|
*/
|
Store3D.prototype.initLight = function () {
|
//首先添加个环境光
|
let ambient = new THREE.AmbientLight(0xffffff, 1); //AmbientLight,影响整个场景的光源
|
ambient.position.set(0, 0, 0);
|
this.addObject(ambient);
|
//添加平行光,平行光类似于太阳光
|
let directionalLight = new THREE.DirectionalLight(0xffffff, 0.3);//模拟远处类似太阳的光源
|
directionalLight.position.set(0, 200, 0);
|
this.addObject(directionalLight);
|
//设置点光源
|
let pointLight1 = new THREE.PointLight(0xffffff, 0.3);
|
pointLight1.position.set(-500, 200, 0);
|
this.addObject(pointLight1);
|
let pointLight2 = new THREE.PointLight(0xffffff, 0.3);
|
pointLight2.position.set(500, 200, 0);
|
this.addObject(pointLight2);
|
},
|
/**
|
初始化相机控件OrbitControl
|
*/
|
Store3D.prototype.initOrbitControl = function () {
|
this.orbitControl = new THREE.OrbitControls(this.camera, this.renderer.domElement);
|
this.orbitControl.enableDamping = true;
|
this.orbitControl.dampingFactor = 0.5;
|
// 视角最小距离
|
this.orbitControl.minDistance = 0;
|
// 视角最远距离
|
this.orbitControl.maxDistance = 2000;
|
// 最大角度
|
this.orbitControl.maxPolarAngle = Math.PI / 2.2;
|
},
|
/**
|
* 初始化性能控件Stats
|
*/
|
Store3D.prototype.initStats = function () {
|
this.stats = new Stats();
|
this.stats.domElement.style.position = 'absolute';
|
this.stats.domElement.style.left = '0px';
|
this.stats.domElement.style.up = '0px';
|
|
document.body.appendChild(this.stats.domElement);
|
},
|
/**
|
* 初始化坐标系辅助控件
|
*/
|
Store3D.prototype.initAxisHelper = function () {
|
let axes = new THREE.AxisHelper(1000);
|
this.addObject(axes);
|
},
|
Store3D.prototype.initLabel = function () {
|
let label = new Label("我们是冠军", {
|
fontsize: 50
|
});
|
label.position.set(0, 500, 0);
|
this.addObject(label);
|
},
|
/**
|
* 向场景中添加物体,并记录到
|
*/
|
Store3D.prototype.addObject = function (object) {
|
this.scene.add(object);
|
this.objects.push(object);
|
},
|
/**
|
创建建筑物
|
*/
|
Store3D.prototype.initBuilding = function () {
|
let buildingData = buildingObjects.objects;
|
for (let i = 0; i < buildingData.length; i++) {
|
let objectOption = buildingData[i];
|
switch (objectOption.objectType) {
|
case "cube":
|
let cube = new Cube(objectOption);
|
this.addObject(cube);
|
break;
|
case "wall":
|
let wall = new Wall(objectOption);
|
this.addObject(wall);
|
break;
|
case "route":
|
let line=new RouteLine(objectOption);
|
this.addObject(line);
|
break;
|
}
|
}
|
},
|
Store3D.prototype.showAgvCar=function(object){
|
object.name = "AGV小车";
|
object.position.set(-100,40,100);
|
this.addObject(object);
|
},
|
|
Store3D.prototype.initAgvCar=function()
|
{
|
let agvCar=new AGVCar();
|
agvCar.Load(this,'showAgvCar')
|
},
|
/***********************************************
|
* 隐藏或者显示所有的仓库对象
|
*/
|
Store3D.prototype.changeStoreShow = function () {
|
if (this.storeIsShow == 1) {
|
for (let i = 0; i < this.objects.length; i++) {
|
let tmpObject = this.objects[i];
|
if (tmpObject.type == "Store") {
|
this.scene.remove(tmpObject);
|
}
|
}
|
this.storeIsShow = 0;
|
} else {
|
for (let i = 0; i < this.objects.length; i++) {
|
let tmpObject = this.objects[i];
|
if (tmpObject.type == "Store") {
|
this.scene.add(tmpObject);
|
}
|
}
|
this.storeIsShow = 1;
|
}
|
},
|
/***********************************************
|
* 隐藏或者显示所有的巷道对象
|
*/
|
Store3D.prototype.changeGroupShow = function () {
|
if (this.groupIsShow == 1) {
|
for (let i = 0; i < this.objects.length; i++) {
|
let tmpObject = this.objects[i];
|
if (tmpObject.type == "StoreGroup") {
|
this.scene.remove(tmpObject);
|
}
|
}
|
this.groupIsShow =0;
|
}
|
else {
|
for (let i = 0; i < this.objects.length; i++) {
|
let tmpObject = this.objects[i];
|
if (tmpObject.type == "StoreGroup") {
|
this.scene.add(tmpObject);
|
}
|
}
|
this.groupIsShow =1;
|
}
|
},
|
/***********************************************
|
* 隐藏或者显示所有仓库框架
|
*/
|
Store3D.prototype.changeShelfShow=function(){
|
if(this.shelfIsShow==1)
|
{
|
for (let i = 0; i < this.objects.length; i++) {
|
let tmpObject = this.objects[i];
|
if (tmpObject.type == "StoreShelf") {
|
this.scene.remove(tmpObject);
|
}
|
}
|
this.shelfIsShow =0;
|
}
|
else
|
{
|
for (let i = 0; i < this.objects.length; i++) {
|
let tmpObject = this.objects[i];
|
if (tmpObject.type == "StoreShelf") {
|
this.scene.add(tmpObject);
|
}
|
}
|
this.shelfIsShow =1;
|
}
|
},
|
/**
|
* 初始化仓库相关对象
|
*/
|
Store3D.prototype.initStoreObjects = function (object) {
|
|
|
if (this.firstTime == 1) {
|
let Store3DData = eval('(' + window.localStorage.getItem('Store3DData') + ')');
|
if (Store3DData !== null) {
|
//显示仓库区域
|
for (let i = 0; i < Store3DData.Areas.length; i++) {
|
let optionArea = Store3DData.Areas[i];
|
let area = new StoreArea(optionArea);
|
object.addObject(area);
|
//显示仓库
|
for (let j = 0; j < optionArea.Stores.length; j++)//
|
{
|
let optionStore = optionArea.Stores[j];
|
optionStore.Position = CommonFunction.transPosition(optionStore.Position, optionArea.Position);
|
let store = new Store(optionStore);
|
object.addObject(store);
|
let storeSign = new StoreSign(optionStore, this);
|
let storeTemperature=new Temperature(optionStore, this);
|
//显示每行信息
|
for (let k = 0; k < optionStore.Groups.length; k++) {
|
let optionGroup = optionStore.Groups[k];
|
optionGroup.Position = CommonFunction.transPosition(optionGroup.Position, optionStore.Position);
|
let shelf = new StoreShelf(optionGroup);
|
let group=new StoreGroup(optionGroup,1);
|
object.addObject(shelf);
|
object.addObject(group);
|
//显示库位上的货物
|
for (let m = 0; m < optionGroup.Bins.length; m++) {
|
let optionBin = optionGroup.Bins[m];
|
let existGoods=this.getExistedGoodType(optionBin.State);
|
let storeGoods = new StoreGoods(optionGroup, optionBin);
|
if(existGoods==null) {
|
let goods=storeGoods.create();
|
object.addObject(goods);
|
this.goodTypes.push({type: optionBin.State, object: goods});
|
}
|
else
|
{
|
let goods= storeGoods.clone(existGoods);
|
object.addObject(goods);
|
}
|
}
|
}
|
|
}
|
}
|
this.firstTime = 0;
|
}
|
}
|
},
|
/**
|
* 显示或者隐藏性能控件
|
*/
|
Store3D.prototype.changeStats = function () {
|
if (this.stats.domElement.style.display == 'none')
|
this.stats.domElement.style.display = 'block';
|
else
|
this.stats.domElement.style.display = 'none';
|
}
|
|
/**
|
* 获取是否已经生成同类型的货物
|
*/
|
Store3D.prototype.getExistedGoodType=function(state){
|
for (let i=0;i<this.goodTypes.length;i++)
|
{
|
let type=this.goodTypes[i];
|
if(type.type===state)
|
{
|
return type.object;
|
}
|
|
}
|
return null;
|
|
},
|
/**
|
* 定时重复刷新
|
*/
|
Store3D.prototype.animate = function () {
|
requestAnimationFrame(this.animate.bind(this));
|
this.renderer.render(this.scene, this.camera);
|
this.stats.update();
|
this.initStoreObjects(this);
|
}
|