flat_tile.gd 3.9 KB

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