Przeglądaj źródła

makeIncremental tool

David Catuhe 12 lat temu
rodzic
commit
750cde3f59

+ 6 - 0
Tools/MakeIncremental/App.config

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<configuration>
+    <startup> 
+        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
+    </startup>
+</configuration>

+ 63 - 0
Tools/MakeIncremental/MakeIncremental.csproj

@@ -0,0 +1,63 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProjectGuid>{E0855FC6-7205-4621-A975-7A8F2886B738}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>MakeIncremental</RootNamespace>
+    <AssemblyName>MakeIncremental</AssemblyName>
+    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+    <SccProjectName>SAK</SccProjectName>
+    <SccLocalPath>SAK</SccLocalPath>
+    <SccAuxPath>SAK</SccAuxPath>
+    <SccProvider>SAK</SccProvider>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <PlatformTarget>AnyCPU</PlatformTarget>
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <PlatformTarget>AnyCPU</PlatformTarget>
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL" />
+    <Reference Include="System" />
+    <Reference Include="System.Core" />
+    <Reference Include="System.Xml.Linq" />
+    <Reference Include="System.Data.DataSetExtensions" />
+    <Reference Include="Microsoft.CSharp" />
+    <Reference Include="System.Data" />
+    <Reference Include="System.Xml" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Program.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <None Include="App.config" />
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
+       Other similar extension points exist, see Microsoft.Common.targets.
+  <Target Name="BeforeBuild">
+  </Target>
+  <Target Name="AfterBuild">
+  </Target>
+  -->
+</Project>

+ 251 - 0
Tools/MakeIncremental/Program.cs

