Raw Dev Log #2: The Beach is Now Part of Your Farm Area
So I'm back for the second dev log! The first dev log on the environmental decorating optimizations did surprisingly well. I'm very happy about that :)


This one's got a lot of stuff in it so there's a jump menu down below if you just want to skip around. The big gameplay thing is you can now place refining fixtures, your furnaces, saws, etc, in the new oceanic area connected to the farm. I was originally not going to allow it because the ocean zone is just so large and I thought it was beyond the scope of having such a huge area with all placeable terrain.
But I decided to do it anyways due to breakthroughs with performance, and figuring out the game can handle more than I thought. Originally this area was planned by the modeler and me as being purely decorative, and we only hoped that maybe somehow it could all be placeable. Happy to see that we pulled it off!

Anyway, making the grid system so much larger, and making sure it was still compatible with people's old save files, there was a lot of coding involved, and careful adjustments. I also built an entirely new tool for painting the terrain, marking off the tiles where you can't place stuff because there's environmental things in the way. Rocks, flowers, cliffs, water, that kind of thing. A lot of work, but it's done now. I tried a few different methods initially, but the new tool was what I ended up making for it. I'll be sharing all of that and a bunch of other stuff too.


The artist also sent over the final version of the new farm zone background recently and it's pretty sweet. It has more personality, like dog faces built into the mountain rocks and other details. We ended up making it kind of a layered thing. Getting it working properly with different camera perspectives was a little bit challenging, but it works!

I also set up a bunch of warp zones to enter and exit the new terrain to different areas. Like going from the bottom cave up to the top cave, or from the farm to the beach, town, junction areas, that kind of thing (sparkly indicators included). I also improved the transition from the waterfall into the water visually, which is a relief because it was a bit rough looking before. I also had to rebuild the animal companion pathfinding setup so your cows, chickens, horses, etc, can actually follow you down into the new zone instead of getting stuck at the old farm edge and glitching out - continuously teleporting to you, lol. There's a bunch of other changes and additions too but it's hard to remember them all.
The New Background#
So like I mentioned at the top, the modeler sent over the finalized version of the new farm zone background a little while ago, and it's pretty sweet. The old background was basically just a flat image back there, not much going on. This new one has a bunch of hand-painted layers stacked at different depths instead. You've got distant snow-capped mountains way in the back, then green mountains with cliff faces in front of those, then a treeline of oaks, and then a dense pine forest up in the foreground. Everything is pixel art and hand-painted by him.

There's like 5 or 6 depth bands in total. The farthest mountains are this pale minty color because they're supposed to be so far away, and then each layer gets a bit darker and more detailed as you come forward. The pine trees in the front are individual sprites so I can just scatter them around however I want, which is nice for making each spot on the farm feel a bit different. He also painted left and right halves of a looping pine strip so it tiles seamlessly across the whole width of the zone.
Here's what each of the layers looks like on its own, going from the back of the scene to the front:













Getting it to work properly with the camera was a bit challenging honestly, because the perspective shifts around a lot as you zoom in and out. My first attempt was a calibration tool where I'd dial in exactly where the background should sit at every zoom level, but I ended up scrapping that whole approach. What I did instead was just angle the tree line downward and extend it way further. So when you're zoomed more top-down, you're mostly seeing trees. When the camera tilts more paper mario style side-on, the mountain line shows through behind them. Way simpler than trying to reposition everything per zoom level.
Waterfall Blending#
I also improved the blending where the waterfall meets the river at the top, which like I mentioned earlier was looking a bit rough before. It used to have kind of a hard seam right where the waterfall mesh started and the flat river water ended. Now there's a custom material on the waterfall that fades into the river surface instead.

