AEJSONSceneProcess.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #include <stdint.h>
  5. #include "AtomicEditor.h"
  6. #include <Atomic/Core/ProcessUtils.h>
  7. #include <Atomic/IO/Log.h>
  8. #include <Atomic/Resource/Image.h>
  9. #include <Atomic/Graphics/IndexBuffer.h>
  10. #include <Atomic/Graphics/VertexBuffer.h>
  11. #include <Atomic/Graphics/Geometry.h>
  12. #include <Atomic/Graphics/Octree.h>
  13. #include <Atomic/Graphics/Zone.h>
  14. #include <Atomic/Graphics/AnimatedModel.h>
  15. #include <Atomic/Graphics/StaticModel.h>
  16. #include <Atomic/Graphics/LMStaticModel.h>
  17. #include <Atomic/Graphics/Terrain.h>
  18. #include <Atomic/Graphics/Animation.h>
  19. #include <Atomic/Graphics/DebugRenderer.h>
  20. #include <Atomic/Physics/CollisionShape.h>
  21. #include <Atomic/Physics/RigidBody.h>
  22. #include <Atomic/Physics/PhysicsWorld.h>
  23. #include <Atomic/Environment/TimeOfDay.h>
  24. #include "AEJSONSceneProcess.h"
  25. #include "AEImportJSON.h"
  26. static String __rootFolder = "/Users/josh/Dev/atomic/AtomicRuntime/Source/AtomicEditor/Projects/OpenWorld/Resources";
  27. namespace AtomicEditor
  28. {
  29. bool JSONSceneProcess::ProcessTextures()
  30. {
  31. const PODVector<JSONTexture*>& textures = importer_->GetTextures();
  32. for (unsigned i = 0; i < textures.Size(); i++)
  33. {
  34. const JSONTexture* jtexture = textures.At(i);
  35. unsigned length;
  36. const unsigned char* pixels = jtexture->GetPNGPixels(length);
  37. String filename = __rootFolder + "/Textures/" + jtexture->GetName() + ".png";
  38. SharedPtr<File> file (new File(context_, filename, FILE_WRITE));
  39. file->Write(pixels, length);
  40. file->Close();
  41. }
  42. return true;
  43. }
  44. bool JSONSceneProcess::ProcessLightmaps()
  45. {
  46. const PODVector<JSONLightmap*>& lightmaps = importer_->GetLightmaps();
  47. for (unsigned i = 0; i < lightmaps.Size(); i++)
  48. {
  49. const JSONLightmap* jlightmap = lightmaps.At(i);
  50. unsigned length;
  51. const unsigned char* pixels = jlightmap->GetPNGPixels(length);
  52. String filename = __rootFolder + "/Textures/" + jlightmap->GetName() + ".png";
  53. SharedPtr<File> file (new File(context_, filename, FILE_WRITE));
  54. file->Write(pixels, length);
  55. file->Close();
  56. }
  57. return true;
  58. }
  59. bool JSONSceneProcess::ProcessMaterials()
  60. {
  61. const PODVector<JSONMaterial*>& materials = importer_->GetMaterials();
  62. ResourceCache* cache = GetSubsystem<ResourceCache>();
  63. for (unsigned i = 0; i < materials.Size(); i++)
  64. {
  65. const JSONMaterial* jmaterial = materials.At(i);
  66. const String& shader = jmaterial->GetShader();
  67. //LOGINFOF("Shader: %s", shader.CString());
  68. // TODO: factor in lightmaps
  69. bool lightmap = false;
  70. String technique("Diff.xml");
  71. if (!lightmap)
  72. {
  73. if (shader == "Transparent/Diffuse")
  74. technique = "DiffAlpha.xml";
  75. else if (shader == "Transparent/Cutout/Diffuse")
  76. technique = "DiffAlphaMask.xml";
  77. else if (shader == "Transparent/Cutout/Specular")
  78. technique = "DiffAlphaMask.xml";
  79. else if (shader == "Transparent/Specular")
  80. technique = "DiffAlpha.xml";
  81. else if (shader == "Hidden/Nature/Tree Creator Leaves Fast Optimized")
  82. technique = "DiffAlphaMask.xml";
  83. }
  84. else
  85. {
  86. technique = "DiffLightMap.xml";
  87. if (shader == "Transparent/Diffuse")
  88. technique = "DiffLightMapAlpha.xml";
  89. else if (shader == "Transparent/Cutout/Diffuse")
  90. technique = "DiffLightMapAlpha.xml";
  91. else if (shader == "Transparent/Cutout/Specular")
  92. technique = "DiffLightMapAlpha.xml";
  93. else if (shader == "Transparent/Specular")
  94. technique = "DiffLightMapAlpha.xml";
  95. }
  96. SharedPtr<Material> material;
  97. material = new Material(context_);
  98. material->SetName("Materials/" + jmaterial->GetName() + ".xml");
  99. Technique* _technique = cache->GetResource<Technique>("Techniques/" + technique);
  100. assert(_technique);
  101. material->SetTechnique(0, _technique);
  102. const String& mainTexture = jmaterial->GetMainTexture();
  103. if (mainTexture.Length())
  104. {
  105. Texture2D* texture = cache->GetResource<Texture2D>("Textures/" + mainTexture + ".png");
  106. material->SetTexture(TU_DIFFUSE, texture);
  107. }
  108. materials_[jmaterial->GetName()] = material;
  109. }
  110. return true;
  111. }
  112. bool JSONSceneProcess::WriteMaterials()
  113. {
  114. String materialFolder("Materials");
  115. HashMap<String, SharedPtr<Material> >::Iterator itr = materials_.Begin();
  116. while (itr != materials_.End())
  117. {
  118. Material* material = itr->second_;
  119. SharedPtr<File> file;
  120. file = new File(context_, __rootFolder + "/" + material->GetName(), FILE_WRITE);
  121. material->Save(*file);
  122. itr++;
  123. }
  124. return true;
  125. }
  126. bool JSONSceneProcess::WriteModels()
  127. {
  128. HashMap<String, SharedPtr<Model> >::Iterator itr = models_.Begin();
  129. while (itr != models_.End())
  130. {
  131. Model* model = itr->second_;
  132. SharedPtr<File> file;
  133. file = new File(context_, __rootFolder + "/" + model->GetName(), FILE_WRITE);
  134. model->Save(*file);
  135. itr++;
  136. }
  137. return true;
  138. }
  139. bool JSONSceneProcess::WriteHierarchy(Scene* scene)
  140. {
  141. List<SharedPtr<Node> >::Iterator itr = hierarchy_.Begin();
  142. while (itr != hierarchy_.End())
  143. {
  144. scene->AddChild(*itr);
  145. itr++;
  146. }
  147. File file(context_);
  148. bool useXML = true;
  149. String filename;
  150. filename.AppendWithFormat("%s", importer_->GetSceneName().CString());
  151. if (!useXML)
  152. filename += ".scene";
  153. else
  154. filename += ".xml";
  155. filename = __rootFolder + "/Scenes/" + filename;
  156. if (!file.Open(filename, FILE_WRITE))
  157. ErrorExit("Could not open output file: Scenes/Test.bin");
  158. if (useXML)
  159. scene->SaveXML(file);
  160. else
  161. scene->Save(file);
  162. file.Close();
  163. return true;
  164. }
  165. bool JSONSceneProcess::ProcessModels()
  166. {
  167. PODVector<JSONMesh*>& meshes = importer_->GetMeshes();
  168. ResourceCache* cache = GetSubsystem<ResourceCache>();
  169. for (unsigned i = 0; i < meshes.Size(); i++)
  170. {
  171. JSONMesh* jmesh = meshes.At(i);
  172. SharedPtr<Model> model(new Model(context_));
  173. model->SetName("Models/" + jmesh->GetName() + ".mdl");
  174. unsigned vertexElementMask = MASK_POSITION | MASK_NORMAL | MASK_TEXCOORD1;// | MASK_COLOR;
  175. if (jmesh->GetNumUVSets() == 2)
  176. vertexElementMask |= MASK_TEXCOORD2;
  177. PODVector<JSONMesh::BoneWeight>& boneWeights = jmesh->GetBoneWeights();
  178. if (boneWeights.Size())
  179. vertexElementMask |= (MASK_BLENDWEIGHTS | MASK_BLENDINDICES);
  180. // the model has to have 65535 or less indices to avoid 32 bit indices, check this
  181. SharedPtr<IndexBuffer> ib (new IndexBuffer(context_));
  182. SharedPtr<VertexBuffer> vb (new VertexBuffer(context_));
  183. // even though shadowing, need to "SetData()" on these below
  184. // could we SetData with the shadowed data?
  185. ib->SetShadowed(true);
  186. vb->SetShadowed(true);
  187. // Setup Vertex Data
  188. unsigned vertexCount = jmesh->GetVertexCount();
  189. vb->SetSize(vertexCount, vertexElementMask);
  190. BoundingBox bbox;
  191. PODVector<Vector3>& vpos = jmesh->GetVertexPositions();
  192. PODVector<Vector3>& vnorm = jmesh->GetVertexNormals();
  193. PODVector<Vector4>& vtan = jmesh->GetVertexTangents();
  194. PODVector<Vector2>& uv0 = jmesh->GetUVSet(0);
  195. PODVector<Vector2>& uv1 = jmesh->GetUVSet(1);
  196. PODVector<float> vdata;
  197. unsigned vertexSize = 8;
  198. if (vertexElementMask & MASK_TEXCOORD2)
  199. vertexSize += 2;
  200. if (vertexElementMask & MASK_BLENDWEIGHTS)
  201. vertexSize += 5;
  202. vdata.Resize(vertexCount * vertexSize);
  203. // TODO: we should be identifying unique vertices
  204. float* v = &vdata[0];
  205. for (unsigned j = 0; j < vertexCount; j++)
  206. {
  207. if (j < vpos.Size())
  208. {
  209. *v = vpos[j].x_; v++;
  210. *v = vpos[j].y_; v++;
  211. *v = vpos[j].z_; v++;
  212. }
  213. else
  214. {
  215. *v = 0; v++;
  216. *v = 0; v++;
  217. *v = 0; v++;
  218. }
  219. if (j < vnorm.Size())
  220. {
  221. *v = vnorm[j].x_; v++;
  222. *v = vnorm[j].y_; v++;
  223. *v = vnorm[j].z_; v++;
  224. }
  225. else
  226. {
  227. *v = 0; v++;
  228. *v = 0; v++;
  229. *v = 0; v++;
  230. }
  231. if (j < uv0.Size())
  232. {
  233. *v = uv0[j].x_; v++;
  234. *v = -uv0[j].y_; v++;
  235. }
  236. else
  237. {
  238. *v = 0; v++;
  239. *v = 0; v++;
  240. }
  241. if (vertexElementMask & MASK_TEXCOORD2)
  242. {
  243. if (j < uv1.Size())
  244. {
  245. *v = uv1[j].x_; v++;
  246. *v = -uv1[j].y_; v++;
  247. }
  248. else
  249. {
  250. *v = 0; v++;
  251. *v = 0; v++;
  252. }
  253. }
  254. if (vertexElementMask & MASK_BLENDWEIGHTS)
  255. {
  256. JSONMesh::BoneWeight& boneWeight = boneWeights[j];
  257. for (unsigned k = 0; k < 4; ++k)
  258. {
  259. *v = boneWeight.weights_[k]; v++;
  260. }
  261. }
  262. if (vertexElementMask & MASK_BLENDINDICES)
  263. {
  264. JSONMesh::BoneWeight& boneWeight = boneWeights[j];
  265. unsigned char* destBytes = (unsigned char*)v;
  266. ++v;
  267. for (unsigned k = 0; k < 4; ++k)
  268. {
  269. *destBytes++ = (unsigned char) boneWeight.indexes_[k];
  270. }
  271. }
  272. bbox.Merge(vpos[j]);
  273. }
  274. vb->SetData(&vdata[0]);
  275. unsigned numIndices = 0;
  276. PODVector<uint16_t> allIndices;
  277. unsigned numGeom = jmesh->GetSubMeshCount();
  278. for (unsigned j = 0; j < numGeom; j++)
  279. {
  280. PODVector<int>& submesh = jmesh->GetSubMesh(j);
  281. numIndices += submesh.Size();
  282. for (unsigned k = 0; k < submesh.Size(); k++)
  283. {
  284. allIndices.Push((uint16_t) submesh[k]);
  285. }
  286. }
  287. ib->SetSize(numIndices, false);
  288. ib->SetData(&allIndices[0]);
  289. Vector<SharedPtr<Geometry> > geometry;
  290. uint16_t start = 0;
  291. for (unsigned j = 0; j < numGeom; j++)
  292. {
  293. PODVector<int>& submesh = jmesh->GetSubMesh(j);
  294. SharedPtr<Geometry> geom(new Geometry(context_));
  295. geometry.Push(geom);
  296. geom->SetIndexBuffer(ib);
  297. geom->SetVertexBuffer(0, vb, vertexElementMask);
  298. geom->SetDrawRange(TRIANGLE_LIST, start, submesh.Size(), false);
  299. start += submesh.Size();
  300. }
  301. model->SetNumGeometries(geometry.Size());
  302. Vector<SharedPtr<VertexBuffer> > vbVector;
  303. Vector<SharedPtr<IndexBuffer> > ibVector;
  304. vbVector.Push(vb);
  305. ibVector.Push(ib);
  306. PODVector<unsigned> emptyMorphRange;
  307. model->SetVertexBuffers(vbVector, emptyMorphRange, emptyMorphRange);
  308. model->SetIndexBuffers(ibVector);
  309. Vector<JSONMesh::Bone>& jbones = jmesh->GetBones();
  310. // FIXME: we need to get the bounds or calculate propoer
  311. if (jbones.Size())
  312. {
  313. bbox.min_ *= 2;
  314. bbox.max_ *= 2;
  315. }
  316. // see fixme's for skeletal geo center/bounds
  317. for (unsigned j = 0; j < geometry.Size(); j++)
  318. {
  319. model->SetNumGeometryLodLevels(j, 1);
  320. model->SetGeometry(j, 0, geometry[j]);
  321. // this could be finer grained
  322. // also, fix this for skeletal
  323. model->SetGeometryCenter(j, jbones.Size() ? Vector3::ZERO : bbox.Center());//Vector3::ZERO);
  324. }
  325. // FIXME: skeletal bounds is off
  326. if (!jbones.Size())
  327. model->SetBoundingBox(bbox);
  328. else
  329. model->SetBoundingBox(BoundingBox(-1024, 1024));
  330. // Build skeleton if necessary
  331. if (jbones.Size())
  332. {
  333. Skeleton skeleton;
  334. Vector<Bone>& bones = skeleton.GetModifiableBones();
  335. unsigned rootindex = 0;
  336. const String& rootBoneName = jmesh->GetRootBone();
  337. for (unsigned j = 0; j < jbones.Size(); ++j)
  338. {
  339. JSONMesh::Bone& jbone = jbones[j];
  340. if (jbone.name_ == rootBoneName)
  341. {
  342. rootindex = j;
  343. break;
  344. }
  345. }
  346. for (unsigned j = 0; j < jbones.Size(); ++j)
  347. {
  348. JSONMesh::Bone& jbone = jbones[j];
  349. String boneName(jbone.name_);
  350. Bone newBone;
  351. newBone.name_ = boneName;
  352. newBone.initialPosition_ = jbone.pos_;
  353. newBone.initialRotation_ = jbone.rot_;
  354. newBone.initialScale_ = jbone.scale_;
  355. // Get offset information if exists
  356. newBone.offsetMatrix_ = Matrix3x4(jmesh->GetBindPoses()[j]);
  357. //newBone.radius_ = model.boneRadii_[i];
  358. //newBone.boundingBox_ = model.boneHitboxes_[i];
  359. //newBone.collisionMask_ = BONECOLLISION_SPHERE | BONECOLLISION_BOX;
  360. newBone.parentIndex_ = j;
  361. bones.Push(newBone);
  362. }
  363. // Set the bone hierarchy
  364. for (unsigned j = 0; j < jbones.Size(); ++j)
  365. {
  366. JSONMesh::Bone& jbone = jbones[j];
  367. String parentName = jbone.parentName_;
  368. if (!parentName.Length())
  369. {
  370. bones[j].parentIndex_ = j;
  371. }
  372. else
  373. {
  374. for (unsigned k = 0; k < bones.Size(); ++k)
  375. {
  376. if (bones[k].name_ == parentName)
  377. {
  378. bones[j].parentIndex_ = k;
  379. break;
  380. }
  381. }
  382. }
  383. }
  384. skeleton.SetRootBoneIndex(rootindex);
  385. model->SetSkeleton(skeleton);
  386. }
  387. models_[jmesh->GetName()] = model;
  388. }
  389. return true;
  390. }
  391. bool JSONSceneProcess::ProcessComponent(Node* node, const JSONTransform* jtransform)
  392. {
  393. node->SetPosition(jtransform->GetLocalPosition());
  394. node->SetRotation(jtransform->GetLocalRotation());
  395. node->SetScale(jtransform->GetLocalScale());
  396. return true;
  397. }
  398. bool JSONSceneProcess::ProcessComponent(Node* node, const JSONTimeOfDay* jtime )
  399. {
  400. TimeOfDay* timeofday = node->CreateComponent<TimeOfDay>();
  401. timeofday->SetTimeOn(jtime->GetTimeOn());
  402. timeofday->SetTimeOff(jtime->GetTimeOff());
  403. return true;
  404. }
  405. bool JSONSceneProcess::ProcessComponent(Node* node, const JSONLight* jlight )
  406. {
  407. if (!jlight->GetRealtime() || jlight->GetLightType() != "Point")
  408. return true;
  409. Light* light = node->CreateComponent<Light>();
  410. light->SetRange(jlight->GetRange());
  411. light->SetCastShadows(jlight->GetCastsShadows());
  412. light->SetColor(jlight->GetColor());
  413. light->SetLightType(LIGHT_POINT);
  414. return true;
  415. }
  416. bool JSONSceneProcess::ProcessComponent(Node* node, const JSONBoxCollider* jbox )
  417. {
  418. CollisionShape* shape = node->CreateComponent<CollisionShape>();
  419. shape->SetBox(jbox->GetSize(), jbox->GetCenter());
  420. return true;
  421. }
  422. bool JSONSceneProcess::ProcessComponent(Node* node, const JSONMeshCollider* jmesh )
  423. {
  424. // JSONMeshCollider should store the mesh, as this may not be on node
  425. CollisionShape* shape = node->CreateComponent<CollisionShape>();
  426. StaticModel* model = node->GetComponent<StaticModel>();
  427. if (!model || !model->GetModel())
  428. {
  429. LOGWARNING("Missing model for MeshCollier");
  430. return true;
  431. }
  432. assert(model && model->GetModel());
  433. if (model)
  434. shape->SetTriangleMesh(model->GetModel());
  435. return true;
  436. }
  437. bool JSONSceneProcess::ProcessComponent(Node* node, const JSONRigidBody* jbody )
  438. {
  439. RigidBody* body = node->CreateComponent<RigidBody>();
  440. body->SetMass(jbody->GetMass());
  441. return true;
  442. }
  443. bool JSONSceneProcess::ProcessComponent(Node* node, const JSONTerrain* jterrain)
  444. {
  445. int heightmapHeight = jterrain->GetHeightMapHeight();
  446. int heightmapWidth = jterrain->GetHeightMapWidth();
  447. unsigned length;
  448. const float* heightmap = jterrain->GetHeightMap(length);
  449. const Vector3 heightmapScale = jterrain->GetHeightMapScale();
  450. const Vector3 heightmapSize = jterrain->GetHeightMapSize();
  451. PODVector<uint8_t> bytes;
  452. bytes.Resize(heightmapHeight * heightmapWidth * 4);
  453. uint8_t* out = &bytes[0];
  454. for (int y = 0; y < heightmapHeight; y++)
  455. {
  456. for (int x = 0; x < heightmapWidth; x++)
  457. {
  458. float h = heightmap[y * heightmapWidth + x];
  459. uint8_t hbyte = uint8_t(255.0 * h);
  460. *out++ = hbyte;
  461. *out++ = hbyte;
  462. *out++ = hbyte;
  463. *out++ = 255;
  464. }
  465. }
  466. SharedPtr<Image> image(new Image(context_));
  467. image->SetSize(heightmapWidth, heightmapHeight, 4);
  468. image->SetData(&bytes[0]);
  469. String heightMapPath = __rootFolder + "/Textures/TerrainHeightMap.png";
  470. image->SavePNG(heightMapPath);
  471. int alphamapWidth = jterrain->GetAlphaMapHeight();
  472. int alphamapHeight = jterrain->GetAlphaMapWidth();
  473. int alphamapLayers = jterrain->GetAlphaMapLayers();
  474. if (alphamapLayers > 3)
  475. alphamapLayers = 3;
  476. const float* alphamap = jterrain->GetAlphaMap(length);
  477. bytes.Resize( alphamapWidth * alphamapWidth * 4);
  478. memset (&bytes[0], 255, alphamapWidth * alphamapWidth * 4);
  479. for (int i = 0; i < alphamapLayers; i++)
  480. {
  481. uint8_t* out = &bytes[i];
  482. for (int y = 0; y < alphamapHeight; y++)
  483. {
  484. for (int x = 0; x < alphamapWidth; x++)
  485. {
  486. float w = alphamap[(i * (alphamapWidth * alphamapHeight)) + y * alphamapWidth + x ];
  487. uint8_t wbyte = uint8_t(255.0 * w);
  488. *out = wbyte;
  489. out += 4;
  490. }
  491. }
  492. }
  493. SharedPtr<Image> alphaWeights(new Image(context_));
  494. alphaWeights->SetSize(alphamapWidth, alphamapHeight, 4);
  495. alphaWeights->SetData(&bytes[0]);
  496. String alphaWeightsPath = __rootFolder + "/Textures/TerrainWeights.png";
  497. alphaWeights->SavePNG(alphaWeightsPath);
  498. ResourceCache* cache = GetSubsystem<ResourceCache>();
  499. Image* heightMap = cache->GetResource<Image>(heightMapPath);
  500. Material* material = cache->GetResource<Material>("Materials/DemoTerrain.xml");
  501. Terrain* terrain = node->CreateComponent<Terrain>();
  502. terrain->SetHeightMap(heightMap);
  503. terrain->SetMaterial(material);
  504. terrain->SetSmoothing(true);
  505. //terrain->SetPatchSize(64);
  506. //terrain->SetSpacing(Vector3(2.0f, 0.5f, 2.0f)); // Spacing between vertices and vertical resolution of the height map
  507. //terrain->SetSmoothing(true);
  508. Vector3 spacing = heightmapScale;
  509. spacing.y_ = heightmapScale.y_ / 250.0f;
  510. terrain->SetSpacing(spacing);
  511. Vector3 pos = node->GetPosition();
  512. pos.x_ += heightmapSize.x_/2.0f;
  513. pos.y_ += .1f;
  514. pos.z_ += heightmapSize.z_/2.0f;
  515. node->SetPosition(pos);
  516. Quaternion rotation;
  517. rotation = node->GetRotation();
  518. rotation.FromEulerAngles(0, -90, 0);
  519. node->SetRotation(rotation);
  520. RigidBody* body = node->CreateComponent<RigidBody>();
  521. // use layer 2 for static
  522. body->SetCollisionLayer(2);
  523. body->SetFriction(1.0f);
  524. CollisionShape* shape = node->CreateComponent<CollisionShape>();
  525. shape->SetTerrain();
  526. return true;
  527. }
  528. bool JSONSceneProcess::ProcessComponent(Node* node, const JSONAnimation* janim )
  529. {
  530. const Vector<JSONAnimation::AnimationClip*>& clips = janim->GetClips();
  531. for (unsigned i = 0; i < clips.Size(); i++)
  532. {
  533. const JSONAnimation::AnimationClip* clip = clips[i];
  534. SharedPtr<Animation> outAnim(new Animation(context_));
  535. outAnim->SetAnimationName(clip->name_);
  536. outAnim->SetLength(clip->GetDuration());
  537. Vector<AnimationTrack> tracks;
  538. for (unsigned j = 0; j < clip->nodes_.Size(); ++j)
  539. {
  540. const JSONAnimation::AnimationNode* node = clip->nodes_[j];
  541. String channelName = node->name_;
  542. if (channelName.Contains('/'))
  543. {
  544. channelName = channelName.Split('/').Back();
  545. }
  546. AnimationTrack track;
  547. track.name_ = channelName;
  548. track.nameHash_ = channelName;
  549. // TODO: optimize channels
  550. track.channelMask_ |= (CHANNEL_POSITION | CHANNEL_ROTATION | CHANNEL_SCALE);
  551. //assert(node->keyframes_.Size());
  552. for (unsigned k = 0; k < node->keyframes_.Size(); k++)
  553. {
  554. const JSONAnimation::Keyframe* keyframe = node->keyframes_[k];
  555. AnimationKeyFrame key;
  556. key.time_ = keyframe->time_;
  557. key.position_ = keyframe->pos_;
  558. key.rotation_ = keyframe->rot_;
  559. key.scale_ = keyframe->scale_;
  560. track.keyFrames_.Push(key);
  561. }
  562. tracks.Push(track);
  563. }
  564. outAnim->SetTracks(tracks);
  565. String filename = __rootFolder;
  566. filename.AppendWithFormat("/Models/AS_FatZombie_FBX_FatZombie_LOD0_%s.ani", clip->name_.CString());
  567. File outFile(context_);
  568. if (!outFile.Open(filename, FILE_WRITE))
  569. ErrorExit("Could not open output file for animation");
  570. outAnim->Save(outFile);
  571. }
  572. return true;
  573. }
  574. bool JSONSceneProcess::ProcessComponent(Node* node, const JSONMeshRenderer* jmeshrenderer )
  575. {
  576. ResourceCache* cache = GetSubsystem<ResourceCache>();
  577. StaticModel* staticModel = NULL;
  578. int lightmapIndex = jmeshrenderer->GetLightmapIndex();
  579. if (lightmapIndex >= 0)
  580. {
  581. LMStaticModel* lmstatic = node->CreateComponent<LMStaticModel>();
  582. staticModel = lmstatic;
  583. const Vector4& tilingOffset = jmeshrenderer->GetLightmapTilingOffset();
  584. lmstatic->lightmapTilingOffset_ = tilingOffset;
  585. lmstatic->lightmapTilingOffset_.w_ = -lmstatic->lightmapTilingOffset_.w_;
  586. String lightmapName;
  587. lightmapName.AppendWithFormat("Textures/%s_Lightmap_%i.png", importer_->GetSceneName().CString(), lightmapIndex);
  588. Texture2D* texture = cache->GetResource<Texture2D>(lightmapName);
  589. assert(texture);
  590. lmstatic->SetLightmapTexure(texture);
  591. }
  592. else
  593. staticModel = node->CreateComponent<StaticModel>();
  594. const JSONMesh* mesh = jmeshrenderer->GetMesh();
  595. assert(models_.Contains(mesh->GetName()));
  596. Model* model = models_[mesh->GetName()];
  597. staticModel->SetModel(model);
  598. for (unsigned i = 0; i < jmeshrenderer->GetNumMaterials(); i++)
  599. {
  600. const JSONMaterial* jmat = jmeshrenderer->GetMaterial(i);
  601. assert(materials_.Contains(jmat->GetName()));
  602. staticModel->SetMaterial(i, materials_[jmat->GetName()]);
  603. }
  604. staticModel->SetCastShadows(jmeshrenderer->GetCastShadows());
  605. /*
  606. CollisionShape* shape = node->CreateComponent<CollisionShape>();
  607. shape->SetTriangleMesh(model, 0);
  608. node->CreateComponent<RigidBody>();
  609. */
  610. BoundingBox bbox = model->GetBoundingBox();
  611. bbox.Transform(node->GetWorldTransform());
  612. Vector3 size = bbox.Size();
  613. //staticModel->SetDrawDistance(size.Length() * 30.0f);
  614. //staticModel->SetShadowDistance(size.Length() * 10.0f);
  615. return true;
  616. }
  617. bool JSONSceneProcess::ProcessComponent(Node* node, const JSONSkinnedMeshRenderer* jmeshrenderer )
  618. {
  619. ResourceCache* cache = GetSubsystem<ResourceCache>();
  620. AnimatedModel* animatedModel = node->CreateComponent<AnimatedModel>();
  621. const JSONMesh* mesh = jmeshrenderer->GetMesh();
  622. assert(models_.Contains(mesh->GetName()));
  623. Model* model = models_[mesh->GetName()];
  624. animatedModel->SetModel(model);
  625. for (unsigned i = 0; i < jmeshrenderer->GetNumMaterials(); i++)
  626. {
  627. const JSONMaterial* jmat = jmeshrenderer->GetMaterial(i);
  628. assert(materials_.Contains(jmat->GetName()));
  629. animatedModel->SetMaterial(i, materials_[jmat->GetName()]);
  630. }
  631. animatedModel->SetCastShadows(jmeshrenderer->GetCastShadows());
  632. return true;
  633. }
  634. Node* JSONSceneProcess::ProcessNode(const JSONNode* jnode, Node* parent)
  635. {
  636. Node* node = parent->CreateChild(jnode->GetName());
  637. const PODVector<JSONComponent*>& components = jnode->GetComponents();
  638. for (unsigned i = 0; i < components.Size(); i++)
  639. {
  640. const JSONComponent* c = components.At(i);
  641. if (c->GetType() == "Transform")
  642. {
  643. ProcessComponent(node, (const JSONTransform*) c);
  644. }
  645. else if (c->GetType() == "MeshRenderer")
  646. {
  647. ProcessComponent(node, (const JSONMeshRenderer*) c);
  648. }
  649. else if (c->GetType() == "SkinnedMeshRenderer")
  650. {
  651. ProcessComponent(node, (const JSONSkinnedMeshRenderer*) c);
  652. }
  653. else if (c->GetType() == "Terrain")
  654. {
  655. ProcessComponent(node, (const JSONTerrain*) c);
  656. }
  657. else if (c->GetType() == "Animation")
  658. {
  659. ProcessComponent(node, (const JSONAnimation*) c);
  660. }
  661. else if (c->GetType() == "BoxCollider")
  662. {
  663. ProcessComponent(node, (const JSONBoxCollider*) c);
  664. }
  665. else if (c->GetType() == "MeshCollider")
  666. {
  667. ProcessComponent(node, (const JSONMeshCollider*) c);
  668. }
  669. else if (c->GetType() == "RigidBody")
  670. {
  671. ProcessComponent(node, (const JSONRigidBody*) c);
  672. }
  673. else if (c->GetType() == "Light")
  674. {
  675. ProcessComponent(node, (const JSONLight*) c);
  676. }
  677. else if (c->GetType() == "TimeOfDay")
  678. {
  679. ProcessComponent(node, (const JSONTimeOfDay*) c);
  680. }
  681. }
  682. if (node->GetComponent<CollisionShape>() && !node->GetComponent<RigidBody>())
  683. {
  684. RigidBody* body = node->CreateComponent<RigidBody>();
  685. body->SetCollisionLayer(2);
  686. }
  687. const PODVector<JSONNode*>& children = jnode->GetChildren();
  688. for (unsigned i = 0; i < children.Size(); i++)
  689. {
  690. node->AddChild(ProcessNode(children.At(i), node));
  691. }
  692. return node;
  693. }
  694. bool JSONSceneProcess::ProcessHierarchy(Scene* scene)
  695. {
  696. const PODVector<JSONNode*>& hierarchy = importer_->GetHierarchy();
  697. for (unsigned i = 0; i < hierarchy.Size(); i++)
  698. {
  699. hierarchy_.Push(SharedPtr<Node>(ProcessNode(hierarchy[i], scene)));
  700. }
  701. return true;
  702. }
  703. bool JSONSceneProcess::Process()
  704. {
  705. scene_ = new Scene(context_);
  706. scene_->CreateComponent<PhysicsWorld>();
  707. scene_->CreateComponent<Octree>();
  708. scene_->CreateComponent<DebugRenderer>();
  709. Node* zoneNode = scene_->CreateChild("Zone");
  710. Zone* zone = zoneNode->CreateComponent<Zone>();
  711. zone->SetBoundingBox(BoundingBox(-10000.0f, 10000.f));
  712. zone->SetAmbientColor(Color(1, 1, 1));
  713. ProcessTextures();
  714. ProcessLightmaps();
  715. ProcessMaterials();
  716. ProcessModels();
  717. ProcessHierarchy(scene_);
  718. return true;
  719. }
  720. bool JSONSceneProcess::Write()
  721. {
  722. WriteMaterials();
  723. WriteModels();
  724. WriteHierarchy(scene_);
  725. return true;
  726. }
  727. }