Code Monkey home page Code Monkey logo

Comments (4)

nikhilaravi avatar nikhilaravi commented on July 4, 2024 4

@Jiayi-Wang-MPI The phong_renderer only uses 1 face for each pixel and applies hard blending i.e uses the only the color of the closest face for each pixel. If you look at the raster_settings you can see the option faces_per_pixel is set to 1. You'll need to make the following changes.

  1. Set the rasterization settings to the same values as for the SilhouetteShader
raster_settings = RasterizationSettings(
    image_size=256, 
    blur_radius=np.log(1. / 1e-4 - 1.) * blend_params.sigma, 
    faces_per_pixel=100, 
    bin_size=0
)
  1. Define a new Phong shader which uses the softmax_rgb_blend function:
class SoftPhongShader(nn.Module):
    def __init__(
        self,
        device="cpu",
        cameras=None,
        lights=None,
        materials=None,
        blend_params=None,
    ):
        super().__init__()
        self.lights = (
            lights if lights is not None else PointLights(device=device)
        )
        self.materials = (
            materials if materials is not None else Materials(device=device)
        )
        self.cameras = (
            cameras
            if cameras is not None
            else OpenGLPerspectiveCameras(device=device)
        )
        self.blend_params = (
            blend_params if blend_params is not None else BlendParams()
        )

    def forward(self, fragments, meshes, **kwargs) -> torch.Tensor:
        texels = interpolate_vertex_colors(fragments, meshes)
        cameras = kwargs.get("cameras", self.cameras)
        lights = kwargs.get("lights", self.lights)
        materials = kwargs.get("materials", self.materials)
        colors = phong_shading(
            meshes=meshes,
            fragments=fragments,
            texels=texels,
            lights=lights,
            cameras=cameras,
            materials=materials,
        )
        images = softmax_rgb_blend(colors, fragments, self.blend_params)
        return images
  1. Initialize the phong_renderer as follows:
phong_renderer = MeshRenderer(
    rasterizer=MeshRasterizer(
        cameras=cameras, 
        raster_settings=raster_settings
    ),
    shader= SoftPhongShader(device=device, lights=lights, blend_params=blend_params)
)

We are making some updates to the Shading API so the above renderer will be available soon to import directly.

from pytorch3d.

Jiayi-Wang-MPI avatar Jiayi-Wang-MPI commented on July 4, 2024 1

@nikhilaravi the new SoftPhongShader works for me. Thanks!

from pytorch3d.

nikhilaravi avatar nikhilaravi commented on July 4, 2024

@Jiayi-Wang-MPI the SoftPhongShader is now available to import directly from pytorch3d so you don't need to define it yourself.

from pytorch3d.

nikhilaravi avatar nikhilaravi commented on July 4, 2024

@Jiayi-Wang-MPI were you able to get this working with the new SoftPhongShader or the code above? Can I close this issue?

from pytorch3d.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.