Tile.gd 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. func genMesh():
  35. var file = File.new()
  36. file.open(datafile, File.READ)
  37. #for lala in range(10):
  38. # file.seek(lala*samplesPerLine*4)
  39. # var blubb = ""
  40. # for bla in range(10):
  41. # blubb += " " + str(file.get_float())
  42. # print(blubb)
  43. rng.randomize()
  44. var arr = []
  45. arr.resize(Mesh.ARRAY_MAX)
  46. #OS.delay_msec(2000)
  47. # PoolVectorXXArrays for mesh construction.
  48. var verts = PoolVector3Array()
  49. verts.resize((lonMax-lonMin) *resolution*(lonMax-lonMin) *resolution)
  50. var uvs = PoolVector2Array()
  51. uvs.resize((lonMax-lonMin) *resolution*(lonMax-lonMin) *resolution)
  52. var normals = PoolVector3Array()
  53. normals.resize((lonMax-lonMin) *resolution*resolution * (lonMax-lonMin))
  54. var indices = PoolIntArray()
  55. indices.resize(6* (lonMax-lonMin)*resolution*(lonMax-lonMin) * resolution*6)
  56. var i_idx = 0
  57. # Vertex indices.
  58. var thisrow = 0
  59. var prevrow = 0
  60. var point = 0
  61. print(
  62. "calculating segment "+str(latMin)+"-"+str(lonMin)+":\n",
  63. "lat:\n",
  64. " min: ", latMin*resolution, "\n",
  65. " max:", latMax*resolution, "\n",
  66. " count: ", (latMax-latMin) * resolution, "\n",
  67. "lon:\n",
  68. " min: ", lonMin*resolution, "\n",
  69. " max:", lonMax*resolution, "\n",
  70. " count: ", (lonMax-lonMin) * resolution, "\n"
  71. )
  72. #OS.delay_msec(2000)
  73. var vert = Vector3()
  74. # Loop over rings.
  75. for latPos in range(latMin*resolution, latMax*resolution):
  76. #jump to last line in file - current local lat - local startLat + local startLon
  77. file.seek(lines*samplesPerLine*4-latPos*samplesPerLine*4 - latMin*samplesPerLine*4 + lonMin*resolution*4)
  78. #file.seek(latPos*samplesPerLine*4 + latMin*samplesPerLine*4 + lonMin*resolution*4)
  79. #file.seek(latPos*samplesPerLine*4 + lonMin*resolution*4)
  80. #file.seek(((latMax*resolution-1-latPos)*samplesPerLine + lonMin*resolution)*4)
  81. #var v = float(latPos-latMin*resolution) / ((latMax-latMin) * resolution)
  82. var v = float(latPos-latMin*resolution) / ((latMax-latMin) * resolution)
  83. var lat = float(latPos)*PI/(resolution*180)
  84. var w = cos(lat)
  85. var y = sin(lat)
  86. #print(v*100, "%")
  87. # Loop over segments in ring.
  88. for lonPos in range(lonMin*resolution, lonMax*resolution):
  89. var u = float(lonPos-lonMin*resolution) / ((lonMax - lonMin) * resolution)
  90. var lon = float(lonPos)*PI/(resolution*180)
  91. var x = sin(lon)
  92. var z = cos(lon)
  93. #print("Lon: ", lon)
  94. var height = file.get_float()/offset*radius+radius
  95. vert = Vector3(scaleFact * x * height * w, scaleFact * y*height, scaleFact * z * height * w)
  96. verts[point] = vert
  97. normals[point] = vert.normalized()
  98. uvs[point] = Vector2(u, 1-v)
  99. point += 1
  100. # Create triangles in ring using indices.
  101. if latPos > lonMin*resolution and lonPos > lonMin*resolution:
  102. indices[i_idx] = (prevrow + (lonPos-lonMin*resolution) - 1)
  103. i_idx+=1
  104. indices[i_idx] = (thisrow + (lonPos-lonMin*resolution) - 1)
  105. i_idx+=1
  106. indices[i_idx] = (prevrow + (lonPos-lonMin*resolution))
  107. i_idx+=1
  108. indices[i_idx] = (prevrow + (lonPos-lonMin*resolution))
  109. i_idx+=1
  110. indices[i_idx] = (thisrow + (lonPos-lonMin*resolution) - 1)
  111. i_idx+=1
  112. indices[i_idx] = (thisrow + (lonPos-lonMin*resolution))
  113. i_idx+=1
  114. #if latPos > latMin*resolution:
  115. # indices[i_idx] = (prevrow + ((latMax - latMin) * resolution) - 1)
  116. # i_idx+=1
  117. # indices[i_idx] = (prevrow)
  118. # i_idx+=1
  119. # indices[i_idx] = (thisrow + ((latMax - latMin) * resolution) - 1)
  120. # i_idx+=1
  121. # indices[i_idx] = (prevrow)
  122. # i_idx+=1
  123. # indices[i_idx] = (prevrow + ((latMax - latMin) * resolution))
  124. # i_idx+=1
  125. # indices[i_idx] = (thisrow + ((latMax - latMin) * resolution) - 1)
  126. # i_idx+=1
  127. #print(vert)
  128. prevrow = thisrow
  129. thisrow = point
  130. file.close()
  131. print(vert)
  132. # Assign arrays to mesh array.
  133. arr[Mesh.ARRAY_VERTEX] = verts
  134. arr[Mesh.ARRAY_TEX_UV] = uvs
  135. arr[Mesh.ARRAY_NORMAL] = normals
  136. arr[Mesh.ARRAY_INDEX] = indices
  137. mesh = Mesh.new()
  138. # Create mesh surface from mesh array.
  139. mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arr)
  140. print(mesh)
  141. ResourceSaver.save("user://tile_"+str(latMin)+"-"+str(lonMin)+".tres", mesh, 32)
  142. print("saved tile_"+str(latMin)+"-"+str(lonMin))
  143. # Called every frame. 'delta' is the elapsed time since the previous frame.
  144. #func _process(delta):
  145. # pass