Babylon file export.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. var meshes = [];
  2. var cameras = [];
  3. var lights = [];
  4. var materials = [];
  5. var BabylonMesh = function(obj)
  6. {
  7. var core = obj.core();
  8. if(core == null)
  9. {
  10. this.notMesh = true;
  11. return;
  12. }
  13. if(obj.materialTags() && obj.materialTags().length > 0)
  14. {
  15. var matId = obj.materialTags()[0].linkedToMaterial();
  16. this.materialId = materials[matId].id;
  17. if(materials[matId].diffuseTexture)
  18. {
  19. var vec2 = obj.materialTags()[0].getParameter("UVOffset");
  20. materials[matId].diffuseTexture.uOffset = vec2.x;
  21. materials[matId].diffuseTexture.vOffset = vec2.y;
  22. vec2 = obj.materialTags()[0].getParameter("UVScale");
  23. materials[matId].diffuseTexture.uScale = vec2.x;
  24. materials[matId].diffuseTexture.vScale = vec2.y;
  25. var vec3 = obj.materialTags()[0].getParameter("shadingRotation");
  26. materials[matId].diffuseTexture.uAng = vec3.x;
  27. materials[matId].diffuseTexture.vAng = vec3.y;
  28. materials[matId].diffuseTexture.wAng = vec3.z;
  29. }
  30. } else
  31. this.materialId = "";
  32. this.name = obj.getParameter("name");
  33. this.id = obj.getParameter("name");
  34. var vec3 = obj.getParameter("position");
  35. this.position = [-vec3.x, vec3.y, vec3.z];
  36. vec3 = obj.getParameter("rotation");
  37. this.rotation = [vec3.y* Math.PI/ 180, -vec3.x* Math.PI/ 180, -vec3.z* Math.PI/ 180];
  38. vec3 = obj.getParameter("scale");
  39. this.scaling = [vec3.x, vec3.y, vec3.z];
  40. this.isVisible = true;
  41. this.isEnabled = true;
  42. this.checkCollisions = false;
  43. this.billboardMode = 0;
  44. this.receiveShadows = true;
  45. this.positions = [];
  46. this.indices = [];
  47. this.uvs = [];
  48. this.uvs2 = [];
  49. this.normals = [];
  50. var tmpnormals = [];
  51. var tmpuv = [];
  52. for (var v = 0; v < core.vertexCount(); v++) {
  53. var vertex = core.vertex(v);
  54. this.positions.push(vertex.x);
  55. this.positions.push(vertex.y);
  56. this.positions.push(vertex.z);
  57. };
  58. for (var p = 0; p < core.polygonCount(); p++) {
  59. for (var t = 0; t < core.polygonSize(p) - 2; t++) {
  60. var triangle = core.triangle(p,t);
  61. for (var i = 0; i < 3; i++) {
  62. this.indices.push(core.vertexIndex(p,triangle[i]));
  63. tmpnormals[core.vertexIndex(p,triangle[i])] = core.normal(p,triangle[i]);
  64. // textcoord0 is [x,y], textcoord1 is [z,w]. Awesome doc work btw Cheetah3D
  65. tmpuv[core.vertexIndex(p,triangle[i])] = core.uvCoord(p, triangle[i]);
  66. };
  67. };
  68. };
  69. for (var n = 0; n < tmpnormals.length; n++) {
  70. var normal = tmpnormals[n];
  71. if(normal == null) // sometimes normals get randomly nulled, wth cheetah3d
  72. {
  73. normal = {};
  74. normal.x = 0;
  75. normal.y = 0;
  76. normal.z = 0;
  77. }
  78. this.normals.push(normal.x);
  79. this.normals.push(normal.y);
  80. this.normals.push(normal.z);
  81. };
  82. for (var n = 0; n < tmpuv.length; n++) {
  83. var uvCoords = tmpuv[n];
  84. if(uvCoords == null) // sometimes normals get randomly nulled, wth cheetah3d
  85. {
  86. uvCoords = {};
  87. uvCoords.x = 0;
  88. uvCoords.y = 0;
  89. uvCoords.z = 0;
  90. uvCoords.w = 0;
  91. }
  92. this.uvs.push(1 - uvCoords.x);
  93. this.uvs.push(1 - uvCoords.y);
  94. this.uvs2.push(1- uvCoords.z);
  95. this.uvs2.push(1 - uvCoords.w);
  96. };
  97. // no multiple submesh for now
  98. this.subMeshes = [
  99. {
  100. "materialIndex": 0,
  101. "verticesStart": 0,
  102. "verticesCount": core.vertexCount(),
  103. "indexStart": 0,
  104. "indexCount": core.triangleCount() * 3
  105. }
  106. ];
  107. }
  108. var BabylonCamera = function(cheetahCam)
  109. {
  110. this.name=cheetahCam.getParameter("name");
  111. this.id=cheetahCam.getParameter("name");
  112. var vec3 = cheetahCam.getParameter("position");
  113. this.position = [-vec3.x, vec3.y, vec3.z];
  114. this.fov=cheetahCam.getParameter("fieldOfView") * Math.PI/ 180;
  115. this.minZ=cheetahCam.getParameter("clipNear");
  116. this.maxZ=cheetahCam.getParameter("clipFar");
  117. // default values until we can find if cheetah3d has such data
  118. this.target=[0,0,0];
  119. this.speed=1;
  120. this.inertia=0.9;
  121. this.checkCollisions=false;
  122. this.applyGravity=false;
  123. this.ellipsoid=[
  124. 0.2,
  125. 0.9,
  126. 0.2
  127. ];
  128. }
  129. var BabylonLight = function(cheetahLight, type)
  130. {
  131. this.name = cheetahLight.getParameter("name");
  132. this.id = cheetahLight.getParameter("name");
  133. this.type = type;
  134. var vec3 = cheetahLight.getParameter("position");
  135. this.position = [-vec3.x, vec3.y, vec3.z];
  136. vec3 = cheetahLight.getParameter("rotation");
  137. var angles = [vec3.y* Math.PI/ 180, -vec3.x* Math.PI/ 180, -vec3.z* Math.PI/ 180];
  138. // shamefully copied from http://www.euclideanspace.com/maths/geometry/rotations/conversions/eulerToQuaternion/
  139. // using quaternion x vector multiplication from http://molecularmusings.wordpress.com/2013/05/24/a-faster-quaternion-vector-multiplication/
  140. var c1 = Math.cos(angles[1]);
  141. var s1 = Math.sin(angles[1]);
  142. var c2 = Math.cos(angles[2]);
  143. var s2 = Math.sin(angles[2]);
  144. var c3 = Math.cos(angles[0]);
  145. var s3 = Math.sin(angles[0]);
  146. var w = Math.sqrt(1.0 + c1 * c2 + c1*c3 - s1 * s2 * s3 + c2*c3) / 2.0;
  147. var w4 = (4.0 * w);
  148. var x = (c2 * s3 + c1 * s3 + s1 * s2 * c3) / w4 ;
  149. var y = (s1 * c2 + s1 * c3 + c1 * s2 * s3) / w4 ;
  150. var z = (-s1 * s3 + c1 * s2 * c3 +s2) / w4 ;
  151. var qv = new Vec3D(x,y,z);
  152. var up = new Vec3D(0,1,0);
  153. var t = qv.cross(up).multiply(2);
  154. var vf = up.add(t.multiply(w).add(qv.cross(t)));
  155. this.direction = [-vf.x,-vf.y,-vf.z];
  156. this.intensity = cheetahLight.getParameter("intensity");
  157. var color4 = cheetahLight.getParameter("color");
  158. this.diffuse = [color4.x,color4.y,color4.z];
  159. this.groundColor = [color4.x, color4.y, color4.z];
  160. this.specular = [1,1,1];
  161. if(type == 2)
  162. {
  163. this.angle = cheetahLight.getParameter("cutOffAngle")* Math.PI/ 180;
  164. this.exponent = cheetahLight.getParameter("cutOffAttenuation");
  165. }
  166. }
  167. var BabylonMaterial = function(material)
  168. {
  169. this.name = material.getParameter("name");
  170. this.id = material.getParameter("name");
  171. this.ambient =[1,1,1]; // ambient does not exist in cheetah3d
  172. var color4 = material.color();
  173. this.diffuse = [color4.x, color4.y, color4.z];
  174. color4 = material.specular();
  175. this.specular = [color4.x, color4.y, color4.z];
  176. this.specularPower = material.shininess();
  177. color4 = material.emission();
  178. this.emissive = [color4.x, color4.y, color4.z];
  179. this.alpha = 1;
  180. this.backFaceCulling = true;
  181. // diffuse texture info available only... this is edgy
  182. if(material.colorMap() != "none")
  183. {
  184. var name = material.colorMap().split("/");
  185. this.diffuseTexture = {
  186. name: name[name.length - 1],
  187. level: 1,
  188. hasAlpha: 0,
  189. coordinatesMode: 0,
  190. uOffset: 0,
  191. vOffset: 0,
  192. uScale: 1,
  193. vScale: 1,
  194. uAng: 0,
  195. vAng: 0,
  196. wAng: 0,
  197. wrapU: true,
  198. wrapV: true,
  199. coordinatesIndex: 0
  200. };}
  201. }
  202. function getChildren(obj, parentId)
  203. {
  204. for (var i = 0; i < obj.childCount(); i++) {
  205. var child = obj.childAtIndex(i);
  206. switch(child.type())
  207. {
  208. case LIGHT:
  209. switch(child.getParameter("lightType"))
  210. {
  211. case 0:
  212. var light = new BabylonLight(child, 3);
  213. lights.push(light);
  214. break;
  215. case 2:
  216. var light = new BabylonLight(child, 1);
  217. lights.push(light);
  218. break;
  219. case 4:
  220. var light = new BabylonLight(child, 2);
  221. lights.push(light);
  222. break;
  223. default:
  224. var light = new BabylonLight(child, 0);
  225. lights.push(light);
  226. break;
  227. }
  228. break;
  229. case CAMERA:
  230. var camera = new BabylonCamera(child);
  231. cameras.push(camera);
  232. break;
  233. default:
  234. var mesh = new BabylonMesh(child);
  235. if(parentId)
  236. mesh.parentId = parentId;
  237. parentId = mesh.id;
  238. if(!mesh.notMesh)
  239. {
  240. meshes.push(mesh);
  241. }
  242. break;
  243. }
  244. if(child.childCount() > 0)
  245. getChildren(child, parentId);
  246. };
  247. }
  248. function main(doc){
  249. var obj = doc.root();
  250. for(var i = 0; i < doc.materialCount(); i++)
  251. {
  252. var mat = new BabylonMaterial(doc.materialAtIndex(i));
  253. materials.push(mat);
  254. }
  255. getChildren(obj, 0);
  256. var scene = {};
  257. scene.autoClear=true;
  258. scene.clearColor=[1,1,1];
  259. scene.ambientColor=[0,0,0];
  260. scene.gravity=[0,0,0];
  261. scene.cameras=cameras;
  262. scene.activeCamera_=cameras[0].id;
  263. scene.lights=lights;
  264. scene.materials = materials;
  265. scene.meshes=meshes;
  266. scene.multiMaterials=[];
  267. scene.shadowGenerators=[];
  268. scene.skeletons=[];
  269. var path=OS.runSavePanel("babylon");
  270. if(path==null){
  271. return;
  272. }
  273. //open file
  274. var file = new File(path);
  275. file.open(WRITE_MODE);
  276. file.write(JSON.stringify(scene));
  277. file.close();
  278. print(materials.length + " materials");
  279. print(meshes.length + " meshes");
  280. print(cameras.length + " cameras");
  281. print(lights.length + " lights");
  282. print("\n\n");
  283. }