Tile.gd 4.3 KB

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