123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- extends MeshInstance
- signal meshLoaded
- var Point = preload("res://point.tscn")
- #var TopoLoader = preload("res://TopoLoader.gd")
- #var tl
- var TextureLoader = preload("res://TextureLoader.gd")
- var texl
- var groundShader = preload("res://shader/ground_shader.tres") as ShaderMaterial
- onready var latMin:float = .get_parent().latMin
- onready var latMax:float = .get_parent().latMax
- onready var lonMin:float = .get_parent().lonMin
- onready var lonMax:float = .get_parent().lonMax
- onready var resolution:int = .get_parent().resolution
- onready var radius:float = .get_parent().radius
- onready var scaleFact:int = .get_parent().scaleFact
- var origin:Vector3
- var center:Vector3
- var heightmap:Image
- var overlay_heightmap = false
- var mat:ShaderMaterial
-
- func _ready():
- init()
-
- func init():
- texl = TextureLoader.new()
- var texPos = texl.open(latMin, lonMin)
- while texPos != Vector2(latMin, lonMin):
- texPos = yield(Messenger, "textureReady")
- self.genMesh()
-
- func _process(delta):
- if Input.is_action_just_pressed("overlay"):
- overlay_heightmap = not overlay_heightmap
- mat.set_shader_param("overlay_heightmap", overlay_heightmap)
-
- func genMesh():
- #print("generating Mesh:")
- #print("latMin: " + str(latMin))
- #print("latMax: " + str(latMax))
- #print("lonMin: " + str(lonMin))
- #print("lonMax: " + str(lonMax))
- #print("resolution: " + str(resolution))
- var chunkPos = Common.posToChunkPos(Vector2(latMin,lonMin))
-
- var tc = TopoLoader.cacheChunk(chunkPos)
- while tc.get_class() != "Reference" and tc.chunkPos != chunkPos and not tc.chunkCached:
- tc = yield(Messenger, "chunkCached")
-
- var verts = convertToCartesian(genSphereCoords())
- var arr = []
- arr.resize(Mesh.ARRAY_MAX)
- arr[Mesh.ARRAY_VERTEX] = verts
- arr[Mesh.ARRAY_TEX_UV] = genUV()
- arr[Mesh.ARRAY_NORMAL] = genNormals(verts)
- arr[Mesh.ARRAY_INDEX] = genIndex()
- mesh = Mesh.new()
- mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arr)
- mat = groundShader.duplicate()
- #mat.albedo_color = Color(0.7,0.7,0.7,1)
- mat.set_shader_param("texture_albedo_big", texl.loadTexture())
- heightmap = TopoLoader.createHeightmap(chunkPos)
- var heighttex = ImageTexture.new()
- heighttex.create_from_image(heightmap, 0)
- mat.set_shader_param("texture_heightmap", heighttex)
- mesh.surface_set_material(0,mat)
- .create_trimesh_collision()
-
- Messenger.emit_signal("meshLoaded")
-
- var drawBubbles = false
- if drawBubbles:
- var i = 0.0
- for vert in verts:
- var point = Point.instance()
- point.translate(vert)
- var pointMat = SpatialMaterial.new()#point.mesh.surface_get_material(0)
- mat.albedo_color = Color(i,0,0,1)
- i+=0.1
- point.mesh.surface_set_material(0,pointMat)
- .get_parent().add_child(point)
- func genNormals(verts:PoolVector3Array):
- var normals = PoolVector3Array()
- normals.resize(len(verts))
- var v_idx = 0
- for vert in verts:
- normals[v_idx] = vert.normalized()
- v_idx+=1
- #print("normals")
- #print(len(normals))
- return normals
- func genUV():
- var uvs = PoolVector2Array()
- uvs.resize((resolution+1) * (resolution+1))
- var v_idx = 0
- for _y in range(0,resolution+1):
- for _x in range(0,resolution+1):
- var y = float(_y)
- var x = float(_x)
- var devider = float(resolution+1)
- uvs[v_idx] = Vector2(x/devider,1-(y/devider))
- v_idx+=1
-
- #for v_idx in range(0,(resolution+1) * (resolution+1)):
- # var y = v_idx / resolution
- # var x = v_idx % resolution
- # uvs[v_idx]=Vector2(clamp(x, 0, resolution),clamp(y, 0, resolution))
- #print("uv")
- #print(len(uvs))
- return uvs
-
- func genIndex():
- var indices = PoolIntArray()
- indices.resize(6* ((resolution) * (resolution)))
-
- var i_idx = 0
- for row in range(0, resolution):
- for col in range(0,resolution):
- var v_idx = row*(resolution+1)+col
-
- indices[i_idx] = v_idx
- i_idx+=1
- indices[i_idx] = v_idx + resolution +1
- i_idx+=1
- indices[i_idx] = v_idx + 1
- i_idx+=1
- indices[i_idx] = v_idx + resolution+1
- i_idx+=1
- indices[i_idx] = v_idx + resolution + 2
- i_idx+=1
- indices[i_idx] = v_idx + 1
- i_idx+=1
-
- #print("indices")
- #print(len(indices))
- return indices
-
- func genSphereCoords():
- var verts = PoolVector3Array()
- var size = (resolution+1) * (resolution+1)#(lonMax-lonMin)*resolution * (latMax-latMin)*resolution
- verts.resize(size)
-
- var lonStep = (lonMax - lonMin)/resolution
- var latStep = (latMax - latMin)/resolution
- var v_idx = 0
- for y in range(0,resolution+1):
- for x in range(0,resolution+1):
- var lat = latMin+latStep*y
- var lon = lonMin+lonStep*x
- var height = TopoLoader.read(lat,lon)
- if not height and height != 0:
- push_error("could not get height")
- return
- verts[v_idx] = Vector3(lat, lon, radius+height*scaleFact)
- v_idx+=1
- return verts
- func convertToCartesian(verts:PoolVector3Array):
- var carVerts = PoolVector3Array()
- carVerts.resize(len(verts))
- var v_idx = 0
- for vert in verts:
- if not origin:
- origin = vert
- if v_idx == len(verts)/2:
- center = vert
- carVerts[v_idx] = Common.latLonToGlobal(vert, Vector3())#, origin)
- v_idx+=1
- #print("Cartesian")
- #print(len(carVerts))
- return carVerts
|