/// <summary> /// Takes a 'preloadedchunk' and forms a loaded chunk /// This must be called from the main thread. /// </summary> /// <param name="pChunk"></param> /// <returns></returns> private LoadedChunk2 CreateChunk(PreLoadedChunk pChunk, ChunkData cd) { GameObject cObj = Instantiate(ChunkPrefab); cObj.transform.parent = ChunkHolder.transform; cObj.transform.position = pChunk.Position.AsVector3() * World.ChunkSize; cObj.name = "chunk_" + pChunk.Position; LoadedChunk2 loaded = cObj.GetComponent <LoadedChunk2>(); MeshFilter mf = loaded.GetComponent <MeshFilter>(); //Create the terrain mesh mf.mesh = PreLoadedChunk.CreateMesh(pChunk.TerrainMesh); mf.mesh.RecalculateNormals(); MeshCollider mc = loaded.GetComponent <MeshCollider>(); mc.sharedMesh = mf.mesh; //Iterate all voxels foreach (Voxel v in MiscUtils.GetValues <Voxel>()) { if (v == Voxel.none) { continue; } if (pChunk.VoxelMesh.TryGetValue(v, out PreMesh pmesh)) { GameObject voxelObj = Instantiate(ChunkVoxelPrefab); MeshFilter voxelMf = voxelObj.GetComponent <MeshFilter>(); voxelMf.mesh = PreLoadedChunk.CreateMesh(pmesh); Material voxMat = ResourceManager.GetVoxelMaterial(v); //Debug.Log(voxMat + " vox mat"); voxelObj.GetComponent <MeshRenderer>().material = voxMat; MeshCollider voxelMc = voxelObj.GetComponent <MeshCollider>(); voxelMc.sharedMesh = voxelMf.mesh; voxelObj.transform.parent = cObj.transform; voxelObj.transform.localPosition = Vector3.zero; voxelMf.mesh.RecalculateNormals(); } } if (cd.WorldObjects != null) { foreach (WorldObjectData objDat in cd.WorldObjects) { WorldObject obj = WorldObject.CreateWorldObject(objDat, cObj.transform, 0.7f); //if(objDat.AutoHeight) // obj.AdjustHeight(); //obj.transform.position = objDat.Position; //obj.transform.localPosition = objDat.Position.Mod(World.ChunkSize); } } loaded.SetChunk(pChunk.ChunkData); return(loaded); }
private LoadedChunk2 CreateChunk(PreLoadedChunk pChunk) { GameObject cObj = Instantiate(ChunkPrefab); cObj.transform.parent = transform; cObj.name = "chunk_" + pChunk.Position; LoadedChunk2 loaded = cObj.GetComponent<LoadedChunk2>(); MeshFilter mf = loaded.GetComponent<MeshFilter>(); //Create the terrain mesh mf.mesh = PreLoadedChunk.CreateMesh(pChunk.TerrainMesh); mf.mesh.RecalculateNormals(); MeshCollider mc = loaded.GetComponent<MeshCollider>(); mc.sharedMesh = mf.mesh; //Iterate all voxels foreach (Voxel v in MiscUtils.GetValues<Voxel>()) { if (v == Voxel.none) continue; if (pChunk.VoxelMesh.TryGetValue(v, out PreMesh pmesh)) { GameObject voxelObj = Instantiate(ChunkVoxelPrefab); MeshFilter voxelMf = voxelObj.GetComponent<MeshFilter>(); MeshRenderer voxelMr = voxelObj.GetComponent<MeshRenderer>(); voxelMr.material = ResourceManager.GetVoxelMaterial(v); voxelMf.mesh = PreLoadedChunk.CreateMesh(pmesh); MeshCollider voxelMc = voxelObj.GetComponent<MeshCollider>(); voxelMc.sharedMesh = voxelMf.mesh; voxelObj.transform.parent = cObj.transform; voxelObj.transform.localPosition = Vector3.up * (pChunk.ChunkData.BaseHeight + 0.7f); voxelMf.mesh.RecalculateNormals(); } } cObj.transform.position = pChunk.Position.AsVector3() * World.ChunkSize; loaded.SetChunk(pChunk.ChunkData); if (pChunk.ChunkData.WorldObjects != null) { foreach (WorldObjectData objData in pChunk.ChunkData.WorldObjects) { Vec2i localPos = Vec2i.FromVector3(objData.Position.Mod(World.ChunkSize)); float off = loaded.Chunk.GetHeight(localPos); WorldObject obj = WorldObject.CreateWorldObject(objData, loaded.transform, off + 0.7f); obj.AdjustHeight(); } } return loaded; }