benArcen

Unity Questions Thread

Recommended Posts

Has anyone had experience with drawing textures onto terrain with code? Any tutorials you can recommend?

Making a randomly generated map is haaarrrd.

Supposedly Sprite Tile on the asset-store is supposed to create tile-based terrarains which would (I assume) make terrain generation much easier if you are going for a certain style. I bought it, but I haven't played around with it yet.

Share this post


Link to post
Share on other sites

A prototype I was working on a while ago painted some roads onto a big terrain. Thought I had saved more pictures of it but apparently not. I didn't find any tutorials really, I think I just had to poke through Unity Answers and cobble this together.

 

0TzaP9o.png

 

This was the main method that was doing work. It's really inefficient and as you can see does not look great. The short version of what you need to do is convert the world coordinates you want to paint to their nearest point on the map grid, then get the alphamap of that terrain coordinate and increase the alpha of the texture you want visible and assign it back to the terrain data. An alphamap is a 3d array that stores the alpha value for every texture (assigned to the terrain in the editor) on a given coordinate.

        public void PaintSquare(Terrain terrain, int x, int y, int texture)
        {
		TerrainData td = terrain.terrainData;
		if(x > 0 && x < td.alphamapWidth && y > 0 && y < td.alphamapHeight)
		{
			float[,,] map = td.GetAlphamaps(x, y, 1, 1);
			for(int i = 0; i < td.splatPrototypes.Length; i++)
			{
				map[0,0,i] = 0;
			}
			map[0,0,texture] = 1;
			
			terrain.terrainData.SetAlphamaps(x,y,map);
		}
	}

 

This is probably super unhelpful though so I'd say google around for 'dynamic terrain painting' and you'll probably be able to figure it out? I'm happy to send you the rest of the code if you think it'll be useful though!

 

edit: a large part of why this was so inefficient was that you should actually generate the entire new alphamap and only call SetAlphamaps one time, but I never bothered refactoring 'cause I ended up just placing sprites over the terrain which looked a lot better for what I was doing (drawing roads) anyway.

Share this post


Link to post
Share on other sites

Ok, I'll have to go with placing sprites as you did, then. I want to make an oldschool map, think google maps, maybe with monopoly style houses on top. I'm really surprised your road turned out the way it did. 

 

Have you attempted to place the sprites on a non-flat surface? I wanted the paper of my map not to be completely flat.

Share this post


Link to post
Share on other sites

Haha, I don't wanna discourage you too much; I think with a better line drawing algorithm and some more subtle textures (and fades between them) it could look very good. Sir You Are Being Hunted I think uses terrain painting in Unity to generate their world, and theirs looks great and generates fairly quickly!

 

I haven't tried placing sprites/textures onto an irregular surface. I donno how you'd do it...I guess something crazy like creating a mesh that deforms to match the surface, and texturing that?

Share this post


Link to post
Share on other sites

I don't know how useful this'll be to folks here, but after dealing with a bunch of bullshit trying to get screenshot sharing to work, I put the result of my efforts up on GitHub. This is a tiny plugin to open the native sharing UI on Android and iOS and pass it a screenshot and some text; hopefully it spares someone else the annoyance of figuring this out.

 

https://github.com/ChrisMaire/unity-native-sharing

Share this post


Link to post
Share on other sites

What it's supposed to look like:

tdiIamP.gif

 

What it looks like ingame (slowed down so you can tell what's happening:

YcWRKbb.gif

 

This is hacked together out of several different stock animations that may or may not have been meant for anything remotely related to what I'm using them for, so it's not surprising that issues would crop up.  Still, as a non animator is there anything I can do about this?

Share this post


Link to post
Share on other sites

How are you handling movement for the character? Based on that bug I would guess it's some kind of root motion controller interfering with the directionality of the movement. What does the object hierarchy look like?

Share this post


Link to post
Share on other sites

It happens even with root motion turned off.  My best guess is that it has something to do with the direction the animations thinks the character is facing conflicting when transitioning between them.  You can see the red arrow flip around in the preview; I'm just not sure why it doesn't reflect that in the preview, or how to/if you can change that direction manually.

Share this post


Link to post
Share on other sites

you should be able to disable rotation setting for a few frames to get around that, though that will likely have gameplay implications you may not want.  It's hard to say what the issue is without being familiar with your input and movement logic, but I've had similar issues that could be fixed by adjusting the parenting hierarchy.  Usually adding a dummy parent on top of the animation that contains all the gameplay relevant code on it will fix things like that, though making the character move based on animations is a bit trickier.  Essentially the dummy parent becomes your character and the art just a representation of it's movement, as opposed to the animation driving the movement.  I'd try throwing a print statement into the console that shows the direction of the characters' movement throughout the course of the animation and see if you can pick up any anomalies.

Share this post


Link to post
Share on other sites

Yeah even with rotation completely locked it still animates like that.  Anyway, I managed to get around the issue by swapping in a non root motion version of the offending animation and controlling movement with a curve.  I still don't understand why the two versions behave differently even with root motion disabled, but the important thing is that it works.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now