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. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question Most efficient way to constantly re-build GameObjects

Discussion in 'Scripting' started by BenjaminApprill, Nov 24, 2022.

  1. BenjaminApprill

    BenjaminApprill

    Joined:
    Aug 1, 2022
    Posts:
    30
    Hey what's up!

    I am working on a space game where the player can explore multiple solar systems. It would be nice to make the skybox accurate to other stars in the Galaxy.

    I don't think object pooling will work here because their are so many objects to pool. My only other idea is to instantiate all of the stars for an instant in order to build a skybox, and the destroy them after... This is considered bad for memory tho, so I'm not sure if that will work.

    Otherwise, I will have to build some kind of custom system to deal with this. I am hoping there is an efficient way to simply leverage Unity functionality, but I am aware that mass creation and deletion of GameObjects like this is not efficient...

    Is there some kind of non ECS/DOTS way to efficiently gather all of the relevant GameObjects like this, without creation/deletion cycles?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    29,825
    Sounds like you're looking for impostors.

    There's some assets on the store for this but no idea how well they would scale to planetary distances.

    Unity is after all a single-precision engine, which gives you between 6 and 9 decimal places to play with, no more.
     
  3. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,051
    Id go with the hand drawn approach if you are teleporting distances. It’s possible to look at the pixel data of Earths view pick a point and regenerate the pixel stars to different sizes based on size they were :) but you’re talking of a very complicated thing to do here but I would fathom it is feasibly possible. Maybe star colour could tell you the distance that star is. And then when you go to that star you regenerate the image in respect of the distance you are now. Very complicated but achievable I imagine.
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    2,173
    If I was you I'd rather think about a skybox texture than objects. Sure some objects could be objects (impostors as Kurt said), some nebulae or clusters, or really bright stars, but the rest of it no. You could probably use compute shader to generate such a texture from a database. I don't have much experience with this though, but it should be doable.

    You would also need a mathematical way to convert between spherical coordinates to two-hemispheres of planar UVs for the skybox textures (edit: though they are already wrapped correctly, so maybe you only need that for the UI). I have some experience with that, so I can help if you decide to take this route.

    In the end it really depends on how many stars you want to generate render.
     
    Last edited: Nov 24, 2022
  5. BenjaminApprill

    BenjaminApprill

    Joined:
    Aug 1, 2022
    Posts:
    30
    Does that include the whole value? Or just the decimal value.

    Not quite. The idea is to have real-time warping through space. That is a good point that traveling to a distant star from that perspective will require real-time updates then. I was more considering from within a "Star System" view, where the scale stays constant. Or a zoomed out view that just has some local stars... I haven't considered the flying "through" space part tho ;P

    Aye. The end result is to use a skybox texture if necessary, but new textures would have to be built each time the player plays a new game... The easiest way is to use the real objects to do this, but that may be too resource intensive...

    The idea is probably to generate at least 1,000 unique star systems on each new game. With potential player settings, it could go higher or lower.

    ---

    I will have to take a look at impostors. I am not familiar with those yet. It seems like I may have to come up with a more sophisticated system to pull this off. It would be real nice to just use the GameObject approach, but I understand this may not be a feasible route... I think I can create some kind of system to generate the textures, the issue will be not using too much memory... I am also considering using some kind of SDF shader to draw all the objects efficiently. If I'm not mistaken, it doesn't really incur an overhead for drawing something paternally, but I've got some more work to do before I can prove that or not.
     
  6. Hannibal_Leo

    Hannibal_Leo

    Joined:
    Nov 5, 2012
    Posts:
    493
    Do you have a picture of what you want to achive?
     
    AnimalMan likes this.
  7. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    2,173
    Actually my idea was to generate each texture on the fly, once per system, not all of them on game load. I assumed you can travel between systems and this is what I'd do. I'm more into procgen, and this sounds like a thing of procgen. My solution is knowledge- and tech- challenged but is actually supposed to run with the least overhead in memory and CPU, on a scale of seconds. It is also multi-threading friendly and there are no runtime costs once it's done. This is why it's a good candidate. You can check how it's done (aesthetically at least) in games such as Elite Dangerous, Dyson Sphere Program, and I presume Space Engine does a similar thing. Star Citizen as well.
     
  8. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    2,173
    Impostors are quads that you position so that they're rendered in front of the skybox. Shader makes sure to always orient its normal toward the camera, aka billboard shading. Impostors are sometimes used as the last step for a LOD system, usually with vegetation and trees, to massively reduce the model complexity without sacrifice in details. With stellar objects and vast distances the parallax is so miniscule that the impostors are the first logical thing, however you're still dealing with potentially thousands of objects, and the only other option you have is to rely on GPU instancing.

    Which is actually another solution: GPU instancing + texture array might give you the variety and quantity you're looking for, letting you introduce tens of thousands of impostors with zero cost to CPU. But this approach lacks the ability to actually feed data to such a shader. (Edit: I'm wrong here, you can do this in code; you basically build a single mesh once, it contains each quad and UVWs bake the stars or other phenomena you'd like to show via texture array. Therefore you can feed a seed or another texture, or even concrete data.)

    Maybe you can generate a noise texture and feed that to such a shader. I don't know how accurate you want your skybox to be, but this way it can be replayable if you can visit the same place again. Ultimately, you lose the ability to bridge UI with it (to sync visuals between CPU and GPU). This is the only reason why I'd consider compute shaders if I were you. (Edit: see previous edit, the above was a decent solution.)

    Edit:
    It's worth noting that when you build a mesh there is some "building" time on the CPU. But this can also be multi-threaded if you want a seamless transition, you just have to fake "Star Trek" travelling time until it's done. It's also on a scale of at most a second. And you only ever need one such model in memory.
     
    Last edited: Nov 25, 2022
  9. BenjaminApprill

    BenjaminApprill

    Joined:
    Aug 1, 2022
    Posts:
    30
    Not exactly. It's just a skybox of stars. Not that complicated of an idea. What kind of picture would you like?
     
  10. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    952
    Single-precision 32bit floating point (see this wonderful IEEE754 online toy for the gory details) can record pretty much any number from -10^38 to +10^38, including tiny values down to 10^-45, but it loses precision as you get farther away from -1 or +1. If you write your number in decimal, only about the 7 most significant digits are stored.

    For example, if Jenny were to write down
    x = 867.5309f
    , the actual value stored is really about 867.5308837890625, which is close but not an exact match. You can see the 7th digit is not actually her number. This is true for 86.75309 (really stored close to 86.75308990478515625) or 86753.09 (really stored closer to 86753.09375).
     
  11. Hannibal_Leo

    Hannibal_Leo

    Joined:
    Nov 5, 2012
    Posts:
    493
    Well, if it's just like that:
    upload_2022-12-1_15-18-39.jpeg
    in 2D or 3D, it's litereally just dots.

    There are Demos of VFX graph rendering millions of such dots, you probably won't need that many.

    I feel like the most difficult part is maintaining all the positions.