public Args(Chunk c, Chunk.Face incomingFace, float bottomLeftX, float bottomLeftY, float topRightX, float topRightY) { this.c = c; this.incomingFace = incomingFace; this.bottomLeftX = bottomLeftX; this.bottomLeftY = bottomLeftY; this.topRightX = topRightX; this.topRightY = topRightY; }
void SetUv(Vector3 b, Chunk c,Chunk.Face f,bool isknown) { Vector2 step; if (isknown) { step = ToOcta((int)c.GetBlockType(b)); } else { step = ToOcta((int)Chunk.BlockType.unknown); } if (c.GetBlockType(b) == Chunk.BlockType.grass) { switch (f) { case Chunk.Face.back: step.x += 1; break; case Chunk.Face.right: step.x += 1; break; case Chunk.Face.front: step.x += 1; break; case Chunk.Face.left: step.x += 1; break; case Chunk.Face.up: break; case Chunk.Face.down: step.x += 2; break; default: break; } } float fixTex = 0.005f; float xMin = 0 + (0.125f * step.x) + fixTex; float xMax = 0.125f + (0.125f * step.x) - fixTex; float yMin = 0 + (0.125f * step.y) + fixTex; float yMax = 0.125f + (0.125f * step.y) - fixTex; finalUV.Add(new Vector2(xMin, yMin)); finalUV.Add(new Vector2(xMin, yMax)); finalUV.Add(new Vector2(xMax, yMax)); finalUV.Add(new Vector2(xMax, yMin)); }
private void drawChunk(Chunk c, Chunk.Face incomingFace, float bottomLeftX, float bottomLeftY, float topRightX, float topRightY) { if (Vector3.SqrMagnitude(c.getCenter() - vecpos) > viewdist * viewdist) { return; } //Debug.Log(c.getCoordinates() + ": " + c.getCenter() + ": " + Vector3.Magnitude(c.getCenter() - vecpos)); Vector3[] corners = new Vector3[8]; Args arg; Stack <Args> stack = new Stack <Args>(); stack.Push(new Args(c, incomingFace, bottomLeftX, bottomLeftY, topRightX, topRightY)); int maxdep = 0; while (stack.Count > 0) { Profiler.BeginSample("args"); arg = stack.Pop(); c = arg.c; incomingFace = arg.incomingFace; bottomLeftX = arg.bottomLeftX; bottomLeftY = arg.bottomLeftY; topRightX = arg.topRightX; topRightY = arg.topRightY; //args = new Pair<Chunk, Chunk.Face>(c, null); Profiler.EndSample(); if (c == null) { continue; } if (chunksProcessed.Contains(c)) { continue; } Profiler.BeginSample("args2"); chunksProcessed.Add(c); int cx = c.getXOffset() - pos.x; int cy = c.getYOffset() - pos.y; int cz = c.getZOffset() - pos.z; int s = Chunk.size; Profiler.EndSample(); Profiler.BeginSample("evaluation"); corners[0] = Camera.main.WorldToScreenPoint(new Vector3(cx + 0, cy + 0, cz + 0)); corners[1] = Camera.main.WorldToScreenPoint(new Vector3(cx + 0, cy + 0, cz + s)); corners[2] = Camera.main.WorldToScreenPoint(new Vector3(cx + 0, cy + s, cz + 0)); corners[3] = Camera.main.WorldToScreenPoint(new Vector3(cx + 0, cy + s, cz + s)); corners[4] = Camera.main.WorldToScreenPoint(new Vector3(cx + s, cy + 0, cz + 0)); corners[5] = Camera.main.WorldToScreenPoint(new Vector3(cx + s, cy + 0, cz + s)); corners[6] = Camera.main.WorldToScreenPoint(new Vector3(cx + s, cy + s, cz + 0)); corners[7] = Camera.main.WorldToScreenPoint(new Vector3(cx + s, cy + s, cz + s)); /* * if (c.getX() == 0 && c.getY() == -1 && c.getZ() == 0) * { * for (int i = 0; i < corners.Length; i++) * GameObject.Find("Image (" + i + ")").GetComponent<RectTransform>().position = corners[i]; * }*/ float minX = Camera.main.pixelWidth; float maxX = 0; float minY = Camera.main.pixelHeight; float maxY = 0; int behind = 0; foreach (Vector3 corner in corners) { if (corner.z <= 0) { //Corner behind camera behind++; continue; } minX = System.Math.Min(minX, corner.x); minY = System.Math.Min(minY, corner.y); maxX = System.Math.Max(maxX, corner.x); maxY = System.Math.Max(maxY, corner.y); } if (c.getX() == 0 && c.getY() == -1 && c.getZ() == 0) { //Debug.Log(minX + "|" + minY + " - " + maxX + "|" + maxY + " / " + bottomLeftX + "|" + bottomLeftY + " - " + topRightX + "|" + topRightY + " => " + (minX >= topRightX || minY >= topRightY || maxX <= bottomLeftX || maxY <= bottomLeftY) + " inc: " + incomingFace); } minX = System.Math.Max(0, minX); minY = System.Math.Max(0, minY); maxX = System.Math.Min(Camera.main.pixelWidth, maxX); maxY = System.Math.Min(Camera.main.pixelHeight, maxY); if (c.getX() == 0 && c.getY() == -1 && c.getZ() == 0 && (minX >= topRightX || minY >= topRightY || maxX <= bottomLeftX || maxY <= bottomLeftY)) { //Debug.Log(minX + "|" + minY + " - " + maxX + "|" + maxY + " / " + bottomLeftX + "|" + bottomLeftY + " - " + topRightX + "|" + topRightY + " => " + (minX >= topRightX || minY >= topRightY || maxX <= bottomLeftX || maxY <= bottomLeftY)); } Profiler.EndSample(); if (behind == 8) { //Chunk behind camera if (enableDebugColors) { c.setDebugColor(Color.blue); } continue; } if (behind > 0) { minX = minY = 0; maxX = Camera.main.pixelWidth; maxY = Camera.main.pixelHeight; } if (minX >= topRightX || minY >= topRightY || maxX <= bottomLeftX || maxY <= bottomLeftY) { //Chunk outside FOV if (enableDebugColors) { c.setDebugColor(Color.red); } continue; } Profiler.BeginSample("recursion"); if (c.getX() == 0 && c.getY() == -1 && c.getZ() == 0) { //Debug.Log("rendering"); } /* * if (enableDebugColors) c.setDebugColor(Color.green); * if (maxdep++ == debugIndex) * { * GameObject.Find("Image (0)").GetComponent<RectTransform>().position = new Vector3(minX, maxY, 0); * GameObject.Find("Image (1)").GetComponent<RectTransform>().position = new Vector3(maxX, minY, 0); * GameObject.Find("Image (2)").GetComponent<RectTransform>().position = new Vector3(minX, minY, 0); * GameObject.Find("Image (3)").GetComponent<RectTransform>().position = new Vector3(maxX, maxY, 0); * }*/ c.setRendered(true); invisibleChunks.Remove(c); ICollection <Chunk.Face> faces; if (incomingFace != null) { faces = incomingFace.getConnectedFaces(); } else { faces = c.getFaces(); } //Debug.Log("Drawing chunk " + c.getCoordinates() + " with " + faces.Count + " connections"); Profiler.EndSample(); Debug.DrawRay(c.getCenter() + new Vector3(1, 1, 1) - vecpos, (Vector3)incomingFace.getNormal() * 5, maxdep++ == 0 ? Color.green : Color.blue, 0.1f); //if (maxdep++ > 4) break; foreach (Chunk.Face face in faces) { if (Vector3.Dot((c.getCenter() - vecpos).normalized, face.getNormal()) < -0.01f && (c.getCenter() - vecpos).magnitude > Chunk.size * 3) { continue; } if (face.getOpposingFace() == null) { if (interestingChunks != null) { interestingChunks.Add(c.getCoordinates() + face.getNormal()); } continue; } Debug.DrawRay(new Vector3(), c.getCenter() - vecpos, Color.yellow, 0.1f); Debug.DrawRay(c.getCenter() - vecpos, (Vector3)face.getNormal() * 5, incomingFace == null ? Color.blue : Color.red, 0.1f); stack.Push(new Args(face.getOpposingFace().getChunk(), face.getOpposingFace(), minX, minY, maxX, maxY)); } } }