fury-gl / fury

FURY - Free Unified Rendering in pYthon.
https://fury.gl
Other
226 stars 165 forks source link

Render a video on a Cube #322 #862

Open robinroy03 opened 4 months ago

robinroy03 commented 4 months ago

Render a video on a cube (#322)

This PR adds a new actor - texture_on_cube to fury/actor.py which takes 6 different texture arguments for the different sides of a cube. It also adds the tutorial and tests for the same.

How it is done

I took 6 planes and merged them to form a cube.

How is video rendered

Video is rendered by changing the texture repeatedly.

Simple static texture demo:

from fury import actor, window
import numpy as np

arr_0 = np.full((720, 1280), 0, dtype=np.uint8)
arr_255 = np.full((720, 1280), 255, dtype=np.uint8)

arr_red = np.dstack((arr_255, arr_0, arr_0))
arr_white = np.full((720, 1280, 3), 255, dtype=np.uint8)
arr_aqua = np.dstack((arr_0, arr_255, arr_255))
arr_green = np.dstack((arr_0, arr_255, arr_0))
arr_blue = np.dstack((arr_0, arr_0, arr_255))
arr_yellow = np.dstack((arr_255, arr_255, arr_0))

cube = actor.texture_on_cube(arr_white, arr_red, arr_green, arr_blue, arr_yellow, arr_aqua)
cube2 = actor.texture_on_cube(arr_white, arr_red, arr_green, arr_blue, arr_yellow, arr_aqua, (3, 3, 3))

scene = window.Scene()
scene.add(*cube)
scene.add(*cube2)

window.show(scene, size=(1280, 720))

https://github.com/fury-gl/fury/assets/115863770/2dd11e67-38f2-4fcd-84a9-a2af09aa1b81

pep8speaks commented 4 months ago

Hello @robinroy03, Thank you for updating!

Cheers! There are no style issues detected in this Pull Request. :beers: To test for issues locally, pip install flake8 and then run flake8 fury.

Comment last updated at 2024-05-01 10:27:17 UTC
skoudoro commented 4 months ago

Hi @robinroy03,

Thank you for this, I will try tomorrow and give you an update

skoudoro commented 4 months ago

during this time, can you fix all the pep8 issues?

robinroy03 commented 4 months ago

@skoudoro I've fixed all the pep8 issues and have placed some of my thoughts in the code as comments. (and also in the PR description) I'll add tests and finish this once you're ok with them.

robinroy03 commented 4 months ago

Hi @skoudoro,

Are there any inbuilt VTK functions that do this? (mapping different textures to different sides of a cube) Because when I searched I found this (https://discourse.vtk.org/t/how-to-apply-different-textures-to-each-face-of-a-tetrahedron/2333/2), and I'm not sure if I should go this path directly.

Are there any resources for UV-Mapping textures in VTK? (I referred both VTKUsersGuide and VTKTextbook, found no examples)

robinroy03 commented 4 months ago

I found this fury/io/load_cubemap_texture function and used it. But I am unable to map the cubemap to the cube. (the below code does not work).

import vtk

from fury import actor, window
from fury.io import load_cubemap_texture

tx = load_cubemap_texture(fnames = [
    "negx.jpg",
    "negy.jpg",
    "negz.jpg",
    "posx.jpg",
    "posy.jpg",
    "posz.jpg"
])

scene = window.Scene()

cubeSource = vtk.vtkCubeSource()

mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(cubeSource.GetOutputPort())

cube_actor = vtk.vtkActor()
cube_actor.SetTexture(tx)
cube_actor.SetMapper(mapper)

scene.add(cube_actor)
window.show(scene)

Can I get some help regarding the general direction?

Or should I take 6 PlaneSources and merge them?

robinroy03 commented 4 months ago

@skoudoro,

Please check the new commit, I've used 6 planes to make the cube. I can use vtkAssembly() to make them 1 single actor if needed.

Use a cubemap texture from here to test.

If this is ok, I'll finish this code with some more methods such as texture_update, get_actor etc ... (or should I format the code differently? I believe this class should go inside actor.py)

Demo Video:

https://github.com/fury-gl/fury/assets/115863770/75f43bd2-c81b-4b60-8f53-c21ef9ae1b5b

This code is not rendering a video, but it'll be done in texture_update. I just wanted to know whether my general direction is correct & feedback about how I'm organizing the code.

codecov[bot] commented 4 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 84.46%. Comparing base (14851c6) to head (b26842a).

Additional details and impacted files [![Impacted file tree graph](https://app.codecov.io/gh/fury-gl/fury/pull/862/graphs/tree.svg?width=650&height=150&src=pr&token=wrshJ6dyDs&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fury-gl)](https://app.codecov.io/gh/fury-gl/fury/pull/862?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fury-gl) ```diff @@ Coverage Diff @@ ## master #862 +/- ## ========================================== + Coverage 84.43% 84.46% +0.03% ========================================== Files 44 44 Lines 10537 10559 +22 Branches 1423 1432 +9 ========================================== + Hits 8897 8919 +22 Misses 1266 1266 Partials 374 374 ``` | [Files](https://app.codecov.io/gh/fury-gl/fury/pull/862?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fury-gl) | Coverage Δ | | |---|---|---| | [fury/actor.py](https://app.codecov.io/gh/fury-gl/fury/pull/862?src=pr&el=tree&filepath=fury%2Factor.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fury-gl#diff-ZnVyeS9hY3Rvci5weQ==) | `85.85% <100.00%> (+0.25%)` | :arrow_up: | | [fury/lib.py](https://app.codecov.io/gh/fury-gl/fury/pull/862?src=pr&el=tree&filepath=fury%2Flib.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fury-gl#diff-ZnVyeS9saWIucHk=) | `100.00% <100.00%> (ø)` | |
robinroy03 commented 3 months ago

@skoudoro I've finished everything. Please take a look and let me know if I've to make modifications.

robinroy03 commented 3 months ago

I've made the changes. For test_actors, I only did a test for 1 side colour render. I avoided remaining because it all comes from the same pipeline, so must be similar.

robinroy03 commented 3 months ago

@skoudoro, I'm sorry but how do I render it locally? Every time I try I get a segf (it's an open issue #481).

I did create docs (by adding the disable examples flag), but my new code is not shown there. What is the procedure to add my code to the docs?

skoudoro commented 3 months ago

What is the procedure to add my code to the docs?

you need to add it in docs/examples/_valid_examples.toml

Every time I try I get a segf

for now, run it multiple times consecutively

robinroy03 commented 3 months ago

All the said modifications are done. I also committed the new file _valid_examples.toml.

I think this is a bug, but viz_play_cube.png will be all black unless I turn interactive = True. It takes a screenshot immediately, even before the actors are rendered. But if we give it some time after turning interactivity on, it works fine (since the actors are rendered). I also noticed these issues with window.snapshot where they take screenshots very immediately even before some actors are rendered.

robinroy03 commented 2 months ago

Hi @skoudoro, PTAL.

Thank you.

robinroy03 commented 2 months ago

@skoudoro,

All the requested changes are made. Should I modify the function to be placed inside the tutorial? We'd discussed using 6 different actors to render the cube multiple times.

skoudoro commented 2 months ago

All the requested changes are made.

Thanks!

Should I modify the function to be placed inside the tutorial?

I do not know yet

We'd discussed using 6 different actors to render the cube multiple times.

I know... the fact that we talk about it multiple time show that I am not completely satisfy with this solution because I can see long term issue with it. I have hard time to block time to dig in the texture actor to see if there are alternatives or something that we are missing.

robinroy03 commented 2 months ago

ok, I'll also keep researching whether there are better ways to do this.