@@ -0,0 +1,251 @@
+using System;
+using System.IO;
+using System.Linq;
+using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
+
+namespace MakeIncremental
+{
+    class Program
+    {
+        static void Main(string[] args)
+        {
+            if (args.Length < 1)
+            {
+                DisplayUsage();
+                return;
+            }
+
+            // Parsing arguments
+            string input = "";
+            bool extractTextures = false;
+            foreach (var arg in args)
+            {
+                var order = arg.Substring(0, 3);
+
+                switch (order)
+                {
+                    case "/i:":
+                        input = arg.Substring(3);
+                        break;
+                    case "/textures":
+                        extractTextures = true;
+                        break;
+                    default:
+                        DisplayUsage();
+                        return;
+                }
+            }
+
+            if (string.IsNullOrEmpty(input))
+            {
+                DisplayUsage();
+                return;
+            }
+
+            ProcessSourceFile(input, extractTextures);
+        }
+
+        static string CreateDelayLoadingFile(dynamic mesh, string outputDir, string rootFilename)
+        {
+            var outputPath = Path.Combine(outputDir, rootFilename + "." + mesh.name + ".babylonmeshdata");
+
+            var result = new JObject();
+            result["positions"] = mesh.positions;
+            result["indices"] = mesh.indices;
+            result["normals"] = mesh.normals;
+
+            if (mesh.uvs != null)
+            {
+                result["uvs"] = mesh.uvs;
+            }
+
+            if (mesh.uvs2 != null)
+            {
+                result["uvs2"] = mesh.uvs2;
+            }
+
+            if (mesh.colors != null)
+            {
+                result["colors"] = mesh.colors;
+            }
+
+            if (mesh.matricesIndices != null)
+            {
+                result["matricesIndices"] = mesh.matricesIndices;
+            }
+
+            if (mesh.matricesWeights != null)
+            {
+                result["matricesWeights"] = mesh.matricesWeights;
+            }
+
+            if (mesh.subMeshes != null)
+            {
+                result["subMeshes"] = mesh.subMeshes;
+            }
+
+            string json = result.ToString(Formatting.None);
+
+            using (var writer = new StreamWriter(outputPath))
+            {
+                writer.Write(json);
+            }
+
+            return Path.GetFileName(outputPath);
+        }
+
+        static void ProcessSourceFile(string input, bool extractTextures)
+        {
+            try
+            {
+                dynamic scene;
+                var outputDir = Path.GetDirectoryName(input);
+                var rootFilename = Path.GetFileNameWithoutExtension(input);
+
+                // Loading
+                Console.ForegroundColor = ConsoleColor.Green;
+                Console.WriteLine("Loading " + input);
+                Console.WriteLine();
+                Console.ResetColor();
+                using (var streamReader = new StreamReader(input))
+                {
+                    using (var reader = new JsonTextReader(streamReader))
+                    {
+                        scene = JObject.Load(reader);
+                    }
+                }
+
+                // Marking scene
+                scene["autoClear"] = true;
+                scene["useDelayedTextureLoading"] = true;
+
+                // Parsing meshes
+                var meshes = (JArray)scene.meshes;
+                foreach (dynamic mesh in meshes)
+                {
+                    if (mesh.checkCollisions.Value) // Do not delay load collisions object
+                    {
+                        continue;
+                    }
+
+                    Console.WriteLine("Extracting " + mesh.name);
+
+                    if (mesh.positions != null && mesh.normals != null && mesh.indices != null)
+                    {
+                        mesh.delayLoadingFile = CreateDelayLoadingFile(mesh, outputDir, rootFilename);
+
+                        // Compute bounding boxes
+                        var positions = ((JArray) mesh.positions).Select(v=>v.Value<float>()).ToArray();
+                        var minimum = new[] {float.MaxValue, float.MaxValue, float.MaxValue};
+                        var maximum = new[] {float.MinValue, float.MinValue, float.MinValue};
+
+                        for (var index = 0; index < positions.Length; index += 3)
+                        {
+                            var x = positions[index];
+                            var y = positions[index + 1];
+                            var z = positions[index + 2];
+
+                            if (x < minimum[0])
+                            {
+                                minimum[0] = x;
+                            }
+                            if (x > maximum[0])
+                            {
+                                maximum[0] = x;
+                            }
+
+                            if (y < minimum[1])
+                            {
+                                minimum[1] = y;
+                            }
+                            if (y > maximum[1])
+                            {
+                                maximum[1] = y;
+                            }
+
+                            if (z < minimum[2])
+                            {
+                                minimum[2] = z;
+                            }
+                            if (z > maximum[2])
+                            {
+                                maximum[2] = z;
+                            }
+                        }
+
+                        mesh["boundingBoxMinimum"] = new JArray(minimum);
+                        mesh["boundingBoxMaximum"] = new JArray(maximum);
+
+                        // Erasing infos
+                        mesh.positions = null;
+                        mesh.normals = null;
+                        mesh.indices = null;
+
+                        if (mesh.uvs != null)
+                        {
+                            mesh["hasUVs"] = true;
+                            mesh.uvs = null;
+                        }
+
+                        if (mesh.uvs2 != null)
+                        {
+                            mesh["hasUVs2"] = true;
+                            mesh.uvs2 = null;
+                        }
+
+                        if (mesh.colors != null)
+                        {
+                            mesh["hasColors"] = true;
+                            mesh.colors = null;
+                        }
+
+                        if (mesh.matricesIndices != null)
+                        {
+                            mesh["hasMatricesIndices"] = true;
+                            mesh.matricesIndices = null;
+                        }
+
+                        if (mesh.matricesWeights != null)
+                        {
+                            mesh["hasMatricesWeights"] = true;
+                            mesh.matricesWeights = null;
+                        }
+
+                        if (mesh.subMeshes != null)
+                        {
+                            mesh.subMeshes = null;
+                        }
+                    }
+                }
+
+                // Saving
+                var outputPath = Path.Combine(outputDir, rootFilename + ".incremental.babylon");
+                Console.ForegroundColor = ConsoleColor.Green;
+                Console.WriteLine("Saving " + outputPath);
+                string json = scene.ToString(Formatting.None);
+
+                using (var writer = new StreamWriter(outputPath))
+                {
+                    writer.Write(json);
+                }
+
+                Console.WriteLine();
+                Console.ResetColor();
+
+            }
+            catch (Exception ex)
+            {
+                Console.ForegroundColor = ConsoleColor.Red;
+                Console.WriteLine("Fatal error encountered:");
+                Console.WriteLine(ex.Message);
+                Console.ResetColor();
+            }
+        }
+
+        static void DisplayUsage()
+        {
+            Console.WriteLine("MakeIncremental usage: MakeIncremental.exe /i:\"source file\" [/textures]");
+        }
+    }
+}

+ 36 - 0
Tools/MakeIncremental/Properties/AssemblyInfo.cs

@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following 
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("MakeIncremental")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("MakeIncremental")]
+[assembly: AssemblyCopyright("Copyright ©  2013")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible 
+// to COM components.  If you need to access a type in this assembly from 
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("863a000d-e69f-4e3b-a150-1e75094c9024")]
+
+// Version information for an assembly consists of the following four values:
+//
+//      Major Version
+//      Minor Version 
+//      Build Number
+//      Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers 
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]