pier luca lanzi, michele pirovano - procedural content generation with unity

63
Pier Luca Lanzi e Michele Pirovano Codemotion Milan November 2015 Procedural Content Generation with Unity

Upload: codemotion

Post on 15-Feb-2017

199 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

Procedural Content Generation with

Unity

Page 2: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

What is Procedural Content Generation?

Procedural Generation

with no or limited human intervention, algorithmically

of Content

of “things that affect the gameplay”, not non-player

character behavior, not the game engine

Page 3: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

so what exactly is procedurally generated?

levels, tracks, maps, terrains, dungeons, puzzles,

buildings, trees, grass, fire, plots, descriptions,

scenarios, dialogues, quests, characters, rules,

boards, parameters, camera viewpoint, dynamics,

weapons, clothing, vehicles, personalities, etc.

Page 4: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

http://en.wikipedia.org/wiki/Rogue_(video_game)#mediaviewer/File:Rogue_Screen_Shot

_CAR.PNG

Page 5: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

Page 6: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

BBC Micro – 2 MHz MOS Technology 6502/6512

16-128 kB RAM 32-128 kB ROM

Page 7: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

https://www.youtube.com/watch?v=ISR4ebdGlOk

Page 8: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

Page 9: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

Page 10: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

what’s the basic principle?

Page 11: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

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);

}

Page 12: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

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

Page 13: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

Outline

geometry

textures

animations

dungeons

Page 14: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

Procedural Geometry

(Quads)

Page 15: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

Page 16: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

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();

Page 17: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

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;

Page 18: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

Procedural Geometry

(Polygons)

Page 19: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

/// specify vertices

Vector3[] vertices = new Vector3[_no_sides];

float d = 2*Mathf.PI/no_sides;

for(int s=0; s<_no_sides; s++) {

vertices[s] = new Vector3(Mathf.Cos (-s*d), 0f,

Mathf.Sin (-s*d)) * _ray;

}

/// compute triangles no_sides-2 triangles, 3 indices each

int[] indices = new int[(_no_sides-2)*3];

for(int t=0; t<_no_sides-2; t++) {

indices[t*3] = t;

indices[t*3+1] = t+1;

indices[t*3+2] = vertices.Length-1;

}

/// ...

Page 20: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

Procedural Geometry

(Surfaces)

Page 21: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

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);

...

Page 22: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

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);

}

}

Page 23: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

Page 24: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

Procedural Geometry

(Cubes, Cylinders, Flowers)

Page 25: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

Page 26: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

Page 27: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

Page 28: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

Procedural Generation of Textures

Page 29: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

Page 30: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

// Create the new texture

texture = new Texture2D(width, height, TextureFormat.RGBA32,

false);

// Assign it to the material

GetComponent<MeshRenderer>().material.mainTexture = texture;

// Generate the pixels

for (int y = 0; y < height; y++){

for (int x = 0; x < width; x++){

R = …

G = …

B = …

A = …

texture.SetPixel(x, y,

new Color (R, G, B, A));

}

}

// Apply the texture

texture.Apply();

Page 31: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

// Noise pattern

noiseR =

SimplexNoise.noiseOctaves(nOctaves,xx*betaWR,yy,seed,frequenc

y);

// Alpha blending

valueR = (1-alphaR)+alphaR*noiseR;

// Sine pattern

valueR *= Mathf.Sin(noiseR*(sineX*x+sineY*y)*sineFrequency);

Page 32: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

Procedural Animations

Page 33: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

Procedural Animation

Page 34: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

Animation in Unity

Skinned mesh renderer

Transforms = Bones

Page 35: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

// Get all children transforms

var bones = gameObject.GetComponentsInChildren<Transform>();

// Rotate them using a sine wave

foreach(var bone in bones)

{

// Create a sine wave

float angle = Mathf.Sin(t + phase)*amplitude;

// Assign rotation

Vector3 tmpRot = bone.localEulerAngles;

tmpRot.z = angle;

bone.localEulerAngles = tmpRot;

}

Page 36: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

Cave Generation

Page 37: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

/// random fill the text map

void RandomFillMap() {

if (useRandomSeed) {

seed = Time.time.ToString();

}

System.Random pseudoRandom =

new System.Random(seed.GetHashCode());

for (int x = 0; x < width; x ++) {

for (int y = 0; y < height; y ++) {

if (x == 0 || x == width-1 ||

y == 0 || y == height -1) {

map[x,y] = 1;

} else {

map[x,y] = (pseudoRandom.Next(0,100) <

randomFillPercent)? 1: 0;

}

}

}

}

Page 38: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

void SmoothMap() {

for (int x = 0; x < width; x ++) {

for (int y = 0; y < height; y ++) {

int neighbourWallTiles =

GetSurroundingWallCount(x,y);

if (neighbourWallTiles > 4)

map[x,y] = 1;

else if (neighbourWallTiles < 4)

map[x,y] = 0;

}

}

}

Page 39: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

Dungeon Generation

Page 40: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

Dungeon Generation

Page 41: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

The Grid Map

Page 42: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

// Mark the starting location

CellLocation currentLocation = starting_location;

map.MarkAsVisited(currentLocation);

// Pick a starting direction

Direction previousDirection = Direction.North;

// Repeat until all cells have been visited

while (!map.AllCellsVisited)

{

// Choose a direction

DirectionPicker directionPicker =

new DirectionPicker(map, currentLocation, directionChangeModifier,

previousDirection);

Direction direction = directionPicker.GetNextDirection(map, currentLocation);

// Check if we can go in that direction

if (direction != Direction.None)

{

// Create a corridor to the next cell and flag this cell as visited

previousLocations.Add(currentLocation);

previousDirection = direction;

currentLocation = map.CreateCorridor(currentLocation, direction);

map.FlagCellAsVisited(currentLocation);

} else {

// Backtrack

currentLocation = previousLocations[previousLocations.Count - 1];

previousLocations.RemoveAt(previousLocations.Count - 1);

}

}

Recursive Backtracking algorithm

Page 43: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

Virtual Map -> Physical Map

// Choose a prefab based on the cell type

GameObject prefab = behaviour.GetPrefab(cell_type);

// Instantiate the prefab at the given position

GameObject go = (GameObject)GameObject.Instantiate(prefab, new

Vector3(l.x*behaviour.tileSize,l.storey*(behaviour.wallHeight+behaviour.storeyS

eparationHeight),l.y*behaviour.tileSize), Quaternion.identity);

go.name = cell_type.ToString();

Page 44: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

we can do it, so can you!

Page 45: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

http://www.youtube.com/watch?v=uIUYWzdMXog

Page 46: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

http://www.michelepirovano.com/portfolio_swordgenerator.php

Page 47: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

http://www.michelepirovano.com/portfolio_swordgenerator.php

Page 48: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

http://www.polimigamecollective.org

http://www.facebook.com/polimigamecollective

http://www.youtube.com/PierLucaLanzi

Page 49: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

49

Page 50: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

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.

50

Page 51: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

Page 52: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

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!

Page 53: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

f(x) = sin(x)

Page 54: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

Page 55: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

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

Page 56: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

Page 57: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

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

Page 58: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

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

Page 59: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

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

Page 60: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

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

Page 61: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

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.

Page 62: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015

ingredient #7

gameplay integration

Is it fun to play? Is the progression

adequate?

Page 63: Pier Luca Lanzi, Michele Pirovano - Procedural Content Generation with Unity

Pier Luca Lanzi e Michele Pirovano – Codemotion Milan

November 2015