123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- extends MeshInstance
- var latMin = 0
- var latMax = 1
- var lonMin = 0
- var lonMax = 1
- var resolution = 512
- var radius = 1737.4
- var rng = RandomNumberGenerator.new()
- var datafile = "user://sldem2015_512_00n_30n_000_045_float.img"
- var samplesPerLine = 23040
- var lines = 15360
- var offset = 1737.4
- var scaleFact = 1
- func init(startLat, startLon):
-
- self.latMin = startLat
- self.latMax = startLat + 1
- self.lonMin = startLon
- self.lonMax = startLon + 1
-
- # Called when the node enters the scene tree for the first time.
- func _ready():
-
- if ResourceLoader.exists("user://tile_"+str(latMin)+"-"+str(lonMin)+".tres"):
- print("loading tile from file: ", "user://tile_"+str(latMin)+"-"+str(lonMin)+".tres")
- mesh = ResourceLoader.load("user://tile_"+str(latMin)+"-"+str(lonMin)+".tres")
- else:
- self.genMesh()
-
- var texture = ImageTexture.new()
- var image = Image.new()
- #image.load("res://m-"+str(latMin+90)+"-"+str(lonMin+180)+".png")
- image.load("user://colorTiles/m-" + str(latMin+90)+"-"+str(lonMin+180)+".png")
- texture.create_from_image(image)
- var material=SpatialMaterial.new()
- material.albedo_texture = texture
- .set_material_override(material)
- #print("creating collition...")
- #.create_trimesh_collision()
-
- func genMesh():
- var file = File.new()
- file.open(datafile, File.READ)
- #for lala in range(10):
- # file.seek(lala*samplesPerLine*4)
- # var blubb = ""
- # for bla in range(10):
- # blubb += " " + str(file.get_float())
- # print(blubb)
- rng.randomize()
- var arr = []
- arr.resize(Mesh.ARRAY_MAX)
- #OS.delay_msec(2000)
- # PoolVectorXXArrays for mesh construction.
- var verts = PoolVector3Array()
- verts.resize((lonMax-lonMin) *resolution*(lonMax-lonMin) *resolution)
- var uvs = PoolVector2Array()
- uvs.resize((lonMax-lonMin) *resolution*(lonMax-lonMin) *resolution)
- var normals = PoolVector3Array()
- normals.resize((lonMax-lonMin) *resolution*resolution * (lonMax-lonMin))
- var indices = PoolIntArray()
- indices.resize(6* (lonMax-lonMin)*resolution*(lonMax-lonMin) * resolution*6)
- var i_idx = 0
-
- # Vertex indices.
- var thisrow = 0
- var prevrow = 0
- var point = 0
- print(
- "calculating segment "+str(latMin)+"-"+str(lonMin)+":\n",
- "lat:\n",
- " min: ", latMin*resolution, "\n",
- " max:", latMax*resolution, "\n",
- " count: ", (latMax-latMin) * resolution, "\n",
- "lon:\n",
- " min: ", lonMin*resolution, "\n",
- " max:", lonMax*resolution, "\n",
- " count: ", (lonMax-lonMin) * resolution, "\n"
- )
-
- #OS.delay_msec(2000)
- var vert = Vector3()
- # Loop over rings.
- for latPos in range(float(latMin)*resolution, float(latMax)*resolution):
- var latCur = float(latPos)/float(resolution)
- #jump to last line in file - current local lat - local startLat + local startLon
- file.seek(lines*samplesPerLine*4-latPos*samplesPerLine*4 - latMin*samplesPerLine*4 + lonMin*resolution*4)
- #file.seek(latPos*samplesPerLine*4 + latMin*samplesPerLine*4 + lonMin*resolution*4)
- #file.seek(latPos*samplesPerLine*4 + lonMin*resolution*4)
- #file.seek(((latMax*resolution-1-latPos)*samplesPerLine + lonMin*resolution)*4)
- #var v = float(latPos-latMin*resolution) / ((latMax-latMin) * resolution)
- var v = float(latPos-latMin*resolution) / ((latMax-latMin) * resolution)
- #var lat = float(latPos)*PI/(resolution*180)
- #var w = cos(lat)
- #var y = sin(lat)
- #print(v*100, "%")
- # Loop over segments in ring.
- for lonPos in range(float(lonMin)*resolution, float(lonMax)*resolution):
- var lonCur = float(lonPos)/float(resolution)
- var u = float(lonPos-lonMin*resolution) / ((lonMax - lonMin) * resolution)
- #var lon = float(lonPos)*PI/(resolution*180)
- #var x = sin(lon)
- #var z = cos(lon)
- #print("Lon: ", lon)
- #var height = file.get_float()/offset*radius+radius
- var height = file.get_float()+radius
- vert = Common.latLonToGlobal(latCur, lonCur, height)
- #vert = Vector3(scaleFact * x * height * w, scaleFact * y*height, scaleFact * z * height * w)
- verts[point] = vert
- normals[point] = vert.normalized()
- uvs[point] = Vector2(u, 1-v)
- point += 1
- # Create triangles in ring using indices.
- if latPos > lonMin*resolution and lonPos > lonMin*resolution:
- indices[i_idx] = (prevrow + (lonPos-lonMin*resolution) - 1)
- i_idx+=1
- indices[i_idx] = (thisrow + (lonPos-lonMin*resolution) - 1)
- i_idx+=1
- indices[i_idx] = (prevrow + (lonPos-lonMin*resolution))
- i_idx+=1
- indices[i_idx] = (prevrow + (lonPos-lonMin*resolution))
- i_idx+=1
- indices[i_idx] = (thisrow + (lonPos-lonMin*resolution) - 1)
- i_idx+=1
- indices[i_idx] = (thisrow + (lonPos-lonMin*resolution))
- i_idx+=1
-
- #if latPos > latMin*resolution:
- # indices[i_idx] = (prevrow + ((latMax - latMin) * resolution) - 1)
- # i_idx+=1
- # indices[i_idx] = (prevrow)
- # i_idx+=1
- # indices[i_idx] = (thisrow + ((latMax - latMin) * resolution) - 1)
- # i_idx+=1
- # indices[i_idx] = (prevrow)
- # i_idx+=1
- # indices[i_idx] = (prevrow + ((latMax - latMin) * resolution))
- # i_idx+=1
- # indices[i_idx] = (thisrow + ((latMax - latMin) * resolution) - 1)
- # i_idx+=1
- #print(vert)
- prevrow = thisrow
- thisrow = point
- file.close()
- print(vert)
- # Assign arrays to mesh array.
- arr[Mesh.ARRAY_VERTEX] = verts
- arr[Mesh.ARRAY_TEX_UV] = uvs
- arr[Mesh.ARRAY_NORMAL] = normals
- arr[Mesh.ARRAY_INDEX] = indices
- mesh = Mesh.new()
- # Create mesh surface from mesh array.
- mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arr)
- print(mesh)
- ResourceSaver.save("user://tile_"+str(latMin)+"-"+str(lonMin)+".tres", mesh, 32)
- print("saved tile_"+str(latMin)+"-"+str(lonMin))
-
- # Called every frame. 'delta' is the elapsed time since the previous frame.
- #func _process(delta):
- # pass
|