123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- extends MeshInstance
- var Point = preload("res://point.tscn")
- var latMin:float = 0
- var latMax:float = 1
- var lonMin:float = 0.0
- var lonMax:float = 1.0
- var resolution:int = 512
- var radius:float = 10
- func setParams(latMinIN, latMaxIN, lonMinIN, lonMaxIN, resolutionIN):
- latMin = latMinIN
- latMax = latMaxIN
- lonMin = lonMinIN
- lonMax = lonMaxIN
- resolution = resolutionIN
- func _ready():
- pass
- #genMesh called from chunk
- #self.genMesh()
-
- 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 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)
-
- var i = 0.0
- for vert in verts:
- var point = Point.instance()
- point.translate(vert)
- var mat = 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,mat)
- .get_parent().add_child(point)
- func genNormals(verts:PoolVector3Array):
- var normals = PoolVector3Array()
- normals.resize((resolution+1) * (resolution+1))
- 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))
-
- 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+1) * (resolution+1)*6)
-
- var i_idx = 0
- var row = 0
- while row < resolution:
- var col = 0
- while col < 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
-
- col += 1
- row += 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 v_idx = 0
- var lat := float(latMin)
- while lat <= float(latMax):
- var lon := float(lonMin)
- while lon <= float(lonMax):
- verts[v_idx] = Vector3(lat, lon, radius)
- v_idx+=1
- lon += (lonMax-lonMin)/resolution
- lat += (latMax-latMin)/resolution
- print("verts")
- print(len(verts))
- return verts
- func convertToCartesian(verts:PoolVector3Array):
- var carVerts = PoolVector3Array()
- carVerts.resize(len(verts))
- var v_idx = 0
- for vert in verts:
- carVerts[v_idx] = Common.latLonToGlobal(vert.x, vert.y, vert.z, Vector3(), Vector3())
- v_idx+=1
- print("Cartesian")
- print(len(carVerts))
- return carVerts
|