python - 项目 2D 指向 3D 球体,每个国家的单个对象

标签 python unity-game-engine math maya

我正在使用 Maya,并尝试创建一个 3D 地球球体,其中所有国家/地区均由对象分隔开,以便稍后可以将文件导出到 Unity。 使用此代码,我创建了代表地球的平面。

我成功地用所有国家的 2D geojson 点创建了一个地球平面。现在我正在尝试将这些点投影到 3D 球体。

用这段代码我创建了一个地球平面

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 球体上发现:

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 https://en.wikipedia.org/wiki/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]

请问有什么建议吗?

谢谢

最佳答案

由于您使用 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)
    )

关于python - 项目 2D 指向 3D 球体,每个国家的单个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56153411/

相关文章:

python - 如何使用 Python 检查 URL 是否是绝对的?

python - 了解如何使用 BeautifulSoup 进行网页抓取

c# - UnityWebRequest WebGL 缺少 Cookie 响应 header

c++ - 如何在保持最小距离的情况下沿有限线随机采样?

c++ - 计算帕斯卡三角形中一行的总数?

python - 在 Python 中集成返回数组的函数

python - 如何在我的模板中给出小数点后两位?

image - 如何更改点击Unity时的按钮图像

c# - 应用下载的纹理后,PNG 文件在 Unity 中增长并通过 EncodeToPNG 转换回 PNG

algorithm - 用最少数量的固定半径圆完全覆盖一个矩形