pmh47 / dirt

DIRT: a fast differentiable renderer for TensorFlow
MIT License
313 stars 63 forks source link

Adding transparency support? #21

Closed gh2k closed 5 years ago

gh2k commented 5 years ago

I'd like to render a stack of transparent shapes, and I'm wondering how easy this would be to achieve.

Currently, I'm rendering shape-by-shape and then alpha-compositing them in separate tensorflow operations, but this is slower than I'd like and I'm finding I'm debugging a number of problems where tf is trying to move things back and forth between the CPU and GPU.

I had a go at hacking together support, but I couldn't get it working. I don't really know opengl programming, so that is probably part of the problem.

Here's what I tried:

This still draws the triangles that I expect in the colours that I expect, but they are still opaque, not transparent. (The triangles I'm feeding in have random numbers for their vertex colour alpha components.)

Any ideas what else I might need to change to make this work?

[edit]: I was incorrect about the triangle colours:

If I pass an alpha of <1, the triangles are rendered in a lighter colour. So:

gh2k commented 5 years ago

Ah, never mind I got it working. I think it was the depth buffer getting in the way, so I disabled it.

I'll tidy this up and make a PR.

Frank-Dz commented 4 years ago

Ah, never mind I got it working. I think it was the depth buffer getting in the way, so I disabled it.

I'll tidy this up and make a PR.

Hi~ May I ask you how to render a picture with a transparent background? All my results are like: image

I know I should adjust the background_attributes, but I do not know how to set it.

pixels = dirt.rasterise_deferred(
        vertices=cube_vertices_clip,
        vertex_attributes=tf.concat([
            tf.ones_like(cube_vertices_object[:, :1]),  # mask
            cube_uvs,  # texture coordinates
            cube_normals_world  # normals
        ], axis=1),
        faces=cube_faces,
        background_attributes=(tf.ones([frame_height, frame_width, 6])*255),
        shader_fn=shader_fn,
        shader_additional_inputs=[texture, light_direction]
    )

Thanks for any help and guidance! Best, Frank