| 57 | |
| 58 | |
| 59 | class FixedDirectionLight(torch.nn.Module): |
| 60 | def __init__(self, direction, amb, diff): |
| 61 | super(FixedDirectionLight, self).__init__() |
| 62 | self.light_dir = direction |
| 63 | self.amb = amb |
| 64 | self.diff = diff |
| 65 | self.is_hacking = not (isinstance(self.amb, float) |
| 66 | or isinstance(self.amb, int)) |
| 67 | |
| 68 | def forward(self, feat): |
| 69 | batch_size = feat.shape[0] |
| 70 | if self.is_hacking: |
| 71 | return torch.concat([self.light_dir, self.amb, self.diff], -1) |
| 72 | else: |
| 73 | return torch.concat([self.light_dir, torch.FloatTensor([self.amb, self.diff]).to(self.light_dir.device)], -1).expand(batch_size, -1) |
| 74 | |
| 75 | def shade(self, feat, kd, normal): |
| 76 | light_params = self.forward(feat) |
| 77 | light_dir = light_params[..., :3][:, None, None, :] |
| 78 | int_amb = light_params[..., 3:4][:, None, None, :] |
| 79 | int_diff = light_params[..., 4:5][:, None, None, :] |
| 80 | shading = (int_amb + int_diff * |
| 81 | torch.clamp(util.dot(light_dir, normal), min=0.0)) |
| 82 | shaded = shading * kd |
| 83 | return shaded, shading |
| 84 | |
| 85 | |
| 86 | def interpolate(attr, rast, attr_idx, rast_db=None): |