AEJSONSceneProcess.cpp 27 KB

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