260 likes | 483 Views
Scene modeling, Mazes, and Terrain. CSE 3541 Matt Boggus. Overview. Hierarchical scene modeling and Parenting in Unity Mazes Terrain. Sample Scene. Hierarchical scene – chair 1. Hierarchical scene – chair 2. Hierarchical scene – chessboard. White chess pieces – child of chessboard.
E N D
Scene modeling, Mazes, and Terrain CSE 3541 Matt Boggus
Overview • Hierarchical scene modeling and Parenting in Unity • Mazes • Terrain
Unity Parenting • Video tutorial (parenting in the GUI) • Transform.parentscripting reference • ParentingExample.unitypackage
Mazes Maze from Image-guided maze construction, Xu and Kaplan 2007, Siggraph Maze tiles from OSU research
Simple mazes – binary grid • Matrix of booleans • ex: 21 x 21 • Arbitrary mapping: • True = black pixel = wall • False = white pixel = open space
Creating the scene Foreach (i,j) in boolean[x,y] maze if(maze[i,j]) Create Cube at position (i,0,j)
Creating GameObjects for(inti = 0; i < numberOfAsteroids; i++){ GameObjectaSphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); aSphere.transform.parent= transform; aSphere.name = "sphere" + i.ToString(); aSphere.transform.position= new Vector3(Random.Range(-10.0f, 10.0f), Random.Range(-10.0f, 10.0f), Random.Range(-10.0f, 10.0f)); aSphere.transform.localScale= new Vector3(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f)); }
Deleting GameObjects GameObjectmyObject; // do stuff with myObject (create, animate, etc.) Destroy(myObject); /* Note: myObjectstill exists as a variable name, only the corresponding GameObject in the scene is removed */
Procedural maze initial values All open space – add walls All walls – add open space
Checking for a solution – Flood-fill • Determine the area connected to a given node in a multi-dimensional array • Applications: • “Paintbucket” tool • Connected components • Pathfinding Animation from http://en.wikipedia.org/wiki/Flood_fill
Recursive flood-fill code Flood-fill(x, y, value, grid) { if(x < 0 || y < 0 || x >= XSIZE || y >= YSIZE) return; if(grid[x,y] == -1 || grid[x,y] == value) return; grid[x,y] = value; Flood-fill(x-1,y, value, grid); Flood-fill(x+1,y, value, grid); Flood-fill(x,y+1, value, grid); Flood-fill(x,y-1, value, grid); } Note: method is prone to stack overflow
Iterative flood-fill algorithm Flood-fill (x, y, value, grid) { Set Q to the empty queue Add position(x,y) to Q While Q is not empty { Dequeue position p if (p.x or p.y out of bounds) continue; if (grid[p.x,p.y] == -1 || grid[p.x,p.y] == value) continue; grid[p.x,p.y] = value; Enqueue (p.x-1,p.y); Enqueue (p.x+1,p.y); Enqueue (p.x,p.y-1); Enqueue (p.x,p.y+1); }
Terrain http://accad.osu.edu/researchmain/gallery/project_gallery/iires-project.html
Heightfield definition • Function u(x,y) gives height at (x,y) • Store height values in an array u[x,y] • Note: limited to one height per (x,y)
Heightfield smoothing • For every grid cell u[i,j], set it to average of itself and neighbors • Critical thinking – implementation concerns: • A. looping order • B. boundary cases • C. both • D. none For more terrain creation and editing operations, see http://www.lighthouse3d.com/opengl/terrain
Heightfield mesh creation u[x,y] ; dimensions n by n
Heightfield mesh creation (clockwise winding order) Quad[0,0] Vertices are U[0,0], U[0,1], U[1,1], U[1,0] Quad[i,j] Vertices are U[i,j], U[i,j+1], U[i+1,j+1], U[i+1,j]
Heightfieldcreation (iteration) Inner loop iterates over i, the x coordinate Last quad: i=5 (i = n-1)
Heightfieldcreation (iteration) Outer loop iterates over j, the z coordinate Last row: j=5 (j = n-1)
Unity terrain engine • Terrain object http://docs.unity3d.com/ScriptReference/Terrain.html • Terrain object has a field terrainDatahttp://docs.unity3d.com/ScriptReference/TerrainData.html • terrainData has methods getHeighthttp://docs.unity3d.com/ScriptReference/TerrainData.GetHeights.html • terrainData has methods setHeightshttp://docs.unity3d.com/ScriptReference/TerrainData.SetHeights.html • Video tutorial on runtime terrain modification https://www.youtube.com/watch?v=YRQur24S0BY