ModelLoader.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. function ModelLoader(layer3D)
  2. {
  3. this.parameter = layer3D.layer.parameter;
  4. this.glassBoxMeshes = layer3D.collectMeshes.glassBoxMeshes;
  5. this.collidableMeshes=layer3D.collectMeshes.collidableMeshes;
  6. this.scene = layer3D.scene3D.scene;
  7. this.engine = layer3D.scene3D.engine;
  8. this.furnitureScal=0.2;
  9. this.currentPhotoMesh=null;
  10. };
  11. //添加画,图
  12. ModelLoader.prototype.showPicture=function(src)
  13. {
  14. var plan = BABYLON.Mesh.CreatePlane("picture-"+src, 1, this.scene);
  15. plan.scaling.x=50;
  16. plan.scaling.y=40;
  17. plan.floor=layer.selectFloor;
  18. plan.position=new BABYLON.Vector3(0, (plan.floor-1)*this.parameter.wallHeight, 0);
  19. var material = new BABYLON.StandardMaterial("planmaterial", this.scene);
  20. material.diffuseTexture = new BABYLON.Texture(this.wallpicture_pre+src, this.scene);
  21. material.bumpTexture = new BABYLON.Texture("images/floor1.png", this.scene);
  22. plan.material=material;
  23. plan.rotation.x=Math.PI/2;
  24. this.collidableMeshes.push(plan);
  25. };
  26. //添加镜框
  27. ModelLoader.prototype.showPhoFrame=function(src)
  28. {
  29. BABYLON.SceneLoader.ImportMesh("", "images/3d/", src, this.scene, function (newMeshes)
  30. {
  31. newMeshes[0].scaling=new BABYLON.Vector3(this.furnitureScal,this.furnitureScal,this.furnitureScal);
  32. newMeshes[0].computeWorldMatrix(true);
  33. newMeshes[0].refreshBoundingInfo();
  34. newMeshes[0].position=new BABYLON.Vector3(0,this.parameter.softDecorationY,0);
  35. //newMeshes[0].name="pho-"+src;
  36. newMeshes[0].thice=Math.abs(newMeshes[0]._boundingInfo.boundingBox.maximumWorld.z-newMeshes[0]._boundingInfo.boundingBox.minimumWorld.z);
  37. newMeshes[0].firstHeigh=Math.abs(newMeshes[0]._boundingInfo.boundingBox.maximumWorld.y-newMeshes[0]._boundingInfo.boundingBox.minimumWorld.y);
  38. this.currentPhotoMesh=newMeshes[0];
  39. this.currentPhotoMesh.name="photo";
  40. this.currentPhotoMesh.height=newMeshes[0].firstHeigh;
  41. this.currentPhotoMesh.length=Math.abs(newMeshes[0]._boundingInfo.boundingBox.maximumWorld.x-newMeshes[0]._boundingInfo.boundingBox.minimumWorld.x);
  42. this.currentPhotoMesh.visibility=0;
  43. document.getElementById("container3d").style.cursor="url('images/brush.ico'),auto";
  44. for(var i=0;i<this.currentPhotoMesh._boundingInfo.boundingBox.vectorsWorld.length;++i)
  45. {
  46. if(this.currentPhotoMesh._boundingInfo.boundingBox.vectorsWorld[i].z>this.currentPhotoMesh._boundingInfo.boundingBox.minimumWorld.z)
  47. {
  48. this.currentPhotoMesh.frontindex=i;
  49. break;
  50. }
  51. }
  52. this.engine.hideLoadingUI();
  53. }.bind(this));
  54. };
  55. //初始化时,会有一些三维模型
  56. ModelLoader.prototype.showProductfirst=function(src,position,rotation)
  57. {
  58. BABYLON.SceneLoader.ImportMesh("", "images/3d/", src, this.scene, function (newMeshes)
  59. {
  60. var mesh;
  61. if(newMeshes.length>1)
  62. {
  63. mesh=BABYLON.Mesh.MergeMeshes(newMeshes);
  64. }
  65. else
  66. {
  67. mesh=newMeshes[0];
  68. }
  69. var name=src.replace(".babylon","");
  70. mesh.scaling=new BABYLON.Vector3(layer.parameter.testModelData[name],layer.parameter.testModelData[name],layer.parameter.testModelData[name]);
  71. mesh.position=position;
  72. if(layer.parameter.cabinet.indexOf(name+",")>-1)
  73. {
  74. var glassMesh = BABYLON.Mesh.CreateBox("box", 1, this.scene);
  75. glassMesh.scaling.x=Math.abs(mesh._boundingInfo.maximum.x-mesh._boundingInfo.minimum.x)*mesh.scaling.x-5;
  76. glassMesh.scaling.y=80;
  77. glassMesh.scaling.z=Math.abs(mesh._boundingInfo.maximum.z-mesh._boundingInfo.minimum.z)*mesh.scaling.z/1.2;
  78. glassMesh.position.x=position.x;
  79. glassMesh.position.y=Math.abs(mesh._boundingInfo.maximum.y-mesh._boundingInfo.minimum.y)*mesh.scaling.y-40;
  80. glassMesh.position.z=position.z-2;
  81. glassMesh.visibility = 0.3;
  82. glassMesh.name="mirror";
  83. this.glassBoxMeshes.push(glassMesh);
  84. }
  85. for(var i=0;i<this.collidableMeshes.length;++i)
  86. {
  87. if (typeof(this.collidableMeshes[i]) == "undefined") {
  88. this.collidableMeshes.splice(i,1);
  89. --i;
  90. continue;
  91. }
  92. if (mesh.intersectsMesh(this.collidableMeshes[i], true))
  93. {
  94. mesh.iscollision=true;
  95. break;
  96. }
  97. }
  98. ++initMesh;
  99. if(initMesh>21)
  100. {
  101. this.engine.hideLoadingUI();
  102. initMesh=0;
  103. }
  104. mesh.floor=layer.selectFloor;
  105. mesh.position.y+=(mesh.floor-1)*this.parameter.wallHeight;
  106. this.collidableMeshes.push(mesh);
  107. }.bind(this));
  108. return this.collidableMeshes[this.collidableMeshes.length-1];
  109. };
  110. //加载三维模型
  111. ModelLoader.prototype.showProduct=function(src)
  112. {
  113. this.engine.displayLoadingUI();
  114. this.engine.loadingUIText = "正在加载家具,请稍等!";
  115. BABYLON.SceneLoader.ImportMesh("", "images/3d/", src, this.scene, function (newMeshes)
  116. {
  117. var mesh;
  118. if(newMeshes.length>1)
  119. {
  120. mesh=BABYLON.Mesh.MergeMeshes(newMeshes);
  121. }
  122. else
  123. {
  124. mesh=newMeshes[0];
  125. }
  126. var name=src.replace(".babylon","");
  127. mesh.scaling=new BABYLON.Vector3(layer.parameter.testModelData[name],layer.parameter.testModelData[name],layer.parameter.testModelData[name]);
  128. mesh.name=src;
  129. for(var i=0;i<this.collidableMeshes.length;++i)
  130. {
  131. if (typeof(this.collidableMeshes[i]) == "undefined") {
  132. this.collidableMeshes.splice(i,1);
  133. --i;
  134. continue;
  135. }
  136. if (mesh.intersectsMesh(this.collidableMeshes[i], true))
  137. {
  138. //第一次加载的模型,如果此时和别的模型发生碰撞,则表示可以碰撞,待不和别的模型发送碰撞时候,再实现不可碰撞
  139. mesh.iscollision=true;
  140. break;
  141. }
  142. }
  143. mesh.floor=layer.selectFloor;
  144. mesh.position.y=(mesh.floor-1)*this.parameter.wallHeight;
  145. this.collidableMeshes.push(mesh);
  146. this.engine.hideLoadingUI();
  147. }.bind(this));
  148. };