Tile.gd 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. extends MeshInstance
  2. var latMin = 0
  3. var latMax = 1
  4. var lonMin = 0
  5. var lonMax = 1
  6. var resolution = 512
  7. var radius = 1737.4
  8. var rng = RandomNumberGenerator.new()
  9. var datafile = "user://sldem2015_512_00n_30n_000_045_float.img"
  10. var samplesPerLine = 23040
  11. var lines = 15360
  12. var offset = 1737.4
  13. var scaleFact = 1
  14. func init(startLat, startLon):
  15. self.latMin = startLat
  16. self.latMax = startLat + 1
  17. self.lonMin = startLon
  18. self.lonMax = startLon + 1
  19. # Called when the node enters the scene tree for the first time.
  20. func _ready():
  21. if ResourceLoader.exists("user://tile_"+str(latMin)+"-"+str(lonMin)+".tres"):
  22. print("loading tile from file: ", "user://tile_"+str(latMin)+"-"+str(lonMin)+".tres")
  23. mesh = ResourceLoader.load("user://tile_"+str(latMin)+"-"+str(lonMin)+".tres")
  24. else:
  25. self.genMesh()
  26. var texture = ImageTexture.new()
  27. var image = Image.new()
  28. #image.load("res://m-"+str(latMin+90)+"-"+str(lonMin+180)+".png")
  29. image.load("user://colorTiles/m-" + str(latMin+90)+"-"+str(lonMin+180)+".png")
  30. texture.create_from_image(image)
  31. var material=SpatialMaterial.new()
  32. material.albedo_texture = texture
  33. .set_material_override(material)
  34. #print("creating collition...")
  35. #.create_trimesh_collision()
  36. func genMesh():
  37. var file = File.new()
  38. file.open(datafile, File.READ)
  39. #for lala in range(10):
  40. # file.seek(lala*samplesPerLine*4)
  41. # var blubb = ""
  42. # for bla in range(10):
  43. # blubb += " " + str(file.get_float())
  44. # print(blubb)
  45. rng.randomize()
  46. var arr = []
  47. arr.resize(Mesh.ARRAY_MAX)
  48. #OS.delay_msec(2000)
  49. # PoolVectorXXArrays for mesh construction.
  50. var verts = PoolVector3Array()
  51. verts.resize((lonMax-lonMin) *resolution*(lonMax-lonMin) *resolution)
  52. var uvs = PoolVector2Array()
  53. uvs.resize((lonMax-lonMin) *resolution*(lonMax-lonMin) *resolution)
  54. var normals = PoolVector3Array()
  55. normals.resize((lonMax-lonMin) *resolution*resolution * (lonMax-lonMin))
  56. var indices = PoolIntArray()
  57. indices.resize(6* (lonMax-lonMin)*resolution*(lonMax-lonMin) * resolution*6)
  58. var i_idx = 0
  59. # Vertex indices.
  60. var thisrow = 0
  61. var prevrow = 0
  62. var point = 0
  63. print(
  64. "calculating segment "+str(latMin)+"-"+str(lonMin)+":\n",
  65. "lat:\n",
  66. " min: ", latMin*resolution, "\n",
  67. " max:", latMax*resolution, "\n",
  68. " count: ", (latMax-latMin) * resolution, "\n",
  69. "lon:\n",
  70. " min: ", lonMin*resolution, "\n",
  71. " max:", lonMax*resolution, "\n",
  72. " count: ", (lonMax-lonMin) * resolution, "\n"
  73. )
  74. #OS.delay_msec(2000)
  75. var vert = Vector3()
  76. # Loop over rings.
  77. for latPos in range(float(latMin)*resolution, float(latMax)*resolution):
  78. var latCur = float(latPos)/float(resolution)
  79. #jump to last line in file - current local lat - local startLat + local startLon
  80. file.seek(lines*samplesPerLine*4-latPos*samplesPerLine*4 - latMin*samplesPerLine*4 + lonMin*resolution*4)
  81. #file.seek(latPos*samplesPerLine*4 + latMin*samplesPerLine*4 + lonMin*resolution*4)
  82. #file.seek(latPos*samplesPerLine*4 + lonMin*resolution*4)
  83. #file.seek(((latMax*resolution-1-latPos)*samplesPerLine + lonMin*resolution)*4)
  84. #var v = float(latPos-latMin*resolution) / ((latMax-latMin) * resolution)
  85. var v = float(latPos-latMin*resolution) / ((latMax-latMin) * resolution)
  86. #var lat = float(latPos)*PI/(resolution*180)
  87. #var w = cos(lat)
  88. #var y = sin(lat)
  89. #print(v*100, "%")
  90. # Loop over segments in ring.
  91. for lonPos in range(float(lonMin)*resolution, float(lonMax)*resolution):
  92. var lonCur = float(lonPos)/float(resolution)
  93. var u = float(lonPos-lonMin*resolution) / ((lonMax - lonMin) * resolution)
  94. #var lon = float(lonPos)*PI/(resolution*180)
  95. #var x = sin(lon)
  96. #var z = cos(lon)
  97. #print("Lon: ", lon)
  98. #var height = file.get_float()/offset*radius+radius
  99. var height = file.get_float()+radius
  100. vert = Common.latLonToGlobal(latCur, lonCur, height)
  101. #vert = Vector3(scaleFact * x * height * w, scaleFact * y*height, scaleFact * z * height * w)
  102. verts[point] = vert
  103. normals[point] = vert.normalized()
  104. uvs[point] = Vector2(u, 1-v)
  105. point += 1
  106. # Create triangles in ring using indices.
  107. if latPos > lonMin*resolution and lonPos > lonMin*resolution:
  108. indices[i_idx] = (prevrow + (lonPos-lonMin*resolution) - 1)
  109. i_idx+=1
  110. indices[i_idx] = (thisrow + (lonPos-lonMin*resolution) - 1)
  111. i_idx+=1
  112. indices[i_idx] = (prevrow + (lonPos-lonMin*resolution))
  113. i_idx+=1
  114. indices[i_idx] = (prevrow + (lonPos-lonMin*resolution))
  115. i_idx+=1
  116. indices[i_idx] = (thisrow + (lonPos-lonMin*resolution) - 1)
  117. i_idx+=1
  118. indices[i_idx] = (thisrow + (lonPos-lonMin*resolution))
  119. i_idx+=1
  120. #if latPos > latMin*resolution:
  121. # indices[i_idx] = (prevrow + ((latMax - latMin) * resolution) - 1)
  122. # i_idx+=1
  123. # indices[i_idx] = (prevrow)
  124. # i_idx+=1
  125. # indices[i_idx] = (thisrow + ((latMax - latMin) * resolution) - 1)
  126. # i_idx+=1
  127. # indices[i_idx] = (prevrow)
  128. # i_idx+=1
  129. # indices[i_idx] = (prevrow + ((latMax - latMin) * resolution))
  130. # i_idx+=1
  131. # indices[i_idx] = (thisrow + ((latMax - latMin) * resolution) - 1)
  132. # i_idx+=1
  133. #print(vert)
  134. prevrow = thisrow
  135. thisrow = point
  136. file.close()
  137. print(vert)
  138. # Assign arrays to mesh array.
  139. arr[Mesh.ARRAY_VERTEX] = verts
  140. arr[Mesh.ARRAY_TEX_UV] = uvs
  141. arr[Mesh.ARRAY_NORMAL] = normals
  142. arr[Mesh.ARRAY_INDEX] = indices
  143. mesh = Mesh.new()
  144. # Create mesh surface from mesh array.
  145. mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arr)
  146. print(mesh)
  147. ResourceSaver.save("user://tile_"+str(latMin)+"-"+str(lonMin)+".tres", mesh, 32)
  148. print("saved tile_"+str(latMin)+"-"+str(lonMin))
  149. # Called every frame. 'delta' is the elapsed time since the previous frame.
  150. #func _process(delta):
  151. # pass