private static void PushFace(SingleMeshBuildInfo current, Vector2 texturePos, byte[] light, bool flip, Color color) { // apply light to verts for (int i = 0; i < light.Length; i++) { current.colors.Add(color * lightColors[light[i]]); } // create triangles int f = flip ? 1 : 0; int faceCount = current.faceCount; current.triangles.Add(faceCount * 4 + ((0 + f) % 4)); current.triangles.Add(faceCount * 4 + ((1 + f) % 4)); current.triangles.Add(faceCount * 4 + ((2 + f) % 4)); current.triangles.Add(faceCount * 4 + ((0 + f) % 4)); current.triangles.Add(faceCount * 4 + ((2 + f) % 4)); current.triangles.Add(faceCount * 4 + ((3 + f) % 4)); // apply textures float tUnit = 1f / T_SIZE; current.uv.Add(new Vector2(tUnit * texturePos.x + tUnit, tUnit * texturePos.y)); current.uv.Add(new Vector2(tUnit * texturePos.x + tUnit, tUnit * texturePos.y + tUnit)); current.uv.Add(new Vector2(tUnit * texturePos.x, tUnit * texturePos.y + tUnit)); current.uv.Add(new Vector2(tUnit * texturePos.x, tUnit * texturePos.y)); // count number of faces in the mesh current.faceCount++; }
ChunkRenderer MakeChunk(Vector3 scenePosition, string name, SingleMeshBuildInfo meshInfo, Material material) { GameObject obj; ChunkRenderer comp; MeshFilter filter; // create or reuse gameobject if (pooledInstances.Count > 0) { comp = pooledInstances[0]; obj = comp.gameObject; obj.transform.position = scenePosition; pooledInstances.RemoveAt(0); obj.SetActive(true); } else { obj = Instantiate(chunkPrefab, scenePosition, Quaternion.identity) as GameObject; obj.transform.parent = transform; comp = obj.GetComponent <ChunkRenderer>(); } filter = obj.GetComponent <MeshFilter>(); // apply new settings obj.name = name; meshInfo.ApplyToMesh(filter.mesh); obj.renderer.material = material; return(comp); }
public static void CubeTop(SingleMeshBuildInfo current, int x, int y, int z, TextureLayout tex, Vector3 center, Vector3 size, byte[] l, bool smooth, Color color) { Vector3 pos = new Vector3(x, y, z); current.vertices.Add(pos + Vertex.tNW(center, size)); current.vertices.Add(pos + Vertex.tNE(center, size)); current.vertices.Add(pos + Vertex.tSE(center, size)); current.vertices.Add(pos + Vertex.tSW(center, size)); // light array format // N // 0 1 2 // W 3 4 5 E // 6 7 8 // S // get light at each corner byte[] corners = new byte[4]; if (smooth) { corners[0] = LightAverage(l[0], l[1], l[3], l[4]); corners[1] = LightAverage(l[1], l[2], l[4], l[5]); corners[2] = LightAverage(l[4], l[5], l[7], l[8]); corners[3] = LightAverage(l[3], l[4], l[6], l[7]); } else { for (int i = 0; i < 4; i++) { corners[i] = l[4]; } } // decide whether to flip triangles bool flip = !smooth ? false : FlipTriangles(corners[0], corners[1], corners[2], corners[3]); // finish up the face byte textureID = tex.texTop; PushFace(current, TextureLocation(textureID), corners, flip, color); }
public void Render(MeshBuildInfo current, World world, Vector3i chunkPos, int x, int y, int z) { SingleMeshBuildInfo transparent = current.transparent; float height = amount * .85f; Vector3 center = new Vector3(.5f, height * .5f, .5f); Vector3 size = new Vector3(1, height, 1); ushort def = Block.DIRT; byte[] l = new byte[9]; Color blockColor = color; int xx, yy, zz; if (world.GetBlockAt(chunkPos, x, y + 1, z, def) != block) { if (smoothLighting) { for (zz = -1; zz <= 1; zz++) { for (xx = -1; xx <= 1; xx++) { l [3 * (zz + 1) + (xx + 1)] = world.GetLightAt(chunkPos, xx + x, y + 1, -zz + z, 0); } } } else { l [4] = world.GetLightAt(chunkPos, x, y + 1, z, 0); } CubeRenderHelper.CubeTop(transparent, x, y, z, layout, center, size, l, smoothLighting, blockColor); } if (world.GetBlockAt(chunkPos, x, y - 1, z, def) != block) { if (smoothLighting) { for (zz = -1; zz <= 1; zz++) { for (xx = -1; xx <= 1; xx++) { l [3 * (zz + 1) + (xx + 1)] = world.GetLightAt(chunkPos, xx + x, y - 1, -zz + z, 0); } } } else { l [4] = world.GetLightAt(chunkPos, x, y - 1, z, 0); } CubeRenderHelper.CubeBottom(transparent, x, y, z, layout, center, size, l, smoothLighting, blockColor); } if (world.GetBlockAt(chunkPos, x + 1, y, z, def) != block) { if (smoothLighting) { for (zz = -1; zz <= 1; zz++) { for (yy = -1; yy <= 1; yy++) { l [3 * (zz + 1) + (yy + 1)] = world.GetLightAt(chunkPos, x + 1, yy + y, -zz + z, 0); } } } else { l [4] = world.GetLightAt(chunkPos, x + 1, y, z, 0); } CubeRenderHelper.CubeEast(transparent, x, y, z, layout, center, size, l, smoothLighting, blockColor); } if (world.GetBlockAt(chunkPos, x - 1, y, z, def) != block) { if (smoothLighting) { for (zz = -1; zz <= 1; zz++) { for (yy = -1; yy <= 1; yy++) { l [3 * (zz + 1) + (yy + 1)] = world.GetLightAt(chunkPos, x - 1, yy + y, -zz + z, 0); } } } else { l [4] = world.GetLightAt(chunkPos, x - 1, y, z, 0); } CubeRenderHelper.CubeWest(transparent, x, y, z, layout, center, size, l, smoothLighting, blockColor); } if (world.GetBlockAt(chunkPos, x, y, z + 1, def) != block) { if (smoothLighting) { for (yy = -1; yy <= 1; yy++) { for (xx = -1; xx <= 1; xx++) { l [3 * (yy + 1) + (xx + 1)] = world.GetLightAt(chunkPos, xx + x, -yy + y, z + 1, 0); } } } else { l [4] = world.GetLightAt(chunkPos, x, y, z + 1, 0); } CubeRenderHelper.CubeNorth(transparent, x, y, z, layout, center, size, l, smoothLighting, blockColor); } if (world.GetBlockAt(chunkPos, x, y, z - 1, def) != block) { if (smoothLighting) { for (yy = -1; yy <= 1; yy++) { for (xx = -1; xx <= 1; xx++) { l [3 * (yy + 1) + (xx + 1)] = world.GetLightAt(chunkPos, xx + x, -yy + y, z - 1, 0); } } } else { l [4] = world.GetLightAt(chunkPos, x, y, z - 1, 0); } CubeRenderHelper.CubeSouth(transparent, x, y, z, layout, center, size, l, smoothLighting, blockColor); } }
public void Render(MeshBuildInfo current, World world, Vector3i chunkPos, int x, int y, int z) { SingleMeshBuildInfo opaque = current.opaque; Vector3 center = Vector3.one / 2f; Vector3 size = Vector3.one; ushort def = Block.DIRT; byte[] l = new byte[9]; Color blockColor = color; int xx, yy, zz; if (!Block.GetInstance(world.GetBlockAt(chunkPos, x, y + 1, z, def)).opaque) { if (smoothLighting) { for (zz = -1; zz <= 1; zz++) { for (xx = -1; xx <= 1; xx++) { l [3 * (zz + 1) + (xx + 1)] = world.GetLightAt(chunkPos, xx + x, y + 1, -zz + z, 0); } } } else { l [4] = world.GetLightAt(chunkPos, x, y + 1, z, 0); } CubeRenderHelper.CubeTop(opaque, x, y, z, layout, center, size, l, smoothLighting, blockColor); } if (!Block.GetInstance(world.GetBlockAt(chunkPos, x, y - 1, z, def)).opaque) { if (smoothLighting) { for (zz = -1; zz <= 1; zz++) { for (xx = -1; xx <= 1; xx++) { l [3 * (zz + 1) + (xx + 1)] = world.GetLightAt(chunkPos, xx + x, y - 1, -zz + z, 0); } } } else { l [4] = world.GetLightAt(chunkPos, x, y - 1, z, 0); } CubeRenderHelper.CubeBottom(opaque, x, y, z, layout, center, size, l, smoothLighting, blockColor); } if (!Block.GetInstance(world.GetBlockAt(chunkPos, x + 1, y, z, def)).opaque) { if (smoothLighting) { for (zz = -1; zz <= 1; zz++) { for (yy = -1; yy <= 1; yy++) { l [3 * (zz + 1) + (yy + 1)] = world.GetLightAt(chunkPos, x + 1, yy + y, -zz + z, 0); } } } else { l [4] = world.GetLightAt(chunkPos, x + 1, y, z, 0); } CubeRenderHelper.CubeEast(opaque, x, y, z, layout, center, size, l, smoothLighting, blockColor); } if (!Block.GetInstance(world.GetBlockAt(chunkPos, x - 1, y, z, def)).opaque) { if (smoothLighting) { for (zz = -1; zz <= 1; zz++) { for (yy = -1; yy <= 1; yy++) { l [3 * (zz + 1) + (yy + 1)] = world.GetLightAt(chunkPos, x - 1, yy + y, -zz + z, 0); } } } else { l [4] = world.GetLightAt(chunkPos, x - 1, y, z, 0); } CubeRenderHelper.CubeWest(opaque, x, y, z, layout, center, size, l, smoothLighting, blockColor); } if (!Block.GetInstance(world.GetBlockAt(chunkPos, x, y, z + 1, def)).opaque) { if (smoothLighting) { for (yy = -1; yy <= 1; yy++) { for (xx = -1; xx <= 1; xx++) { l [3 * (yy + 1) + (xx + 1)] = world.GetLightAt(chunkPos, xx + x, -yy + y, z + 1, 0); } } } else { l [4] = world.GetLightAt(chunkPos, x, y, z + 1, 0); } CubeRenderHelper.CubeNorth(opaque, x, y, z, layout, center, size, l, smoothLighting, blockColor); } if (!Block.GetInstance(world.GetBlockAt(chunkPos, x, y, z - 1, def)).opaque) { if (smoothLighting) { for (yy = -1; yy <= 1; yy++) { for (xx = -1; xx <= 1; xx++) { l [3 * (yy + 1) + (xx + 1)] = world.GetLightAt(chunkPos, xx + x, -yy + y, z - 1, 0); } } } else { l [4] = world.GetLightAt(chunkPos, x, y, z - 1, 0); } CubeRenderHelper.CubeSouth(opaque, x, y, z, layout, center, size, l, smoothLighting, blockColor); } }
public MeshBuildInfo() { opaque = new SingleMeshBuildInfo(); transparent = new SingleMeshBuildInfo(); }