flat_tile.gd 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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)*6)
  67. var i_idx = 0
  68. var row = 0
  69. while row < resolution:
  70. var col = 0
  71. while col < 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. col += 1
  86. row += 1
  87. print("indices")
  88. print(len(indices))
  89. return indices
  90. func genSphereCoords():
  91. var verts = PoolVector3Array()
  92. var size = (resolution+1) * (resolution+1)#(lonMax-lonMin)*resolution * (latMax-latMin)*resolution
  93. verts.resize(size)
  94. var v_idx = 0
  95. var lat := float(latMin)
  96. while lat <= float(latMax):
  97. var lon := float(lonMin)
  98. while lon <= float(lonMax):
  99. verts[v_idx] = Vector3(lat, lon, radius)
  100. v_idx+=1
  101. lon += (lonMax-lonMin)/resolution
  102. lat += (latMax-latMin)/resolution
  103. print("verts")
  104. print(len(verts))
  105. return verts
  106. func convertToCartesian(verts:PoolVector3Array):
  107. var carVerts = PoolVector3Array()
  108. carVerts.resize(len(verts))
  109. var v_idx = 0
  110. for vert in verts:
  111. carVerts[v_idx] = Common.latLonToGlobal(vert.x, vert.y, vert.z, Vector3(), Vector3())
  112. v_idx+=1
  113. print("Cartesian")
  114. print(len(carVerts))
  115. return carVerts