1
1
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
2
2
3
+ import warnings
3
4
4
5
import torch
5
6
import torch .nn as nn
13
14
from ..lighting import PointLights
14
15
from ..materials import Materials
15
16
from .shading import flat_shading , gouraud_shading , phong_shading
16
- from .texturing import interpolate_texture_map , interpolate_vertex_colors
17
17
18
18
19
19
# A Shader should take as input fragments from the output of rasterization
@@ -57,7 +57,7 @@ def forward(self, fragments, meshes, **kwargs) -> torch.Tensor:
57
57
or in the forward pass of HardPhongShader"
58
58
raise ValueError (msg )
59
59
60
- texels = interpolate_vertex_colors (fragments , meshes )
60
+ texels = meshes . sample_textures (fragments )
61
61
lights = kwargs .get ("lights" , self .lights )
62
62
materials = kwargs .get ("materials" , self .materials )
63
63
blend_params = kwargs .get ("blend_params" , self .blend_params )
@@ -104,9 +104,11 @@ def forward(self, fragments, meshes, **kwargs) -> torch.Tensor:
104
104
msg = "Cameras must be specified either at initialization \
105
105
or in the forward pass of SoftPhongShader"
106
106
raise ValueError (msg )
107
- texels = interpolate_vertex_colors (fragments , meshes )
107
+
108
+ texels = meshes .sample_textures (fragments )
108
109
lights = kwargs .get ("lights" , self .lights )
109
110
materials = kwargs .get ("materials" , self .materials )
111
+ blend_params = kwargs .get ("blend_params" , self .blend_params )
110
112
colors = phong_shading (
111
113
meshes = meshes ,
112
114
fragments = fragments ,
@@ -115,7 +117,7 @@ def forward(self, fragments, meshes, **kwargs) -> torch.Tensor:
115
117
cameras = cameras ,
116
118
materials = materials ,
117
119
)
118
- images = softmax_rgb_blend (colors , fragments , self . blend_params )
120
+ images = softmax_rgb_blend (colors , fragments , blend_params )
119
121
return images
120
122
121
123
@@ -154,6 +156,12 @@ def forward(self, fragments, meshes, **kwargs) -> torch.Tensor:
154
156
lights = kwargs .get ("lights" , self .lights )
155
157
materials = kwargs .get ("materials" , self .materials )
156
158
blend_params = kwargs .get ("blend_params" , self .blend_params )
159
+
160
+ # As Gouraud shading applies the illumination to the vertex
161
+ # colors, the interpolated pixel texture is calculated in the
162
+ # shading step. In comparison, for Phong shading, the pixel
163
+ # textures are computed first after which the illumination is
164
+ # applied.
157
165
pixel_colors = gouraud_shading (
158
166
meshes = meshes ,
159
167
fragments = fragments ,
@@ -210,54 +218,25 @@ def forward(self, fragments, meshes, **kwargs) -> torch.Tensor:
210
218
return images
211
219
212
220
213
- class TexturedSoftPhongShader (nn .Module ):
221
+ def TexturedSoftPhongShader (
222
+ device = "cpu" , cameras = None , lights = None , materials = None , blend_params = None
223
+ ):
214
224
"""
215
- Per pixel lighting applied to a texture map. First interpolate the vertex
216
- uv coordinates and sample from a texture map. Then apply the lighting model
217
- using the interpolated coords and normals for each pixel.
218
-
219
- The blending function returns the soft aggregated color using all
220
- the faces per pixel.
221
-
222
- To use the default values, simply initialize the shader with the desired
223
- device e.g.
224
-
225
- .. code-block::
226
-
227
- shader = TexturedPhongShader(device=torch.device("cuda:0"))
225
+ TexturedSoftPhongShader class has been DEPRECATED. Use SoftPhongShader instead.
226
+ Preserving TexturedSoftPhongShader as a function for backwards compatibility.
228
227
"""
229
-
230
- def __init__ (
231
- self , device = "cpu" , cameras = None , lights = None , materials = None , blend_params = None
232
- ):
233
- super ().__init__ ()
234
- self .lights = lights if lights is not None else PointLights (device = device )
235
- self .materials = (
236
- materials if materials is not None else Materials (device = device )
237
- )
238
- self .cameras = cameras
239
- self .blend_params = blend_params if blend_params is not None else BlendParams ()
240
-
241
- def forward (self , fragments , meshes , ** kwargs ) -> torch .Tensor :
242
- cameras = kwargs .get ("cameras" , self .cameras )
243
- if cameras is None :
244
- msg = "Cameras must be specified either at initialization \
245
- or in the forward pass of TexturedSoftPhongShader"
246
- raise ValueError (msg )
247
- texels = interpolate_texture_map (fragments , meshes )
248
- lights = kwargs .get ("lights" , self .lights )
249
- materials = kwargs .get ("materials" , self .materials )
250
- blend_params = kwargs .get ("blend_params" , self .blend_params )
251
- colors = phong_shading (
252
- meshes = meshes ,
253
- fragments = fragments ,
254
- texels = texels ,
255
- lights = lights ,
256
- cameras = cameras ,
257
- materials = materials ,
258
- )
259
- images = softmax_rgb_blend (colors , fragments , blend_params )
260
- return images
228
+ warnings .warn (
229
+ """TexturedSoftPhongShader is now deprecated;
230
+ use SoftPhongShader instead.""" ,
231
+ PendingDeprecationWarning ,
232
+ )
233
+ return SoftPhongShader (
234
+ device = device ,
235
+ cameras = cameras ,
236
+ lights = lights ,
237
+ materials = materials ,
238
+ blend_params = blend_params ,
239
+ )
261
240
262
241
263
242
class HardFlatShader (nn .Module ):
@@ -291,7 +270,7 @@ def forward(self, fragments, meshes, **kwargs) -> torch.Tensor:
291
270
msg = "Cameras must be specified either at initialization \
292
271
or in the forward pass of HardFlatShader"
293
272
raise ValueError (msg )
294
- texels = interpolate_vertex_colors (fragments , meshes )
273
+ texels = meshes . sample_textures (fragments )
295
274
lights = kwargs .get ("lights" , self .lights )
296
275
materials = kwargs .get ("materials" , self .materials )
297
276
blend_params = kwargs .get ("blend_params" , self .blend_params )
0 commit comments