Skip to content

Commit 0e85652

Browse files
bottlerfacebook-github-bot
authored andcommitted
ambient lighting
Summary: Specific object to represent light which is 100% everywhere. Sometimes lighting is irrelevant, for example when viewing a mesh which has lighting already baked in. This is not primarily aiming for a performance win but I think the test which has changed might be a bit faster. Reviewed By: theschnitz Differential Revision: D26405151 fbshipit-source-id: 82eae55de0bee918548a3eaf031b002cb95e726c
1 parent 61e38de commit 0e85652

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

pytorch3d/renderer/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
ray_bundle_to_ray_points,
3535
ray_bundle_variables_to_ray_points,
3636
)
37-
from .lighting import DirectionalLights, PointLights, diffuse, specular
37+
from .lighting import AmbientLights, DirectionalLights, PointLights, diffuse, specular
3838
from .materials import Materials
3939
from .mesh import (
4040
HardFlatShader,

pytorch3d/renderer/lighting.py

+38-2
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def __init__(
158158
diffuse_color=((0.3, 0.3, 0.3),),
159159
specular_color=((0.2, 0.2, 0.2),),
160160
direction=((0, 1, 0),),
161-
device: str = "cpu",
161+
device="cpu",
162162
):
163163
"""
164164
Args:
@@ -219,7 +219,7 @@ def __init__(
219219
diffuse_color=((0.3, 0.3, 0.3),),
220220
specular_color=((0.2, 0.2, 0.2),),
221221
location=((0, 1, 0),),
222-
device: str = "cpu",
222+
device="cpu",
223223
):
224224
"""
225225
Args:
@@ -268,6 +268,42 @@ def specular(self, normals, points, camera_position, shininess) -> torch.Tensor:
268268
)
269269

270270

271+
class AmbientLights(TensorProperties):
272+
"""
273+
A light object representing the same color of light everywhere.
274+
By default, this is white, which effectively means lighting is
275+
not used in rendering.
276+
"""
277+
278+
def __init__(self, *, ambient_color=None, device="cpu"):
279+
"""
280+
If ambient_color is provided, it should be a sequence of
281+
triples of floats.
282+
283+
Args:
284+
ambient_color: RGB color
285+
device: torch.device on which the tensors should be located
286+
287+
The ambient_color if provided, should be
288+
- 3 element tuple/list or list of lists
289+
- torch tensor of shape (1, 3)
290+
- torch tensor of shape (N, 3)
291+
"""
292+
if ambient_color is None:
293+
ambient_color = ((1.0, 1.0, 1.0),)
294+
super().__init__(ambient_color=ambient_color, device=device)
295+
296+
def clone(self):
297+
other = self.__class__(device=self.device)
298+
return super().clone(other)
299+
300+
def diffuse(self, normals, points) -> torch.Tensor:
301+
return torch.zeros_like(points)
302+
303+
def specular(self, normals, points, camera_position, shininess) -> torch.Tensor:
304+
return torch.zeros_like(points)
305+
306+
271307
def _validate_light_properties(obj):
272308
props = ("ambient_color", "diffuse_color", "specular_color")
273309
for n in props:

tests/test_render_meshes.py

+4-19
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
PerspectiveCameras,
2525
look_at_view_transform,
2626
)
27-
from pytorch3d.renderer.lighting import PointLights
27+
from pytorch3d.renderer.lighting import AmbientLights, PointLights
2828
from pytorch3d.renderer.materials import Materials
2929
from pytorch3d.renderer.mesh import TexturesAtlas, TexturesUV, TexturesVertex
3030
from pytorch3d.renderer.mesh.rasterizer import MeshRasterizer, RasterizationSettings
@@ -626,12 +626,7 @@ def test_join_uvs(self):
626626
image_size=256, blur_radius=0.0, faces_per_pixel=1
627627
)
628628

629-
lights = PointLights(
630-
device=device,
631-
ambient_color=((1.0, 1.0, 1.0),),
632-
diffuse_color=((0.0, 0.0, 0.0),),
633-
specular_color=((0.0, 0.0, 0.0),),
634-
)
629+
lights = AmbientLights(device=device)
635630
blend_params = BlendParams(
636631
sigma=1e-1,
637632
gamma=1e-4,
@@ -780,12 +775,7 @@ def test_join_verts(self):
780775
image_size=256, blur_radius=0.0, faces_per_pixel=1
781776
)
782777

783-
lights = PointLights(
784-
device=device,
785-
ambient_color=((1.0, 1.0, 1.0),),
786-
diffuse_color=((0.0, 0.0, 0.0),),
787-
specular_color=((0.0, 0.0, 0.0),),
788-
)
778+
lights = AmbientLights(device=device)
789779
blend_params = BlendParams(
790780
sigma=1e-1,
791781
gamma=1e-4,
@@ -863,12 +853,7 @@ def test_join_atlas(self):
863853
perspective_correct=False,
864854
)
865855

866-
lights = PointLights(
867-
device=device,
868-
ambient_color=((1.0, 1.0, 1.0),),
869-
diffuse_color=((0.0, 0.0, 0.0),),
870-
specular_color=((0.0, 0.0, 0.0),),
871-
)
856+
lights = AmbientLights(device=device)
872857
blend_params = BlendParams(
873858
sigma=1e-1,
874859
gamma=1e-4,

0 commit comments

Comments
 (0)