Skip to content

Commit

Permalink
Add support for multiple frames with their own transforms. Used the c…
Browse files Browse the repository at this point in the history
…ars.vox model as a test case.
  • Loading branch information
Arlorean committed Jun 6, 2023
1 parent 8ef4446 commit 0ce5538
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 21 deletions.
3 changes: 2 additions & 1 deletion Voxels.CommandLine/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ void ConvertFiles(string[] filenames, Color[] pallete) {
var voxelData = ImageToVoxel.Import(filename, pallete);
if (voxelData != null) {
using (var stream = File.Create(Path.ChangeExtension(filename, ".vox"))) {
MagicaVoxel.Write(stream, voxelData);
var magicaVoxel = new MagicaVoxel(voxelData);
magicaVoxel.Write(stream);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions Voxels.CommandLine/Voxels.CommandLine.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\NUnit3TestAdapter.4.5.0\build\net462\NUnit3TestAdapter.props" Condition="Exists('..\packages\NUnit3TestAdapter.4.5.0\build\net462\NUnit3TestAdapter.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -103,5 +104,6 @@
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\SkiaSharp.1.59.1\build\net45\SkiaSharp.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\SkiaSharp.1.59.1\build\net45\SkiaSharp.targets'))" />
<Error Condition="!Exists('..\packages\NUnit3TestAdapter.4.5.0\build\net462\NUnit3TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit3TestAdapter.4.5.0\build\net462\NUnit3TestAdapter.props'))" />
</Target>
</Project>
2 changes: 2 additions & 0 deletions Voxels.CommandLine/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="McMaster.Extensions.CommandLineUtils" version="4.0.2" targetFramework="net48" />
<package id="NUnit.ConsoleRunner" version="3.16.3" targetFramework="net48" />
<package id="NUnit3TestAdapter" version="4.5.0" targetFramework="net48" />
<package id="SkiaSharp" version="1.59.1" targetFramework="net48" />
<package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net48" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net48" />
Expand Down
2 changes: 1 addition & 1 deletion Voxels.Core
Submodule Voxels.Core updated 1 files
+143 −8 MagicaVoxel.cs
4 changes: 2 additions & 2 deletions Voxels.SkiaSharp/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static byte[] RenderSvg(VoxelData voxelData, RenderSettings renderSetting
static SKMatrix44 GetMatrix(VoxelData voxelData, RenderSettings renderSettings) {
var s = renderSettings.Size;
var r = 1.61803398875f;
var d = s / (r*voxelData.size.MaxDimension);
var d = s / (r*voxelData.Size.MaxDimension);
var tran = SKMatrix44.CreateTranslate(s*0.5f, s*0.5f, 0);
var rotx = SKMatrix44.CreateRotationDegrees(1, 0, 0, renderSettings.Pitch);
var roty = SKMatrix44.CreateRotationDegrees(0, 1, 0, renderSettings.Yaw);
Expand All @@ -77,7 +77,7 @@ static SKMatrix44 GetMatrix(VoxelData voxelData, RenderSettings renderSettings)
matrix.PreConcat(rotx);
matrix.PreConcat(roty);
matrix.PreScale(d, -d, d);
matrix.PreTranslate(-voxelData.size.X*0.5f, -voxelData.size.Z*0.5f, voxelData.size.Y*0.5f);
matrix.PreTranslate(-voxelData.Size.X*0.5f, -voxelData.Size.Z*0.5f, voxelData.Size.Y*0.5f);
return matrix;
}

Expand Down
25 changes: 20 additions & 5 deletions Voxels.Test/TestAmbientOcclusion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,52 @@ public void SetUp() {
[Test]
public void TestCase0() {
using (var stream = File.OpenRead("3x3x3.vox")) {
var voxelData = MagicaVoxel.Read(stream);
var magicaVoxel = new MagicaVoxel();
magicaVoxel.Read(stream);

var voxelData = magicaVoxel.Flatten();
Assert.AreEqual(0, AmbientOcclusion.CalculateAO(voxelData, new XYZ(1, 1, 1), XYZ.OneY, XYZ.OneX, XYZ.OneZ));
}
}

[Test]
public void TestCase1() {
using (var stream = File.OpenRead("3x3x3.vox")) {
var voxelData = MagicaVoxel.Read(stream);
var magicaVoxel = new MagicaVoxel();
magicaVoxel.Read(stream);

var voxelData = magicaVoxel.Flatten();
Assert.AreEqual(1, AmbientOcclusion.CalculateAO(voxelData, new XYZ(1, 0, 1), XYZ.OneY, XYZ.OneX, XYZ.OneZ));
Assert.AreEqual(1, AmbientOcclusion.CalculateAO(voxelData, new XYZ(0, 1, 1), XYZ.OneY, XYZ.OneX, XYZ.OneZ));
}
}
[Test]
public void TestCase2() {
using (var stream = File.OpenRead("3x3x3.vox")) {
var voxelData = MagicaVoxel.Read(stream);
var magicaVoxel = new MagicaVoxel();
magicaVoxel.Read(stream);

var voxelData = magicaVoxel.Flatten();
Assert.AreEqual(2, AmbientOcclusion.CalculateAO(voxelData, new XYZ(1, 1, 0), XYZ.OneY, XYZ.OneX, XYZ.OneZ));
}
}
[Test]
public void TestCase3() {
using (var stream = File.OpenRead("3x3x3.vox")) {
var voxelData = MagicaVoxel.Read(stream);
var magicaVoxel = new MagicaVoxel();
magicaVoxel.Read(stream);

var voxelData = magicaVoxel.Flatten();
Assert.AreEqual(3, AmbientOcclusion.CalculateAO(voxelData, new XYZ(2, 2, 2), XYZ.OneY, XYZ.OneX, XYZ.OneZ));
}
}
[Test]
public void TestCaseComplex() {
using (var stream = File.OpenRead("2x2x2.vox")) {
var voxelData = MagicaVoxel.Read(stream);
var magicaVoxel = new MagicaVoxel();
magicaVoxel.Read(stream);

var voxelData = magicaVoxel.Flatten();
Assert.AreEqual(0, AmbientOcclusion.CalculateAO(voxelData, new XYZ(0, 0, 0), XYZ.OneY, XYZ.OneX, XYZ.OneZ));
Assert.AreEqual(0, AmbientOcclusion.CalculateAO(voxelData, new XYZ(0, 1, 1), XYZ.OneX, -XYZ.OneZ, -XYZ.OneY));
Assert.AreEqual(0, AmbientOcclusion.CalculateAO(voxelData, new XYZ(1, 0, 1), -XYZ.OneZ, XYZ.OneY, -XYZ.OneX));
Expand Down
38 changes: 32 additions & 6 deletions Voxels.Test/TestMagicaVoxel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,54 @@ public void SetUp() {
[Test]
public void Test1x2x3() {
using (var stream = File.OpenRead("1x2x3.vox")) {
var voxelData = MagicaVoxel.Read(stream);
var magicaVoxel = new MagicaVoxel();
magicaVoxel.Read(stream);

var voxelData = magicaVoxel.Flatten();
Assert.AreEqual(6, voxelData.Count);
Assert.AreEqual(1, voxelData.size.X);
Assert.AreEqual(2, voxelData.size.Y);
Assert.AreEqual(3, voxelData.size.Z);
Assert.AreEqual(1, voxelData.Size.X);
Assert.AreEqual(2, voxelData.Size.Y);
Assert.AreEqual(3, voxelData.Size.Z);
}
}

[Test]
public void Test3x3x3() {
using (var stream = File.OpenRead("3x3x3.vox")) {
var voxelData = MagicaVoxel.Read(stream);
var magicaVoxel = new MagicaVoxel();
magicaVoxel.Read(stream);

var voxelData = magicaVoxel.Flatten();
Assert.AreEqual(20, voxelData.Count);
}
}

[Test]
public void Test8x8x8() {
using (var stream = File.OpenRead("8x8x8.vox")) {
var voxelData = MagicaVoxel.Read(stream);
var magicaVoxel = new MagicaVoxel();
magicaVoxel.Read(stream);

var voxelData = magicaVoxel.Flatten();
Assert.AreEqual(80, voxelData.Count);
}
}

[Test]
public void TestCars() {
using (var stream = File.OpenRead("cars.vox")) {
var magicaVoxel = new MagicaVoxel();
magicaVoxel.Read(stream);

Assert.AreEqual(200, magicaVoxel.Version);
Assert.AreEqual(2, magicaVoxel.Models.Count);

Assert.AreEqual(3132, magicaVoxel.Models[0].Count);
Assert.AreEqual(3235, magicaVoxel.Models[1].Count);

Assert.AreEqual(3132 + 3235, magicaVoxel.Flatten().Count);
}
}

}
}
22 changes: 17 additions & 5 deletions Voxels.Test/Voxels.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\NUnit.3.13.3\build\NUnit.props" Condition="Exists('..\packages\NUnit.3.13.3\build\NUnit.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand All @@ -10,6 +11,8 @@
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -36,8 +39,8 @@
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="nunit.framework, Version=3.7.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.7.1\lib\net45\nunit.framework.dll</HintPath>
<Reference Include="nunit.framework, Version=3.13.3.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.13.3\lib\net45\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
</ItemGroup>
Expand All @@ -63,9 +66,6 @@
<Content Include="3x3x1.vox">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
Expand All @@ -76,5 +76,17 @@
<Name>Voxels</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="cars.vox">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSHARP.Targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\NUnit.3.13.3\build\NUnit.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit.3.13.3\build\NUnit.props'))" />
</Target>
</Project>
Binary file added Voxels.Test/cars.vox
Binary file not shown.
2 changes: 1 addition & 1 deletion Voxels.Test/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NUnit" version="3.7.1" targetFramework="net461" />
<package id="NUnit" version="3.13.3" targetFramework="net48" />
</packages>

0 comments on commit 0ce5538

Please sign in to comment.