-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_random_sphere.py
81 lines (63 loc) · 2.46 KB
/
generate_random_sphere.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import argparse
from pathlib import Path
import numpy as np
import torch
from pytorch3d.renderer import (FoVOrthographicCameras, MeshRasterizer,
MeshRenderer, RasterizationSettings,
SoftPhongShader, Textures,
look_at_view_transform)
from pytorch3d.renderer.blending import BlendParams
from pytorch3d.structures import Meshes
from pytorch3d.utils import ico_sphere
from sh_lights import SphericalHarmonicsLights
from utils import mkdir, save_image
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--save_folder", default="results")
args = parser.parse_args()
IMAGE_SIZE = 128
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
mesh = ico_sphere(3, device=device)
verts, faces = mesh.get_mesh_verts_faces(0)
textures = Textures(verts_rgb=[torch.ones_like(verts)])
meshes = Meshes(verts=[verts], faces=[faces], textures=textures)
print(f"vertices: {meshes.verts_list()[0].shape}")
print(f"faces: {meshes.faces_list()[0].shape}")
# Render the mesh
## Create Camera
R, T = look_at_view_transform(dist=2.7, elev=0, azim=0)
cameras = FoVOrthographicCameras(device=device, R=R, T=T)
sh_params = torch.rand(1, 9, 3, device=device)
print(f"sh_params: {sh_params[0].shape}")
## Create Light
lights = SphericalHarmonicsLights(device=device, sh_params=sh_params)
## Setup render
rasterize_settings = RasterizationSettings(
image_size=IMAGE_SIZE,
blur_radius=0.0,
faces_per_pixel=1,
)
renderer = MeshRenderer(
rasterizer=MeshRasterizer(
cameras=cameras,
raster_settings=rasterize_settings,
),
shader=SoftPhongShader(
device=device,
cameras=cameras,
lights=lights,
blend_params=BlendParams(background_color=(0., 0., 0.))
),
)
result = renderer(meshes.extend(len(cameras)))
result = np.clip(result.cpu().numpy(), 0, 1)
result = np.concatenate(result, axis=1)
result, alpha = result[..., :-1], result[..., -1:]
alpha = np.repeat(alpha, 3, axis=-1)
vis = np.concatenate([result, alpha], axis=1)
vis = (vis * 255).astype(np.uint8)
save_folder = mkdir(args.save_folder)
save_image(save_folder / "sphere_random.jpg", vis)
print("Save results to", save_folder / "sphere_random.jpg")
if __name__ == "__main__":
main()