sl@cccfr.de hace 2 años
padre
commit
1e11b5469f
Se han modificado 10 ficheros con 244 adiciones y 24 borrados
  1. 24 15
      Main.gd
  2. 20 2
      Main.tscn
  3. 4 4
      Player.gd
  4. 27 0
      chunk.gd
  5. 10 0
      chunk.tscn
  6. 2 3
      common.gd
  7. 136 0
      flat_tile.gd
  8. 4 0
      point.tres
  9. 12 0
      point.tscn
  10. 5 0
      project.godot

+ 24 - 15
Main.gd

@@ -4,32 +4,41 @@ extends Spatial
 # Declare member variables here. Examples:
 # var a = 2
 # var b = "text"
-var tileGenScene = preload("res://TileGenerator.tscn")
+#var tileGenScene = preload("res://TileGenerator.tscn")
+var Chunk = preload("res://chunk.tscn")
 
-
-var minlat = 0
-var maxlat = 6
+var minlat = -3
+var maxlat = 3
 var minlon = 0
-var maxlon = 3
+var maxlon = 6
 
 var curlat = 0
 var curlon = 0
 
 func _ready():
-	curlat = minlat
-	curlon = minlon
+	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
+	curlat = minlat*30
+	curlon = minlon*60
+	
+func _input(event):
+	if event.is_action_pressed("ui_cancel"):
+		Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
+	if event.is_action_pressed("click"):
+		if Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE:
+			Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
+			get_tree().set_input_as_handled()
 
 func _process(delta):
-	if curlat < maxlat:
-		if curlon < maxlon:
-			var moontile = tileGenScene.instance()
-			moontile.init(curlat,curlon)
+	if curlat < maxlat*30:
+		if curlon < maxlon*60:
+			var chunk = Chunk.instance()
+			chunk.setParams(curlat,curlat+30,curlon,curlon+60,4)
 			#moontile.scale=Vector3(1000,1000,1000)
-			$Tiles.add_child(moontile)
-			.get_node("UI/3").text="LatLon=="+str(Common.latLonToGlobal(curlat,curlon))
-			curlon += 1
+			$Tiles.add_child(chunk)
+			.get_node("UI/3").text="lastLatLon="+str(curlat)+":"+str(curlon)
+			curlon += 60
 		else:
-			curlat +=1
+			curlat +=30
 			curlon = 0
 			if curlat >= maxlat:
 				print("loading complete")

+ 20 - 2
Main.tscn

@@ -1,4 +1,4 @@
-[gd_scene load_steps=6 format=2]
+[gd_scene load_steps=8 format=2]
 
 [ext_resource path="res://Main.gd" type="Script" id=1]
 [ext_resource path="res://Player.gd" type="Script" id=2]
@@ -13,8 +13,19 @@ background_sky = SubResource( 4 )
 
 [sub_resource type="SphereShape" id=3]
 
+[sub_resource type="SpatialMaterial" id=6]
+albedo_color = Color( 0.168627, 0.164706, 0.913725, 1 )
+
+[sub_resource type="SphereMesh" id=5]
+material = SubResource( 6 )
+radius = 7.0
+height = 14.0
+
 [node name="Spatial" type="Spatial"]
 script = ExtResource( 1 )
+__meta__ = {
+"_edit_lock_": true
+}
 
 [node name="WorldEnvironment" type="WorldEnvironment" parent="."]
 environment = SubResource( 2 )
@@ -36,7 +47,9 @@ transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.319878, -0.629829 )
 light_energy = 11.83
 
 [node name="Tiles" type="Spatial" parent="."]
-transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -96.8491, 0, 3.33984 )
+__meta__ = {
+"_edit_lock_": true
+}
 
 [node name="UI" type="Control" parent="."]
 margin_right = 40.0
@@ -83,3 +96,8 @@ text = "4"
 __meta__ = {
 "_edit_use_anchors_": false
 }
+
+[node name="reference" type="MeshInstance" parent="."]
+visible = false
+mesh = SubResource( 5 )
+material/0 = null

+ 4 - 4
Player.gd

@@ -18,7 +18,7 @@ var mouseDelta : Vector2 = Vector2()
 onready var camera : Camera = get_node("Camera")
 
 func _ready():
-	move_and_slide(Common.latLonToGlobal(0,0,1800.4), Vector3.UP)
+	move_and_slide(Common.latLonToGlobal(0,0,11), Vector3.UP)
 	ray = RayCast.new()
 	.get_parent().add_child(ray)
 
