Project 2D指向3D球体,每个国家/地区使用单个对象

时间:2019-05-15 15:57:58

标签: python unity3d math maya

我正在使用Maya,并且试图创建一个3D球形地球,其中所有国家/地区都被对象隔开,因此以后可以将文件导出到Unity。 借助此代码,我创建了代表地球的平面。

我设法用所有竞争的2D geojson点创建了一个地球平面。现在,我试图将这些点投影到3D球面。

使用此代码,我创建了一个地球Earth

Earth Plane

# EXAMPLE OF THE POINTS FORMAT

d = {"type":"FeatureCollection","features":[
{"type":"Feature","id":"ALB","properties":{"name":"Albania"},"geometry":{"type":"Polygon","coordinates":[[[20.590247,41.855404],[20.463175,41.515089],[20.605182,41.086226],[21.02004,40.842727],[20.99999,40.580004],[20.674997,40.435],[20.615,40.110007],[20.150016,39.624998],[19.98,39.694993],[19.960002,39.915006],[19.406082,40.250773],[19.319059,40.72723],[19.40355,41.409566],[19.540027,41.719986],[19.371769,41.877548],[19.304486,42.195745],[19.738051,42.688247],[19.801613,42.500093],[20.0707,42.58863],[20.283755,42.32026],[20.52295,42.21787],[20.590247,41.855404]]]}},
{"type":"Feature","id":"ARE","properties":{"name":"United Arab Emirates"},"geometry":{"type":"Polygon","coordinates":[[[51.579519,24.245497],[51.757441,24.294073],[51.794389,24.019826],[52.577081,24.177439],[53.404007,24.151317],[54.008001,24.121758],[54.693024,24.797892],[55.439025,25.439145],[56.070821,26.055464],[56.261042,25.714606],[56.396847,24.924732],[55.886233,24.920831],[55.804119,24.269604],[55.981214,24.130543],[55.528632,23.933604],[55.525841,23.524869],[55.234489,23.110993],[55.208341,22.70833],[55.006803,22.496948],[52.000733,23.001154],[51.617708,24.014219],[51.579519,24.245497]]]}}
...

for feat in d.get("features"):
    r = []
    coords = feat.get("geometry").get("coordinates")
    type = feat.get("geometry").get("type")
    for coord in coords:
        for c in coord:
            if type == "MultiPolygon":
                r = []
                for a in c:
                    r.append((a[0],a[1],0))
                poly = cmds.polyCreateFacet(p=r)
                poly = cmds.rename(feat.get("properties").get("name"))
            else:
                r.append((c[0],c[1], 0))

    if not type == "MultiPoligon":
    poly = cmds.polyCreateFacet(p=r)
        poly = cmds.rename(feat.get("properties").get("name"))

搜索有关如何将2d点投影到3d球体上的信息:

here how map 2d grid points (x,y) onto sphere as 3d points (x,y,z)

我也寻找墨卡托投影 https://forums.tigsource.com/index.php?topic=17522.0 https://wiki.openstreetmap.org/wiki/Mercator#Elliptical_.28true.29_Mercator_Projection

我尝试过...


def range_n(n, min, max):
    return ((n - min) / (max - min)) * (1 - 0) + 0

def to_3d_v3(x,y):
    # this points are the bounds of the points
    x = range_n(x, -180, 180)
    y = range_n(y, -85.609038, 42.688247)

    phi = y * 2 * math.pi
    z = 2 * x -1
    rho = math.sqrt(1-z*z)
    rho = 20

    x = rho * x
    y = rho * math.log(math.tan((y + math.pi/2)/2))

    return (rho * math.cos(x) * math.cos(y), rho * math.cos(x) * math.sin(y), rho * math.sin(x))

def to_3d_v2(x,y):
    x = range_n(x, -180, 180)
    y = range_n(y, -85.609038, 42.688247)

    z = -1 + 2 * x
    phi = 2 * math.pi * y
    theta = math.asin(z)
    return (math.cos(theta) * math.cos(phi), math.cos(theta) * math.sin(phi), z)

def to_3d(x,y):
    x = range_n(x, -180, 180)
    y = range_n(y, -85.609038, 42.688247)

    z = 2 * x -1
    phi = y * x -1
    rho = 1
    rho = math.sqrt(1-z*z)
    return (rho * math.cos(y), rho * math.sin(y), z)

# not a lof of changes
# just filtering all points with to_3d function
for feat in d.get("features"):
    r = []
    coords = feat.get("geometry").get("coordinates")
    type = feat.get("geometry").get("type")
    for coord in coords:
        for c in coord:
            if type == "MultiPolygon":
                r = []
                for a in c:
                    r.append(to_3d(a[0],a[1]))
                poly = cmds.polyCreateFacet(p=r)
                poly = cmds.rename(feat.get("properties").get("name"))
            else:
                r.append(to_3d(c[0],c[1]))
    #print(feat.get("id"), r)

    if not type == "MultiPoligon":
        poly = cmds.polyCreateFacet(p=r)
        poly = cmds.rename(feat.get("properties").get("name"))

但是结果是像这样的一些奇怪的事情

to_3d_v3:img

to_3d_v2:img]

to_3d:img]

有什么建议吗?

谢谢

1 个答案:

答案 0 :(得分:0)

由于您将x和y用作经度和纬度,因此应将它们直接输入到公式中,以将纬度/经度投影到球面how map 2d grid points (x,y) onto sphere as 3d points (x,y,z)

此外,由于x是水平位置,因此意味着x是经度。同样,y是垂直位置,所以y是纬度。你把它们倒退了。

def to_3d_v4(x,y):
    #convert from degrees to radians
    longitude = math.radians(x)
    latitude = math.radians(y)

    # select a radius:
    radius = 10

    # project to 3d
    return (
        rho * math.cos(latitude) * math.cos(longitude), 
        rho * math.cos(latitude) * math.sin(longitude), 
        rho * math.sin(latitude)
    )