// Use this for initialization void Start() { splashCanvas = GameObject.Find("SplashCanvas"); myMain = GameObject.Find("Main Camera"); myMain.SetActive(false); aiSpawner = GameObject.Find("AISpawner").GetComponent <AISpawner>(); getHeight = GetComponent <GetHeight>(); }
public PathGenerator(uint seed, GetHeight getHeight) { m_getHeight = getHeight; var fastRandom = new FastRandom(seed); m_cellNoise = new CellNoise2D(fastRandom.NextUInt()); m_sources = new Dictionary<uint, PathGraphNode>(); m_sinks = new Dictionary<uint, PathGraphNode>(); m_general = new Dictionary<uint, PathGraphNode>(); m_paths = new List<PathNodeList>(); }
private Mesh CreateMesh(GetHeight func) { var mesh = new Mesh(); mesh.Clear(); mesh.vertices = vertices.Select(a => new Vector3(a.x, func(a.x, a.z), a.z)).ToArray(); mesh.triangles = triangles.ToArray(); mesh.RecalculateNormals(); mesh.RecalculateBounds(); mesh.Optimize(); mesh.UploadMeshData(true); return(mesh); }
private List<PathData> GeneratePath(SimplexNoise2D noise, GetHeight getHeightFunction, PathGraphNode origin, PathGraphNode destination) { var originProjection = new Vector2(origin.Position.X, origin.Position.Z); var destinationProjection = new Vector2(destination.Position.X, destination.Position.Z); var gradient = Vector2.Subtract(destinationProjection, originProjection); var distance = gradient.LengthSquared(); gradient.Normalize(); //just checking if we'll get a NaN so the quality oprtator should be fine // ReSharper disable CompareOfFloatsByEqualityOperator var deltaX = gradient.X == 0 ? 0 : 1 / Math.Abs(gradient.X); var deltaZ = gradient.Y == 0 ? 0 : 1 / Math.Abs(gradient.Y); if (deltaX == 0 && deltaZ == 0) { return new List<PathData>(); } var stepX = Math.Sign(gradient.X); var stepZ = Math.Sign(gradient.Y); var maxX = 0.0f; var maxZ = 0.0f; var x = (int)origin.Position.X; var z = (int)origin.Position.Z; var previousHeight = (int)origin.Position.Y; var path = new List<PathData>(); do { var node = BuildPathData(noise, x, z, 0, gradient, getHeightFunction, previousHeight); path.Add(node); previousHeight = (int)node.Position.Y; if (deltaZ == 0 || (deltaX != 0 && maxX < maxZ)) { maxX += deltaX; x += stepX; } else { maxZ += deltaZ; z += stepZ; } } while (Vector2.DistanceSquared(originProjection, new Vector2(x, z)) <= distance); // ReSharper restore CompareOfFloatsByEqualityOperator return path; }
private PathData BuildPathData(SimplexNoise2D noise, int x, int z, int w, Vector2 gradient, GetHeight getHeightFunction, int previousHeight) { var radius = noise.Perlin(x, z) * 1 + 2; return new PathData(radius, x, z, w, gradient, getHeightFunction, previousHeight); }
public PathNodeList(PathGraphNode head, GetHeight getHeightFunction) { var noise = new SimplexNoise2D(head.Id); Head = head; var stack = new Stack<PathGraphNode>(); var minPosX = float.MaxValue; var minPosZ = float.MaxValue; var maxPosX = -float.MaxValue; var maxPosZ = -float.MaxValue; m_slices = new Dictionary<PathGraphNode, Dictionary<PathGraphNode, Lazy<List<PathData>>>>(); stack.Push(head); while (stack.Count > 0) { var node = stack.Pop(); var nodePos = node.Position; if (nodePos.X < minPosX) minPosX = nodePos.X; if (nodePos.Z < minPosZ) minPosZ = nodePos.Z; if (nodePos.X > maxPosX) maxPosX = nodePos.X; if (nodePos.Z > maxPosZ) maxPosZ = nodePos.Z; if (node.Edges.Count(val => val.Value > 0) == 0) { continue; } m_slices[node] = new Dictionary<PathGraphNode, Lazy<List<PathData>>>(); foreach (var graphNode in node.Edges) { if (graphNode.Value < 0) { continue; } m_slices[node][graphNode.Key] = new Lazy<List<PathData>>(() => GeneratePath(noise, getHeightFunction, node, graphNode.Key)); stack.Push(graphNode.Key); } } BoundingBox = new BoundingBox(new Vector3(minPosX - 1.5f, 0, minPosZ - 1.5f), new Vector3(maxPosX + 1.5f, 0, maxPosZ + 1.5f)); }
private int CalculateHeight(int x, int z, int w, Vector2 gradient, GetHeight getHeightFunction, int previousHeight) { var expandedRadius = Radius*1.2f; var pos = new Vector2(x, z); var samplePoint0 = new Vector2(gradient.Y, - gradient.X); samplePoint0.Normalize(); samplePoint0 = (samplePoint0 * expandedRadius) + pos; var samplePoint1 = new Vector2(-gradient.Y, gradient.X); samplePoint1.Normalize(); samplePoint1 = (samplePoint1 * expandedRadius) + pos; var height0 = getHeightFunction(samplePoint0.X, samplePoint0.Y); var height1 = getHeightFunction(samplePoint1.X, samplePoint1.Y); var heightM = getHeightFunction(x, z); var nodeHeight = (int)(height0 < height1 ? height0 : height1); nodeHeight = (int) (heightM < nodeHeight ? heightM : nodeHeight); return nodeHeight < previousHeight ? nodeHeight : previousHeight; }
public PathData(float radius, int x, int z, int w, Vector2 gradient, GetHeight getHeightFunction, int previousHeight) { Radius = radius; Position = new Vector3(x, CalculateHeight(x, z, w, gradient, getHeightFunction, previousHeight), z); lock (this) { Id = ++s_nextUid; } }
public Mesh BakeMesh(Chunk chunk, GetHeight func) { leafs = chunk.GetLeafs(); foreach (var leaf in leafs) { var v1 = new Vector3Int(leaf.area.xMin, 0, leaf.area.yMin); var v2 = new Vector3Int(leaf.area.xMin, 0, leaf.area.yMax); var v3 = new Vector3Int(leaf.area.xMax, 0, leaf.area.yMax); var v4 = new Vector3Int(leaf.area.xMax, 0, leaf.area.yMin); var v5 = new Vector3Int(leaf.area.xMin + leaf.area.width / 2, 0, leaf.area.yMin + leaf.area.height / 2); var i1 = GetVerticeOrAppend(v1); var i2 = GetVerticeOrAppend(v2); var i3 = GetVerticeOrAppend(v3); var i4 = GetVerticeOrAppend(v4); var i5 = GetVerticeOrAppend(v5); var steiners = NeedsSteiner(leaf); if (steiners[0]) { var v6 = new Vector3Int(leaf.area.xMin, 0, leaf.area.yMin + leaf.area.height / 2); var i6 = GetVerticeOrAppend(v6); triangles.AddRange(new int[] { i6, i5, i1, i2, i5, i6 }); } else { triangles.AddRange(new int[] { i2, i5, i1 }); } if (steiners[1]) { var v6 = new Vector3Int(leaf.area.xMin + leaf.area.width / 2, 0, leaf.area.yMax); var i6 = GetVerticeOrAppend(v6); triangles.AddRange(new int[] { i6, i5, i2, i3, i5, i6 }); } else { triangles.AddRange(new int[] { i3, i5, i2 }); } if (steiners[2]) { var v6 = new Vector3Int(leaf.area.xMax, 0, leaf.area.yMin + leaf.area.height / 2); var i6 = GetVerticeOrAppend(v6); triangles.AddRange(new int[] { i6, i5, i3, i4, i5, i6 }); } else { triangles.AddRange(new int[] { i4, i5, i3 }); } if (steiners[3]) { var v6 = new Vector3Int(leaf.area.xMin + leaf.area.width / 2, 0, leaf.area.yMin); var i6 = GetVerticeOrAppend(v6); triangles.AddRange(new int[] { i6, i5, i4, i1, i5, i6 }); } else { triangles.AddRange(new int[] { i1, i5, i4 }); } } return(CreateMesh(func)); }