引言
在计算机图形学中,3D效果的2D渲染是一个迷人的领域。今天,我将分享一个使用Python和Pygame库创建的粉色粒子爱心效果。这个项目不仅视觉效果惊艳,而且代码简洁易懂,非常适合图形编程初学者学习3D渲染的基础概念。
项目概述
这个程序创建了一个由5000个粉色粒子组成的3D爱心,在黑色背景中优雅旋转。粒子会根据其深度改变大小和亮度,创造出真实的3D透视效果。
技术实现
1. 初始化设置
import pygame
import math
import random
from pygame.locals import *# 初始化pygame
pygame.init()# 设置窗口
WIDTH, HEIGHT = 550, 400
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("粉色花生米")# 颜色定义
BLACK = (0, 0, 0)
PINK = (255, 182, 193) # 粉红色
LIGHT_PINK = (255, 209, 220) # 浅粉色
2. 粒子类设计
粒子类是项目的核心,负责每个粒子的3D位置、颜色和渲染:
class Particle:def __init__(self):# 使用球坐标生成爱心形状的点theta = random.uniform(0, math.pi)phi = random.uniform(0, 2 * math.pi)# 爱心形状的球坐标转换r = self.heart_shape(theta, phi)# 转换为3D笛卡尔坐标self.x = r * math.sin(theta) * math.cos(phi)self.y = r * math.sin(theta) * math.sin(phi)self.z = r * math.cos(theta)# 添加随机扰动使粒子分布更自然self.x += random.uniform(-0.3, 0.3)self.y += random.uniform(-0.3, 0.3)self.z += random.uniform(-0.3, 0.3)# 粒子属性self.base_color = random.choice([PINK, LIGHT_PINK])self.base_size = random.uniform(0.5, 1.5)self.angle_x = 0self.angle_y = 0self.angle_z = 0
3. 爱心形状算法
爱心形状是通过球坐标转换实现的数学魔法:
def heart_shape(self, theta, phi):"""将球坐标转换为爱心形状"""t = theta * 2 # 映射到0-2πheart_factor = (13*math.cos(t) - 5*math.cos(2*t) - 2*math.cos(3*t) - math.cos(4*t)) / 13# 转换为球体形状r = 10 * (0.8 + 0.2 * heart_factor)return r
这个公式基于心形线的极坐标方程,经过调整后适合在3D空间中使用。
4. 3D旋转与投影
粒子在3D空间中的旋转和2D投影是创造3D效果的关键:
def update(self):# 旋转角度增量self.angle_x += 0.002self.angle_y += 0.003self.angle_z += 0.001# 3D旋转 - 绕x、y、z轴旋转y_rot = self.y * math.cos(self.angle_x) - self.z * math.sin(self.angle_x)z_rot = self.y * math.sin(self.angle_x) + self.z * math.cos(self.angle_x)x_rot = self.x * math.cos(self.angle_y) + z_rot * math.sin(self.angle_y)z_rot = -self.x * math.sin(self.angle_y) + z_rot * math.cos(self.angle_y)x_final = x_rot * math.cos(self.angle_z) - y_rot * math.sin(self.angle_z)y_final = x_rot * math.sin(self.angle_z) + y_rot * math.cos(self.angle_z)# 透视投影scale = 15distance = 10# 计算2D坐标x_proj = WIDTH//2 + int(x_final * scale)y_proj = HEIGHT//2 - int(y_final * scale)# 根据深度调整大小和颜色depth_factor = (z_rot + distance) / (2 * distance)self.curr_size = max(1, int(self.base_size * depth_factor))color_factor = min(1.0, max(0.4, depth_factor * 1.3))r = min(255, max(0, int(self.base_color[0] * color_factor)))g = min(255, max(0, int(self.base_color[1] * color_factor)))b = min(255, max(0, int(self.base_color[2] * color_factor)))self.curr_color = (r, g, b)return x_proj, y_proj
5. 主循环与渲染
# 创建5000个粒子
particles = [Particle() for _ in range(5000)]# 主循环
clock = pygame.time.Clock()
running = Truewhile running:for event in pygame.event.get():if event.type == QUIT:running = False# 清屏并添加淡出效果screen.fill(BLACK)fade_surface = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)fade_surface.fill((0, 0, 0, 15)) # 淡出拖尾效果screen.blit(fade_surface, (0, 0))# 绘制所有粒子for particle in particles:particle.draw(screen)# 添加文字font = pygame.font.SysFont('微软雅黑', 30)text = font.render("particles", True, (255, 255, 255))screen.blit(text, (WIDTH//2 - text.get_width()//2, 30))pygame.display.flip()clock.tick(60)pygame.quit()
效果特点
-
3D透视:粒子根据深度改变大小和亮度,远处的粒子更小更暗
-
平滑动画:三轴不同速度的旋转创造出复杂的运动轨迹
-
淡出拖尾:通过半透明覆盖层实现粒子轨迹的淡出效果
-
自然分布:随机扰动使粒子分布更加自然,避免机械感
扩展思路
这个基础项目可以进一步扩展:
-
交互功能:添加鼠标交互,让爱心跟随鼠标或响应点击
-
颜色渐变:实现粒子颜色的动态变化
-
粒子系统:添加发射器,创建粒子流动效果
-
多爱心组合:渲染多个不同大小和旋转速度的爱心
结语
这个粉色粒子爱心项目展示了如何使用简单的数学原理和基础的图形编程技术创造出令人惊艳的视觉效果。通过调整参数,你可以创建出各种不同的3D形状和动画效果。希望这个项目能激发你对计算机图形学的兴趣!
完整代码已在文章开头提供,复制到Python环境中即可运行(需安装pygame库)。尝试修改参数,创造属于你自己的粒子艺术吧!
小提示:运行代码前请确保已安装pygame库,可以通过
pip install pygame
安装。