跳到正文
DEV Community· Sharavana Kumar C K·· 5 小時前AI 評分47

用 Python(無需 GPU)從球體建立 3D 角色,再將其轉成數字

Build a 3D Character from Spheres, Then Turn It into Numbers (Python, No GPU)

AI 導讀

這篇入門教學示範如何在 Google Colab 用 Python 從球體建立 3D 角色,再轉成神經網絡可學習的 voxel grid。示例約用 40 行程式碼,將空間劃分為網格並標記填滿或留空,亦可比較 8、16、32 的解析度。

正文

"A beginner-friendly intro to how 3D AI sees shapes: voxel grids, in about 40 lines of Python in Google Colab."

FROM ZERO TO FIRST Training a 3D Generator Without a PhD

3D generative AI looks like it needs a PhD. But the first step is surprisingly small: AI doesn't see shapes, it sees numbers.

Let's prove it. In free Google Colab (no install, no GPU), we'll build a simple character from spheres and then turn it into a voxel grid, the kind of array a neural network can learn from.

Step 1: A character made of spheres

import numpy as np
import matplotlib.pyplot as plt

# (name, center, radius, color)
parts = [
    ("body",       ( 0.0, 0, 0.0), 1.0, "tomato"),
    ("head",       ( 0.0, 0, 1.4), 0.6, "gold"),
    ("left hand",  (-1.2, 0, 0.2), 0.3, "gold"),
    ("right hand", ( 1.2, 0, 0.2), 0.3, "gold"),
]

def sphere(center, r, n=30):
    u, v = np.mgrid[0:2*np.pi:n*1j, 0:np.pi:n*1j]
    x = center[0] + r * np.cos(u) * np.sin(v)
    y = center[1] + r * np.sin(u) * np.sin(v)
    z = center[2] + r * np.cos(v)
    return x, y, z

fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(projection="3d")
for name, center, r, color in parts:
    ax.plot_surface(*sphere(center, r), color=color)
ax.set_xlim(-2, 2); ax.set_ylim(-2, 2); ax.set_zlim(-2, 2)
ax.set_axis_off()
plt.show()

You should see a round tomato body with a gold head and two hands. Change a radius or a color and run it again. That's how you start to feel what the numbers control.

Step 2: Turn it into a voxel grid

A voxel is a 3D pixel. We split space into a grid of tiny cubes and mark each one as filled (inside any sphere) or empty.

def voxelize(parts, R=16):
    axis = np.linspace(-2, 2, R)
    X, Y, Z = np.meshgrid(axis, axis, axis, indexing="ij")
    grid = np.zeros((R, R, R), dtype=bool)
    for _, (cx, cy, cz), r, _ in parts:
        grid |= (X - cx)**2 + (Y - cy)**2 + (Z - cz)**2 <= r**2
    return grid

for R in (8, 16, 32):
    g = voxelize(parts, R)
    print(f"{R}x{R}x{R} grid -> {g.sum()} filled cells")

Your character is now a tensor of True/False values. Higher resolution means a smoother shape and more numbers to store. This is the format models learn from.

Step 3: See it

g = voxelize(parts, 16)
ax = plt.figure(figsize=(5, 5)).add_subplot(projection="3d")
ax.voxels(g, facecolors="tomato", edgecolor="k", linewidth=0.2)
ax.set_axis_off()
plt.show()

Try R = 8, 16 and 32 and compare. You'll see why resolution matters.

Where this goes next

Once a shape is an array, the usual machine learning toolbox applies:

  1. Signed distance fields: store the distance to the surface instead of just filled or empty.
  2. Training: a loop that guesses, measures the error (the loss) and adjusts.
  3. Diffusion: add noise to data step by step, then train a model to remove it. Start from pure noise and it can generate new shapes.

I cover this whole path, from the first sphere to training a tiny diffusion-based 3D character generator, in a book I wrote for beginners: From Zero to First Character: Training a 3D Generator Without a PhD. Everything runs in Google Colab.

Disclosure: I'm the author. https://www.amazon.in/dp/B0HSKF3DVM

What would you like to see next: signed distance fields, the training loop or diffusion? Tell me in the comments.

來源:DEV Community · dev.to