Browse Source

procedual loading

sl@cccfr.de 2 years ago
parent
commit
107de8982b
5 changed files with 84 additions and 30 deletions
  1. 49 0
      ChunkSystem.gd
  2. 22 29
      Main.gd
  3. 3 1
      Main.tscn
  4. 5 0
      Player.gd
  5. 5 0
      common.gd

+ 49 - 0
ChunkSystem.gd

@@ -0,0 +1,49 @@
+extends Spatial
+
+var Chunk = preload("res://chunk.tscn")
+onready var Player = $"../Player"
+
+var resolution = 512
+var radius = 1737.4
+var renderDistance = 1
+
+var chunkIndex = {}
+
+# Called when the node enters the scene tree for the first time.
+func _ready():
+	pass # Replace with function body.
+
+
+# Called every frame. 'delta' is the elapsed time since the previous frame.
+func _process(delta):
+	var playerSpos = Player.getSpos()
+	var playerRoundSpos = playerSpos.round()
+	var playerChunkPos = Vector2(int(playerRoundSpos.x), int(playerRoundSpos.y))
+	var playerInterChunkPos = Vector2(playerSpos.x - playerChunkPos.x, playerSpos.y - playerChunkPos.y)
+	
+	for i in [Vector2(1,0),Vector2(1,1),Vector2(0,1),Vector2(-1,0),Vector2(0,-1),Vector2(-1,-1),Vector2(1,-1),Vector2(-1,1)]:
+		var dist = Vector2(playerSpos.x, playerSpos.y).distance_to(playerChunkPos+i+Vector2(0.5,0.5))
+		if dist < renderDistance:
+			add_chunk(playerChunkPos+i)
+
+
+func add_chunk(chunkPos = Vector2()):
+	var lat = int(chunkPos.x)
+	var lon = int(chunkPos.y)
+	if chunkIndex.has(lon) and chunkIndex[lon].has(lat):
+		return
+	var chunk = Chunk.instance()
+	chunk.setParams(lat,lat+1,lon,lon+1,resolution,radius)
+	#moontile.scale=Vector3(1000,1000,1000)
+	.add_child(chunk)
+	if not chunkIndex.has(lon):
+		chunkIndex[lon]={}
+	chunkIndex[lon][lat] = chunk
+
+func remove_chunk(chunkPos = Vector2()):
+	var lat = int(chunkPos.x)
+	var lon = int(chunkPos.y)
+	if chunkIndex.has(lon) and chunkIndex[lon].has(lat):
+		chunkIndex[lon][lat].queue_free()
+		chunkIndex[lon].delete(lat)
+

+ 22 - 29
Main.gd

@@ -5,27 +5,24 @@ extends Spatial
 # var a = 2
 # var b = "text"
 #var tileGenScene = preload("res://TileGenerator.tscn")
-var Chunk = preload("res://chunk.tscn")
+#var Chunk = preload("res://chunk.tscn")
 
-var minlat = 20
-var maxlat = 26
-var minlon = 308
-var maxlon = 313
-var steplat = 1
-var steplon = 1
+#var minlat = 20
+#var maxlat = 26
+#var minlon = 308
+#var maxlon = 313
+#var steplat = 1
+#var steplon = 1
 
-var resolution = 512
+#var resolution = 512
 var radius = 1737.4#*1000
-
 var startPos = Vector3(20,308,radius)
 
-var curlat = 0
-var curlon = 0
+#var curlat = 0
+#var curlon = 0
 
 func _ready():
 	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
-	curlat = minlat*steplat
-	curlon = minlon*steplon
 	get_node("Player").moveTo(startPos)
 	
 func _input(event):
@@ -37,22 +34,18 @@ func _input(event):
 			get_tree().set_input_as_handled()
 
 func _process(delta):
-	if curlat < maxlat*steplat:
-		if curlon < maxlon*steplon:
-			call_deferred("add_chunk", curlon, curlat)
-			curlon += steplon
-		else:
-			curlat +=steplat
-			curlon = 0
-			if curlat >= maxlat:
-				print("loading complete")
-
-func add_chunk(curlon, curlat):
-	var chunk = Chunk.instance()
-	chunk.setParams(curlat,curlat+steplat,curlon,curlon+steplon,resolution,radius)
-	#moontile.scale=Vector3(1000,1000,1000)
-	$Chunks.add_child(chunk)
-	.get_node("UI/3").text="lastLatLon="+str(curlat)+":"+str(curlon)
+	pass
+	#if curlat < maxlat*steplat:
+	#	if curlon < maxlon*steplon:
+	#		.get_node("Chunks").call_deferred("add_chunk", Vector2(curlat,curlon))
+	#		curlon += steplon
+	#	else:
+	#		curlat +=steplat
+	#		curlon = 0
+	#		if curlat >= maxlat:
+	#			print("loading complete")
+
+
 
 # Called every frame. 'delta' is the elapsed time since the previous frame.
 #func _process(delta):

+ 3 - 1
Main.tscn

@@ -1,7 +1,8 @@
-[gd_scene load_steps=9 format=2]
+[gd_scene load_steps=10 format=2]
 
 [ext_resource path="res://Main.gd" type="Script" id=1]
 [ext_resource path="res://Player.gd" type="Script" id=2]
+[ext_resource path="res://ChunkSystem.gd" type="Script" id=3]
 
 [sub_resource type="ProceduralSky" id=4]
 sun_latitude = 135.0
@@ -58,6 +59,7 @@ transform = Transform( 1, 0, 0, 0, 0.573905, -0.818922, 0, 0.818922, 0.573905, -
 light_energy = 7.3
 
 [node name="Chunks" type="Spatial" parent="."]
+script = ExtResource( 3 )
 __meta__ = {
 "_edit_lock_": true
 }

+ 5 - 0
Player.gd

@@ -46,6 +46,11 @@ func moveTo(pos = Vector3()):
 		pos.z = 1737.4
 	move_and_slide(Common.latLonToGlobal(Vector3(pos.x,pos.y,pos.z)), Vector3.UP)
 
+func getSpos():
+	var sPos = Common.globalToLatLon(self.global_transform.origin)
+	sPos.y = Common.relativeToAbsolute(sPos.y)
+	return sPos
+
 func _process(delta):
 	var pos = self.global_transform.origin
 	.get_parent().get_node("UI/1").text="x:%s y:%s z:%s" %[pos.x, pos.y, pos.z]

+ 5 - 0
common.gd

@@ -25,6 +25,11 @@ func globalToLatLon(pos: Vector3, offset=Vector3()):
 	#print("x:"+str(x)+" y:"+str(y)+" z:"+str(z)))
 	#print("lat:"+str(lat)+" lon:"+str(lon))
 	return Vector3(lat, lon, height)
+	
+func relativeToAbsolute(rel = float()):
+	var absolute = float()
+	absolute = rel+180-(sign(rel)*180)
+	return absolute
 
 #func globalToRad(pos: Vector3):
 #	var floor_normal = pos - Vector3()