static void FillParcel(IntermediateMap map, Parcel parcel, List <Parcel> loadedParcels, int offsetX, int offsetY, int offsetZ, bool hasDonePalette, ParcelJsonConfiguration configuration) { //If no sourcefile, skip, well, sourcefile! and just plop in plots if (!emptySourceFileNames.Contains(parcel.sourceFile)) { ParcelVoxReader parcelReader = new ParcelVoxReader(); VoxReader r = new VoxReader(parcel.sourceFile, parcelReader); r.Read(); parcelReader.insertReadParcel(map, offsetX, offsetY, offsetZ); //Do the palette if (!hasDonePalette) { hasDonePalette = loadPalette(map, parcelReader.palette); } } foreach (Plot plot in parcel.parcelPlots) { Parcel insertParcel = findMatchingParcel(plot, parcel.parcelPlots, loadedParcels, configuration); FillParcel(map, insertParcel, loadedParcels, offsetX + plot.offsetX, offsetY + plot.offsetY, offsetZ + plot.offsetZ, hasDonePalette, configuration); } }
private void Start() { Loader loader = new Loader(); string path = Application.dataPath + "\\Puzzles\\Vox\\" + nameOfVoxFile; VoxReader reader = new VoxReader(@path, loader); reader.Read(); #if UNITY_EDITOR Puzzle newPuzzle = ScriptableObject.CreateInstance <Puzzle>(); byte[] flatData = new byte[loader.SizeX * loader.SizeY * loader.SizeZ]; for (int i = 0; i < loader.SizeX; i++) { for (int j = 0; j < loader.SizeY; j++) { for (int k = 0; k < loader.SizeZ; k++) { flatData[k + j * loader.SizeZ + i * loader.SizeY * loader.SizeZ] = loader.Data[i, j, k]; } } } newPuzzle.Palette = loader.Palette; newPuzzle.Data = flatData; newPuzzle.SizeX = loader.SizeX; newPuzzle.SizeY = loader.SizeY; newPuzzle.SizeZ = loader.SizeZ; string name = AssetDatabase.GenerateUniqueAssetPath("Assets/Puzzles/" + newPuzzleName + ".asset"); AssetDatabase.CreateAsset(newPuzzle, name); AssetDatabase.SaveAssets(); #endif }
public void VoxReader_Read_ModelCountIsCorrect(string file, int expectedCount) { file = Zip.UnzipFilesFromSevenZipArchive(file).First(); IVoxFile voxFile = VoxReader.Read(file); voxFile.Models.Should().HaveCount(expectedCount); }
public void VoxReader_Read_VoxelCountIsCorrectForSmallestModel() { string file = Zip.UnzipFilesFromSevenZipArchive(TestFile_1x1).First(); IVoxFile voxFile = VoxReader.Read(file); IModel model = voxFile.Models.First(); model.Voxels.Should().HaveCount(1); }
public void VoxReader_Read_VoxelColorIsCorrectForSmallestModel() { string file = Zip.UnzipFilesFromSevenZipArchive(TestFile_1x1).First(); IVoxFile voxFile = VoxReader.Read(file); IModel model = voxFile.Models.First(); model.Voxels.First(voxel => voxel.Position == new Vector3(0, 0, 0)).Color.Should().Be(new Color(123, 162, 63, 255)); }
public void VoxReader_Read_VoxelColorIsCorrectForLargestModel() { string file = Zip.UnzipFilesFromSevenZipArchive(TestFile_256x256).First(); IVoxFile voxFile = VoxReader.Read(file); IModel model = voxFile.Models.First(); foreach (Voxel voxel in model.Voxels) { voxel.Color.Should().Be(new Color(123, 162, 63, 255)); } }
public void VoxReader_Read_VoxelColorIsCorrect() { string file = Zip.UnzipFilesFromSevenZipArchive(TestFile_3x3).First(); IVoxFile voxFile = VoxReader.Read(file); IModel model = voxFile.Models.First(); model.Voxels.First(voxel => voxel.Position == new Vector3(0, 0, 0)).Color.Should().Be(new Color(255, 177, 27, 255)); model.Voxels.First(voxel => voxel.Position == new Vector3(2, 0, 0)).Color.Should().Be(new Color(203, 64, 66, 255)); model.Voxels.First(voxel => voxel.Position == new Vector3(0, 2, 0)).Color.Should().Be(new Color(27, 129, 62, 255)); model.Voxels.First(voxel => voxel.Position == new Vector3(0, 0, 2)).Color.Should().Be(new Color(0, 92, 175, 255)); }
public void VoxReader_Read_VoxelCountIsCorrect(string file, params int[] expectedCount) { file = Zip.UnzipFilesFromSevenZipArchive(file).First(); IVoxFile voxFile = VoxReader.Read(file); var models = voxFile.Models; models.Should().HaveCount(expectedCount.Length); for (int i = 0; i < expectedCount.Length; i++) { models[i].Voxels.Should().HaveCount(expectedCount[i]); } }
// Format refer to // https://github.com/ephtracy/voxel-model/blob/master/MagicaVoxel-file-format-vox.txt public static IEnumerable <VoxelData> ParseFrom(string path, out Color32[] palette) { List <VoxelData> outputData = new List <VoxelData>(); using (VoxReader reader = new VoxReader(path)) { reader.Read(); foreach (var model in reader.m_Models) { VoxelData data = CreateInstance <VoxelData>(); data.m_Width = model.m_SizeX; data.m_Height = model.m_SizeY; data.m_Depth = model.m_SizeZ; data.m_RawData = new Voxel[model.m_SizeX * model.m_SizeY * model.m_SizeZ]; foreach (var voxel in model.m_Voxels) { if (voxel.colourIndex == 0) { Debug.LogWarning("Colour Index 0 found!"); } data.m_RawData[data.GetRawIndex(voxel.x, voxel.y, voxel.z)] = new Voxel(voxel.colourIndex); } outputData.Add(data); } palette = new Color32[reader.m_Palette.Length]; for (int i = 0; i < palette.Length; ++i) { palette[i] = reader.GetColour(i); } } return(outputData); }
public override void OnCreate() { try { Console.WriteLine(ShaderPreprocessor.Execute(@"res\shaders\raytracing\fragment.glsl")); Shader voxelVertexShader = new Shader(ShaderType.VertexShader, ShaderPreprocessor.Execute(@"res\shaders\raytracing\vertex.glsl")); Shader voxelFragmentShader = new Shader(ShaderType.FragmentShader, ShaderPreprocessor.Execute(@"res\shaders\raytracing\fragment.glsl")); Shader scaleFragmentShader = new Shader(ShaderType.FragmentShader, ShaderPreprocessor.Execute(@"res\shaders\raytracing\scale.glsl")); _voxelRenderer = new VoxelRenderer(new ShaderProgram(new[] { voxelVertexShader, voxelFragmentShader }), new ShaderProgram(new[] { voxelVertexShader, scaleFragmentShader })); Shader spriteVertexShader = new Shader(ShaderType.VertexShader, ShaderPreprocessor.Execute(@"res\shaders\ui\vertex.glsl")); Shader spriteFragmentShader = new Shader(ShaderType.FragmentShader, ShaderPreprocessor.Execute(@"res\shaders\ui\fragment.glsl")); _spriteRenderer = new SpriteRenderer(new ShaderProgram(new[] { spriteVertexShader, spriteFragmentShader })); } catch (Exception ex) { Console.WriteLine(ex.ToString()); throw; } _voxelRenderer.GenerateFramebuffer(window); //List<Material> materials = new List<Material>(); //for (int i = 0; i < 256; i++) // materials.Add(new Material(Vector3.One, 1.5f)); //_voxelRenderer.Materials = materials; Console.WriteLine("Shader compiled <o/"); //epic it work _model = _voxelRenderer.CreateModel(32, 32, 32, new Transform(new Vector3(24.0f, 0.0f, 0.0f), Vector3.Zero, new Vector3(0.5f))); for (int x = 0; x < 32; x++) { for (int y = 0; y < 32; y++) { for (int z = 0; z < 32; z++) { _model[x, y, z] = (byte)((x + y + z) & 1);//new Voxel((ushort) ((x + y + z) & 1)); } } } MyVoxLoader CastleVox = new MyVoxLoader(); VoxReader r = new VoxReader(@"res\maps\map.vox", CastleVox); r.Read(); //Use palette of castlevox List <Material> materials = new List <Material>(); for (int i = 0; i < 256; i++) { Vector3 color = new Vector3((float)CastleVox._materials[i].r / 255f, (float)CastleVox._materials[i].g / 255f, (float)CastleVox._materials[i].b / 255f); float ior = 1.01f; if (i == 252) { ior = 1.1f; } if (i == 254) { ior = 1.1f; } //Vector3 color = new Vector3(1f, 0f, 0f); materials.Add(new Material(color, ior)); } _voxelRenderer.Materials.Set(materials); _model2 = _voxelRenderer.CreateModel(CastleVox.Width, CastleVox.Height, CastleVox.Depth, new Transform(new Vector3(-24.0f, 0.0f, 0.0f), new Vector3(0.0f, 0.1f, 0.0f), new Vector3(0.5f))); //for (int x = -16; x < 16; x++) //for (int y = -16; y < 16; y++) //for (int z = -16; z < 16; z++) // _model2[x + 16, y + 16, z + 16] = (x * x + y * y + z * z < 16 * 16) ? (byte)1 : (byte)0; for (int x = 0; x < CastleVox.Width; x++) { for (int y = 0; y < CastleVox.Height; y++) { for (int z = 0; z < CastleVox.Depth; z++) { _model2[x, y, z] = CastleVox._data[x, y, z]; } } } Console.WriteLine("Epic"); //int s = 512; //VoxelModel model3 = _voxelRenderer.CreateModel(s,1,s, // new Transform(new Vector3(0.0f, -48.0f, 0.0f), Vector3.Zero, new Vector3(1.0f))); //for (int x = 0; x < s; x++) //for (int y = 0; y < 1; y++) //for (int z = 0; z < s; z++) // model3[x,y,z] = (byte)1; Console.WriteLine("Epic"); Sprite crosshair = new Sprite(new Texture("res/textures/crosshair.png", TextureMinFilter.Linear, TextureMagFilter.Linear)); _spriteRenderer.Add(crosshair); crosshair.Colour = new Colour(1.0f, 1.0f, 1.0f, 1.0f); crosshair.Transform.Scale = new Vector3(128.0f, 128.0f, 0.25f); List <DirectionalLight> dirLights = new List <DirectionalLight>(); var sun = new DirectionalLight(); sun.direction = new Vector3(-0.5f, 1.5f, -1.0f); sun.intensity = 0.8f; sun.colour = new Vector3(1f, 1f, 1f); dirLights.Add(sun); var sun2 = new DirectionalLight(); sun2.direction = new Vector3(-0.4f, 1.5f, 0.8f); sun2.intensity = 0.6f; sun2.colour = new Vector3(0.7f, 0.8f, 1f); dirLights.Add(sun2); var sun3 = new DirectionalLight(); sun3.direction = new Vector3(0.4f, 0.1f, 0.2f); sun3.intensity = 0.6f; sun3.colour = new Vector3(0.4f, 0.8f, 1f); dirLights.Add(sun3); _voxelRenderer.DirectionalLights = dirLights; List <PointLight> pointLights = new List <PointLight>(); var pointlight = new PointLight(); pointlight.position = new Vector3(0f, 0f, -20f); pointlight.intensity = 0.5f; pointlight.colour = new Vector3(1f, 0.0f, 0.0f); pointLights.Add(pointlight); _voxelRenderer.PointLights = pointLights; MyVoxLoader CharBodyVox = new MyVoxLoader(); VoxReader rCharBody = new VoxReader(@"res\char1_body.vox", CharBodyVox); rCharBody.Read(); //Use palette of castlevox //List<Material> materialsChar = new List<Material>(); //for (int i = 0; i < 256; i++) //{ // Vector3 color = new Vector3((float)CharBodyVox._materials[i].r / 255f, (float)CharBodyVox._materials[i].g / 255f, (float)CharBodyVox._materials[i].b / 255f); // float ior = 1.01f; // if (i == 252) ior = 1.1f; // if (i == 254) ior = 1.1f; // //Vector3 color = new Vector3(1f, 0f, 0f); // materialsChar.Add(new Material(color, ior)); //} //_voxelRenderer.Materials.Set(materialsChar); TcpConnection connection = new TcpConnection(IPAddress.Parse("127.0.0.1"), 42069, (IConnection c, byte[] data) => { //Console.WriteLine("Received packet..."); if (data.Length > 1) { if (data[0] == 1) { Console.WriteLine("Received our player id!"); _playerId = BitConverter.ToUInt64(data, 1); _mainPlayer = new Player(data[1], _playerId, true); _mainPlayer._world = _model2; _mainPlayer._modelBody = _voxelRenderer.CreateModel(CharBodyVox.Width, CharBodyVox.Height, CharBodyVox.Depth, new Transform(new Vector3(-24.0f, 0.0f, 0.0f), new Vector3(0.0f, 0.1f, 0.0f), new Vector3(0.5f))); //for (int x = 0; x < CharBodyVox.Width; x++) // for (int y = 0; y < CharBodyVox.Height; y++) // for (int z = 0; z < CharBodyVox.Depth; z++) // _mainPlayer._modelBody[x, y, z] = CharBodyVox._data[x, y, z]; //Commenting this line out will give you access to the FreeCamera //_camera = _mainPlayer.camera; _entityManager.Add(_mainPlayer); } else if (data[0] == 0) { //Not sure if myId should be data[5] or _playerId if (NetworkEntity.HandlePacket(_entityManager, data, _playerId) == false) { //Player doesn't exist yet Console.WriteLine("Adding new player"); Player player = new Player(data[1], data[5], false); player._modelBody = _voxelRenderer.CreateModel(CharBodyVox.Width, CharBodyVox.Height, CharBodyVox.Depth, new Transform(new Vector3(-24.0f, 0.0f, 0.0f), new Vector3(0.0f, 0.1f, 0.0f), new Vector3(0.5f))); for (int x = 0; x < CharBodyVox.Width; x++) { for (int y = 0; y < CharBodyVox.Height; y++) { for (int z = 0; z < CharBodyVox.Depth; z++) { player._modelBody[x, y, z] = CharBodyVox._data[x, y, z]; } } } _entityManager.Add(player); } } } }); _packetSender.AddReceiver(connection); }