Tile.gd 4.2 KB

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