Tile.gd 4.9 KB

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