Video game design and programming course for the Master in Computer Engineering at the Politecnico di Milano. http://www.facebook.com/polimigamecollective https://twitter.com/@POLIMIGC http://www.youtube.com/PierLucaLanzi http://www.polimigamecollective.org
1. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
Procedural Content Generation with Unity
2. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
what’s the basic principle?
3. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
f(x) = sin(x)
float y = 0f;
for (float x = 0f; x<6.28f; x+=.01f) {
y = Mathf.Sin(x);
}
4. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
What the ingredients?
domain knowledge
structured randomness
multi-layering
filters, limits & restrictions
specialized algorithms
artificial intelligence
gameplay integration
5. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
Outline
procedural geometry
colors & music
6. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
Procedural Geometry
7. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
8. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
/// specify vertices
Vector3[] vertices = new Vector3[4];
vertices[0] = new Vector3(0.0f, 0.0f, 0.0f);
vertices[1] = new Vector3(0.0f, 0.0f, m_Length);
vertices[2] = new Vector3(m_Width, 0.0f, m_Length);
vertices[3] = new Vector3(m_Width, 0.0f, 0.0f);
/// specify 2 triangles
int[] indices = new int[6];
/// triangle 1
indices[0] = 0;
indices[1] = 1;
indices[2] = 2;
/// triangle 2
indices[3] = 0;
indices[4] = 2;
indices[5] = 3;
/// create the mesh
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = indices;
mesh.RecalculateBounds();
9. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
/// create the normals
Vector3[] normals = new Vector3[4];
normals[0] = Vector3.up;
normals[1] = Vector3.up;
normals[2] = Vector3.up;
normals[3] = Vector3.up;
mesh.normals = normals;
/// create the UV for the full texture
Vector2[] uv = new Vector2[4];
uv[0] = new Vector2(0.0f, 0.0f);
uv[1] = new Vector2(0.0f, 1.0f);
uv[2] = new Vector2(1.0f, 1.0f);
uv[3] = new Vector2(1.0f, 0.0f);
mesh.uv = uv;
/// create the UV for half texture
Vector2[] uv = new Vector2[4];
uv[0] = new Vector2(0.0f, 0.0f);
uv[1] = new Vector2(0.0f, 1.0f);
uv[2] = new Vector2(0.5f, 1.0f);
uv[3] = new Vector2(0.5f, 0.0f);
mesh.uv = uv;
10. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
public class MeshBuilder
{
public List<Vector3> Vertices;
public List<Vector3> Normals;
public List<Vector2> UVs;
public void AddTriangle(int i0, int i1, int i2);
public Mesh CreateMesh()
}
MeshBuilder meshBuilder = new MeshBuilder();
//Set up the vertices and triangles:
meshBuilder.Vertices.Add(new Vector3(0.0f, 0.0f, 0.0f));
meshBuilder.UVs.Add(new Vector2(0.0f, 0.0f));
meshBuilder.Normals.Add(Vector3.up);
...
meshBuilder.AddTriangle(0, 1, 2);
meshBuilder.AddTriangle(0, 2, 3);
...
11. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
MeshBuilder meshBuilder = new MeshBuilder();
for (int i = 0; i < m_SegmentCount; i++)
{
float z = m_Length * i;
for (int j = 0; j < m_SegmentCount; j++)
{
float x = m_Width * j;
Vector3 offset =
new Vector3(x, Random.Range(0.0f, m_Height), z);
BuildQuad(meshBuilder, offset);
}
}
12. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
12
13. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
Ingredients #1 & #2
Domain Knowledge & Artificial Intelligence
• Domain Knowledge
§ To generate something you need to know it
§ PCG typically aims at building an artificial level
designer, usually needs domain knowledge
about level design
• Artificial Intelligence
§ Need algorithms that can work on complex
knowledge and generate plausible content
§ Search-based methods, L-systems, evolutionary
computation, fractals, cellular automata,
agent-based methods, planning, etc.
13
14. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
14
ESEMPIO CON LA CAVE?
ESEMPIO DI DUNGEON?
15. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
16. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
ingredient #3
structured randomness
things look like they have been randomly
generated but it is not completely at random!
17. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
f(x) = sin(x)
18. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
19. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
they both look like “noise”
but one of them feels like it has structure…
it is structured randomness
20. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
21. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
ingredient #4
multi-layering
typically more layers of procedural
content generation are applied in sequence
22. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
• Warzone 2100
§ Heights & Cliffs
§ Roads
§ Textures
§ Player Bases
§ Local Features
• Civilization 4
§ Fractal Heightfield
§ Plate Tectonics
§ Tile Types
§ Rivers and Lakes
§ Map Bonuses
23. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
ingredient #5
Filters, Limits & Restrictions
“In Civ3 I would say we even shipped with a sub-standard resource
placement algorithm where all the iron could be concentrated in just
a few small locations on the map, and for one player there may be
literally no way for them to build swordsmen.” – Soren Johnson
"With Civ4 we instituted randomness with limitations. There
always has to be a minimum distance between each element of
iron, or each element of horses, and that completely solved the
problem.” – Soren Johnson
24. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
ingredient #6
specialized algorithms
placing special items, requires special tricks
this tricks must be encoded in the PCG
25. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
1. Connect all bases, the resources,
pick three random points and
connect them
2. Apply a customize A* heuristic
and reuse roads!
3. Customize A* heuristic with
randomize cost of non-road
grid cells.
26. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
ingredient #7
gameplay integration
Is it fun to play? Is the progression adequate?
27. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
28. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
is this all there is?
29. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
PCG
Is it done online?
Or offline?
Is it necessary content?
Or optional?
Do you use random
seeds or parameter
vectors?
Is it stochastic?
Or deterministic?
Generate and test?
Constructive?
30. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
we can do it, so can you!
31. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015http://trackgen.pierlucalanzi.net
32. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
http://www.youtube.com/watch?v=uIUYWzdMXog
33. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015http://www.michelepirovano.com/portfolio_swordgenerator.php
34. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015http://www.michelepirovano.com/portfolio_swordgenerator.php
35. Pier Luca Lanzi e Michele Pirovano – Codemotion Milan November 2015
http://www.polimigamecollective.org
http://www.facebook.com/polimigamecollective
http://www.youtube.com/PierLucaLanzi