Tile.gd 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. extends MeshInstance
  2. signal meshLoaded
  3. var Point = preload("res://point.tscn")
  4. #var TopoLoader = preload("res://TopoLoader.gd")
  5. #var tl
  6. var TextureLoader = preload("res://TextureLoader.gd")
  7. var texl
  8. var groundShader = preload("res://shader/ground_shader.tres") as ShaderMaterial
  9. onready var latMin:float = .get_parent().latMin
  10. onready var latMax:float = .get_parent().latMax
  11. onready var lonMin:float = .get_parent().lonMin
  12. onready var lonMax:float = .get_parent().lonMax
  13. onready var resolution:int = .get_parent().resolution
  14. onready var radius:float = .get_parent().radius
  15. onready var scaleFact:int = .get_parent().scaleFact
  16. var origin:Vector3
  17. var center:Vector3
  18. var heightmap:Image
  19. var overlay_heightmap = false
  20. var mat:ShaderMaterial
  21. func _ready():
  22. init()
  23. func init():
  24. texl = TextureLoader.new()
  25. var texPos = texl.open(latMin, lonMin)
  26. while texPos != Vector2(latMin, lonMin):
  27. texPos = yield(Messenger, "textureReady")
  28. self.genMesh()
  29. func _process(delta):
  30. if Input.is_action_just_pressed("overlay"):
  31. overlay_heightmap = not overlay_heightmap
  32. mat.set_shader_param("overlay_heightmap", overlay_heightmap)
  33. func genMesh():
  34. #print("generating Mesh:")
  35. #print("latMin: " + str(latMin))
  36. #print("latMax: " + str(latMax))
  37. #print("lonMin: " + str(lonMin))
  38. #print("lonMax: " + str(lonMax))
  39. #print("resolution: " + str(resolution))
  40. var chunkPos = Common.posToChunkPos(Vector2(latMin,lonMin))
  41. var tc = TopoLoader.cacheChunk(chunkPos)
  42. while tc.get_class() != "Reference" and tc.chunkPos != chunkPos and not tc.chunkCached:
  43. tc = yield(Messenger, "chunkCached")
  44. var verts = convertToCartesian(genSphereCoords())
  45. var arr = []
  46. arr.resize(Mesh.ARRAY_MAX)
  47. arr[Mesh.ARRAY_VERTEX] = verts
  48. arr[Mesh.ARRAY_TEX_UV] = genUV()
  49. arr[Mesh.ARRAY_NORMAL] = genNormals(verts)
  50. arr[Mesh.ARRAY_INDEX] = genIndex()
  51. mesh = Mesh.new()
  52. mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arr)
  53. mat = groundShader.duplicate()
  54. #mat.albedo_color = Color(0.7,0.7,0.7,1)
  55. mat.set_shader_param("texture_albedo_big", texl.loadTexture())
  56. heightmap = TopoLoader.createHeightmap(chunkPos)
  57. var heighttex = ImageTexture.new()
  58. heighttex.create_from_image(heightmap, 0)
  59. mat.set_shader_param("texture_heightmap", heighttex)
  60. mesh.surface_set_material(0,mat)
  61. .create_trimesh_collision()
  62. Messenger.emit_signal("meshLoaded")
  63. var drawBubbles = false
  64. if drawBubbles:
  65. var i = 0.0
  66. for vert in verts:
  67. var point = Point.instance()
  68. point.translate(vert)
  69. var pointMat = SpatialMaterial.new()#point.mesh.surface_get_material(0)
  70. mat.albedo_color = Color(i,0,0,1)
  71. i+=0.1
  72. point.mesh.surface_set_material(0,pointMat)
  73. .get_parent().add_child(point)
  74. func genNormals(verts:PoolVector3Array):
  75. var normals = PoolVector3Array()
  76. normals.resize(len(verts))
  77. var v_idx = 0
  78. for vert in verts:
  79. normals[v_idx] = vert.normalized()
  80. v_idx+=1
  81. #print("normals")
  82. #print(len(normals))
  83. return normals
  84. func genUV():
  85. var uvs = PoolVector2Array()
  86. uvs.resize((resolution+1) * (resolution+1))
  87. var v_idx = 0
  88. for _y in range(0,resolution+1):
  89. for _x in range(0,resolution+1):
  90. var y = float(_y)
  91. var x = float(_x)
  92. var devider = float(resolution+1)
  93. uvs[v_idx] = Vector2(x/devider,1-(y/devider))
  94. v_idx+=1
  95. #for v_idx in range(0,(resolution+1) * (resolution+1)):
  96. # var y = v_idx / resolution
  97. # var x = v_idx % resolution
  98. # uvs[v_idx]=Vector2(clamp(x, 0, resolution),clamp(y, 0, resolution))
  99. #print("uv")
  100. #print(len(uvs))
  101. return uvs
  102. func genIndex():
  103. var indices = PoolIntArray()
  104. indices.resize(6* ((resolution) * (resolution)))
  105. var i_idx = 0
  106. for row in range(0, resolution):
  107. for col in range(0,resolution):
  108. var v_idx = row*(resolution+1)+col
  109. indices[i_idx] = v_idx
  110. i_idx+=1
  111. indices[i_idx] = v_idx + resolution +1
  112. i_idx+=1
  113. indices[i_idx] = v_idx + 1
  114. i_idx+=1
  115. indices[i_idx] = v_idx + resolution+1
  116. i_idx+=1
  117. indices[i_idx] = v_idx + resolution + 2
  118. i_idx+=1
  119. indices[i_idx] = v_idx + 1
  120. i_idx+=1
  121. #print("indices")
  122. #print(len(indices))
  123. return indices
  124. func genSphereCoords():
  125. var verts = PoolVector3Array()
  126. var size = (resolution+1) * (resolution+1)#(lonMax-lonMin)*resolution * (latMax-latMin)*resolution
  127. verts.resize(size)
  128. var lonStep = (lonMax - lonMin)/resolution
  129. var latStep = (latMax - latMin)/resolution
  130. var v_idx = 0
  131. for y in range(0,resolution+1):
  132. for x in range(0,resolution+1):
  133. var lat = latMin+latStep*y
  134. var lon = lonMin+lonStep*x
  135. var height = TopoLoader.read(lat,lon)
  136. if not height and height != 0:
  137. push_error("could not get height")
  138. return
  139. verts[v_idx] = Vector3(lat, lon, radius+height*scaleFact)
  140. v_idx+=1
  141. return verts
  142. func convertToCartesian(verts:PoolVector3Array):
  143. var carVerts = PoolVector3Array()
  144. carVerts.resize(len(verts))
  145. var v_idx = 0
  146. for vert in verts:
  147. if not origin:
  148. origin = vert
  149. if v_idx == len(verts)/2:
  150. center = vert
  151. carVerts[v_idx] = Common.latLonToGlobal(vert, Vector3())#, origin)
  152. v_idx+=1
  153. #print("Cartesian")
  154. #print(len(carVerts))
  155. return carVerts