Parcourir la source

add texture download

Malf il y a 2 ans
Parent
commit
ac58d2a1ef
5 fichiers modifiés avec 37 ajouts et 8 suppressions
  1. 21 3
      TextureLoader.gd
  2. 7 2
      Tile.gd
  3. 1 1
      TopoLoader.gd
  4. 6 0
      chunk.gd
  5. 2 2
      downloader.gd

+ 21 - 3
TextureLoader.gd

@@ -3,13 +3,31 @@ extends Reference
 
 var filePrexif = "user://colorTiles"
 
+var lat = 0
+var lon = 0
 
 # Called when the node enters the scene tree for the first time.
 func _ready():
 	pass # Replace with function body.
 
-func loadTexture(lat, lon):
-	var texName = getTexName(lat, lon)
+# this function essentially checks if the file exists and starts a download if not
+func open(latIN, lonIN):
+	lat = latIN
+	lon = lonIN
+	var file = File.new()
+	var err = file.open(getTexName(), File.READ)
+	if err == 7:
+		Downloader.download(getTexName(), "color")
+		return false
+	elif err != OK:
+		print("error opening tex ", err)
+		return false
+	else:
+		file.close()
+		return true
+
+func loadTexture():
+	var texName = getTexName()
 	print("opening texture "+texName)
 	var image = Image.new()
 	var err = image.load(texName)
@@ -19,7 +37,7 @@ func loadTexture(lat, lon):
 	tex.create_from_image(image, 0)
 	return tex
 	
-func getTexName(lat, lon):
+func getTexName():
 	return "%s/m-%s-%s.png" % [filePrexif, (int(lat)+90)%180, (int(lon)+180)%360]
 	#return "res://tex/grid.png"
 

+ 7 - 2
Tile.gd

@@ -4,6 +4,7 @@ var Point = preload("res://point.tscn")
 var TopoLoader = preload("res://TopoLoader.gd")
 var tl
 var TextureLoader = preload("res://TextureLoader.gd")
+var texl
 
 var groundShader = preload("res://shader/ground_shader.tres") as ShaderMaterial
 
@@ -42,6 +43,11 @@ func _ready():
 	#genMesh called from chunk
 	#self.genMesh()
 	
+func initTexture():
+	texl = TextureLoader.new()
+	var exists = texl.open(latMin, lonMin)
+	return exists
+	
 func genMesh():
 	#print("generating Mesh:")
 	#print("latMin: " + str(latMin))
@@ -61,8 +67,7 @@ func genMesh():
 	mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arr)
 	var mat = groundShader.duplicate()
 	#mat.albedo_color = Color(0.7,0.7,0.7,1)
-	var texl = TextureLoader.new()
-	mat.set_shader_param("texture_albedo_big", texl.loadTexture(latMin, lonMin))
+	mat.set_shader_param("texture_albedo_big", texl.loadTexture())
 	mesh.surface_set_material(0,mat)
 	.create_trimesh_collision()
 	

+ 1 - 1
TopoLoader.gd

@@ -73,7 +73,7 @@ func cacheArea(latMinIN, latMaxIN, lonMinIN, lonMaxIN):
 		return
 	
 	if not file.is_open():
-		file.open(getFilepath(), File.READ)
+		var err = file.open(getFilepath(), File.READ)
 		
 	var lat = latMinIN
 	while lat <= latMaxIN:

+ 6 - 0
chunk.gd

@@ -39,6 +39,12 @@ func initialize():
 		while not download._file_name == .get_node("Mesh").tl.getFilepath():
 			print("awaiting file download for %s" %.get_node("Mesh").tl.getFilepath())
 			download = yield(Messenger, "fileDownloaded")
+	var texInitialized = .get_node("Mesh").initTexture()
+	if not texInitialized:
+		var dl = Downloader.Download.new()
+		while not dl._file_name == .get_node("Mesh").texl.getTexName():
+			print("awaiting file download for %s" %.get_node("Mesh").texl.getTexName())
+			dl = yield(Messenger, "fileDownloaded")
 	.get_node("Mesh").genMesh()
 	.get_node("Origin").transform.origin = .get_node("Mesh").origin
 	.get_node("Center").transform.origin = .get_node("Mesh").center

+ 2 - 2
downloader.gd

@@ -2,7 +2,7 @@ extends HTTPRequest
 
 var kinds = {
 	"topo": "http://imbrium.mit.edu/DATA/SLDEM2015/TILES/FLOAT_IMG/%s",
-	"color": "" 
+	"color": "https://nextcloud.alfi.li/nextcloud/index.php/s/dMELm8P8sPxbC5f/download?path=%2F&files=%s" 
 }
 
 var downloads = {}
@@ -161,7 +161,7 @@ func _calculate_percentage() -> void:
 	var error : int
 	error = _file.open(download_file, File.READ)
 
-	if error == OK:
+	if error == OK and currentDownload._file_size > 0:
 		currentDownload._downloaded_size    = _file.get_len()
 		currentDownload._downloaded_percent = (currentDownload._downloaded_size / currentDownload._file_size) *100