Tile.gd 4.0 KB

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