public void GetCube(int x, int y, int z, out Cube cube) { //Wrap coords so that the array is a 3d circular queue x = x & _maskX; z = z & _maskZ; cube = Cubes[x, y, z]; }
public void CheckYGetCube(int x, int y, int z, out Cube cube) { if (y < 0 || y >= _lenY) { cube = Cube.NULL; return; } //Wrap coords so that the array is a 3d circular queue x = x & _maskX; z = z & _maskZ; cube = Cubes[x, y, z]; }
public CubeVertex(ref Vector3 position, ref NormalizedByte4 normal, ref Vector2 texture, ref Cube cube, ref Cube n1, ref Cube n2, ref Cube n3, ref Cube n4) { this.VertexPosition = position; this.Normal = normal; this.Texture = texture; this.Red = cube.Red; this.Green = cube.Green; this.Blue = cube.Blue; this.Alpha = (byte)cube.Alpha; int lr = 0; int lg = 0; int lb = 0; if (n1.IsTransparent) { lr = n1.Red; lg = n1.Green; lb = n1.Blue; } if (n2.IsTransparent) { lr = lr + n2.Red; lg = lg + n2.Green; lb = lb + n2.Blue; } if (n3.IsTransparent) { lr = lr + n3.Red; lg = lg + n3.Green; lb = lb + n3.Blue; } if (n4.IsTransparent) { lr = lr + n4.Red; lg = lg + n4.Green; lb = lb + n4.Blue; } this.LocalRed = (byte)(lr >> 2); this.LocalGreen = (byte)(lg >> 2); this.LocalBlue = (byte)(lb >> 2); byte light = (byte)((n1.LightLevels + n2.LightLevels + n3.LightLevels + n4.LightLevels) >> 2); this.LocalLight = (byte)((light & 240) >> 4); this.SkyLight = (short)(light & 15); }
public void GetCubeNeighbors(int x, int y, int z, out Cube curr, out Cube posX, out Cube negX, out Cube posY, out Cube negY, out Cube posZ, out Cube negZ) { x = x & _maskX; z = z & _maskZ; curr = Cubes[x, y, z]; int reg = (x + 1) & _maskX; posX = Cubes[reg, y, z]; reg = (x - 1) & _maskX; negX = Cubes[reg, y, z]; reg = y + 1; posY = (reg < _lenY) ? Cubes[x, reg, z] : Cube.NULL; reg = y - 1; negY = (reg >= 0) ? Cubes[x, reg, z] : Cube.NULL; reg = (z + 1) & _maskZ; posZ = Cubes[x, y, reg]; reg = (z - 1) & _maskZ; negZ = Cubes[x, y, reg]; }
public void LoadChunks(Object threadContext) { Chunk curr; Cube air = new Cube(CubeMaterial.None,0); Cube cube; float noisevalue; int worldX; int worldZ; int height; int mountain; int finalHeight; float[,] heightmap = new float[Chunk.WIDTH, Chunk.WIDTH]; float[,] mountains = new float[Chunk.WIDTH, Chunk.WIDTH]; while (_running) { lock (_loadQueue.SyncRoot) { Monitor.Wait(_loadQueue.SyncRoot, 500); } while (_loadQueue.TryTake(out curr)) { if (curr.State == ChunkState.Unloading) continue; if (!curr.LoadFromDisk()) { //Generate using terrain generation //Get the heightmap noise.FillMap2D(heightmap, curr.Coords.X, curr.Coords.Z, octaves: 2, startFrequency: .5f, startAmplitude: 5); noise.FillMap2D(mountains, curr.Coords.X, curr.Coords.Z, octaves: 4, startFrequency: .05f, startAmplitude: 100); for (int x = 0; x < Chunk.WIDTH; x++) { worldX = x + curr.Coords.X * Chunk.WIDTH; for (int z = 0; z < Chunk.WIDTH; z++) { worldZ = z + curr.Coords.Z * Chunk.WIDTH; height = (int)(heightmap[x, z] + Settings.SEA_LEVEL); mountain = (int)(mountains[x, z] + Settings.SEA_LEVEL); //Create ground height = (height <= Chunk.HEIGHT) ? height : Chunk.HEIGHT; for (int y = 0; y < height; y++) { cube = new Cube(CubeMaterial.Stone,0); CubeStorage.SetMaterialAt(worldX, y, worldZ, ref cube); } finalHeight = height; if (mountain - height > 1) { finalHeight = (mountain<=Chunk.HEIGHT)?mountain:Chunk.HEIGHT; for (int y = height; y < finalHeight; y++) { cube = new Cube(CubeMaterial.Dirt,0); CubeStorage.SetMaterialAt(worldX, y, worldZ, ref cube); } } for (int y = finalHeight; y < Chunk.HEIGHT; y++) { cube = new Cube(CubeMaterial.None,0); CubeStorage.SetMaterialAt(worldX, y, worldZ, ref cube); } //Create caves noisevalue = 0; for (int y = 0; y < Chunk.HEIGHT; y++) { //noisevalue = noise.GetValue3D(worldX, y, worldZ, octaves: 6, startFrequency: .5f, startAmplitude: 2); if(noisevalue < -0.5f) { cube = new Cube(CubeMaterial.None,0); CubeStorage.SetMaterialAt(worldX, y, worldZ, ref cube); } } } } } curr.ChangeState(this, ChunkState.Loaded); } } }
public void SetMaterialAt(int x, int y, int z, ref Cube cube) { //Wrap coords so that the array is a 3d circular queue x = x & _maskX; z = z & _maskZ; Cubes[x, y, z] = cube; }