Rebuilding the Placement Grid#
Okay to back up a bit, the game already lets you place refining fixtures out in the open on your farm, anywhere you want. (Or actually any area in the game too that makes sense.) Furnaces, saws, sprinklers, scarecrows, tumblers, that kind of stuff. Anywhere there's a flat tile or a slope that isn't extreme, you can place stuff down. (That includes planting fruit trees, berry bushes and other stuff)
So with the new ocean zone connecting to the farm, I wanted the same thing to be true down there too. Like all the same rules just in a much bigger area. Drop your furnace on the beach if you want, same as up top.
The problem is the old placement system wasn't really built for an area this big, with this much decoration on it. And it wasn't really built for handling old save files from people in early access either, since they already have their whole farm laid out on the old terrain shape, and all their saved soil values and vegetable plant positions... So I had to kind of redo the whole thing from scratch to handle the changes as well as old save compatability. (btw, doing development with having to worry about saving/loading compatability and translations makes everything harder, so I recommend new dev's think about that.)
I also had to build a new tool in the editor for marking off the spots where the player shouldn't be able to place stuff. Basically it's like a painter tool where I paint red tiles directly on the terrain. All the decorative rocks, flowerbeds, stonework, water, cliff edges, stuff like that gets painted red on a per tile basis as the game is setup. I can paint from the top down view for covering big areas fast, or switch to the in-game perspective view for the fiddly spots where I need to nudge tiles around individual decorative props. I end up switching back and forth between the two views a lot.

The tool itself is done now, and I've also already finished hand-painting all the blocked spots across the whole new terrain too, which was honestly something I procrastinated a bit on... Some of these screenshots are from while I was still in the middle of it.
On a different topic; Another other thing I had to fix was the actual blue grid line visual itself due to the new varied terrain. When you're playing and looking to place a refining fixture in placement mode, the game draws these blue grid lines on the ground so you can see which tiles and available and which are occupied (in red). Technically the way it works is it projects a decal downward from above the camera, and the shader reads the depth buffer to figure out where the ground is and draws the lines onto it. On the old mostly flat terrain that worked fine. But on the new terrain with all the cliffs and waterfalls and elevation changes, the grid was drawing on everything including some cliff faces too. Not exactly professional to keep it like that.


So the fix was basically a few things. The original version of the shader was asking the terrain what direction it was facing, and using that to decide where to draw the grid. The problem was the terrain shaders were kind of lying about it. They were writing "I'm flat" into the G-Buffer even on cliff faces, because of some weirdness with how terrain normals work in Unity. So no matter how much I tightened the angle cutoff, the grid still showed up on the cliffs, because the number it was checking against was garbage.
What I ended up doing instead was making the shader figure out the actual slope on its own, by looking at how the world position changes from pixel to pixel across the screen (ddx and ddy derivatives, for any shader people reading this). That gives you the real slope of the ground at every pixel, no matter what the terrain shader claims. Then anything steeper than about 30 degrees from horizontal gets cut off, and I also shrunk the height of the projector box so it doesn't reach as far down vertically, which means it catches less cliff face in the first place. There's also a smooth fade near the cutoff angle now so the grid doesn't just hard-clip at the edge, it kind of fades out gradually, which looks way more natural on slopes.

How To Make A Grid Blocker Tool (For Other Unity Devs)
If you want to build something similar, here's how I wired this one up. Skip past this part if you're not into Unity editor stuff.
The Component
The blocker is a MonoBehaviour that sits on the Terrain GameObject. The data it saves is just two int arrays called blockedTilesX and blockedTilesZ. Entry i in both arrays together makes up one blocked tile. I went with two flat int arrays instead of a list of Vector2Int mostly because Unity's default serialization handles int arrays fine without me having to write any custom save code for it.
When the game starts up, ApplyToGrid() walks through the arrays and for each pair it converts the world coordinate into a grid index, then flips the matching entry in a second boolean map called gridMapSimplifiedCantPlaceHere to true. The placement system already had a gridMapSimplified layer that tracked whether something was currently sitting on a tile, so adding a second "is this tile allowed at all" layer was basically one extra line in the placement check:
if (!groundGridReference.gridMapSimplified[x, z] &&
!groundGridReference.gridMapSimplifiedCantPlaceHere[x, z])
{
// Placement allowed.
}
So now placement just checks both maps instead of one. Old map says "is something already here," new map says "is this tile even allowed." If either one trips, nothing gets placed.
The Editor Tool
The editor side is a custom Unity Editor inspector on the component, plus a scene view handler that takes over your mouse while paint mode is on. It works in both the top-down orthographic view and the regular perspective scene view. Basically the same thing happens in either one. It shoots a ray from the mouse cursor down onto the ground, grabs whichever tile is under the cursor, and paints that. I use top-down most of the time when I'm painting over a big open area. For the fiddlier spots, like tiles tucked under a rock I can't really see from straight above, I'll flip to perspective and tilt the camera around like I'm the player. There's a T key shortcut that snaps you into top-down view whenever you want it.
There's a few different paint modes, and they all work the same in either view.
The main one is just a brush. Ctrl plus left click paints whichever tile you're hovering, or a square of tiles if brush size is bigger than 1. Ctrl plus right click erases. Brush size only goes up in odd numbers, 1 through 15, so the brush always centers cleanly on the tile your cursor's on. A HashSet keeps track of whatever tiles got touched during the current session, so painting over the same area twice doesn't add duplicate entries.
There's also a box fill mode for bigger areas. Ctrl plus shift plus drag draws a rectangle across the ground. While you're dragging, there's an overlay in the scene view that shows the live width, height, and total tile count of the box you're drawing. I added that because without the feedback I kept accidentally painting 40 by 40 squares and wondering why half the map had gone red.
And then there's a local undo on the Z key that pops the last action off a 30 step stack. I kept hitting Ctrl Z out of muscle memory and triggering Unity's own undo system, which would undo half my paint stroke and then some unrelated gameobject move from ten minutes ago, so I ended up moving the local undo onto its own key.
Performance
Not really something I had to worry about here. It's one boolean lookup per placement attempt, and that only happens when the player clicks to put something down. So there wasn't much to optimize on this one.
Warp Zones#
Okay so like I also mentioned up top, I set up a bunch of warp zones connecting the new terrain to the other zones around it. Bottom cave up to the top cave, farm down to the beach, into town, the junction area, all that kind of stuff. Makes the whole map feel way more connected instead of just one flat expanse of disconnected zones. Sparkly indicators included so you know where they are.

The funny part is how I was actually measuring where the player should spawn on the other side of each warp. Every warp has to drop you into the destination zone at a specific spot, facing the right way, and not clipping into a wall or a rock or whatever. So I needed like a reference body roughly the size of the player to stick in the scene while I was lining it all up.
I ended up just using Rufus the farmer's sprite for this, which is kind of a weird technique but it worked pretty well! Literally just placing Rufus at the landing spot in each zone so I could eyeball whether the size and position and angle all felt right. He's the perfect size for it.




Farm Animal Pathfinding#
One more thing I wanted to mention was the farm animal pathfinding stuff. So all your farm animals, like your cows, your chickens, your horses, whatever you've got following you around, they use A star pathfinding to keep up with you. Which is basically a grid layer that sits underneath the whole terrain and says what spots are walkable and what spots aren't, and then each animal figures out a path from wherever it's standing over to wherever you are on its own.
The problem is the old pathfinding graph only covered the old farm area. So when I extended the world down into the ocean zone, the animals would follow you right to the edge of the farm and then just stop. Sit there and watch you walk off into the ocean without them, which was kind of sad and definitely not what I wanted.

So I rescanned the pathfinding graph across the whole new terrain. Like farm, junction, ocean zone, all of it. Now the animals can trail you all the way down to the beach without getting stuck at an invisible fence somewhere.
The other thing was that big decorative props like the cliff rocks, thick clusters of trees and stuff like that, they don't automatically block the pathfinding on their own. Each of those big props gets a "Pet Obstacle for A*" marker on it now, which carves out a non-walkable zone around the prop when the pathfinding graph bakes.

Now the animals route around all that stuff the way they should, and they can follow you anywhere in the new terrain without getting stuck. Much happier about how they behave now :)
-david

Comments