@@ -34,10 +34,11 @@ func _process(delta):
 	
 	# reset the mouseDelta vector
 	mouseDelta = Vector2()
+	var origin = get_global_transform().origin
+	.get_parent().get_node("UI/2").text="latlonhight: "+str(Common.globalToLatLon(origin.x,origin.y,origin.z, origin.length()))+","+str(origin.length())
  
 func _input(event):
-	
-	if event is InputEventMouseMotion:
+	if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
 		mouseDelta = event.relative
 
 func _physics_process(delta):
@@ -96,6 +97,5 @@ func _physics_process(delta):
 		#rotate_x(-1)
 	
 	.get_parent().get_node("UI/1").text="x:%s y:%s z:%s" %[pos.x, pos.y, pos.z]
-	.get_parent().get_node("UI/2").text="lat:%s lon:%s" %[polPos.x, polPos.y]
 	#.get_node("UI/3").text="LatLonPos=="+str(latLonToGlobal( self.global_transform.origin.x,self.global_transform.origin.y))
 	

+ 27 - 0
chunk.gd

@@ -0,0 +1,27 @@
+extends Spatial
+
+
+# Declare member variables here. Examples:
+# var a = 2
+# var b = "text"
+var latMin:float = 0
+var latMax:float = 1
+var lonMin:float = 0.0
+var lonMax:float = 1.0
+var resolution:int = 512
+
+func setParams(latMinIN, latMaxIN, lonMinIN, lonMaxIN, resolutionIN):
+	latMin = latMinIN
+	latMax = latMaxIN
+	lonMin = lonMinIN
+	lonMax = lonMaxIN
+	resolution = resolutionIN
+	
+# Called when the node enters the scene tree for the first time.
+func _ready():
+	.get_node("Mesh").setParams(latMin, latMax, lonMin, lonMax, resolution)
+	.get_node("Mesh").genMesh()
+
+# Called every frame. 'delta' is the elapsed time since the previous frame.
+#func _process(delta):
+#	pass

+ 10 - 0
chunk.tscn

@@ -0,0 +1,10 @@
+[gd_scene load_steps=3 format=2]
+
+[ext_resource path="res://chunk.gd" type="Script" id=1]
+[ext_resource path="res://flat_tile.gd" type="Script" id=2]
+
+[node name="Chunk" type="Spatial"]
+script = ExtResource( 1 )
+
+[node name="Mesh" type="MeshInstance" parent="."]
+script = ExtResource( 2 )

+ 2 - 3
common.gd

@@ -1,16 +1,15 @@
 extends Node
 
 
-func latLonToGlobal(latPos, lonPos, height=1737.4, offset=Vector3()):
+func latLonToGlobal(latPos, lonPos, height=1737.4, offset:=Vector3(), origin:=Vector3()):
 	height = height + offset.z
 	var lat = float(latPos+offset.x)*PI/180
 	var w = cos(lat)
 	var y = sin(lat)
-	var u = float(lonPos) / 360
 	var lon = float(lonPos+offset.y)*PI/180
 	var x = sin(lon)
 	var z = cos(lon)
-	return Vector3(x * height * w, y*height, z * height * w)
+	return Vector3(x * height * w, y*height, z * height * w)-origin
 			
 func globalToLatLon(x, y, z, height=1737.4, offset=Vector3()):
 	height = height + offset.z

+ 136 - 0
flat_tile.gd

