Skip to content

Commit e850e89

Browse files
committed
Preparing some meshes to async texture load
1 parent cb92730 commit e850e89

2 files changed

Lines changed: 93 additions & 43 deletions

File tree

engine/core/subsystem/MeshSystem.cpp

Lines changed: 89 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ MeshSystem::~MeshSystem(){
2727

2828
}
2929

30-
void MeshSystem::createSprite(SpriteComponent& sprite, MeshComponent& mesh, CameraComponent& camera){
30+
bool MeshSystem::createSprite(SpriteComponent& sprite, MeshComponent& mesh, CameraComponent& camera){
3131
mesh.submeshes[0].primitiveType = PrimitiveType::TRIANGLES;
3232
mesh.submeshes[0].hasTextureRect = true;
3333
mesh.submeshes[0].textureShadow = true;
@@ -46,9 +46,23 @@ void MeshSystem::createSprite(SpriteComponent& sprite, MeshComponent& mesh, Came
4646

4747
Texture& mainTexture = mesh.submeshes[0].material.baseColorTexture;
4848

49-
mainTexture.load();
50-
unsigned int texWidth = mainTexture.getWidth();
51-
unsigned int texHeight = mainTexture.getHeight();
49+
unsigned int texWidth = 0;
50+
unsigned int texHeight = 0;
51+
52+
if (!mainTexture.empty()){
53+
TextureLoadResult texResult = mainTexture.load();
54+
if (texResult.state == ResourceLoadState::Finished){
55+
texWidth = mainTexture.getWidth();
56+
texHeight = mainTexture.getHeight();
57+
}else if (texResult.state == ResourceLoadState::Loading){
58+
return false;
59+
}
60+
}
61+
62+
if (texWidth == 0 || texHeight == 0){
63+
texWidth = sprite.width;
64+
texHeight = sprite.height;
65+
}
5266

5367
if (sprite.width == 0 && sprite.height == 0){
5468
sprite.width = texWidth;
@@ -139,9 +153,11 @@ void MeshSystem::createSprite(SpriteComponent& sprite, MeshComponent& mesh, Came
139153

140154
if (mesh.loaded)
141155
mesh.needUpdateBuffer = true; // buffer is not immutable
156+
157+
return true;
142158
}
143159

144-
void MeshSystem::createMeshPolygon(MeshPolygonComponent& polygon, MeshComponent& mesh){
160+
bool MeshSystem::createMeshPolygon(MeshPolygonComponent& polygon, MeshComponent& mesh){
145161
mesh.submeshes[0].primitiveType = PrimitiveType::TRIANGLE_STRIP;
146162
mesh.submeshes[0].faceCulling = false;
147163
mesh.numSubmeshes = 1;
@@ -196,9 +212,11 @@ void MeshSystem::createMeshPolygon(MeshPolygonComponent& polygon, MeshComponent&
196212

197213
if (mesh.loaded)
198214
mesh.needReload = true;
215+
216+
return true;
199217
}
200218

201-
void MeshSystem::createTilemap(TilemapComponent& tilemap, MeshComponent& mesh){
219+
bool MeshSystem::createTilemap(TilemapComponent& tilemap, MeshComponent& mesh){
202220
mesh.submeshes[0].primitiveType = PrimitiveType::TRIANGLES;
203221
mesh.submeshes[0].hasTextureRect = true;
204222

@@ -254,14 +272,24 @@ void MeshSystem::createTilemap(TilemapComponent& tilemap, MeshComponent& mesh){
254272

255273
unsigned int texWidth = 0;
256274
unsigned int texHeight = 0;
257-
if (texture.load()){
258-
tileRect = normalizeTileRect(tileRect, texture.getWidth(), texture.getHeight());
259-
texWidth = texture.getWidth();
260-
texHeight = texture.getHeight();
261-
}else if (mainTexture.load()){
262-
tileRect = normalizeTileRect(tileRect, mainTexture.getWidth(), mainTexture.getHeight());
263-
texWidth = mainTexture.getWidth();
264-
texHeight = mainTexture.getHeight();
275+
if (!texture.empty()){
276+
TextureLoadResult texResult = texture.load();
277+
if (texResult.state == ResourceLoadState::Finished){
278+
tileRect = normalizeTileRect(tileRect, texture.getWidth(), texture.getHeight());
279+
texWidth = texture.getWidth();
280+
texHeight = texture.getHeight();
281+
}else if (texResult.state == ResourceLoadState::Loading){
282+
return false;
283+
}
284+
}else if (!mainTexture.empty()){
285+
TextureLoadResult texResult = mainTexture.load();
286+
if (texResult.state == ResourceLoadState::Finished){
287+
tileRect = normalizeTileRect(tileRect, mainTexture.getWidth(), mainTexture.getHeight());
288+
texWidth = mainTexture.getWidth();
289+
texHeight = mainTexture.getHeight();
290+
}else if (texResult.state == ResourceLoadState::Loading){
291+
return false;
292+
}
265293
}
266294

267295
float texCutRatioW = 0;
@@ -331,6 +359,8 @@ void MeshSystem::createTilemap(TilemapComponent& tilemap, MeshComponent& mesh){
331359
}
332360
}
333361
tilemap.numTiles = numTiles;
362+
363+
return true;
334364
}
335365

336366
void MeshSystem::changeFlipY(bool& flipY, CameraComponent& camera, MeshComponent& mesh){
@@ -935,7 +965,7 @@ void MeshSystem::createTerrainNode(TerrainComponent& terrain, float x, float y,
935965
}
936966
}
937967

938-
void MeshSystem::createTerrain(TerrainComponent& terrain, MeshComponent& mesh){
968+
bool MeshSystem::createTerrain(TerrainComponent& terrain, MeshComponent& mesh){
939969
for (int s = 0; s < 2; s++){
940970
terrain.nodesbuffer[s].clear();
941971
terrain.nodesbuffer[s].addAttribute(AttributeType::TERRAINNODEPOSITION, 2, true);
@@ -951,23 +981,15 @@ void MeshSystem::createTerrain(TerrainComponent& terrain, MeshComponent& mesh){
951981
mesh.buffer.addAttribute(AttributeType::POSITION, 3);
952982
mesh.buffer.addAttribute(AttributeType::NORMAL, 3);
953983

954-
if (scene->getCamera() == NULL_ENTITY){
955-
Log::error("Cannot create terrain without defined camera in scene");
956-
return;
957-
}
958-
959-
if (MAX_TERRAINGRID < (terrain.rootGridSize*terrain.rootGridSize)){
960-
Log::error("Cannot create full terrain, increase MAX_TERRAINGRID to %u", (terrain.rootGridSize*terrain.rootGridSize));
961-
return;
962-
}
963-
964984
mesh.indices.clear();
965985

966986
terrain.heightMap.setReleaseDataAfterLoad(false);
967987

968-
if (!terrain.heightMap.load()){
969-
Log::error("Terrain must have a heightmap");
970-
return;
988+
if (!terrain.heightMap.empty()){
989+
TextureLoadResult texResult = terrain.heightMap.load();
990+
if (texResult.state == ResourceLoadState::Loading){
991+
return false;
992+
}
971993
}
972994

973995
size_t idealSize = getTerrainGridArraySize(terrain.rootGridSize, terrain.levels);
@@ -1021,6 +1043,7 @@ void MeshSystem::createTerrain(TerrainComponent& terrain, MeshComponent& mesh){
10211043

10221044
terrain.heightMapLoaded = true;
10231045

1046+
return true;
10241047
}
10251048

10261049
void MeshSystem::createPlane(MeshComponent& mesh, float width, float depth, unsigned int tiles){
@@ -2618,19 +2641,42 @@ bool MeshSystem::createOrUpdateSprite(SpriteComponent& sprite, MeshComponent& me
26182641
changeFlipY(sprite.flipY, camera, mesh);
26192642
}
26202643

2621-
createSprite(sprite, mesh, camera);
2622-
2623-
sprite.needUpdateSprite = false;
2644+
if (createSprite(sprite, mesh, camera)){
2645+
sprite.needUpdateSprite = false;
2646+
}else{
2647+
return false;
2648+
}
26242649
}
26252650

26262651
return true;
26272652
}
26282653

26292654
bool MeshSystem::createOrUpdateTerrain(TerrainComponent& terrain, MeshComponent& mesh){
26302655
if (terrain.needUpdateTerrain){
2631-
createTerrain(terrain, mesh);
26322656

2633-
terrain.needUpdateTerrain = false;
2657+
if (scene->getCamera() == NULL_ENTITY){
2658+
Log::error("Cannot create terrain without defined camera in scene");
2659+
terrain.needUpdateTerrain = false;
2660+
return false;
2661+
}
2662+
2663+
if (MAX_TERRAINGRID < (terrain.rootGridSize*terrain.rootGridSize)){
2664+
Log::error("Cannot create full terrain, increase MAX_TERRAINGRID to %u", (terrain.rootGridSize*terrain.rootGridSize));
2665+
terrain.needUpdateTerrain = false;
2666+
return false;
2667+
}
2668+
2669+
if (terrain.heightMap.empty()){
2670+
Log::error("Terrain must have a heightmap");
2671+
terrain.needUpdateTerrain = false;
2672+
return false;
2673+
}
2674+
2675+
if (createTerrain(terrain, mesh)){
2676+
terrain.needUpdateTerrain = false;
2677+
}else{
2678+
return false;
2679+
}
26342680
}
26352681

26362682
return true;
@@ -2643,9 +2689,11 @@ bool MeshSystem::createOrUpdateMeshPolygon(MeshPolygonComponent& polygon, MeshCo
26432689
changeFlipY(polygon.flipY, camera, mesh);
26442690
}
26452691

2646-
createMeshPolygon(polygon, mesh);
2647-
2648-
polygon.needUpdatePolygon = false;
2692+
if (createMeshPolygon(polygon, mesh)){
2693+
polygon.needUpdatePolygon = false;
2694+
}else{
2695+
return false;
2696+
}
26492697
}
26502698

26512699
return true;
@@ -2658,9 +2706,11 @@ bool MeshSystem::createOrUpdateTilemap(TilemapComponent& tilemap, MeshComponent&
26582706
changeFlipY(tilemap.flipY, camera, mesh);
26592707
}
26602708

2661-
createTilemap(tilemap, mesh);
2662-
2663-
tilemap.needUpdateTilemap = false;
2709+
if (createTilemap(tilemap, mesh)){
2710+
tilemap.needUpdateTilemap = false;
2711+
}else{
2712+
return false;
2713+
}
26642714
}
26652715

26662716
return true;

engine/core/subsystem/MeshSystem.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ namespace Supernova{
2121
class SUPERNOVA_API MeshSystem : public SubSystem {
2222

2323
private:
24-
void createSprite(SpriteComponent& sprite, MeshComponent& mesh, CameraComponent& camera);
25-
void createMeshPolygon(MeshPolygonComponent& polygon, MeshComponent& mesh);
26-
void createTilemap(TilemapComponent& tilemap, MeshComponent& mesh);
24+
bool createSprite(SpriteComponent& sprite, MeshComponent& mesh, CameraComponent& camera);
25+
bool createMeshPolygon(MeshPolygonComponent& polygon, MeshComponent& mesh);
26+
bool createTilemap(TilemapComponent& tilemap, MeshComponent& mesh);
2727

2828
void changeFlipY(bool& flipY, CameraComponent& camera, MeshComponent& mesh);
2929
Rect normalizeTileRect(Rect tileRect, unsigned int texWidth, unsigned int texHeight);
@@ -54,7 +54,7 @@ namespace Supernova{
5454
float maxTerrainHeightArea(TerrainComponent& terrain, float x, float z, float w, float h);
5555
float minTerrainHeightArea(TerrainComponent& terrain, float x, float z, float w, float h);
5656
void createPlaneNodeSubmesh(unsigned int submeshIndex, TerrainComponent& terrain, MeshComponent& mesh, int width, int height, int widthSegments, int heightSegments);
57-
void createTerrain(TerrainComponent& terrain, MeshComponent& mesh);
57+
bool createTerrain(TerrainComponent& terrain, MeshComponent& mesh);
5858
void createTerrainNode(TerrainComponent& terrain, float x, float y, float size, int lodDepth);
5959

6060
public:

0 commit comments

Comments
 (0)