Explorar o código

Terrain: Fix engine context errors

trethaller %!s(int64=6) %!d(string=hai) anos
pai
achega
1c479fe98a
Modificáronse 2 ficheiros con 35 adicións e 32 borrados
  1. 5 0
      hide/comp/Scene.hx
  2. 30 32
      hide/prefab/terrain/Terrain.hx

+ 5 - 0
hide/comp/Scene.hx

@@ -212,6 +212,11 @@ class Scene extends Component implements h3d.IDrawable {
 			engine.driver.uploadTextureBitmap(t, cast bmp, 0, 0);
 			t.realloc = onLoaded;
 			t.flags.unset(Loading);
+			@:privateAccess if( t.waitLoads != null ) {
+				var arr = t.waitLoads;
+				t.waitLoads = null;
+				for( f in arr ) f();
+			}
 			if( onReady != null ) {
 				onReady(t);
 				onReady = null;

+ 30 - 32
hide/prefab/terrain/Terrain.hx

@@ -183,47 +183,45 @@ class Terrain extends Object3D {
 		ctx.local3d = terrain;
 		ctx.local3d.name = name;
 
+		function waitAll() {
+			for(surface in terrain.surfaces)
+				if(surface == null || surface.albedo == null || surface.normal == null || surface.pbr == null )
+					return;
+			
+			terrain.generateSurfaceArray();
+			loadTerrain(ctx);
+			for(tile in terrain.tiles)
+				tile.blendEdges();
+		}
+
 		for(surfaceProps in tmpSurfacesProps){
 			var surface = terrain.addEmptySurface();
 			var albedo = ctx.shared.loadTexture(surfaceProps.albedo);
 			var normal = ctx.shared.loadTexture(surfaceProps.normal);
 			var pbr = ctx.shared.loadTexture(surfaceProps.pbr);
 			function wait() {
-				if( albedo.flags.has(Loading) || normal.flags.has(Loading)|| pbr.flags.has(Loading))
-					haxe.Timer.delay(wait, 1);
-				else{
-					surface.albedo = albedo;
-					surface.normal = normal;
-					surface.pbr = pbr;
-					surface.offset.x = surfaceProps.offsetX;
-					surface.offset.y = surfaceProps.offsetY;
-					surface.angle = surfaceProps.angle;
-					surface.tilling = surfaceProps.tilling;
-					albedo.dispose();
-					normal.dispose();
-					pbr.dispose();
-				}
-			}
-			wait();
-		}
+				if(albedo.isDisposed())
+					return;
+				if( albedo.flags.has(Loading) || normal.flags.has(Loading) || pbr.flags.has(Loading))
+					return;
+				surface.albedo = albedo;
+				surface.normal = normal;
+				surface.pbr = pbr;
+				surface.offset.x = surfaceProps.offsetX;
+				surface.offset.y = surfaceProps.offsetY;
+				surface.angle = surfaceProps.angle;
+				surface.tilling = surfaceProps.tilling;
+				albedo.dispose();
+				normal.dispose();
+				pbr.dispose();
 
-		function waitAll() {
-			var ready = true;
-			for(surface in terrain.surfaces)
-				if(surface == null || surface.albedo == null || surface.normal == null || surface.pbr == null ){
-					ready = false;
-					break;
-				}
-			if(ready){
-				terrain.generateSurfaceArray();
-				loadTerrain(ctx);
-				for(tile in terrain.tiles)
-					tile.blendEdges();
+				waitAll();
 			}
-			else
-				haxe.Timer.delay(waitAll, 1);
+			albedo.waitLoad(wait);
+			normal.waitLoad(wait);
+			pbr.waitLoad(wait);
 		}
-		waitAll();
+
 		updateInstance(ctx);
 		return ctx;
 	}