public Rect GetTexture(Chunk chunk, ref Vector3Int localPos, Direction direction) { if (usesConnectedTextures) { ChunkBlocks blocks = chunk.blocks; int index = Helpers.GetChunkIndex1DFrom3D(localPos.x, localPos.y, localPos.z); ushort blockType = blocks.Get(index).Type; bool nw = ConnectedTextures.IsSame(blocks, index, -1, 1, direction, blockType); bool n = ConnectedTextures.IsSame(blocks, index, 0, 1, direction, blockType); bool ne = ConnectedTextures.IsSame(blocks, index, 1, 1, direction, blockType); bool w = ConnectedTextures.IsSame(blocks, index, -1, 0, direction, blockType); bool e = ConnectedTextures.IsSame(blocks, index, 1, 0, direction, blockType); bool se = ConnectedTextures.IsSame(blocks, index, 1, -1, direction, blockType); bool s = ConnectedTextures.IsSame(blocks, index, 0, -1, direction, blockType); bool sw = ConnectedTextures.IsSame(blocks, index, -1, -1, direction, blockType); return(connectedTextures[ConnectedTextures.GetTexture(n, e, s, w, nw, ne, se, sw)]); } if (textures.Count == 1) { return(textures[0]); } if (textures.Count > 1) { int hash = localPos.GetHashCode(); if (hash < 0) { hash *= -1; } float randomNumber = (hash % 100) / 100f; randomNumber *= textures.Count; return(textures[(int)randomNumber]); } Debug.LogError("There were no textures for " + textureName); return(new Rect()); }
public override void GenerateStructures(Chunk chunk, int layerIndex) { //if (chunk.pos.x!=-30 || chunk.pos.y!=30 || chunk.pos.z!=0) return; int minX = chunk.Pos.x; int maxX = chunk.Pos.x + Env.ChunkSize1; int minZ = chunk.Pos.z; int maxZ = chunk.Pos.z + Env.ChunkSize1; int structureID = 0; for (int x = minX; x <= maxX; x++) { for (int z = minZ; z <= maxZ; z++) { Vector3Int pos = new Vector3Int(x, 0, z); float chanceAtPos = Randomization.RandomPrecise(pos.GetHashCode(), 44); if (chance > chanceAtPos) { if (Randomization.RandomPrecise(pos.Add(1, 0, 0).GetHashCode(), 44) > chanceAtPos && Randomization.RandomPrecise(pos.Add(-1, 0, 0).GetHashCode(), 44) > chanceAtPos && Randomization.RandomPrecise(pos.Add(0, 0, 1).GetHashCode(), 44) > chanceAtPos && Randomization.RandomPrecise(pos.Add(0, 0, -1).GetHashCode(), 44) > chanceAtPos) { int xx = Helpers.Mod(x, Env.ChunkSize); int zz = Helpers.Mod(z, Env.ChunkSize); int height = Helpers.FastFloor(terrainGen.GetTerrainHeightForChunk(chunk, xx, zz)); if (chunk.Pos.y <= height && chunk.Pos.y + Env.ChunkSize1 >= height) { Vector3Int worldPos = new Vector3Int(x, height, z); structure.Build(chunk, structureID++, ref worldPos, this); } } } } } }
public override void BuildBlock(Chunk chunk, ref Vector3Int localPos, int materialID) { var pools = Globals.WorkPool.GetPool(chunk.ThreadID); RenderGeometryBatcher batcher = chunk.RenderGeometryHandler.Batcher; // Using the block positions hash is much better for random numbers than saving the offset and height in the block data int hash = localPos.GetHashCode(); float blockHeight = (hash & 63) * coef * Env.BlockSize; hash *= 39; float offsetX = (hash & 63) * coef * Env.BlockSizeHalf - Env.BlockSizeHalf * 0.5f; hash *= 39; float offsetZ = (hash & 63) * coef * Env.BlockSizeHalf - Env.BlockSizeHalf * 0.5f; // Converting the position to a vector adjusts it based on block size and gives us real world coordinates for x, y and z Vector3 vPos = localPos; vPos += new Vector3(offsetX, 0, offsetZ); float x1 = vPos.x - BlockUtils.blockPadding; float x2 = vPos.x + BlockUtils.blockPadding + Env.BlockSize; float y1 = vPos.y - BlockUtils.blockPadding; float y2 = vPos.y + BlockUtils.blockPadding + blockHeight; float z1 = vPos.z - BlockUtils.blockPadding; float z2 = vPos.z + BlockUtils.blockPadding + Env.BlockSize; var verts = pools.Vector3ArrayPool.PopExact(4); var uvs = pools.Vector2ArrayPool.PopExact(4); var colors = pools.Color32ArrayPool.PopExact(4); BlockUtils.PrepareTexture(chunk, ref localPos, uvs, Direction.north, texture, false); // TODO: How do I make sure that if I supply no color value, white is used? // TODO: These colors could be removed and memory would be saved { colors[0] = new Color32(255, 255, 255, 255); colors[1] = new Color32(255, 255, 255, 255); colors[2] = new Color32(255, 255, 255, 255); colors[3] = new Color32(255, 255, 255, 255); } { verts[0] = new Vector3(x1, y1, z2); verts[1] = new Vector3(x1, y2, z2); verts[2] = new Vector3(x2, y2, z1); verts[3] = new Vector3(x2, y1, z1); batcher.AddFace(materialID, verts, colors, uvs, false); } { verts[0] = new Vector3(x2, y1, z1); verts[1] = new Vector3(x2, y2, z1); verts[2] = new Vector3(x1, y2, z2); verts[3] = new Vector3(x1, y1, z2); batcher.AddFace(materialID, verts, colors, uvs, false); } { verts[0] = new Vector3(x2, y1, z2); verts[1] = new Vector3(x2, y2, z2); verts[2] = new Vector3(x1, y2, z1); verts[3] = new Vector3(x1, y1, z1); batcher.AddFace(materialID, verts, colors, uvs, false); } { verts[0] = new Vector3(x1, y1, z1); verts[1] = new Vector3(x1, y2, z1); verts[2] = new Vector3(x2, y2, z2); verts[3] = new Vector3(x2, y1, z2); batcher.AddFace(materialID, verts, colors, uvs, false); } pools.Color32ArrayPool.Push(colors); pools.Vector2ArrayPool.Push(uvs); pools.Vector3ArrayPool.Push(verts); }
public Rect GetTexture(Chunk chunk, ref Vector3Int localPos, Direction direction) { if (m_uvs.Count == 1) { return(m_uvs[0]); } if (m_textureType == TextureConfigType.Connected) { ChunkBlocks blocks = chunk.Blocks; int localPosIndex = Helpers.GetChunkIndex1DFrom3D(localPos.x, localPos.y, localPos.z); ushort blockType = blocks.Get(localPosIndex).Type; // Side blocks bool n_, _e, s_, _w; // Corner blocks bool nw, ne, se, sw; int index1, index2, index3; int sizeWithPadding = chunk.SideSize + Env.ChunkPadding2; int sizeWithPaddingPow2 = sizeWithPadding * sizeWithPadding; switch (direction) { case Direction.up: index1 = localPosIndex + sizeWithPaddingPow2; // + (0,1,0) index2 = index1 - sizeWithPadding; // - (0,0,1) index3 = index1 + sizeWithPadding; // + (0,0,1) sw = blocks.Get(index2 - 1).Type == blockType; // -1,1,-1 s_ = blocks.Get(index2).Type == blockType; // 0,1,-1 se = blocks.Get(index2 + 1).Type == blockType; // 1,1,-1 _w = blocks.Get(index1 - 1).Type == blockType; // -1,1, 0 _e = blocks.Get(index1 + 1).Type == blockType; // 1,1, 0 nw = blocks.Get(index3 - 1).Type == blockType; // -1,1, 1 n_ = blocks.Get(index3).Type == blockType; // 0,1, 1 ne = blocks.Get(index3 + 1).Type == blockType; // 1,1, 1 break; case Direction.down: index1 = localPosIndex - sizeWithPaddingPow2; // - (0,1,0) index2 = index1 - sizeWithPadding; // - (0,0,1) index3 = index1 + sizeWithPadding; // + (0,0,1) sw = blocks.Get(index2 - 1).Type == blockType; // -1,-1,-1 s_ = blocks.Get(index2).Type == blockType; // 0,-1,-1 se = blocks.Get(index2 + 1).Type == blockType; // 1,-1,-1 _w = blocks.Get(index1 - 1).Type == blockType; // -1,-1, 0 _e = blocks.Get(index1 + 1).Type == blockType; // 1,-1, 0 nw = blocks.Get(index3 - 1).Type == blockType; // -1,-1, 1 n_ = blocks.Get(index3).Type == blockType; // 0,-1, 1 ne = blocks.Get(index3 + 1).Type == blockType; // 1,-1, 1 break; case Direction.north: index1 = localPosIndex + sizeWithPadding; // + (0,0,1) index2 = index1 - sizeWithPaddingPow2; // - (0,1,0) index3 = index1 + sizeWithPaddingPow2; // + (0,1,0) sw = blocks.Get(index2 - 1).Type == blockType; // -1,-1,1 se = blocks.Get(index2 + 1).Type == blockType; // 1,-1,1 _w = blocks.Get(index1 - 1).Type == blockType; // -1, 0,1 _e = blocks.Get(index1 + 1).Type == blockType; // 1, 0,1 nw = blocks.Get(index3 - 1).Type == blockType; // -1, 1,1 s_ = blocks.Get(index2).Type == blockType; // 0,-1,1 n_ = blocks.Get(index3).Type == blockType; // 0, 1,1 ne = blocks.Get(index3 + 1).Type == blockType; // 1, 1,1 break; case Direction.south: index1 = localPosIndex - sizeWithPadding; // - (0,0,1) index2 = index1 - sizeWithPaddingPow2; // - (0,1,0) index3 = index1 + sizeWithPaddingPow2; // + (0,1,0) sw = blocks.Get(index2 - 1).Type == blockType; // -1,-1,-1 se = blocks.Get(index2 + 1).Type == blockType; // 1,-1,-1 _w = blocks.Get(index1 - 1).Type == blockType; // -1, 0,-1 _e = blocks.Get(index1 + 1).Type == blockType; // 1, 0,-1 nw = blocks.Get(index3 - 1).Type == blockType; // -1, 1,-1 s_ = blocks.Get(index2).Type == blockType; // 0,-1,-1 n_ = blocks.Get(index3).Type == blockType; // 0, 1,-1 ne = blocks.Get(index3 + 1).Type == blockType; // 1, 1,-1 break; case Direction.east: index1 = localPosIndex + 1; // + (1,0,0) index2 = index1 - sizeWithPaddingPow2; // - (0,1,0) index3 = index1 + sizeWithPaddingPow2; // + (0,1,0) sw = blocks.Get(index2 - sizeWithPadding).Type == blockType; // 1,-1,-1 s_ = blocks.Get(index2).Type == blockType; // 1,-1, 0 se = blocks.Get(index2 + sizeWithPadding).Type == blockType; // 1,-1, 1 _w = blocks.Get(index1 - sizeWithPadding).Type == blockType; // 1, 0,-1 _e = blocks.Get(index1 + sizeWithPadding).Type == blockType; // 1, 0, 1 nw = blocks.Get(index3 - sizeWithPadding).Type == blockType; // 1, 1,-1 n_ = blocks.Get(index3).Type == blockType; // 1, 1, 0 ne = blocks.Get(index3 + sizeWithPadding).Type == blockType; // 1, 1, 1 break; default: //case Direction.west: index1 = localPosIndex - 1; // - (1,0,0) index2 = index1 - sizeWithPaddingPow2; // - (0,1,0) index3 = index1 + sizeWithPaddingPow2; // + (0,1,0) sw = blocks.Get(index2 - sizeWithPadding).Type == blockType; // -1,-1,-1 s_ = blocks.Get(index2).Type == blockType; // -1,-1, 0 se = blocks.Get(index2 + sizeWithPadding).Type == blockType; // -1,-1, 1 _w = blocks.Get(index1 - sizeWithPadding).Type == blockType; // -1, 0,-1 _e = blocks.Get(index1 + sizeWithPadding).Type == blockType; // -1, 0, 1 nw = blocks.Get(index3 - sizeWithPadding).Type == blockType; // -1, 1,-1 n_ = blocks.Get(index3).Type == blockType; // -1, 1, 0 ne = blocks.Get(index3 + sizeWithPadding).Type == blockType; // -1, 1, 1 break; } int uvIndex = ConnectedTextures.GetTexture(n_, _e, s_, _w, nw, ne, se, sw); return(m_uvs[uvIndex]); } if (m_uvs.Count > 1) { int hash = localPos.GetHashCode(); if (hash < 0) { hash *= -1; } float randomNumber = (hash % 100) / 100f; randomNumber *= m_uvs.Count; return(m_uvs[(int)randomNumber]); } Debug.LogError("There were no textures for " + m_textureName); return(new Rect()); }