How I Created a Voxel Raycaster in Python...

Поділитися
Вставка
  • Опубліковано 26 гру 2024

КОМЕНТАРІ • 74

  • @dungeonrobot
    @dungeonrobot 5 місяців тому +133

    For performance you should rewrite it in Rust and then hand $5,000 in cash to me immediately. This will surely improve your FPS

    • @lukasjetu9776
      @lukasjetu9776 5 місяців тому +4

      hey, i want some too

    • @dungeonrobot
      @dungeonrobot 5 місяців тому

      @@lukasjetu9776 Always happy to lend some performance. I’ll cut you a sweet deal too: $3,000

    • @JJoe1
      @JJoe1 5 місяців тому

      after making it in rust do it again in C++ as it is just a bit better (le joke)

  • @Bennet2391
    @Bennet2391 3 місяці тому +6

    - march along the ray not from left to right (fill columns first, helps with caching)
    - use numba with forced inlining to improve the speed
    - use a non linear LOD function
    - use a custom array accessor for numpy arrays (unsafe but faster)
    What i did for my python voxel engine is:
    - used numbas prange() which is parallel processing. (This is very simple)
    - rewrote it in nim with sdl2 and it was still 10x faster than the optimized python version.
    - currently in the process of using compute shaders to speed it up (grahpics card => parallel processing)
    if you get some headroom from optimizations, i would suggest adding a bilinear filter for the colormap and heightmap, which impoves the visual quality alot.

  • @Lexxaro
    @Lexxaro 5 місяців тому +7

    Some Ideas on optimizing:
    -Profile your code! If you don't know what's actually slowing down your program you'll probably spend huge amounts of time just optimizing small things that aren't actually the bottleneck of your program. And in the worst case you'll even slow down parts of your program.
    -Make sure your using library calls correctly. My personal experience with python graphics is very limited, but it could be that the calls to functions drawing your image and presenting it are taking much longer then expected. In graphics programming generally when rendering the image try to keep calculation of the image and presentation seperate. Hand the rendering API only the finished image as memory operations are much more expensive than math operations.
    -And lastly: If you're actually trying to get comparable speeds, you will need to use a faster language, like c/c++/rust. It may seem daunting at first, but in the end they are also just programming languages and thus more similar to python than not. Your rendering code for example could look nearly identical. And by using all the high level features provided by the standard libraries of these languages you don't have to be some kind of wizard to use them.
    Well I'm interested to see what you'll code in the future!

  • @ThankYouESM
    @ThankYouESM 5 місяців тому +17

    Cache Combine Distant Voxels: when more than one voxel looks like a single pixel.

  • @monke2220
    @monke2220 2 місяці тому +1

    great video as always

  • @yovo7499
    @yovo7499 4 місяці тому +4

    bro was like 'refrain from mentioning parallel processing' but since you can separate and calculate all the verical lines independently of each other you can just use numba in parallel for one of the loops. You have to reorder your z and x loops though, so that you go first through each vertical line and then for it you go through each z distance. For the same resolution that you set and with render distance of 2000 I got like 700-800fps...

    • @shifteleven
      @shifteleven 4 місяці тому +1

      Is there source for this code he and you did?

    • @quedemontage8094
      @quedemontage8094 2 місяці тому

      Hi, sorry to bother you, but what you suggest seem really interesting but I didn't really get the point on the fact of calculating vertical line, what is it about ?

  • @FinFET
    @FinFET 5 місяців тому +6

    Pretty coool, I would suggest using numba to speed things up

  • @ULTRACREATIVEGRAPHICS
    @ULTRACREATIVEGRAPHICS 5 місяців тому +2

    1000 subscribers! Gratz!

  • @poptre67
    @poptre67 5 місяців тому +26

    i think you could optimize it by using a different coding language and using parallel processing

  • @The-python-guy
    @The-python-guy 5 місяців тому +8

    Dude I had a choice between 2 amazing youtubers and well looking I noticed you, ofc I have to click God bless your sorce code on a textured raycaster helped me so much. funny enough I've used that exact same voxel pixel renderer that your showing on the screen right now to make a doom clone deathmatch shooter that me and my bother love Amen man wish you well

  • @coleslater1419
    @coleslater1419 5 місяців тому +2

    this reminds me of a spoonkid video, this is wonderful

  • @gamera4286
    @gamera4286 Місяць тому

    _group voxels into chunks and raycast the chunks
    -frustum culling
    -sparse voxel octree for more efficient raycasting

  • @Bunn-y2officialyt
    @Bunn-y2officialyt 5 місяців тому +1

    Wow make this more popular get this guy to 1m subs

  • @ginodianna-h1z
    @ginodianna-h1z 5 місяців тому

    you need to blow up i love your videos

  • @electrobean
    @electrobean 5 місяців тому

    Also, this is coming from someone writing a Vulkan renderer for fun, so take this with a grain of salt, but OpenGL is not very hard to learn. Some of it doesn’t transfer great to Python, so if you really want it to go fast I’d honestly say moving to Kotlin (likely using LWJGL’s OpenGL bindings) would probably go reasonably well considering you know Python already.

  • @ThankYouESM
    @ThankYouESM 5 місяців тому

    Just in case still too slow no matter what you do... have Javascript with JSON creating temp (buffer overwriting) PNG1 + PNG2 to do all the heavy lifting so it reads like MP4 streaming instead.

  • @moldybot9387
    @moldybot9387 5 місяців тому

    Using a jit compiler should improve performance drastically

  • @SNOUTxTOUT
    @SNOUTxTOUT 5 місяців тому

    rendering with the experimental _sdl2 module in pygame-ce should speed things up a lot as it will draw using the GPU
    i've seen some performance increase using pypy with pygame too

  • @1ups_15
    @1ups_15 5 місяців тому +1

    how much faster do you think this could get if you did all the calculations using numpy?

  • @jethdaflip7741
    @jethdaflip7741 13 днів тому

    What engine or library are you using for this? Looks cool

    • @Gpopcorn
      @Gpopcorn  12 днів тому

      Just Pygame for the display

  • @anzo.7806
    @anzo.7806 5 місяців тому

    First of, what I don't see a lot of ppl mentionning here, is to try identify what truly makes your code slow down... Forget about implementing x or y blindly. Without knowing what kind of issue you are addressing (memory banwidth ? Too much computation ?) you are not going to make significant improvement.
    So get a python profiler, learn a bit how pygame works (bc imho you may be calling its graphic api in an very unsufficiant maneer), and see which functions calls slow you down. Then, and only then try to think about optimization :
    Is computing the square root too much ? Maybe try an approx.
    Is blitting one pixel after another too expensive ? Maybe try to coerce chunk of colors together. etc etc..
    Finally, when this repeated over and over, and you can't get much more, only then should you consider multiprocessing (which is a whole another beast on its own)
    That's how I would do it. Have fun !!!

  • @axiomgd5416
    @axiomgd5416 5 місяців тому

    you could try running it with pypy
    its an alternative python implementation thats quite fast and very compatible with cpython code

  • @tochukwuukpabi901
    @tochukwuukpabi901 5 місяців тому

    wow, just wow

  • @MountainLabsYT
    @MountainLabsYT 2 місяці тому

    Use a DDA alg. just use a DDA alg. that's literally all it takes. instead of using a fixed step size, use a DDA to march through the voxel grid and check each voxel for collision with the ground. its superior. Also compute shaders bro.

  • @oneomany
    @oneomany 5 місяців тому +1

    why's young garfield teaching me python

  • @DavidRobichaud-c2j
    @DavidRobichaud-c2j 5 місяців тому

    Great work!

  • @ThankYouESM
    @ThankYouESM 5 місяців тому

    Avoid the use of floating point numbers altogether when rendering in gameplay by using dict() cache or memoization.

  • @ThankYouESM
    @ThankYouESM 5 місяців тому

    Maybe use resize * 16 + PIL blur to create the height map

  • @ThankYouESM
    @ThankYouESM 5 місяців тому

    Binary Meshing (Cache): 0 = space. 1 = voxel... create a pre-calc rotation dict()

  • @iamtimsson
    @iamtimsson 5 місяців тому +1

    please try nonperpendicular processin

  • @Person-who-exists
    @Person-who-exists 5 місяців тому

    Try running it in pypy.
    Pypy is just a python interpreter but is much faster than cpython.

  • @jasm0_0
    @jasm0_0 5 місяців тому

    one optimization technique is to use c++

  • @BiuerBoris
    @BiuerBoris 5 місяців тому

    I bet Shoriminimoe has some great optimization tips

  • @ThankYouESM
    @ThankYouESM 5 місяців тому +1

    How Sets Can Truly OPTIMIZE Your Python Code - ua-cam.com/video/k_Hp4C-OQmM/v-deo.html

  • @blaz2892
    @blaz2892 5 місяців тому +2

    Now do it in C

  • @ThankYouESM
    @ThankYouESM 5 місяців тому +1

    Speeding Up Python Code With Caching - ua-cam.com/video/0_ievUF0L_E/v-deo.html

  • @thoughted6069
    @thoughted6069 5 місяців тому

    Do rendering on the GPU please.
    Use a graphics API with python bindings.
    Rendering on the CPU is a goddamn crime.
    Yet alone rendering on a CPU with python!

  • @LLDJ_
    @LLDJ_ 5 місяців тому

    why does this only have have 76 views 😭😭

  • @wijiler5834
    @wijiler5834 5 місяців тому

    Make it in c and vulkan with hardware raytracing (very fun trust me)

  • @hillybankok
    @hillybankok 5 місяців тому

    use parrallel processing.

  • @felixhartl4813
    @felixhartl4813 5 місяців тому

    PLEASE USE THE NUMBA MODULE!!!

  • @MuffinTastic
    @MuffinTastic 5 місяців тому +1

    i'm thinking of two words and they both begin with the letter p

  • @theevilcottonball
    @theevilcottonball 2 місяці тому

    I like it when people in Python actually know that Python is dogshit slow. I know there is "fast Python" like Cython, PyPy, multiprocessing, etc... But at that point it is easier to just use something else, like C.
    This is clearly for fun. Like writing DOOM in bash. Who cares about performance when you can have fun, am I right?

  • @ulixir
    @ulixir 5 місяців тому

    time to switch to lua boy 300x the performance frfr

  • @link1581
    @link1581 5 місяців тому

    He should add multithreading 😆

  • @moonyl5341
    @moonyl5341 5 місяців тому +1

    2:29 y'uore're*

  • @electrobean
    @electrobean 5 місяців тому

    I’m sure people have already said this but please just profile before listening to anyone’s suggestions, fix what’s slow not what looks slow.

  • @cheesepop7175
    @cheesepop7175 Місяць тому

    2:29 *your

  • @krikusZ80
    @krikusZ80 5 місяців тому

    Shaders pls, just use shaders of any kind, compute shaders or fragment… you can save up so many resources by using them for other cool stuff and allow it to run on any potato

  • @iamtimsson
    @iamtimsson 5 місяців тому

    2:45 and java
    xD

  • @cvabds
    @cvabds 5 місяців тому

    I bet you can't create it on templeOS

  • @mokouu
    @mokouu 5 місяців тому

    what the hell man dont find me

  • @theplaneguy45
    @theplaneguy45 5 місяців тому +7

    cool! please never code graphics in python again! thanks!

  • @akranmeister
    @akranmeister 5 місяців тому

    Use C++ 😂

  • @sjoerdev
    @sjoerdev 5 місяців тому +1

    why python tho......

  • @D01Light
    @D01Light 5 місяців тому

    Python isn't that slow

    • @theglowpt3
      @theglowpt3 5 місяців тому +1

      wait until you find out about the GIL

    • @Bennet2391
      @Bennet2391 3 місяці тому

      @@theglowpt3 ... flashbacks. GIL is such a headache.