Tile.gd 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 initialized = false
  18. func initialize(latMinIN, latMaxIN, lonMinIN, lonMaxIN, resolutionIN, radiusIN:float, scaleIN):
  19. latMin = latMinIN
  20. latMax = latMaxIN
  21. lonMin = lonMinIN
  22. lonMax = lonMaxIN
  23. resolution = resolutionIN
  24. radius = radiusIN
  25. scaleFact = scaleIN
  26. tl = TopoLoader.new()
  27. add_child(tl)
  28. var fileReady = tl.open(latMin, lonMin)
  29. if fileReady and not fileReady is GDScriptFunctionState :
  30. initialized = true
  31. return true
  32. else:
  33. return false
  34. func _ready():
  35. pass
  36. #genMesh called from chunk
  37. #self.genMesh()
  38. func initTexture():
  39. texl = TextureLoader.new()
  40. var exists = texl.open(latMin, lonMin)
  41. return exists
  42. func genMesh():
  43. #print("generating Mesh:")
  44. #print("latMin: " + str(latMin))
  45. #print("latMax: " + str(latMax))
  46. #print("lonMin: " + str(lonMin))
  47. #print("lonMax: " + str(lonMax))
  48. #print("resolution: " + str(resolution))
  49. var verts = convertToCartesian(genSphereCoords())
  50. var arr = []
  51. arr.resize(Mesh.ARRAY_MAX)
  52. arr[Mesh.ARRAY_VERTEX] = verts
  53. arr[Mesh.ARRAY_TEX_UV] = genUV()
  54. arr[Mesh.ARRAY_NORMAL] = genNormals(verts)
  55. arr[Mesh.ARRAY_INDEX] = genIndex()
  56. mesh = Mesh.new()
  57. mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arr)
  58. var mat = groundShader.duplicate()
  59. #mat.albedo_color = Color(0.7,0.7,0.7,1)
  60. mat.set_shader_param("texture_albedo_big", texl.loadTexture())
  61. mesh.surface_set_material(0,mat)
  62. .create_trimesh_collision()
  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. tl.cacheArea(latMin, latMax, lonMin, lonMax)
  126. var verts = PoolVector3Array()
  127. var size = (resolution+1) * (resolution+1)#(lonMax-lonMin)*resolution * (latMax-latMin)*resolution
  128. verts.resize(size)
  129. var lonStep = (lonMax - lonMin)/resolution
  130. var latStep = (latMax - latMin)/resolution
  131. var v_idx = 0
  132. for y in range(0,resolution+1):
  133. for x in range(0,resolution+1):
  134. var lat = latMin+latStep*y
  135. var lon = lonMin+lonStep*x
  136. verts[v_idx] = Vector3(lat, lon, radius+tl.read(lat,lon)*scaleFact)
  137. v_idx+=1
  138. tl.close()
  139. return verts
  140. func convertToCartesian(verts:PoolVector3Array):
  141. var carVerts = PoolVector3Array()
  142. carVerts.resize(len(verts))
  143. var v_idx = 0
  144. for vert in verts:
  145. if not origin:
  146. origin = vert
  147. if v_idx == len(verts)/2:
  148. center = vert
  149. carVerts[v_idx] = Common.latLonToGlobal(vert, Vector3())#, origin)
  150. v_idx+=1
  151. #print("Cartesian")
  152. #print(len(carVerts))
  153. return carVerts