You need to enable JavaScript to run this app. 基于Pygame的3D点透视投影至2D屏幕实现问题咨询
优惠活动
大模型
产品
解决方案
定价
更多

基于Pygame的3D点透视投影至2D屏幕实现问题咨询

3D点投影到2D屏幕实现及优化指导

需求说明

实现3D空间点到2D屏幕的坐标转换,在正交投影基础上通过x、y坐标除以深度z分量的方式实现透视效果。

核心代码

3D转2D坐标类

class dtod(pygame.sprite.Sprite):
def __init__(self, point, scale):
    super().__init__()
    self.point = numpy.array(point)
    width, height = pygame.display.get_surface().get_size()
    self.angle = 0
    self.point[0] = (self.point[0] * 1) / self.point[2]
    self.point[1] = (self.point[1] * 1) / self.point[2]
    self.projection = numpy.array(
        [[math.cos(self.angle), math.sin(self.angle), 0.], [math.sin(self.angle), math.cos(self.angle), 0.]])
    self.point = numpy.dot(self.projection, self.point)
    self.point = ((self.point[0] * scale) + width / 2, height / 2 - (self.point[1] * scale))

主绘制代码

import pygame
import numpy as np
from coord import dtod
import math

pygame.init()
screen = pygame.display.set_mode((0, 0))
run = True
width, height = screen.get_size()
colors = {"white": (255, 255, 255), "red": (255, 0, 0), "green": (150, 253, 55), "blue": (0, 
227, 227), "orange": (255, 127, 39), "grey": (64, 64, 64), "yellow": (255, 240, 0)}
tx, ty, tz = 0., 0., 0.
fps = pygame.time.Clock()

def cos(x):
    return math.cos(x)


def sin(x):
    return math.sin(x)


rx = np.array([[1., 0., 0.], [0., cos(tx), -sin(tx)], [0., sin(tx), cos(tx)]])
ry = np.array([[cos(ty), 0., sin(ty)], [0., 1., 0.], [-sin(ty), 0., cos(ty)]])
rz = np.array([[cos(tz), -sin(ty), 0.], [sin(ty), cos(ty), 0.], [0., 0., 1.]])
scale = 200
p1 = np.array([0.5, 0.5, 1.])
p2 = np.array([-0.5, 0.5, 1.])
p3 = np.array([-0.5, -0.5, 1.])
p4 = np.array([0.5, -0.5, 1.])
p5 = np.array([0.5, 0.5, -2.])
p6 = np.array([-0.5, 0.5, -2.])
p7 = np.array([-0.5, -0.5, -2.])
p8 = np.array([0.5, -0.5, -2.])

def transform(tx, ty, tz):
    global rx, ry, rz, p1, p2, p3, p4, p5, p6, p7, p8
    rx = np.array([[1., 0., 0.], [0., cos(tx), -sin(tx)], [0., sin(tx), cos(tx)]])
    ry = np.array([[cos(ty), 0., sin(ty)], [0., 1., 0.], [-sin(ty), 0., cos(ty)]])
    rz = np.array([[cos(tz), -sin(ty), 0.], [sin(ty), cos(ty), 0.], [0., 0., 1.]])
    p1 = np.dot(rx, p1)
    p1 = np.dot(ry, p1)
    p1 = np.dot(rz, p1)
    p2 = np.dot(rx, p2)
    p2 = np.dot(ry, p2)
    p2 = np.dot(rz, p2)
    p3 = np.dot(rx, p3)
    p3 = np.dot(ry, p3)
    p3 = np.dot(rz, p3)
    p4 = np.dot(rx, p4)
    p4 = np.dot(ry, p4)
    p4 = np.dot(rz, p4)
    p5 = np.dot(rx, p5)
    p5 = np.dot(ry, p5)
    p5 = np.dot(rz, p5)
    p6 = np.dot(rx, p6)
    p6 = np.dot(ry, p6)
    p6 = np.dot(rz, p6)
    p7 = np.dot(rx, p7)
    p7 = np.dot(ry, p7)
    p7 = np.dot(rz, p7)
    p8 = np.dot(rx, p8)
    p8 = np.dot(ry, p8)
    p8 = np.dot(rz, p8)
