The first patterns
June 2024
The koi patterns in koi farm 2 need much more variety than the ones in koi farm 1 had. There are of course much more ways to render colors in 3d than in 2d, since I can let light react to the koi scales in many different ways; some scales can be very shiny, others can be opalescent, and the scales themselves can have many different shapes. I've worked on a new system of shape generation and shape mutation that will allow me to generate a large number of pattern types too.
Before I can render any patterns, I made a renderer that satisfies a number of requirements:

- The renderer should be able to render the maximum amount of koi all at once. That's 4096 koi in total.
- To do this, all koi patterns must fit in a limited amount of memory. That means the texture resolution is rather low.
- I don't want distinct pattern "types" like I had in koi farm 1. There will be no difference between a "stripes" and a "spots" pattern, since there's a large spectrum between them. Besides, these distinct pattern types caused a lot of confusion in koi farm 1. Many questions on the Discord server were about spots patterns that didn't fit a stripes card slot in the book. Users mutated spots into something that looked like stripes, but the game didn't recognize them as such since they still had the "spots" type.
- The pattern generation system must be very flexible. I want to be able to introduce many mutable properties that can generate new distinct patterns to make sure the game has replay value.
To address the memory constraint issue, I've concluded that storing 4096 patterns as textures was not viable. GPU memory usage would be too high if the textures had an acceptable resolution when zooming in on a single koi. Inspecting koi up close is an important feature for breeding specific traits, so a different system had to be used. Because generating complex patterns can be very slow, generating high quality versions of a pattern when you get close to a fish would consume too much CPU resources.
I've decided to divide pattern data up into three parts:
- The shapes of pattern spots (or other shapes).
- The different materials a pattern has (the base material plus up to four layer materials).
- The scales, which will be added later.

The shapes are stored as signed distance fields, a method I've used earlier to store the pond shapes in a memory efficient way. These distance fields don't become pixelated when zooming in, which is very important; spots have high quality curves and edges at every level. Since there are up to four layers on each koi, four distance fields are required. I'm storing these four fields in a texture with a pretty low resolution, using the red, green, blue and alpha channels.
To add more precision to the koi patterns, I'm not stretching the texture over the entire koi body. There's no pattern on koi bellies, and the pattern doesn't extend all the way to the tail fin.

In koi farm 1, all koi patterns were generated using cubic noise. Even with filters and modifications, noise can generate only so many patterns before it becomes repetitive. That's why I chose for a very different approach for koi farm 2. Patterns are now "grown" instead of being read from a fixed mathematical function:
- First, random neutral pigment cells are spread over the koi body. These cells haven't specialized into being and behaving like a specific pigment yet.
- Then, pigment cells start to subdivide and specialize over time, taking up more space.
- Until a certain cell density is reached, step 2 is repeated.
- The clouds of pigment cells are connected and converted to blobs, which are then rendered to the koi textures.
This system allows me to define an infinite number of subdivision rules, cell behaviors, and guidance rules the cells will follow while growing. All these rules will be read from koi DNA, so the rules mix when two koi crossbreed. Some rules will overrule others, and some will only be active in very specific situations. Many will be optional, so certain mutations can appear over time that result in new previously unseen patterns. The animation shows a very simple rule set in action: three different types of pigment cells simply subdivided until the area has been filled with cells.