private void AddChunk(int x, int z) { HeightInfo hi = HmWithCoords(x, z); if (hi == null) { Vector2f vec = new Vector2f(x, z); if (vec.Distance(this.v2cp) < maxDist) { TerrainChunkNode hmtc = CreateNewChunk(physicsWorld, this, x, z, scale, chunkSize, null); heightmaps.Add(hi = new HeightInfo(this, vec, hmtc)); hi.chunk.Create(); ApplyTerrainMaterial(hmtc); hi.chunk.SetLocalTranslation(new Vector3f(x * (chunkSize - 1) * scale.x, 0, z * (chunkSize - 1) * scale.z)); hi.midpoint = new Vector2f(hi.chunk.GetLocalTranslation().x - ((chunkSize - 1) * scale.x / 2), hi.chunk.GetLocalTranslation().z - (chunkSize - 1) * scale.z / 2); Vector2f[] neighbors = GetNeighbors(hi); hi.neighbors = neighbors; rootNode.AddChild(hi.chunk); hi.pageState = PageState.Loaded; } } else { } }
protected Vector2f[] GetNeighbors(HeightInfo origin) { Vector2f[] nb = new Vector2f[8]; nb[0] = new Vector2f(origin.position.x + 1, origin.position.y); nb[1] = new Vector2f(origin.position.x - 1, origin.position.y); nb[2] = new Vector2f(origin.position.x, origin.position.y + 1); nb[3] = new Vector2f(origin.position.x, origin.position.y - 1); nb[4] = new Vector2f(origin.position.x + 1, origin.position.y - 1); nb[5] = new Vector2f(origin.position.x - 1, origin.position.y - 1); nb[6] = new Vector2f(origin.position.x + 1, origin.position.y + 1); nb[7] = new Vector2f(origin.position.x - 1, origin.position.y - 1); return(nb); }
public float GetHeight(Vector3f worldPosition) { Vector3f chunkSpace1 = WorldToChunkSpace(worldPosition); HeightInfo closest = HmWithCoords((int)chunkSpace1.x, (int)chunkSpace1.z); if (closest != null) { chunkSpace1.x -= closest.position.x; chunkSpace1.z -= closest.position.y; chunkSpace1.MultiplyStore((chunkSize - 1)); return(closest.chunk.GetHeight(chunkSpace1)); } return(float.NaN); }
public override void Update() { // if (queueTime > maxQueueTime) // { if (queue.Count > 0) { Vector2f v = queue[0]; int x = (int)v.x; int y = (int)v.y; AddChunk(x, y); queue.Remove(v); queueTime = 0f; } // } // queueTime += 1f; if (updateTime > maxUpdateTime) { cp.Set(Camera.Translation); cp.SubtractStore(rootNode.GetWorldTranslation()); cp.MultiplyStore(1f / (((int)chunkSize - 1) * (scale.x))); v2cp.Set(cp.x, cp.z); for (int i = heightmaps.Count - 1; i > -1; i--) { HeightInfo hinf = heightmaps[i]; if (hinf.pageState == PageState.Loaded) { if (hinf.position.Distance(v2cp) > maxDist) { hinf.pageState = PageState.Unloaded; } else { foreach (Vector2f v2 in hinf.neighbors) { AddChunk((int)v2.x, (int)v2.y); // AddToQueue(new Vector2f((int)v2.x, (int)v2.y)); } } hinf.UpdateChunk(); } else if (hinf.pageState == PageState.Unloading) { hinf.UpdateChunk(); } else if (hinf.pageState == PageState.Unloaded) { if (hinf != null) { hinf.UpdateChunk(); rootNode.RemoveChild(hinf.chunk); heightmaps.Remove(hinf); hinf.neighbors = null; hinf.midpoint = null; hinf.position = null; hinf.chunk.hm.Dispose(); hinf.chunk.hm = null; hinf.chunk = null; hinf = null; System.GC.Collect(); } } } AddChunk((int)cp.x, (int)cp.z); updateTime = 0f; } else { updateTime += this.Environment.TimePerFrame * 10.0f; } }
protected Vector2f[] GetNeighbors(HeightInfo origin) { Vector2f[] nb = new Vector2f[8]; nb[0] = new Vector2f(origin.position.x + 1, origin.position.y); nb[1] = new Vector2f(origin.position.x - 1, origin.position.y); nb[2] = new Vector2f(origin.position.x, origin.position.y + 1); nb[3] = new Vector2f(origin.position.x, origin.position.y - 1); nb[4] = new Vector2f(origin.position.x + 1, origin.position.y - 1); nb[5] = new Vector2f(origin.position.x - 1, origin.position.y - 1); nb[6] = new Vector2f(origin.position.x + 1, origin.position.y + 1); nb[7] = new Vector2f(origin.position.x - 1, origin.position.y - 1); return nb; }