flat_tile.gd 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. extends MeshInstance
  2. var Point = preload("res://point.tscn")
  3. var TopoLoader = preload("res://TopoLoader.gd")
  4. var latMin:float = 0
  5. var latMax:float = 1
  6. var lonMin:float = 0.0
  7. var lonMax:float = 1.0
  8. var resolution:int = 512
  9. var radius:float = 10
  10. var origin:Vector3
  11. func setParams(latMinIN, latMaxIN, lonMinIN, lonMaxIN, resolutionIN, radiusIN:float):
  12. latMin = latMinIN
  13. latMax = latMaxIN
  14. lonMin = lonMinIN
  15. lonMax = lonMaxIN
  16. resolution = resolutionIN
  17. radius = radiusIN
  18. func _ready():
  19. pass
  20. #genMesh called from chunk
  21. #self.genMesh()
  22. func genMesh():
  23. #print("generating Mesh:")
  24. #print("latMin: " + str(latMin))
  25. #print("latMax: " + str(latMax))
  26. #print("lonMin: " + str(lonMin))
  27. #print("lonMax: " + str(lonMax))
  28. #print("resolution: " + str(resolution))
  29. var verts = convertToCartesian(genSphereCoords())
  30. var arr = []
  31. arr.resize(Mesh.ARRAY_MAX)
  32. arr[Mesh.ARRAY_VERTEX] = verts
  33. arr[Mesh.ARRAY_TEX_UV] = genUV()
  34. arr[Mesh.ARRAY_NORMAL] = genNormals(verts)
  35. arr[Mesh.ARRAY_INDEX] = genIndex()
  36. mesh = Mesh.new()
  37. mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arr)
  38. var mat = SpatialMaterial.new()
  39. mat.albedo_color = Color(0.7,0.7,0.7,1)
  40. mesh.surface_set_material(0,mat)
  41. var drawBubbles = false
  42. if drawBubbles:
  43. var i = 0.0
  44. for vert in verts:
  45. var point = Point.instance()
  46. point.translate(vert)
  47. var pointMat = SpatialMaterial.new()#point.mesh.surface_get_material(0)
  48. mat.albedo_color = Color(i,0,0,1)
  49. i+=0.1
  50. point.mesh.surface_set_material(0,pointMat)
  51. .get_parent().add_child(point)
  52. func genNormals(verts:PoolVector3Array):
  53. var normals = PoolVector3Array()
  54. normals.resize(len(verts))
  55. var v_idx = 0
  56. for vert in verts:
  57. normals[v_idx] = vert.normalized()
  58. v_idx+=1
  59. #print("normals")
  60. #print(len(normals))
  61. return normals
  62. func genUV():
  63. var uvs = PoolVector2Array()
  64. uvs.resize((resolution+1) * (resolution+1))
  65. for v_idx in range(0,(resolution+1) * (resolution+1)):
  66. var y = v_idx / resolution
  67. var x = v_idx % resolution
  68. uvs[v_idx]=Vector2(clamp(x, 0, resolution),clamp(y, 0, resolution))
  69. #print("uv")
  70. #print(len(uvs))
  71. return uvs
  72. func genIndex():
  73. var indices = PoolIntArray()
  74. indices.resize(6* ((resolution) * (resolution)))
  75. var i_idx = 0
  76. for row in range(0, resolution):
  77. for col in range(0,resolution):
  78. var v_idx = row*(resolution+1)+col
  79. indices[i_idx] = v_idx
  80. i_idx+=1
  81. indices[i_idx] = v_idx + resolution +1
  82. i_idx+=1
  83. indices[i_idx] = v_idx + 1
  84. i_idx+=1
  85. indices[i_idx] = v_idx + resolution+1
  86. i_idx+=1
  87. indices[i_idx] = v_idx + resolution + 2
  88. i_idx+=1
  89. indices[i_idx] = v_idx + 1
  90. i_idx+=1
  91. #print("indices")
  92. #print(len(indices))
  93. return indices
  94. func genSphereCoords():
  95. var tl = TopoLoader.new()
  96. tl.open()
  97. tl.cacheArea(latMin, latMax, lonMin, lonMax)
  98. var verts = PoolVector3Array()
  99. var size = (resolution+1) * (resolution+1)#(lonMax-lonMin)*resolution * (latMax-latMin)*resolution
  100. verts.resize(size)
  101. var lonStep = (lonMax - lonMin)/resolution
  102. var latStep = (latMax - latMin)/resolution
  103. var v_idx = 0
  104. for y in range(0,resolution+1):
  105. for x in range(0,resolution+1):
  106. var lat = latMin+latStep*y
  107. var lon = lonMin+lonStep*x
  108. verts[v_idx] = Vector3(lat, lon, radius+tl.read(lat,lon))
  109. v_idx+=1
  110. tl.close()
  111. return verts
  112. func convertToCartesian(verts:PoolVector3Array):
  113. var carVerts = PoolVector3Array()
  114. carVerts.resize(len(verts))
  115. var v_idx = 0
  116. for vert in verts:
  117. if not origin:
  118. origin = vert
  119. carVerts[v_idx] = Common.latLonToGlobal(vert.x, vert.y, vert.z, Vector3())#, origin)
  120. v_idx+=1
  121. #print("Cartesian")
  122. #print(len(carVerts))
  123. return carVerts