function Action3D(layer3D){ this.scene = layer3D.scene3D.scene; this.ground = layer3D.scene3D.ground; this.camera = layer3D.scene3D.camera; this.canvas = layer3D.scene3D.canvas; this.layer=layer3D.layer; this.parameter=layer3D.layer.parameter; this.calculateLine=layer3D.layer.calculateLine; this.materials=layer3D.materials; this.layer3D=layer3D; //编辑模型的时候,出现的绿色的面 //this.selectmat=null; this.vertexPoints=[]; //选择的当前mesh this.currentSelectMesh=null; //当前操作都作用于model的mesh this.currentModelMesh=null; //选择模型时,显示的circle this.selectCircleMesh=null; //选择模型时,rotator this.selectRotatorMesh=null; //选择模型时,出现的sprite this.currentInfoMesh=null; this.sprite=null; //是否有arrow this.isArrow=false; //拖拽 this.drags = { //拖拽的面,用来编辑模型 planeMesh:null, //拖拽的面的初始位置 planeStartPosition:null, //点击模型的时候,与地面的交点的位置 modelStartPosition:null, //移动模型的时候,鼠标与地面的交点的位置 moveModelPosition:null, //鼠标与箭头相交的位置 arrowIntersectPosition:null }; this.collidableMeshes=layer3D.collectMeshes.collidableMeshes; this.arrowMeshes=layer3D.collectMeshes.arrowMeshes; //刷墙 this.paintSet=[]; //锁住canvas this.lock=false; //判断其name的值 //this.noModelMeshName = ["wallplane","skyBox","ground","floorPlane","wall","door","window","selectCircle","rotator"]; this.noModelMeshName = ["wallplane","skyBox","walltop","ground","door","window"]; this.noCareMeshName = ["wallplane","skyBox","walltop"]; this.arrows={ orgtopPosition:null, orgdownPosition:null, orgleftPosition:null, orgrightPosition:null, topMesh:null, downMesh:null, leftMesh:null, rightMesh:null, activeMesh:null, orgactivePosition:null, sideDistance:null, lineTopToDown:null, lineLeftToRight:null }; this.measures={ textMesh:null, endpointMesh1:null, endpointMesh2:null, arrowMesh1:null, arrowMesh2:null, linkMesh:null }; this.measures.textMesh=null; this.control = { editProduct:false, addPhoto:false, addPicture:false, //是否旋转模型 actionRotator:false }; }; //用来编辑模型的大小 Action3D.prototype.createSelectMat=function() { var mat=new BABYLON.StandardMaterial("selectMat", this.scene); mat.alpha = 1.0; mat.diffuseColor = new BABYLON.Color3.Green(); mat.backFaceCulling = false; return mat; }; //从模型数组中移除某个模型 Action3D.prototype.removeMesh = function(meshes,elem) { for (var i=meshes.length-1; i>=0; i--) { if (meshes[i] === elem) { meshes.splice(i, 1); } } return meshes; }; Action3D.prototype.getAngleFromWallPlane=function(mesh) { var points = mesh._boundingInfo.boundingBox.vectorsWorld; var point1 = points[0]; var point2=null; if(points[1].y == point1.y) { point2 = points[1]; } else { point2 = points[2]; } var line = this.layer.calculateLine.createLine(new BABYLON.Vector2(point1.x.toFixed(2),point1.z.toFixed(2)),new BABYLON.Vector2(point2.x.toFixed(2),point2.z.toFixed(2))); if(line.hasOwnProperty('x')) { return Math.PI/2; } else if(line.hasOwnProperty('y')) { return 0; } else { return line.a%(2*Math.PI); } }; //移除选择的模型 Action3D.prototype.removeSelectMesh=function() //Action3D.prototype.clearMesh=function(meshes,elem) { this.clearArrow(); this.closeConfigProduct(); this.currentSelectMesh = null; this.currentModelMesh = null; this.drags.modelStartPosition = null; }; //通过鼠标移动模型或者移动arrow,将位置和模型的长宽赋值给弹出窗口 Action3D.prototype.setProductInfo=function() { var position=null; if(this.currentModelMesh==null) { return; } else{ position = this.currentModelMesh.position; } var position_x=document.getElementById("position-x"); var position_y=document.getElementById("position-y"); var position_z=document.getElementById("position-z"); position_x.childNodes[1].childNodes[0].value=Math.floor(position.x); position_y.childNodes[1].childNodes[0].value=Math.floor(position.y); position_z.childNodes[1].childNodes[0].value=Math.floor(position.z); var params_width=document.getElementById("params-width"); var params_length=document.getElementById("params-length"); if(this.currentModelMesh.name=="photo") { params_width.childNodes[1].childNodes[0].value=Math.floor(this.currentModelMesh.height); params_width.childNodes[1].childNodes[0].alt=Math.floor(this.currentModelMesh.height); params_length.childNodes[1].childNodes[0].value=Math.floor(this.currentModelMesh.length); params_length.childNodes[1].childNodes[0].alt=Math.floor(this.currentModelMesh.length); } else { var width=Math.abs(this.currentModelMesh._boundingInfo.boundingBox.maximumWorld.z-this.currentModelMesh._boundingInfo.boundingBox.minimumWorld.z); var length=Math.abs(this.currentModelMesh._boundingInfo.boundingBox.maximumWorld.x-this.currentModelMesh._boundingInfo.boundingBox.minimumWorld.x); params_width.childNodes[1].childNodes[0].value=Math.floor(width); params_width.childNodes[1].childNodes[0].alt=Math.floor(width); params_length.childNodes[1].childNodes[0].value=Math.floor(length); params_length.childNodes[1].childNodes[0].alt=Math.floor(length); } }; //给编辑框里赋值 Action3D.prototype.editProductInfo=function(h,position) { var width=BABYLON.Vector2.Distance(new BABYLON.Vector2(this.arrows.orgdownPosition.x,this.arrows.orgdownPosition.z),new BABYLON.Vector2(this.arrows.orgtopPosition.x,this.arrows.orgtopPosition.z)); var length=BABYLON.Vector2.Distance(new BABYLON.Vector2(this.arrows.orgrightPosition.x,this.arrows.orgrightPosition.z),new BABYLON.Vector2(this.arrows.orgleftPosition.x,this.arrows.orgleftPosition.z)); var height=h; var positionX=position.x; var positionY=position.y; var positionZ=position.z; //var angleY=Math.floor((this.currentModelMesh.angle/Math.PI)*180); var angleY=Math.floor((this.currentSelectMesh.angle/Math.PI)*180); var rotationy=document.getElementById("rotation-y"); if(angleY<0) { angleY=360+angleY; } else if(angleY>360) { angleY=angleY-360; } rotationy.childNodes[1].childNodes[0].value=angleY; rotationy.childNodes[1].childNodes[1].value=angleY; var position_x=document.getElementById("position-x"); position_x.childNodes[1].childNodes[0].value=Math.floor(position.x); var position_y=document.getElementById("position-y"); position_y.childNodes[1].childNodes[0].value=Math.floor(position.y); var position_z=document.getElementById("position-z"); position_z.childNodes[1].childNodes[0].value=Math.floor(position.z); var params_width=document.getElementById("params-width"); params_width.childNodes[1].childNodes[0].value=Math.floor(width); params_width.childNodes[1].childNodes[0].alt=Math.floor(width); var params_length=document.getElementById("params-length"); params_length.childNodes[1].childNodes[0].value=Math.floor(length); params_length.childNodes[1].childNodes[0].alt=Math.floor(length); var params_height=document.getElementById("params-height"); params_height.childNodes[1].childNodes[0].value=Math.floor(height); params_height.childNodes[1].childNodes[0].alt=Math.floor(height); }; //获取模型的boundingbox上方的四个顶点 Action3D.prototype.setVertex=function(points,min,max) { var y=points[0].y; var y1=points[1].y; for(var i=0;i-1; }); if (pickinfo.hit) { return pickinfo.pickedPoint; } return null; }; Action3D.prototype.getDiff2=function(current,line) { var result=null; var interpoint=this.calculateLine.getJoinLinePoint({x:current.x,y:current.z},line); result=new BABYLON.Vector3(interpoint.x,current.y,interpoint.y); return result; }; Action3D.prototype.setselectMesh=function() { var currentModelMesh = this.currentModelMesh; if(this.currentModelMesh==null) { return; } var shape = [ new BABYLON.Vector3(this.parameter.selectCircle.downRadius, 0, 0), new BABYLON.Vector3(this.parameter.selectCircle.topRadius, this.parameter.selectCircle.topHeight, 0) ]; var top=1; var radius= BABYLON.Vector2.Distance(new BABYLON.Vector2(currentModelMesh._boundingInfo.boundingBox.maximumWorld.x,currentModelMesh._boundingInfo.boundingBox.maximumWorld.z),new BABYLON.Vector2(currentModelMesh._boundingInfo.boundingBox.minimumWorld.x,currentModelMesh._boundingInfo.boundingBox.minimumWorld.z))/2; if(radius>(this.parameter.selectRotator.differRadius+1)) { top=radius-this.parameter.selectRotator.differRadius; } //如果加载的模型是软饰,就不要圈圈,否则就得有圈圈 if(this.selectCircleMesh==null) { if(currentModelMesh.name!="photo"&¤tModelMesh.name.indexOf("picture-")!=0) { this.selectCircleMesh = BABYLON.Mesh.CreateLathe('selectCircle', shape , radius,top, this.scene); this.selectCircleMesh.material = this.materials.selectMaterial; var positionY = currentModelMesh.position.y+5; this.selectCircleMesh.position=new BABYLON.Vector3(currentModelMesh._boundingInfo.boundingBox.center.x,positionY,currentModelMesh._boundingInfo.boundingBox.center.z); if(this.selectRotatorMesh==null) { this.selectRotatorMesh=this.createRotator(); } this.selectRotatorMesh.position.z = -radius; this.selectRotatorMesh.position.y = 5; if(this.selectCircleMesh!=null) { this.selectRotatorMesh.parent = this.selectCircleMesh; } this.selectRotatorMesh.rotation.y = this.parameter.selectRotator.rotationY; this.selectRotatorMesh.rotation.x = this.parameter.selectRotator.rotationX; this.selectRotatorMesh.computeWorldMatrix(true); this.selectRotatorMesh.refreshBoundingInfo(); } } else { if(currentModelMesh.name.indexOf("picture-")==0||currentModelMesh.name=="photo") { this.selectCircleMesh.dispose(); this.selectCircleMesh=null; if(this.selectRotatorMesh!=null) { this.selectRotatorMesh.dispose(); this.selectRotatorMesh=null; } } } if(!currentModelMesh.rotationQuaternion) { currentModelMesh.angle=0; } else { var angleY=currentModelMesh.rotation.y; if(angleY<0) { angleY=2*Math.PI+angleY; } else if(angleY>2*Math.PI) { angleY=angleY-2*Math.PI; } currentModelMesh.angle=angleY; } this.createinfo(); }; Action3D.prototype.switchCnofigureProduct = function (type) { //显示普通模型 if(type==1) { document.getElementById("params-width").style.display=""; document.getElementById("editptrot").style.display=""; document.getElementById("rotation-y").style.display=""; document.getElementById("item-2").style.display=""; document.getElementById("item-1").style.display=""; } //显示软饰 else { document.getElementById("params-width").style.display="none"; document.getElementById("editptrot").style.display="none"; document.getElementById("rotation-y").style.display="none"; document.getElementById("item-2").style.display="none"; document.getElementById("item-1").style.display="none"; } }; //选择模型后,出现标记 Action3D.prototype.createinfo=function() { var currentModelMesh=this.currentModelMesh; if(this.currentModelMesh==null) { return; } currentModelMesh.arrowHeigh=currentModelMesh._boundingInfo.boundingBox.maximumWorld.y/2; if(this.sprite!=null) { this.removeinfo(); } this.sprite = new BABYLON.SpriteManager("meshManager", this.parameter.selectSprite.img, this.parameter.selectSprite.capacity, this.parameter.selectSprite.cellSize, this.scene); this.sprite.isPickable = true; // First animated player this.currentInfoMesh = new BABYLON.Sprite("editFurniture", this.sprite); this.currentInfoMesh.position= new BABYLON.Vector3(currentModelMesh.position.x, currentModelMesh._boundingInfo.boundingBox.maximumWorld.y+this.parameter.selectSprite.extendY, currentModelMesh.position.z); this.currentInfoMesh.size = this.parameter.selectSprite.size; this.currentInfoMesh.isPickable = true; // click action for player this.currentInfoMesh.actionManager = new BABYLON.ActionManager(this.scene); this.currentInfoMesh.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnPickUpTrigger, function () { currentModelMesh.computeWorldMatrix(true); currentModelMesh.refreshBoundingInfo(); //弹出框,显示产品的大小,位置,角度等等 var configuratorProduct=document.getElementById("configuratorWindow"); configuratorProduct.style.display="block"; //创造编辑产品的箭头 this.createArrows(currentModelMesh,this.scene,this.parameter.arrow3DImgUrl); this.switchCnofigureProduct(1); this.currentInfoMesh.dispose(); this.currentInfoMesh=null; if(this.selectCircleMesh!=null) { this.selectCircleMesh.dispose(); this.selectCircleMesh=null; if(this.selectRotatorMesh!=null) { this.selectRotatorMesh.dispose(); this.selectRotatorMesh=null; } } this.editProductInfo(Math.abs(currentModelMesh._boundingInfo.boundingBox.maximumWorld.y- currentModelMesh._boundingInfo.boundingBox.minimumWorld.y),currentModelMesh.position); }.bind(this))); }; //选择箭头标记 Action3D.prototype.selectArrow=function() { this.drags.planeMesh=BABYLON.Mesh.CreatePlane("temp", 1, this.scene); if(this.currentSelectMesh.name=="downarrow") { this.drags.planeMesh.scaling.x=this.currentModelMesh.getBoundingInfo().boundingBox.extendSize.x*this.currentModelMesh.scaling.x*2; this.drags.planeMesh.scaling.y=this.currentModelMesh.getBoundingInfo().boundingBox.extendSize.y*this.currentModelMesh.scaling.y*2; this.drags.planeMesh.rotate(BABYLON.Axis.Y, this.currentModelMesh.angle, BABYLON.Space.LOCAL); this.drags.planeMesh.position=this.arrows.orgdownPosition; //olddistance=BABYLON.Vector3.Distance(this.arrows.orgdownPosition,this.arrows.orgtopPosition); var distance1= BABYLON.Vector2.Distance(new BABYLON.Vector2(this.camera.position.x,this.camera.position.z),new BABYLON.Vector2(this.arrows.orgleftPosition.x,this.arrows.orgleftPosition.z)); var distance2= BABYLON.Vector2.Distance(new BABYLON.Vector2(this.camera.position.x,this.camera.position.z),new BABYLON.Vector2(this.arrows.orgrightPosition.x,this.arrows.orgrightPosition.z)); if(distance1>distance2) { this.arrows.orgactivePosition=this.arrows.orgrightPosition; this.arrows.activeMesh=this.arrows.rightMesh; } else { this.arrows.orgactivePosition=this.arrows.orgleftPosition; this.arrows.activeMesh=this.arrows.leftMesh; } } else if(this.currentSelectMesh.name=="toparrow") { this.drags.planeMesh.scaling.x=this.currentModelMesh.getBoundingInfo().boundingBox.extendSize.x*this.currentModelMesh.scaling.x*2; this.drags.planeMesh.scaling.y=this.currentModelMesh.getBoundingInfo().boundingBox.extendSize.y*this.currentModelMesh.scaling.y*2; this.drags.planeMesh.rotate(BABYLON.Axis.Y, this.currentModelMesh.angle, BABYLON.Space.LOCAL); this.drags.planeMesh.position=this.arrows.orgtopPosition; var distance1= BABYLON.Vector2.Distance(new BABYLON.Vector2(this.camera.position.x,this.camera.position.z),new BABYLON.Vector2(this.arrows.orgleftPosition.x,this.arrows.orgleftPosition.z)); var distance2= BABYLON.Vector2.Distance(new BABYLON.Vector2(this.camera.position.x,this.camera.position.z),new BABYLON.Vector2(this.arrows.orgrightPosition.x,this.arrows.orgrightPosition.z)); if(distance1>distance2) { this.arrows.orgactivePosition=this.arrows.orgrightPosition; this.arrows.activeMesh=this.arrows.rightMesh; } else { this.arrows.orgactivePosition=this.arrows.orgleftPosition; this.arrows.activeMesh=this.arrows.leftMesh; } } else if(this.currentSelectMesh.name=="leftarrow") { this.drags.planeMesh.rotate(BABYLON.Axis.Y, this.currentModelMesh.angle+Math.PI/2, BABYLON.Space.LOCAL); this.drags.planeMesh.scaling.x=this.currentModelMesh.getBoundingInfo().boundingBox.extendSize.z*this.currentModelMesh.scaling.z*2; this.drags.planeMesh.scaling.y=this.currentModelMesh.getBoundingInfo().boundingBox.extendSize.y*this.currentModelMesh.scaling.y*2; this.drags.planeMesh.position=this.arrows.orgleftPosition; var distance1= BABYLON.Vector2.Distance(new BABYLON.Vector2(this.camera.position.x,this.camera.position.z),new BABYLON.Vector2(this.arrows.orgtopPosition.x,this.arrows.orgtopPosition.z)); var distance2= BABYLON.Vector2.Distance(new BABYLON.Vector2(this.camera.position.x,this.camera.position.z),new BABYLON.Vector2(this.arrows.orgdownPosition.x,this.arrows.orgdownPosition.z)); if(distance1>distance2) { this.arrows.orgactivePosition=this.arrows.orgdownPosition; this.arrows.activeMesh=this.arrows.downMesh; } else { this.arrows.orgactivePosition=this.arrows.orgtopPosition; this.arrows.activeMesh=this.arrows.topMesh; } } else if(this.currentSelectMesh.name=="rightarrow") { this.drags.planeMesh.rotate(BABYLON.Axis.Y, this.currentModelMesh.angle+Math.PI/2, BABYLON.Space.LOCAL); this.drags.planeMesh.scaling.x=this.currentModelMesh.getBoundingInfo().boundingBox.extendSize.z*this.currentModelMesh.scaling.z*2; this.drags.planeMesh.scaling.y=this.currentModelMesh.getBoundingInfo().boundingBox.extendSize.y*this.currentModelMesh.scaling.y*2; this.drags.planeMesh.position=this.arrows.orgrightPosition; var distance1= BABYLON.Vector2.Distance(new BABYLON.Vector2(this.camera.position.x,this.camera.position.z),new BABYLON.Vector2(this.arrows.orgtopPosition.x,this.arrows.orgtopPosition.z)); var distance2= BABYLON.Vector2.Distance(new BABYLON.Vector2(this.camera.position.x,this.camera.position.z),new BABYLON.Vector2(this.arrows.orgdownPosition.x,this.arrows.orgdownPosition.z)); if(distance1>distance2) { this.arrows.orgactivePosition=this.arrows.orgdownPosition; this.arrows.activeMesh=this.arrows.downMesh; } else { this.arrows.orgactivePosition=this.arrows.orgtopPosition; this.arrows.activeMesh=this.arrows.topMesh; } } this.arrows.sideDistance=this.arrows.activeMesh.position.subtract(this.currentModelMesh._boundingInfo.boundingBox.center); this.drags.planeMesh.material=this.materials.selectMaterial; this.drags.planeStartPosition=new BABYLON.Vector3(this.drags.planeMesh.position.x,this.drags.planeMesh.position.y,this.drags.planeMesh.position.z); this.control.editProduct = true; }; //移除选择标记及箭头 Action3D.prototype.removeinfo=function() { if(this.currentInfoMesh!=null&&!this.currentInfoMesh._isDisposed) { this.currentInfoMesh.dispose(); this.currentInfoMesh=null; this.sprite.dispose(); this.sprite=null; } this.clearArrow(); if(this.selectCircleMesh!=null) { this.selectCircleMesh.dispose(); this.selectCircleMesh=null; if(this.selectRotatorMesh!=null) { this.selectRotatorMesh.dispose(); this.selectRotatorMesh=null; } } }; //编辑模型完成 Action3D.prototype.finishEditMesh=function() { if(this.drags.planeMesh!=null) { var dxz=this.drags.planeMesh.position.subtract(this.drags.planeStartPosition); var newscal; //m_newdistance表示当前情况下model的长或者宽 //m_olddistance表示过去model的长或者宽 var m_newdistance,m_olddistance; if(this.currentSelectMesh.name=="downarrow") { m_newdistance=this.arrows.orgdownPosition.subtract(this.arrows.orgtopPosition); m_olddistance=this.drags.planeStartPosition.subtract(this.arrows.orgtopPosition); } else if(this.currentSelectMesh.name=="toparrow") { m_newdistance=this.arrows.orgtopPosition.subtract(this.arrows.orgdownPosition); m_olddistance=this.drags.planeStartPosition.subtract(this.arrows.orgdownPosition); } else if(this.currentSelectMesh.name=="leftarrow") { m_newdistance=this.arrows.orgleftPosition.subtract(this.arrows.orgrightPosition); m_olddistance=this.drags.planeStartPosition.subtract(this.arrows.orgrightPosition); } else if(this.currentSelectMesh.name=="rightarrow") { m_newdistance=this.arrows.orgrightPosition.subtract(this.arrows.orgleftPosition); m_olddistance=this.drags.planeStartPosition.subtract(this.arrows.orgleftPosition); } newscal=new BABYLON.Vector3(m_newdistance.x.toFixed(2)/m_olddistance.x.toFixed(2)*this.currentModelMesh.scaling.x.toFixed(2),0,m_newdistance.z.toFixed(2)/m_olddistance.z.toFixed(2)*this.currentModelMesh.scaling.z.toFixed(2)); if(this.currentSelectMesh.name=="downarrow"||this.currentSelectMesh.name=="toparrow") { if(!isNaN(newscal.z)&&newscal.z!=-Number.POSITIVE_INFINITY&&newscal.z!=Number.POSITIVE_INFINITY) { this.currentModelMesh.scaling.z=newscal.z; } } else if(this.currentSelectMesh.name=="leftarrow"||this.currentSelectMesh.name=="rightarrow") { if(!isNaN(newscal.x)&&newscal.x!=-Number.POSITIVE_INFINITY&&newscal.x!=Number.POSITIVE_INFINITY) { this.currentModelMesh.scaling.x=newscal.x; } } this.currentModelMesh.computeWorldMatrix(true); this.currentModelMesh.refreshBoundingInfo(); var dxz2; if(this.currentSelectMesh.name=="downarrow"||this.currentSelectMesh.name=="toparrow") { dxz2=this.currentModelMesh._boundingInfo.boundingBox.center.subtract(new BABYLON.Vector3((this.arrows.orgdownPosition.x+this.arrows.orgtopPosition.x)/2,0,(this.arrows.orgdownPosition.z+this.arrows.orgtopPosition.z)/2)); this.currentModelMesh.position.addInPlace(new BABYLON.Vector3(-dxz2.x,0,-dxz2.z)); this.arrows.orgleftPosition.addInPlace(new BABYLON.Vector3(dxz.x/2,0,dxz.z/2)); this.arrows.orgrightPosition.addInPlace(new BABYLON.Vector3(dxz.x/2,0,dxz.z/2)); } else if(this.currentSelectMesh.name=="leftarrow"||this.currentSelectMesh.name=="rightarrow") { dxz2=this.currentModelMesh._boundingInfo.boundingBox.center.subtract(new BABYLON.Vector3((this.arrows.orgleftPosition.x+this.arrows.orgrightPosition.x)/2,0,(this.arrows.orgleftPosition.z+this.arrows.orgrightPosition.z)/2)); this.currentModelMesh.position.addInPlace(new BABYLON.Vector3(-dxz2.x,0,-dxz2.z)); this.arrows.orgtopPosition.addInPlace(new BABYLON.Vector3(dxz.x/2,0,dxz.z/2)); this.arrows.orgdownPosition.addInPlace(new BABYLON.Vector3(dxz.x/2,0,dxz.z/2)); } this.arrows.lineTopToDown=this.calculateLine.createLine(new BABYLON.Vector2(this.arrows.orgdownPosition.x,this.arrows.orgdownPosition.z),new BABYLON.Vector2(this.arrows.orgtopPosition.x,this.arrows.orgtopPosition.z)); this.arrows.lineLeftToRight=this.calculateLine.createLine(new BABYLON.Vector2(this.arrows.orgleftPosition.x,this.arrows.orgleftPosition.z),new BABYLON.Vector2(this.arrows.orgrightPosition.x,this.arrows.orgrightPosition.z)); this.drags.planeMesh.dispose(); this.drags.planeMesh=null; } }; //通过控制arrow来编辑模型 Action3D.prototype.editingMesh=function(type) { var newpoint; this.clearMeasureMesh(); var movdistance,diff1,diff2; //表示当前选择的arrow所在的线段的两个端点 var startpoint,endpoint if(this.currentSelectMesh.name=="downarrow"||this.currentSelectMesh.name=="toparrow") { if(type==1) { newpoint=this.getDiff2(this.drags.arrowIntersectPosition,this.arrows.lineTopToDown); newpoint.y=this.currentSelectMesh.position.y; } else //针对相框和墙贴 { newpoint=new BABYLON.Vector3(this.arrows.lineTopToDown.x,this.currentSelectMesh.position.y,this.drags.arrowIntersectPosition.z); } diff1=this.arrows.orgactivePosition.subtract(this.currentModelMesh._boundingInfo.boundingBox.center); diff2=this.arrows.sideDistance; movdistance=diff2.subtract(diff1); movdistance.x=movdistance.x/4; movdistance.y=movdistance.y/4; movdistance.z=movdistance.z/4; startpoint=this.arrows.orgtopPosition.add(diff2); startpoint.addInPlace(movdistance); endpoint=this.arrows.orgdownPosition.add(diff2); endpoint.addInPlace(movdistance); var fontpt=new BABYLON.Vector3((startpoint.x+endpoint.x)/2,(startpoint.y+endpoint.y)/2,(startpoint.z+endpoint.z)/2); fontpt.addInPlace(movdistance); if(this.measures.textMesh==null) { this.measures.textMesh = this.createTextPlane(BABYLON.Vector3.Distance(startpoint,endpoint).toFixed(2), "black", this.parameter.measureFontSize,fontpt); } else { this.updateTextPlane(BABYLON.Vector3.Distance(startpoint,endpoint).toFixed(2),this.parameter.measureFontSize,fontpt); } this.measures.textMesh.rotation.x=Math.PI/2; if(this.arrows.activeMesh.name=="rightarrow") { this.measures.textMesh.rotation.y=-Math.PI/2+this.currentModelMesh.angle; } else { this.measures.textMesh.rotation.y=Math.PI/2+this.currentModelMesh.angle; } } else if(this.currentSelectMesh.name=="leftarrow"||this.currentSelectMesh.name=="rightarrow") { if(type==1) { newpoint=this.getDiff2(this.drags.arrowIntersectPosition,this.arrows.lineLeftToRight); newpoint.y=this.currentSelectMesh.position.y; } else { newpoint=new BABYLON.Vector3(this.arrows.lineLeftToRight.y,this.currentSelectMesh.position.y,this.drags.arrowIntersectPosition.x); } diff1=this.arrows.orgactivePosition.subtract(this.currentModelMesh._boundingInfo.boundingBox.center); diff2=this.arrows.sideDistance; movdistance=diff2.subtract(diff1); movdistance.x=movdistance.x/4; movdistance.y=movdistance.y/4; movdistance.z=movdistance.z/4; startpoint=this.arrows.orgleftPosition.add(diff2); startpoint.addInPlace(movdistance); endpoint=this.arrows.orgrightPosition.add(diff2); endpoint.addInPlace(movdistance); var fontpt=new BABYLON.Vector3((startpoint.x+endpoint.x)/2,(startpoint.y+endpoint.y)/2,(startpoint.z+endpoint.z)/2); fontpt.addInPlace(movdistance); //this.measures.textMesh = this.createTextPlane(BABYLON.Vector3.Distance(startpoint,endpoint).toFixed(2), "black", 50,fontpt); if(this.measures.textMesh==null) { this.measures.textMesh = this.createTextPlane(BABYLON.Vector3.Distance(startpoint,endpoint).toFixed(2), "black", this.parameter.measureFontSize,fontpt); } else { this.updateTextPlane(BABYLON.Vector3.Distance(startpoint,endpoint).toFixed(2),this.parameter.measureFontSize,fontpt); } this.measures.textMesh.rotation.x=Math.PI/2; if(this.arrows.activeMesh.name=="toparrow") { this.measures.textMesh.rotation.y=Math.PI+this.currentModelMesh.angle; if(this.measures.textMesh.rotation.y>2*Math.PI) { this.measures.textMesh.rotation.y-=2*Math.PI; } } } var startpoint1=startpoint.add(movdistance); var startpoint2=startpoint.subtract(movdistance); var endpoint1=endpoint.add(movdistance); var endpoint2=endpoint.subtract(movdistance); this.showMeasureValue(startpoint,endpoint,startpoint1,endpoint1,startpoint2,endpoint2); return newpoint; }; //显示测量值 Action3D.prototype.showMeasureValue=function(startpoint,endpoint,startpoint1,endpoint1,startpoint2,endpoint2) { this.measures.endpointMesh1=BABYLON.Mesh.CreateLines("start",[startpoint1,startpoint2],this.scene); this.measures.endpointMesh2=BABYLON.Mesh.CreateLines("end",[endpoint1,endpoint2],this.scene); this.measures.linkMesh=BABYLON.Mesh.CreateLines("end",[startpoint,endpoint],this.scene); var pts1=this.drawArrow({x:startpoint.x,y:startpoint.z},{x:endpoint.x,y:endpoint.z},endpoint.y); var pts2=this.drawArrow({x:endpoint.x,y:endpoint.z},{x:startpoint.x,y:startpoint.z},startpoint.y); this.measures.arrowMesh1=BABYLON.Mesh.CreateLines("arrowend",pts1,this.scene); this.measures.arrowMesh2=BABYLON.Mesh.CreateLines("arrowstart",pts2,this.scene); this.measures.endpointMesh1.color = new BABYLON.Color3.Black(); this.measures.endpointMesh2.color = new BABYLON.Color3.Black(); this.measures.linkMesh.color = new BABYLON.Color3.Black(); this.measures.arrowMesh1.color = new BABYLON.Color3.Black(); this.measures.arrowMesh2.color = new BABYLON.Color3.Black(); }; //关闭配置窗口 Action3D.prototype.closeConfigProduct=function() { var configuratorProduct=document.getElementById("configuratorWindow"); configuratorProduct.style.display="none"; this.removeinfo(); }; //刷墙 Action3D.prototype.paintWallFromSubMesh=function(subMeshId) { var addMat = new BABYLON.StandardMaterial("paint", this.scene); addMat.alpha = 1.0; var r=this.layer3D.paint.value.r; var g=this.layer3D.paint.value.g; var b=this.layer3D.paint.value.b; addMat.diffuseColor = new BABYLON.Color3(r, g, b); addMat.specularColor =new BABYLON.Color3(r, g, b); addMat.emissiveColor = new BABYLON.Color3(r, g, b); addMat.backFaceCulling = false; addMat.toward=this.currentSelectMesh.material.subMaterials[subMeshId/2].toward; this.currentSelectMesh.material.subMaterials[subMeshId/2]=addMat; var flag=false; for(var i=0;i=0) { //刷墙 if(this.layer3D.paint.beginPaint) { this.paintWallFromSubMesh(pickInfo.subMeshId); this.layer3D.paint.beginPaint=false; this.currentSelectMesh = null; return; } //添加相框 else if(this.layer3D.modelLoader.currentPhotoMesh) { this.control.addPhoto=false; document.getElementById("container3d").style.cursor="default"; var joinpoint=pickInfo.pickedPoint; var toward= this.currentSelectMesh.material.subMaterials[pickInfo.subMeshId/2].toward; var indexplane=this.currentSelectMesh.material.subMaterials[pickInfo.subMeshId/2].indexplane; var point1=this.currentSelectMesh.material.subMaterials[pickInfo.subMeshId/2].points.point1; var point2=this.currentSelectMesh.material.subMaterials[pickInfo.subMeshId/2].points.point2; var photoFrameMesh = this.layer3D.modelLoader.currentPhotoMesh; var tempjoin=this.calculateLine.getPositionIntersect3D(point1,point2,toward,photoFrameMesh.thice,joinpoint); var tempjoin1=new BABYLON.Vector3(tempjoin.point1.x,tempjoin.point1.y,tempjoin.point1.z); var tempjoin2=new BABYLON.Vector3(tempjoin.point2.x,tempjoin.point2.y,tempjoin.point2.z); photoFrameMesh.rotation.y=this.collidableMeshes[indexplane].rotation.y; photoFrameMesh.position=tempjoin1; photoFrameMesh.computeWorldMatrix(true); photoFrameMesh.refreshBoundingInfo(); if(photoFrameMesh._boundingInfo.boundingBox.maximumWorld.y>this.parameter.wallHeight||photoFrameMesh._boundingInfo.boundingBox.minimumWorld.y<0) { photoFrameMesh.position.y=this.parameter.wallHeight/2; tempjoin1.y=this.parameter.wallHeight/2; tempjoin2.y=this.parameter.wallHeight/2; } photoFrameMesh.planeindex=pickInfo.subMeshId/2; var diffplane=tempjoin2.subtract(tempjoin1); if(diffplane.x==0&&diffplane.y==0&&diffplane.z==0) { photoFrameMesh.visibility=1; alert("2275"); return ; } else { var add=1; while(!this.collidableMeshes[indexplane].intersectsMesh(photoFrameMesh, true)) { tempjoin1.addInPlace(new BABYLON.Vector3(diffplane.x*0.1,diffplane.y*0.1,diffplane.z*0.1)); ++add; if(add>19) { //alert(2324); break; } photoFrameMesh.position=tempjoin1; photoFrameMesh.computeWorldMatrix(true); photoFrameMesh.refreshBoundingInfo(); } photoFrameMesh.visibility=1; var fontpt=photoFrameMesh._boundingInfo.boundingBox.vectorsWorld[photoFrameMesh.frontindex]; var toward2=this.calculateLine.getToward(point1,point2,{x:fontpt.x,y:fontpt.z}); if(toward!=toward2) { photoFrameMesh.rotation.y+=Math.PI; } if(photoFrameMesh.rotation.y>=2*Math.PI) { photoFrameMesh.rotation.y-=2*Math.PI; } for(var i=0;i=0) { //地板换贴图 if(this.layer3D.paint.beginPaint) { this.layer3D.paint.beginPaint=false; this.layer3D.paint.currentId=this.currentSelectMesh.id; this.layer3D.decorate3D.refreshFloor(this.layer3D.paint.currentId); this.removeSelectMesh(); return; } this.removeSelectMesh(); } //当前点击的是相框或者图画 else if(this.currentSelectMesh.name=="photo"||this.currentSelectMesh.name.indexOf("picture-")>=0) { var configuratorProduct=document.getElementById("configuratorWindow"); configuratorProduct.style.display="block"; this.switchCnofigureProduct(2); var params_height=document.getElementById("params-height"); var params_length=document.getElementById("params-length"); var height=this.currentSelectMesh.height; var length=this.currentSelectMesh.length; params_height.childNodes[1].childNodes[0].value=Math.floor(height); params_height.childNodes[1].childNodes[0].alt=Math.floor(height); params_length.childNodes[1].childNodes[0].value=Math.floor(length); params_length.childNodes[1].childNodes[0].alt=Math.floor(length); this.currentModelMesh = this.currentSelectMesh; setTimeout(function () { this.camera.detachControl(this.canvas); }.bind(this), 0); return; } //选择了编辑模型的圈圈 else if(this.currentSelectMesh.name=="selectCircle"||this.currentSelectMesh.name=="rotator") { this.control.actionRotator=true; setTimeout(function () { this.camera.detachControl(this.canvas); }.bind(this), 0); return; } //选择了编辑模型的箭头 else if(this.isArrow&&this.currentSelectMesh.name.indexOf("arrow")>-1) { if(!this.currentModelMesh) { alert("1083"); } this.selectArrow(); } //选择了模型 else if(this.currentSelectMesh.name!="door"&&this.currentSelectMesh.name!="ground"&&this.currentSelectMesh.name!="window"&&this.currentSelectMesh.name.indexOf("arrow")<0) { this.currentModelMesh = this.currentSelectMesh; this.setselectMesh(); this.setVertex(this.currentSelectMesh._boundingInfo.boundingBox.vectorsWorld,this.currentSelectMesh._boundingInfo.boundingBox.minimumWorld,this.currentSelectMesh._boundingInfo.boundingBox.maximumWorld); if (this.drags.modelStartPosition) { // we need to disconnect this.camera from canvas setTimeout(function () { this.camera.detachControl(this.canvas); }.bind(this), 0); } } else { this.removeSelectMesh(); } } else { this.removeSelectMesh(); } }; Action3D.prototype.onPointerUp = function () { if(this.lock) { return; } this.camera.attachControl(this.canvas, true); if (this.drags.modelStartPosition) { this.drags.modelStartPosition = null; if(this.control.editProduct) { this.finishEditMesh(); this.clearMeasureMesh(); } if(document.getElementById("configuratorWindow").style.display!="none") { this.setProductInfo(); } this.control.actionRotator=false; return; } }; Action3D.prototype.drawArrow= function( p0, p1,height) { var _ANGLE = 20; var _RADIUS = 10.0; var _ANGLE_CROSS = 90.0; var result=[]; var nP = {}; var angle = Math.atan2(p0.x-p1.x, p0.y-p1.y); result.push(new BABYLON.Vector3(p1.x,height,p1.y)); //rotate by _ANGLE var tAngle = angle - _ANGLE * Math.PI/180.0; nP.x = parseFloat(p1.x) + _RADIUS * Math.sin(tAngle); nP.y = parseFloat(p1.y) + _RADIUS * Math.cos(tAngle); result.push(new BABYLON.Vector3(nP.x,height,nP.y)); //lower part tAngle = angle + _ANGLE * Math.PI/180.0; nP.x = parseFloat(p1.x) + _RADIUS * Math.sin(tAngle); nP.y = parseFloat(p1.y) + _RADIUS * Math.cos(tAngle); result.push(new BABYLON.Vector3(p1.x,height,p1.y)); result.push(new BABYLON.Vector3(nP.x,height,nP.y)); return result; }; Action3D.prototype.clearMeasureMesh=function() { if(this.measures.endpointMesh1!=null) { this.measures.endpointMesh1.dispose(); this.measures.endpointMesh1=null; } if(this.measures.endpointMesh2!=null) { this.measures.endpointMesh2.dispose(); this.measures.endpointMesh2=null; } if(this.measures.linkMesh!=null) { this.measures.linkMesh.dispose(); this.measures.linkMesh=null; } if(this.measures.arrowMesh1!=null) { this.measures.arrowMesh1.dispose(); this.measures.arrowMesh1=null; } if(this.measures.arrowMesh2!=null) { this.measures.arrowMesh2.dispose(); this.measures.arrowMesh2=null; } if(this.measures.textMesh!=null) { this.measures.textMesh.dispose(); this.measures.textMesh=null; } }; Action3D.prototype.createTextPlane = function(text, color,size,position) { var dynamicTexture = new BABYLON.DynamicTexture("DynamicTexture", 50, this.scene, true); dynamicTexture.hasAlpha = true; var plane = BABYLON.Mesh.CreatePlane("TextPlane", size, this.scene, true); plane.material = new BABYLON.StandardMaterial("TextPlaneMaterial", this.scene); plane.material.backFaceCulling = false; plane.material.specularColor = new BABYLON.Color3(0, 0, 0); plane.material.diffuseTexture = dynamicTexture; plane.position=position; dynamicTexture._context.font = "normal 12px Arial"; var textSize=dynamicTexture._context.measureText(text); if(size-1) { var diff2,diff3; var startpoint,endpoint; var newpoint=this.editingMesh(1); diff = newpoint.subtract(this.currentSelectMesh.position); this.drags.planeMesh.position.addInPlace(diff); if(this.currentSelectMesh.name=="downarrow"||this.currentSelectMesh.name=="toparrow") { this.arrows.leftMesh.position.addInPlace(new BABYLON.Vector3(diff.x/2,diff.y/2,diff.z/2)); this.arrows.rightMesh.position.addInPlace(new BABYLON.Vector3(diff.x/2,diff.y/2,diff.z/2)); } else if(this.currentSelectMesh.name=="leftarrow"||this.currentSelectMesh.name=="rightarrow") { this.arrows.downMesh.position.addInPlace(new BABYLON.Vector3(diff.x/2,diff.y/2,diff.z/2)); this.arrows.topMesh.position.addInPlace(new BABYLON.Vector3(diff.x/2,diff.y/2,diff.z/2)); } this.currentSelectMesh.position=newpoint; this.currentSelectMesh.computeWorldMatrix(true); this.currentSelectMesh.refreshBoundingInfo(); this.drags.modelStartPosition = this.drags.moveModelPosition; } evt.stopPropagation(); return; } //通过移动模型的圈圈来旋转模型 else if(this.control.actionRotator) { var angle1=BABYLON.Angle.BetweenTwoPoints(new BABYLON.Vector2(this.drags.modelStartPosition.x,this.drags.modelStartPosition.z),new BABYLON.Vector2(this.selectCircleMesh.position.x,this.selectCircleMesh.position.z)); var angle2=BABYLON.Angle.BetweenTwoPoints(new BABYLON.Vector2(this.drags.moveModelPosition.x,this.drags.moveModelPosition.z),new BABYLON.Vector2(this.selectCircleMesh.position.x,this.selectCircleMesh.position.z)); if(this.currentModelMesh.angle==null) { this.currentModelMesh.angle=angle1._radians-angle2._radians; } else { this.currentModelMesh.angle+=angle1._radians-angle2._radians; } this.selectCircleMesh.rotate(BABYLON.Axis.Y, angle1._radians-angle2._radians, BABYLON.Space.LOCAL); this.selectCircleMesh.rotation.y+=angle1._radians-angle2._radians; this.currentModelMesh.rotate(BABYLON.Axis.Y, angle1._radians-angle2._radians, BABYLON.Space.LOCAL); this.currentModelMesh.rotation.y+=angle1._radians-angle2._radians; this.currentModelMesh.angle=this.currentModelMesh.rotation.y; this.drags.modelStartPosition = this.drags.moveModelPosition; } //软饰 else if((this.currentSelectMesh.name=="photo"||this.currentSelectMesh.name.indexOf("picture-")==0)) { var m_pt=this.getWallPlanePosition(); if(m_pt!=null) { var temp_last=null; var temp_pt =new BABYLON.Vector3(this.currentSelectMesh.position.x,this.currentSelectMesh.position.y,this.currentSelectMesh.position.z); if(this.currentSelectMesh.lastPos!=null) { temp_last=new BABYLON.Vector3(this.currentSelectMesh.lastPos.x,this.currentSelectMesh.lastPos.y,this.currentSelectMesh.lastPos.z); var diff2=m_pt.point.subtract(this.currentSelectMesh.lastPos); this.currentSelectMesh.lastPos=m_pt.point; this.currentSelectMesh.position.addInPlace(diff2); } else { this.currentSelectMesh.lastPos=m_pt.point; } this.currentSelectMesh.computeWorldMatrix(true); this.currentSelectMesh.refreshBoundingInfo(); if(this.currentSelectMesh._boundingInfo.boundingBox.maximumWorld.y>this.parameter.wallHeight||this.currentSelectMesh._boundingInfo.boundingBox.minimumWorld.y<0) { this.currentSelectMesh.lastPos=temp_last; this.currentSelectMesh.position=temp_pt; } else { var photoFrameMesh = this.layer3D.modelLoader.currentPhotoMesh; if(m_pt.index==photoFrameMesh.planeindex) { for(var i=0;i19) { //alert(2851); break; } this.currentSelectMesh.position=pos; this.currentSelectMesh.computeWorldMatrix(true); this.currentSelectMesh.refreshBoundingInfo(); } var fontpt=this.currentSelectMesh._boundingInfo.boundingBox.vectorsWorld[this.currentSelectMesh.frontindex]; var toward2=this.calculateLine.getToward(this.collidableMeshes[m_pt.index].pts.point1,this.collidableMeshes[m_pt.index].pts.point2,{x:fontpt.x,y:fontpt.z}); if(this.collidableMeshes[m_pt.index].toward!=toward2) { this.currentSelectMesh.rotation.y+=Math.PI; } if(this.currentSelectMesh.rotation.y>=2*Math.PI) { this.currentSelectMesh.rotation.y-=2*Math.PI; } photoFrameMesh.planeindex=m_pt.index; } } } } } //移动模型 else { //判断选择的模型是否可以发生碰撞,true表示不能,false表示可以 var flag=true; //this.currentSelectMesh.position = new BABYLON.Vector3(this.drags.moveModelPosition.x, this.drags.moveModelPosition.y, this.drags.moveModelPosition.z); this.currentSelectMesh.position.addInPlace(new BABYLON.Vector3(diff.x,0,diff.z)); //移动模型上面的info标记 if(this.currentInfoMesh!=null) { this.currentInfoMesh.position= new BABYLON.Vector3(this.currentSelectMesh.position.x, this.currentSelectMesh._boundingInfo.boundingBox.maximumWorld.y+30, this.currentSelectMesh.position.z); } //移动模型下面的圈圈 if(this.selectCircleMesh!=null) { this.selectCircleMesh.position.x=this.currentSelectMesh.position.x; this.selectCircleMesh.position.z=this.currentSelectMesh.position.z; } this.currentSelectMesh.computeWorldMatrix(true); this.currentSelectMesh.refreshBoundingInfo(); //判断模型是否发生碰撞,第一次加载的模型可以和其他模型重合 var collidableMesh=null; for(var i=0;ithis.collidableMeshes[i]._boundingInfo.boundingBox.maximumWorld.y&&this.currentSelectMesh._boundingInfo.boundingBox.maximumWorldMaxMark) { markpy=MaxMark; } mark.style.marginTop=markpy+"px"; }; Action3D.prototype.active = function () { this.canvas.addEventListener("mousedown", Action3D.prototype.onPointerDown.bind(this), false); this.canvas.addEventListener("mouseup", Action3D.prototype.onPointerUp.bind(this), false); this.canvas.addEventListener("mousemove", Action3D.prototype.onPointerMove.bind(this), false); this.canvas.addEventListener("mousewheel", Action3D.prototype.onCameraWheel.bind(this),false); }; Action3D.prototype.onDispose = function () { this.canvas.removeEventListener("mousedown", onPointerDown); this.canvas.removeEventListener("mouseup", onPointerUp); this.canvas.removeEventListener("mousemove", onPointerMove); this.canvas.removeEventListener("mousewheel", onCameraWheel); };