@@ -0,0 +1,136 @@
+extends MeshInstance
+
+var Point = preload("res://point.tscn")
+
+var latMin:float = 0
+var latMax:float = 1
+var lonMin:float = 0.0
+var lonMax:float = 1.0
+var resolution:int = 512
+var radius:float = 10
+
+func setParams(latMinIN, latMaxIN, lonMinIN, lonMaxIN, resolutionIN):	
+	latMin = latMinIN
+	latMax = latMaxIN
+	lonMin = lonMinIN
+	lonMax = lonMaxIN
+	resolution = resolutionIN
+
+func _ready():
+	pass
+	#genMesh called from chunk
+	#self.genMesh()
+	
+func genMesh():
+	print("generating Mesh:")
+	print("latMin: " + str(latMin))
+	print("latMax: " + str(latMax))
+	print("lonMin: " + str(lonMin))
+	print("lonMax: " + str(lonMax))
+	print("resolution: " + str(resolution))
+	
+	var verts = convertToCartesian(genSphereCoords())
+	var arr = []
+	arr.resize(Mesh.ARRAY_MAX)
+	arr[Mesh.ARRAY_VERTEX] = verts
+	arr[Mesh.ARRAY_TEX_UV] = genUV()
+	arr[Mesh.ARRAY_NORMAL] = genNormals(verts)
+	arr[Mesh.ARRAY_INDEX] = genIndex()
+	mesh = Mesh.new()
+	mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arr)
+	
+	var i = 0.0
+	for vert in verts:
+		var point = Point.instance()
+		point.translate(vert)
+		var mat = SpatialMaterial.new()#point.mesh.surface_get_material(0)
+		mat.albedo_color = Color(i,0,0,1)
+		i+=0.1
+		point.mesh.surface_set_material(0,mat)
+		.get_parent().add_child(point)
+
+func genNormals(verts:PoolVector3Array):
+	var normals = PoolVector3Array()
+	normals.resize((resolution+1) * (resolution+1))
+	var v_idx = 0
+	for vert in verts:
+		normals[v_idx] = vert.normalized()
+		v_idx+=1
+	print("normals")
+	print(len(normals))
+	return normals
+
+func genUV():
+	var uvs = PoolVector2Array()
+	uvs.resize((resolution+1) * (resolution+1))
+	
+	for v_idx in range(0,(resolution+1) * (resolution+1)):
+		var y = v_idx / resolution
+		var x = v_idx % resolution
+		uvs[v_idx]=Vector2(clamp(x, 0, resolution),clamp(y, 0, resolution))
+	print("uv")
+	print(len(uvs))
+	return uvs
+	
+
+func genIndex():
+	var indices = PoolIntArray()
+	indices.resize(6* (resolution+1) * (resolution+1)*6)
+	
+	var i_idx = 0
+	var row = 0
+	while row < resolution:
+		var col = 0
+		while col < resolution:
+			var v_idx = row*(resolution+1)+col
+			
+			indices[i_idx] = v_idx
+			i_idx+=1
+			indices[i_idx] = v_idx + resolution +1
+			i_idx+=1
+			indices[i_idx] = v_idx + 1
+			i_idx+=1
+
+			indices[i_idx] = v_idx + resolution+1
+			i_idx+=1
+			indices[i_idx] = v_idx + resolution + 2
+			i_idx+=1
+			indices[i_idx] = v_idx + 1
+			i_idx+=1
+			
+			col += 1
+		row += 1
+		
+	print("indices")
+	print(len(indices))
+	return indices
+		
+func genSphereCoords():
+	var verts = PoolVector3Array()
+	var size = (resolution+1) * (resolution+1)#(lonMax-lonMin)*resolution * (latMax-latMin)*resolution
+	verts.resize(size)
+	
+	var v_idx = 0
+	var lat := float(latMin)
+	while lat <= float(latMax):
+		var lon := float(lonMin)
+		while lon <= float(lonMax):
+			verts[v_idx] = Vector3(lat, lon, radius)
+			v_idx+=1
+			lon += (lonMax-lonMin)/resolution
+		lat += (latMax-latMin)/resolution
+	print("verts")
+	print(len(verts))
+	return verts
+
+
+func convertToCartesian(verts:PoolVector3Array):
+	var carVerts = PoolVector3Array()
+	carVerts.resize(len(verts))
+	var v_idx = 0
+	for vert in verts:
+		carVerts[v_idx] = Common.latLonToGlobal(vert.x, vert.y, vert.z, Vector3(), Vector3())
+		v_idx+=1
+	print("Cartesian")
+	print(len(carVerts))
+	return carVerts

+ 4 - 0
point.tres

@@ -0,0 +1,4 @@
+[gd_resource type="SpatialMaterial" format=2]
+
+[resource]
+albedo_color = Color( 0.188235, 0.27451, 0.85098, 1 )

+ 12 - 0
point.tscn

@@ -0,0 +1,12 @@
+[gd_scene load_steps=3 format=2]
+
+[ext_resource path="res://point.tres" type="Material" id=1]
+
+[sub_resource type="SphereMesh" id=1]
+material = ExtResource( 1 )
+radius = 0.1
+height = 0.2
+
+[node name="point" type="MeshInstance"]
+mesh = SubResource( 1 )
+material/0 = null

+ 5 - 0
project.godot

@@ -55,6 +55,11 @@ sprint={
 "events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777237,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
  ]
 }
+click={
+"deadzone": 0.5,
+"events": [ Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"button_mask":0,"position":Vector2( 0, 0 ),"global_position":Vector2( 0, 0 ),"factor":1.0,"button_index":1,"pressed":false,"doubleclick":false,"script":null)
+ ]
+}
 
 [physics]