flat_tile.gd 3.6 KB

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