flat_tile.gd 3.4 KB

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