Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Looking for most efficient way to enable/disable and/or recolor meshes

Discussion in 'Scripting' started by flipwon, Jun 17, 2023.

  1. flipwon

    flipwon

    Joined:
    Dec 29, 2016
    Posts:
    179
    I'm making a simple 3d roguelike for a jam with a buddy and have been experimenting with using a classic FOV system using bresenham lines or raycasting for vision. The backend for identifying the objects in a grid is working very quickly, but my attempts at showing/hiding/recoloring the mesh are slow.

    My main goal is to have the mesh completely hidden, show it in full color when set to visible, and then show at a darker color when set out of sight.

    I've tried different combinations of:
    Enabling/disabling gameobjects
    Enabling/disabling mesh
    Changing material of meshes from dark/light
    changing material color of meshes in code

    All of these methods work, but not as fast as I'd like. The fastest way of these so far seems to be just changing out the materials entirely, but I'm sure there is a better method that I just haven't though of.

    Any ideas?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Ideally hook up the profiler and see what you can find out (Window -> Analysis -> Profiler)

    I am guessing that it might take just as long to colorize something the SAME color, so re-blasting the color to all objects each frame could get expensive.

    This means it might be better if you keep a "what is my last-updated visibility" set of data, then only send it to Unity objects when it changes.

    The other obvious improvement is to gate off what you update with what is visible, eg, don't do visibility down long hallways leading away from the player offscreen.

    I'm kinda curious, would love to see a screenshot how it is going! Are you posting progress publicly, like on Twitter?
     
  3. flipwon

    flipwon

    Joined:
    Dec 29, 2016
    Posts:
    179
    Thanks for the reply! Currently our algorithm is returning only exactly which to enable, and which are headed to previously visible, so only objects that need to be changed are getting changed. The issue is when our field of view is large (which is ideal for the game we're making) and we're changing 30-40+ tiles at a time, this is when the hitches occur.

    I was hoping there are other ways to fully hide/colorize a mesh that I'm unaware of, something with fancy shaders or something?



    Nah, just a jam. Gunna see where it goes
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    I'm surprised these few items cause you an issue. What is inside one of those tiles? How much changes when you flip visibility states? It seems the Material-swapout would be fastest, followed by complete prefab-swap-out for ultimate flexibility with future assets.
     
  5. flipwon

    flipwon

    Joined:
    Dec 29, 2016
    Posts:
    179
    The main issues are we're aiming for mobile devices, and while one call of the line of sight changes maybe 30-40 objects at a time, when pathfinding a large path you can end up doing many in succession. While it doesn't make the game completely die, there's a slight hitch that I'd love to work around.

    But yeah, so far the best I've found is completely disabling the mesh while undiscovered, turning on when in line of sight, and then swapping out the material for a darker material once out of line of sight.

    Was hoping that there is in fact a better way :(
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Yanno... I only learned about this a couple of days ago, haven't studied it AT ALL, so I have no idea what it all means limitation-wise, etc.

    BUT... check out GPU instancing. This guy threw my code through ChatGPT4 and told it to use GPU instancing and claims a massive increase in performance. I have not yet tried to investigate or verify:

    https://forum.unity.com/threads/how...imal-performance-issues.1448491/#post-9079345
     
  7. flipwon

    flipwon

    Joined:
    Dec 29, 2016
    Posts:
    179
    I do this on all shaders at the moment. Unfortunately on a mobile device it only does so much, as their gpu is pretty crappy lol
     
    Kurt-Dekker likes this.
  8. flipwon

    flipwon

    Joined:
    Dec 29, 2016
    Posts:
    179
    I've just found out about compute shaders and I believe this to be the answer to my problem, for anyone wondering in the future.. Though I have no idea how to use them at the moment.. Time to study I guess!
     
  9. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    897
    if your target is mobile then compute shaders are likely not a viable option. Mobiles are not known for having powerful gpus. and a lot of mobile phones lack compute shader support.

    I would recommend profiling, it may be memory allocation, it may be your gpu catching on fire, it may be poorly optimized loops. how much GC alloc is happening? where is your code spending the most time in? how much memory are you using?

    Simply saying something is "slow" could mean anything. Do you have a target fps you want to reach? Since its a jam you may not have set one. but if performance is your primary issue it may do you good to set a couple metrics and benchmarks.