EmiOnGit / warbler_grass

A bevy plugin for creating 3d grass in your game
Apache License 2.0
120 stars 11 forks source link

Make chunks serializable #50

Open EmiOnGit opened 1 year ago

EmiOnGit commented 1 year ago

What problem does this solve or what need does it fill?

It would be nice if we could serialize chunks completely This would especially benefit the editor as this would be the base for proper saving

Describe the solution you'd like

Save the chunk in a ron file with following format:

Chunk1(
  transform: (0,0,0), // spawn at position (0,0,0)
  height_map: "grass/chunk1/height_map.png", // path to height map
  density_map: "grass/chunk1/density_map.png", // path to height map
  height: "grass/chunk1/heights_map.png", // path or a just a f32 for the uniform
  aabb: (20.,5.,20.) // the aabb of the chunk
  grid_id: (10,10), // optional for a chunk manager implementation
),
Chunk2(...)

Describe what is for debate

The format can differ and is mostly up to the person implementing. Also json is for fine, though I like ron a bit more since it's easily readable

Additional context

This is ultimately needed by #49

ethereumdegen commented 3 months ago

Instead of only serializing with the paths to the data, i think it would be very nice to have the struct be serializeable in a way where the data is also included [and you can serialize it in binary with bincode. ] this is how i handle serializing xpbd colliders for my terrain: this is a lot cleaner

ethereumdegen commented 3 months ago

Naturally this would mean that a serialized file would be tens of kb potentially but then you wouldnt need to go fetch the heightmap and everything later its a monolithic serialized file w everything you need for that chunk

EmiOnGit commented 3 months ago

That's a great idea. Seeing how the crate changed, I don't think that my previous idea is very great. Currently, the heightmap is used in the shader directly and interpolate the height of the blades. I like this idea because it let the user 1) Change the height of a region very dynamically 2) Decide how large the heightmap texture should be. If there are not many changes in heights, a very small heightmap is totally fine

In fact, in the current Pipeline the grass blades are only stored as a x,z coordinate relative to one corner of the chunk and everything else is calculated on the gpu side.

The extraction of the x,z coords from the density texture happens to be rather cpu expensive and a bit unnecessary. Therefore it would be nice if this could be skipped and instead be loaded directly from a file.

EmiOnGit commented 3 months ago

Instead of only serializing with the paths to the data, i think it would be very nice to have the struct be serializeable in a way where the data is also included [and you can serialize it in binary with bincode. ]

While this wouldn't be completely solved, I believe you could also store the heightmap in the binary without much problem. Using Image::from_buffer(include_bytes!("assets/heightmap.png")) this shouldn't be to bad, right? Feel free to tell me I'm wrong here, I'm still trying to figure everything out :D