MCPcopy Create free account
hub / github.com/NVIDIAGameWorks/Falcor / compute_normals

Method compute_normals

scripts/inv-rendering/mesh_utils.py:110–137  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

108 # From nvdiffrec.
109 # Compute smooth vertex normals.
110 def compute_normals(self):
111 idx = [
112 self.tri_idx[:, 0].type(torch.int64),
113 self.tri_idx[:, 1].type(torch.int64),
114 self.tri_idx[:, 2].type(torch.int64),
115 ]
116 pos = [self.v_pos[idx[0], :], self.v_pos[idx[1], :], self.v_pos[idx[2], :]]
117 face_normals = torch.cross(pos[1] - pos[0], pos[2] - pos[0])
118
119 # Splat face normals to vertices.
120 v_normals = torch.zeros_like(self.v_pos)
121 v_normals.scatter_add_(0, idx[0][:, None].repeat(1, 3), face_normals)
122 v_normals.scatter_add_(0, idx[1][:, None].repeat(1, 3), face_normals)
123 v_normals.scatter_add_(0, idx[2][:, None].repeat(1, 3), face_normals)
124
125 # Normalize, replace zero (degenerated) normals with some default value.
126 v_normals = torch.where(
127 length(v_normals) > EPS,
128 v_normals,
129 torch.tensor([0.0, 0.0, 1.0], dtype=torch.float32, device="cuda"),
130 )
131 v_normals = normalize_safe(v_normals)
132
133 if torch.is_anomaly_enabled():
134 assert torch.all(torch.isfinite(v_normals))
135
136 self.v_norm = v_normals
137 return v_normals
138
139 # From nvdiffrec.
140 # Compute tangent space from texture map coordinates.

Callers 1

compute_shading_frameMethod · 0.95

Calls 2

normalize_safeFunction · 0.85
lengthFunction · 0.70

Tested by

no test coverage detected