flat_tile.gd 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 i = 0.0
  36. for vert in verts:
  37. var point = Point.instance()
  38. point.translate(vert)
  39. var mat = SpatialMaterial.new()#point.mesh.surface_get_material(0)
  40. mat.albedo_color = Color(i,0,0,1)
  41. i+=0.1
  42. point.mesh.surface_set_material(0,mat)
  43. .get_parent().add_child(point)
  44. func genNormals(verts:PoolVector3Array):
  45. var normals = PoolVector3Array()
  46. normals.resize((resolution+1) * (resolution+1))
  47. var v_idx = 0
  48. for vert in verts:
  49. normals[v_idx] = vert.normalized()
  50. v_idx+=1
  51. print("normals")
  52. print(len(normals))
  53. return normals
  54. func genUV():
  55. var uvs = PoolVector2Array()
  56. uvs.resize((resolution+1) * (resolution+1))
  57. for v_idx in range(0,(resolution+1) * (resolution+1)):
  58. var y = v_idx / resolution
  59. var x = v_idx % resolution
  60. uvs[v_idx]=Vector2(clamp(x, 0, resolution),clamp(y, 0, resolution))
  61. print("uv")
  62. print(len(uvs))
  63. return uvs
  64. func genIndex():
  65. var indices = PoolIntArray()
  66. indices.resize(6* ((resolution+1) * (resolution+1)))
  67. var i_idx = 0
  68. for row in range(0, resolution+1):
  69. for col in range(0,resolution+1):
  70. var v_idx = row*(resolution+1)+col
  71. indices[i_idx] = v_idx
  72. i_idx+=1
  73. indices[i_idx] = v_idx + resolution +1
  74. i_idx+=1
  75. indices[i_idx] = v_idx + 1
  76. i_idx+=1
  77. indices[i_idx] = v_idx + resolution+1
  78. i_idx+=1
  79. indices[i_idx] = v_idx + resolution + 2
  80. i_idx+=1
  81. indices[i_idx] = v_idx + 1
  82. i_idx+=1
  83. print("indices")
  84. print(len(indices))
  85. return indices
  86. func genSphereCoords():
  87. var verts = PoolVector3Array()
  88. var size = (resolution+1) * (resolution+1)#(lonMax-lonMin)*resolution * (latMax-latMin)*resolution
  89. verts.resize(size)
  90. var lonStep = (lonMax - lonMin)/resolution
  91. var latStep = (latMax - latMin)/resolution
  92. var v_idx = 0
  93. for y in range(0,resolution+1):
  94. for x in range(0,resolution+1):
  95. verts[v_idx] = Vector3(latMin+latStep*y, lonMin+lonStep*x, radius)
  96. v_idx+=1
  97. #var v_idx = 0
  98. #var lat := float(latMin)
  99. #while lat <= float(latMax):
  100. # var lon := float(lonMin)
  101. # while lon <= float(lonMax):
  102. # verts[v_idx] = Vector3(lat, lon, radius)
  103. # v_idx+=1
  104. # lon += (lonMax-lonMin)/resolution
  105. # lat += (latMax-latMin)/resolution
  106. print("verts")
  107. print(len(verts))
  108. return verts
  109. func convertToCartesian(verts:PoolVector3Array):
  110. var carVerts = PoolVector3Array()
  111. carVerts.resize(len(verts))
  112. var v_idx = 0
  113. for vert in verts:
  114. carVerts[v_idx] = Common.latLonToGlobal(vert.x, vert.y, vert.z, Vector3(), Vector3())
  115. v_idx+=1
  116. print("Cartesian")
  117. print(len(carVerts))
  118. return carVerts