I am making a farming game. I want the fields to have areas of fertility.
As you can see from the picture below, I have generated something that looks like a fertility map for a farmland. The shapes you see are actually gameobjects, NOT a texture on the terrain. The positions of the game objects have been selected via perlin noise
So the following step is complete: Generate natural looking areas of fertility. All coordinates for where fertility is present have been successfully generated
Now obviously I can’t have game objects like this all over. I need to give the player a visual to see where the areas of fertility are
How do I basically paint these coordinates onto the terrain. And how do I lessen their color as the fertility at a coordinate lowers (due to crops being planted)
I would like this all to be done in runtime, smoothly. I would like the fertility to be lowered when a crop has been harvested. So I think I need to generate one texture per call to HarvestCrop()? - seems expensive but please advice. Maybe there is some way to paint?
Thank you
First thing you need to do is have a pure data model that stores this soil fertility data. Once you have that, then you can easily derive or drive all other visual and gameplay elements from that.
How to visualise it depends on what method you’re using. Is this meant to simply a visualisation? You can generate a texture for your data, assigning a colour depending on the value. This texture can be updated as the data changes as well; you don’t need to generate a new texture each time.
There’s plenty of examples on the docs on how to generate and update textures: Unity - Scripting API: Texture2D.SetPixels
The data model will simply be a map (Vector3 point: int)
And how would I generate the texture at a certain point? If my gameobject is x by y in dimensions, how do I convert some coordinate (a, b) into the correct texture pixel?
Thank you
At a basic level you make a texture the same size as your data. So if you have a 500 x 500 grid, you make a texture of the same size. Then it’s a 1:1 conversion. Each x,y of your grid is a pixel in your texture.
Just note texture coordinates start at bottom left. So you may have to flip the y coords of your grid, or semantically code it to follow the same.
Thank you. How do I make a texture the size of (x, y)?
Look at the documentation. All this simple stuff is covered by the docs.
Which documentation do I look at?
The documentation for textures? Unity - Scripting API: Texture2D
I didn’t see a way to resize a pixel. What if I take my game object and split it into multiple chunks of 100 x 100, and then apply a 1 x 1 texture to the 100 x 100 area?
I just can’t figure out how to put a texture on the whole object if it’s more than 100x100
You can’t resize pixels. That doesn’t make sense. The size a pixel appears depends on the size of the mesh and the dimensions of the texture.
If you want a pixel to appear a certain size, you need to make sure the model its applied to is the right size, same for the dimensions of the texture itself.
The “right” size will be specific to your context.
Sorry I meant, I can’t figure out a way to resize a texture
You resize a texture by making a new texture at the size you want and transfering your pixels over to the new one. Often using interpolation from the source texture: Unity - Scripting API: Texture2D.GetPixelBilinear