while run:
    screen.fill((0, 0, 0))
    fps.tick(60)
    transform(tx, ty, tz)
    pygame.draw.circle(screen, colors["white"], dtod(p1, scale).point, 5)
    pygame.draw.circle(screen, colors["white"], dtod(p2, scale).point, 5)
    pygame.draw.circle(screen, colors["white"], dtod(p3, scale).point, 5)
    pygame.draw.circle(screen, colors["white"], dtod(p4, scale).point, 5)
    pygame.draw.circle(screen, colors["white"], dtod(p5, scale).point, 5)
    pygame.draw.circle(screen, colors["white"], dtod(p6, scale).point, 5)
    pygame.draw.circle(screen, colors["white"], dtod(p7, scale).point, 5)
    pygame.draw.circle(screen, colors["white"], dtod(p8, scale).point, 5)
    pygame.draw.line(screen, colors["white"], dtod(p1, scale).point, dtod(p2, scale).point)
    pygame.draw.line(screen, colors["white"], dtod(p2, scale).point, dtod(p3, scale).point)
    pygame.draw.line(screen, colors["white"], dtod(p3, scale).point, dtod(p4, scale).point)
    pygame.draw.line(screen, colors["white"], dtod(p4, scale).point, dtod(p1, scale).point)
    pygame.draw.line(screen, colors["white"], dtod(p5, scale).point, dtod(p6, scale).point)
    pygame.draw.line(screen, colors["white"], dtod(p6, scale).point, dtod(p7, scale).point)
    pygame.draw.line(screen, colors["white"], dtod(p7, scale).point, dtod(p8, scale).point)
    pygame.draw.line(screen, colors["white"], dtod(p8, scale).point, dtod(p5, scale).point)
    pygame.draw.line(screen, colors["white"], dtod(p1, scale).point, dtod(p5, scale).point)
    pygame.draw.line(screen, colors["white"], dtod(p6, scale).point, dtod(p2, scale).point)
    pygame.draw.line(screen, colors["white"], dtod(p7, scale).point, dtod(p3, scale).point)
    pygame.draw.line(screen, colors["white"], dtod(p8, scale).point, dtod(p4, scale).point)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_r:
                run = False

    pygame.display.update()
pygame.quit()

运行效果

运行效果示例图

实现逻辑

采用正交投影为基础,为实现透视效果,在执行3D转2D坐标转换前,将x、y坐标除以深度值(z分量),模拟近大远小的视觉效果。

优化指导

  • 修复主循环退出逻辑BUG:原代码中pygame.quit()放在while循环内部,会导致程序运行一帧就直接退出,需要移到while循环外部,等退出循环后再执行。
  • 修复Z轴旋转矩阵错误:原rz矩阵中错误使用了ty参数,替换为tz即可实现正常的绕Z轴旋转,修正后矩阵为:
    rz = np.array([[cos(tz), -sin(tz), 0.], [sin(tz), cos(tz), 0.], [0., 0., 1.]])
    
  • 避免浮点精度累积:原transform函数直接修改原始顶点坐标,多次变换后会出现浮点误差累积导致模型变形,建议存储一份原始顶点坐标,每次变换都基于原始坐标计算,不要直接修改原始值。
  • 增加除零保护:当前直接除以z分量,当z值为0时会触发除零错误,建议增加z值阈值判断,z小于极小值的点直接裁剪不渲染。
  • 优化性能:原代码中绘制每个点、每条边时都会重复实例化dtod类计算坐标,建议每帧提前计算好8个点的投影坐标存为列表,后续绘制直接复用,减少冗余计算。
  • 提升可扩展性:可以将顶点、边的连接关系存储为列表,不要硬编码8个点和12条边的绘制逻辑,后续要渲染其他3D模型时可以直接复用逻辑。

内容的提问来源于stack exchange,提问作者Samyak Marathe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 16:12:01