private void resetWidget() { light = (bone != null && bone.Type == BoneType.Light && bone.Attachment != null ? bone.Attachment as LIGHT : new LIGHT()); bInitialising = true; lblLightType.Text = string.Format(lblLightType.Tag.ToString(), light.Type); lblLightName.Text = light.Name; chkLightOn.Checked = true; nudRange.Value = (Decimal)light.Range; nudInner.Value = (Decimal)light.Inner; nudOuter.Value = (Decimal)light.Outer; nudRed.Value = (Decimal)(light.R * 255); nudGreen.Value = (Decimal)(light.G * 255); nudBlue.Value = (Decimal)(light.B * 255); nudIntensity.Value = (Decimal)light.Intensity; updateLightColour(); chkCastShadows.Checked = light.Flags.HasFlag(LIGHT.LightFlags.CastShadow); chkParallelSplit.Checked = false; chkParallelSplit.Enabled = false; chkVisualiseSplits.Checked = false; chkVisualiseSplits.Enabled = false; chkUsePool.Checked = light.Flags.HasFlag(LIGHT.LightFlags.UsePool); nudSplitCount.Value = light.SplitCount; nudSplitDistribn.Value = (Decimal)light.SplitDistribution; nudShadCoverX.Value = (Decimal)light.ShadowCoverX; nudShadCoverY.Value = (Decimal)light.ShadowCoverY; nudShadResX.Value = light.ShadowResolutionX; nudShadResY.Value = light.ShadowResolutionY; nudShadIntensity.Value = (Decimal)light.ShadowIntensity; nudGoboScaleX.Value = (Decimal)light.GoboScaleX; nudGoboScaleY.Value = (Decimal)light.GoboScaleY; nudGoboOffsetX.Value = (Decimal)light.GoboOffsetX; nudGoboOffsetY.Value = (Decimal)light.GoboOffsetY; nudShadowBias.Value = (Decimal)light.ShadowBias; nudLightNearClip.Value = (Decimal)light.LightNearClip; nudShadowDist.Value = (Decimal)light.ShadowDistance; chkUseGobo.Checked = light.Flags.HasFlag(LIGHT.LightFlags.UsesGobo); lblGobo.Text = light.GOBO; chkEdgeColour.Checked = light.UseEdgeColour; nudEdgeRed.Value = light.EdgeColourR; nudEdgeGreen.Value = light.EdgeColourG; nudEdgeBlue.Value = light.EdgeColourB; updateEdgeColour(); toggleShadowUI(); toggleEdgeColourUI(); setButtonText(); bInitialising = false; }
public override Asset Import(string path) { LIGHT light = LIGHT.Load(path); Model model = new Model(); SceneManager.Current.UpdateProgress(string.Format("Processing {0}", Path.GetFileName(path))); int boneIndex = model.AddMesh(null, 0); model.SetName(Path.GetFileNameWithoutExtension(path), boneIndex); model.Bones[boneIndex].Type = BoneType.Light; model.Bones[boneIndex].Attachment = light; SceneManager.Current.UpdateProgress(string.Format("Loaded {0}", Path.GetFileName(path))); return(model); }
private void btnOK_Click(object sender, EventArgs e) { SceneManager.Current.OnProgress += scene_OnProgress; btnOK.Visible = false; btnCancel.Visible = false; gbProgress.Visible = true; pbProgress.Visible = true; Application.DoEvents(); timer.Start(); if (!Directory.Exists(txtPath.Text)) { Directory.CreateDirectory(txtPath.Text); } flump.Settings["level"] = level; flump.Settings["level.pretty.name"] = txtPrettyLevelName.Text; flump.Settings["level.race.name"] = txtRaceName.Text; lblInfo = lblInfoMeshes; lblProgress = lblProgressMeshes; lblProgress.ForeColor = Color.FromKnownColor(KnownColor.ControlText); progressMax = 30; (new CNTExporter()).Export(SceneManager.Current.Models[0], Path.Combine(txtPath.Text, "level.cnt")); (new MDLExporter()).Export(SceneManager.Current.Models[0], txtPath.Text); lblProgress.Text = "✓"; lblProgress.ForeColor = Color.Green; lblInfo.Text = "Meshes"; pbProgress.Value = progressMax; Application.DoEvents(); lblInfo = lblInfoTextures; lblProgress = lblProgressTextures; lblProgress.ForeColor = Color.FromKnownColor(KnownColor.ControlText); progressMax = 50; List <string> textures = new List <string>(); foreach (Material material in SceneManager.Current.Materials) { string fileName = Path.Combine(txtPath.Text, "NON_VT", material.Texture.Name); if (!textures.Contains(material.Texture.Name)) { if (!File.Exists($"{fileName}.tdx")) { TDXExporter tx = new TDXExporter(); tx.ExportSettings.AddSetting("Format", ToxicRagers.Helpers.D3DFormat.DXT5); tx.Export(material.Texture, Path.Combine(txtPath.Text, "NON_VT")); } textures.Add(material.Texture.Name); } } lblProgress.Text = "✓"; lblProgress.ForeColor = Color.Green; lblInfo.Text = "Textures"; pbProgress.Value = progressMax; Application.DoEvents(); lblInfo = lblInfoMaterials; lblProgress = lblProgressMaterials; lblProgress.ForeColor = Color.FromKnownColor(KnownColor.ControlText); progressMax = 60; foreach (Material material in SceneManager.Current.Materials) { string fileName = Path.Combine(txtPath.Text, $"{material.Name}.mt2"); if (!File.Exists(fileName)) { simple_base simple = new simple_base { DiffuseColour = material.Texture.Name, Walkable = Troolean.True }; simple.Save(fileName); } } lblProgress.Text = "✓"; lblProgress.ForeColor = Color.Green; lblInfo.Text = "Materials"; pbProgress.Value = progressMax; Application.DoEvents(); lblInfo = lblInfoPaperwork; lblProgress = lblProgressPaperwork; lblProgress.ForeColor = Color.FromKnownColor(KnownColor.ControlText); progressMax = 75; using (StreamWriter w = File.CreateText(Path.Combine(txtPath.Text, "audio.lol"))) { w.WriteLine("audio:load(\"audio.sounds_peds_impact\")"); w.WriteLine("audio:load(\"audio.sounds_impacts\")"); w.WriteLine("audio:load(\"audio.sounds_misc\")"); w.WriteLine("audio:load(\"audio.sounds_announcer\")"); w.WriteLine("audio:load(\"audio.sounds_powerups\")"); w.WriteLine("audio:load(\"audio.sounds_vehicles\")"); } using (StreamWriter w = File.CreateText(Path.Combine(txtPath.Text, "environment.lol"))) { w.WriteLine("module((...), environment_config, package.seeall)"); w.WriteLine($@"txt[""fe_environment_{txtLevel.Text.ToLower()}""] = ""{txtPrettyLevelName.Text}"""); w.WriteLine($"name = txt.fe_environment_{txtLevel.Text.ToLower()}"); } using (StreamWriter w = File.CreateText(Path.Combine(txtPath.Text, "environment.txt"))) { w.WriteLine("[LUMP]"); w.WriteLine("environment"); } using (StreamWriter w = File.CreateText(Path.Combine(txtPath.Text, "level.txt"))) { w.WriteLine("[LUMP]"); w.WriteLine("level"); w.WriteLine(); w.WriteLine("[ENVIRONMENT]"); w.WriteLine(txtLevel.Text.ToLower().Replace(" ", "_")); w.WriteLine(); w.WriteLine("[RACE_NAMES]"); w.WriteLine(txtRaceName.Text); w.WriteLine(); w.WriteLine("[RACE_WRITEUP]"); w.WriteLine("Pretty sure this doesn't show up in the UI anymore"); w.WriteLine(); w.WriteLine("[RACE_IMAGES]"); w.WriteLine($"race\\{level}_01"); w.WriteLine(); w.WriteLine("[RACE_BACKGROUNDS]"); w.WriteLine($"race\\{level}_01"); w.WriteLine(); w.WriteLine("[VERSION]"); w.WriteLine("2.500000"); w.WriteLine(); w.WriteLine("[RACE_LAYERS]"); w.WriteLine("race01"); w.WriteLine(); w.WriteLine("[LUA_SCRIPTS]"); w.WriteLine("setup.lua"); w.WriteLine(); } if (SceneManager.Current.Entities.Count > 0) { using (StreamWriter wacc = File.CreateText(Path.Combine(txtPath.Text, "level.lol"))) using (StreamWriter wpup = File.CreateText(Path.Combine(txtPath.Text, "powerups.lol"))) { Dictionary <string, StreamWriter> streams = new Dictionary <string, StreamWriter> { { "StartingGrid", wacc }, { "Accessory", wacc }, { "Powerup", wpup } }; wacc.WriteLine("module((...), level_accessory_setup)"); wacc.WriteLine("accessories = {"); wpup.WriteLine("module((...), level_powerup_setup)"); wpup.WriteLine("accessories = {"); for (int i = 0; i < SceneManager.Current.Entities.Count; i++) { IEntity entity = SceneManager.Current.Entities[i]; if (!streams.ContainsKey(entity.GetType().Name)) { continue; } StreamWriter w = streams[entity.GetType().Name]; w.WriteLine($"\t{$"entity{i:0000}"} = {{"); w.WriteLine($"\t\ttype = \"{entity.Name}\","); if (entity is Core.Entities.Powerup) { w.WriteLine($"\t\tname = \"{entity.Tag}\","); } w.WriteLine("\t\tlayer = \"race01\","); w.WriteLine("\t\ttransform = {"); w.WriteLine($"\t\t\t{{{entity.Transform.M11},{entity.Transform.M21},{entity.Transform.M31}}},"); w.WriteLine($"\t\t\t{{{entity.Transform.M12},{entity.Transform.M22},{entity.Transform.M32}}},"); w.WriteLine($"\t\t\t{{{entity.Transform.M13},{entity.Transform.M23},{entity.Transform.M33}}},"); w.WriteLine($"\t\t\t{{{entity.Transform.M41},{entity.Transform.M42},{entity.Transform.M43}}},"); w.WriteLine("\t\t},"); w.WriteLine("\t\tcolour = { 255, 255, 255 }"); w.Write("\t}"); w.WriteLine((i + 1 < SceneManager.Current.Entities.Count ? "," : "")); } wacc.WriteLine("}"); wpup.WriteLine("}"); } } using (StreamWriter w = File.CreateText(Path.Combine(txtPath.Text, "minimap_definition.lol"))) { w.WriteLine("module((...), minimap_definition)"); w.WriteLine("bounds = {"); w.WriteLine(" min_bound = {-640, -270},"); w.WriteLine(" max_bound = {640, 450}"); w.WriteLine("}"); w.WriteLine("minimap_area = 200"); w.WriteLine("max_minimap_area = 400"); w.WriteLine("speed_for_max_minimap_area = 60"); } using (StreamWriter w = File.CreateText(Path.Combine(txtPath.Text, "setup.lol"))) { w.WriteLine("view:loadSky(\"sky\")"); w.WriteLine("view:loadLight(\"sun\")"); w.WriteLine($"view:loadPostFX(\"post_process.{txtLevel.Text.ToLower().Replace(" ", "_")}\")"); //w.WriteLine("view:loadShProbes(\"Reprocessor\")"); w.WriteLine(@" if view.setBigShadowMapAutoFitEnabled ~= nil then view:setBigShadowMapAutoFitEnabled(false) view:setBigShadowMapResolution(2048, 2048) view:setUseBigShadowMapBeyondShadowEnd(true) end view.VehicleAmbientShadowStrength = { 1, 1, 1 } view.DynamicCubeMapClippingPlanes = {0.1, 120} view.ClippingPlanes = {0.3, 700} view.Ambient = { 28, 22, 16 } view.SphericalHarmonicsScale = 0.25 view.FogEnabled = true view.FogColour = { 120, 130, 150 } view.FogStart = 0 view.FogEnd = 900 view.FogAlphaStart = 0 view.FogAlphaEnd = 0 view.UnderwaterAmbient = { 51, 102, 204 } view.UnderwaterFogEnabled = true view.UnderwaterFogColour = { 32, 96, 128 } view.UnderwaterFogStart = 0 view.UnderwaterFogEnd = 120 view.UnderwaterFogAlphaStart = 0 view.UnderwaterFogAlphaEnd = 0 view.AOSampleOffset = 0.5 view.AOBlur = true view.AOBilateralSensitivity = 8 view.AOBias = 0.01 view.AOScale = 0.5 view.AOPowerExponent = 6 view.ShadowBias = 0.0001 view.ShadowSlopeBias = 2 view.NumShadowMaps = 4 view.ShadowMapPoolStats = ""1:1024:1024:8"" view.ShadowSplitResolution = { 1024, 1024} view.ShadowSplitManualUse = false view.ShadowSplitDistribution = 0.8 view.ShadowEnd = 160 view.SunPos = { 0, 3536, -3536 } track:setSubstanceTyreParticles(""ROAD_TARMAC"", ""Effect"", ""w_kick_dusty_dirt_track"") track:setSubstanceTyreParticles(""RACE_TARMAC"", ""Effect"", ""w_kick_dusty_dirt_track"") track:setSplashColour("""", 255, 255, 255, 255) "); } if (!File.Exists(Path.Combine(txtPath.Text, "sun.light"))) { LIGHT sun = new LIGHT { Type = LIGHT.LightType.Directional, Range = 100, Inner = 22.5f, Outer = 45, R = 234 / 255.0f, G = 202 / 255.0f, B = 149 / 255.0f, Intensity = 1.0f, Flags = LIGHT.LightFlags.CastShadow | LIGHT.LightFlags.Unknown8, SplitCount = 4, SplitDistribution = 0.8f, ShadowResolutionX = 1024, ShadowResolutionY = 1024, ShadowIntensity = 1, GoboScaleX = 1, GoboScaleY = 1, ShadowBias = 0.00001f, LightNearClip = 1, ShadowDistance = 160, UseEdgeColour = true, EdgeColourR = 121, EdgeColourG = 121, EdgeColourB = 121 }; sun.Save(Path.Combine(txtPath.Text, "sun.light")); } if (!File.Exists(Path.Combine(txtPath.Text, "sun.cnt"))) { CNT cnt = new CNT { Name = "sun", Transform = ToxicRagers.Helpers.Matrix3D.CreateRotationZ(-119.520f) * ToxicRagers.Helpers.Matrix3D.CreateRotationY(46.042f) * ToxicRagers.Helpers.Matrix3D.CreateRotationX(112.176f), Section = CNT.NodeType.LITg, EmbeddedLight = false, LightName = "sun" }; cnt.Save(Path.Combine(txtPath.Text, "sun.cnt")); } if (!Directory.Exists(Path.Combine(txtPath.Text, "post_process"))) { Directory.CreateDirectory(Path.Combine(txtPath.Text, "post_process")); } if (!File.Exists(Path.Combine(txtPath.Text, "post_process", $"{txtLevel.Text.ToLower().Replace(" ", "_")}.lol"))) { PostFX postFX = new PostFX(); postFX.Save(Path.Combine(txtPath.Text, "post_process", $"{txtLevel.Text.ToLower().Replace(" ", "_")}.lol")); } lblProgress.Text = "✓"; lblProgress.ForeColor = Color.Green; lblInfo.Text = "Paperwork"; pbProgress.Value = progressMax; Application.DoEvents(); lblInfo = lblInfoZAD; lblProgress = lblProgressZAD; lblProgress.ForeColor = Color.FromKnownColor(KnownColor.ControlText); progressMax = 100; MINGE minge = new MINGE { Name = txtPrettyLevelName.Text, //Author = Properties.Settings.Default.PersonalAuthor, //Website = Properties.Settings.Default.PersonalWebsite, Type = MINGE.ModType.Level }; minge.Save(Path.Combine(txtPath.Text, $"{txtLevel.Text}.minge")); ZAD zad = ZAD.Create(Path.Combine(txtPath.Text, $"{txtLevel.Text}.zip")); zad.AddDirectory(Path.GetDirectoryName(txtPath.Text)); lblProgress.Text = "✓"; lblProgress.ForeColor = Color.Green; lblInfo.Text = "CarMODgeddon ZIP file"; pbProgress.Value = progressMax; flump.Save(Path.Combine(txtPath.Text, "level.flump")); timer.Stop(); SceneManager.Current.OnProgress -= scene_OnProgress; btnClose.Visible = true; Application.DoEvents(); SceneManager.Current.UpdateProgress($"Level '{level}' saved successfully!"); }
internal static HandleRef getCPtr(LIGHT obj) { return((obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr); }
public override Asset Import(string path) { FBX fbx = FBX.Load(path); Model model = new Model(); Dictionary <long, object> components = new Dictionary <long, object>(); Dictionary <long, Matrix4D> transforms = new Dictionary <long, Matrix4D>(); Dictionary <long, string> triangulationErrors = new Dictionary <long, string>(); string name = Path.GetFileNameWithoutExtension(path); if (fbx == null) { SceneManager.Current.RaiseError($"File \"{name}\" could not be opened. Please ensure this is a binary FBX file."); return(null); } FBXElem objects = fbx.Elements.Find(e => e.ID == "Objects"); Matrix4D worldMatrix = createTransformFor(fbx.Elements.Find(e => e.ID == "GlobalSettings").Children[1], out Quaternion.RotationOrder order); foreach (FBXElem material in objects.Children.Where(e => e.ID == "Material")) { string matName = material.Properties[1].Value.ToString(); matName = matName.Substring(0, matName.IndexOf("::")); Material m = new Material { Name = matName }; components.Add((long)material.Properties[0].Value, m); Console.WriteLine($"Added material \"{matName}\" ({material.Properties[0].Value})"); } foreach (FBXElem video in objects.Children.Where(e => e.ID == "Video")) { FBXElem content = video.Children.Find(e => e.ID == "Content"); if (content.Properties[0].Size > 4) { components.Add((long)video.Properties[0].Value, (byte[])content.Properties[0].Value); } } IEnumerable <FBXElem> textures = objects.Children.Where(e => e.ID == "Texture"); foreach (FBXElem texture in textures) { string fullFile = texture.Children.Find(e => e.ID == "FileName").Properties[0].Value.ToString(); if (fullFile.IndexOf('.') == -1) { continue; } string file = Path.GetFileName(fullFile); Texture t = new Texture(); long videoKey = (long)fbx.Elements.Find(e => e.ID == "Connections").Children.Where(c => (long)c.Properties[2].Value == (long)texture.Properties[0].Value).First().Properties[1].Value; if (components.ContainsKey(videoKey)) { using (FileStream fs = new FileStream(Path.Combine(Path.GetDirectoryName(path), file), FileMode.Create)) using (BinaryWriter bw = new BinaryWriter(fs)) { bw.Write((byte[])components[videoKey]); } } t = SceneManager.Current.Content.Load(Path.GetFileName(file)); switch (fbx.Elements.Find(e => e.ID == "Connections").Children.Where(c => (long)c.Properties[1].Value == (long)texture.Properties[0].Value).First().Properties.Last().Value.ToString()) { case "NormalMap": t.Type = Texture.TextureType.Normal; break; case "SpecularColor": t.Type = Texture.TextureType.Specular; break; } if (!components.ContainsKey((long)texture.Properties[0].Value)) { components.Add((long)texture.Properties[0].Value, t); Console.WriteLine($"Added texture \"{file}\" ({texture.Properties[0].Value})"); } } foreach (FBXElem element in objects.Children.Where(e => e.ID == "Model")) { string modelName = element.Properties[1].Value.ToString(); modelName = modelName.Substring(0, modelName.IndexOf("::")); components.Add((long)element.Properties[0].Value, new ModelMesh { Name = modelName, Tag = (long)element.Properties[0].Value }); Console.WriteLine("Added model \"{0}\" ({1})", modelName, element.Properties[0].Value); FBXElem properties = element.Children.Find(c => c.ID == "Properties70"); Matrix4D m = Matrix4D.Identity; bool bRotationActive = false; Vector3 lclTranslation = Vector3.Zero; Quaternion lclRotation = Quaternion.Identity; Quaternion preRotation = Quaternion.Identity; Quaternion postRotation = Quaternion.Identity; Vector3 rotationPivot = Vector3.Zero; Vector3 rotationOffset = Vector3.Zero; Vector3 lclScaling = Vector3.One; Vector3 scalingPivot = Vector3.Zero; Vector3 scalingOffset = Vector3.Zero; Vector3 geoPosition = Vector3.Zero; Quaternion geoRotation = Quaternion.Identity; Vector3 geoScale = Vector3.One; FBXElem property; property = properties.Children.GetProperty("RotationActive"); if (property != null) { bRotationActive = ((int)property.Properties[4].Value == 1); } property = properties.Children.GetProperty("ScalingPivot"); if (property != null) { scalingPivot = new Vector3( Convert.ToSingle(property.Properties[4].Value), Convert.ToSingle(property.Properties[5].Value), Convert.ToSingle(property.Properties[6].Value) ); } property = properties.Children.GetProperty("Lcl Scaling"); if (property != null) { lclScaling = new Vector3( Convert.ToSingle(property.Properties[4].Value), Convert.ToSingle(property.Properties[5].Value), Convert.ToSingle(property.Properties[6].Value) ); } property = properties.Children.GetProperty("ScalingOffset"); if (property != null) { scalingOffset = new Vector3( Convert.ToSingle(property.Properties[4].Value), Convert.ToSingle(property.Properties[5].Value), Convert.ToSingle(property.Properties[6].Value) ); } property = properties.Children.GetProperty("RotationPivot"); if (property != null) { rotationPivot = new Vector3( Convert.ToSingle(property.Properties[4].Value), Convert.ToSingle(property.Properties[5].Value), Convert.ToSingle(property.Properties[6].Value) ); } property = properties.Children.GetProperty("PostRotation"); if (property != null) { postRotation = MakeQuaternion( Convert.ToSingle(property.Properties[4].Value), Convert.ToSingle(property.Properties[5].Value), Convert.ToSingle(property.Properties[6].Value), order ); } property = properties.Children.GetProperty("Lcl Rotation"); if (property != null) { lclRotation = MakeQuaternion( Convert.ToSingle(property.Properties[4].Value), Convert.ToSingle(property.Properties[5].Value), Convert.ToSingle(property.Properties[6].Value), order ); } property = properties.Children.GetProperty("PreRotation"); if (property != null) { preRotation = MakeQuaternion( Convert.ToSingle(property.Properties[4].Value), Convert.ToSingle(property.Properties[5].Value), Convert.ToSingle(property.Properties[6].Value), order ); } property = properties.Children.GetProperty("RotationOffset"); if (property != null) { rotationOffset = new Vector3( Convert.ToSingle(property.Properties[4].Value), Convert.ToSingle(property.Properties[5].Value), Convert.ToSingle(property.Properties[6].Value) ); } property = properties.Children.GetProperty("Lcl Translation"); if (property != null) { lclTranslation = new Vector3( Convert.ToSingle(property.Properties[4].Value), Convert.ToSingle(property.Properties[5].Value), Convert.ToSingle(property.Properties[6].Value) ); } property = properties.Children.GetProperty("GeometricTranslation"); if (property != null) { geoPosition = new Vector3( Convert.ToSingle(property.Properties[4].Value), Convert.ToSingle(property.Properties[5].Value), Convert.ToSingle(property.Properties[6].Value) ); } property = properties.Children.GetProperty("GeometricRotation"); if (property != null) { geoRotation = MakeQuaternion( Convert.ToSingle(property.Properties[4].Value), Convert.ToSingle(property.Properties[5].Value), Convert.ToSingle(property.Properties[6].Value), order ); } property = properties.Children.GetProperty("GeometricScaling"); if (property != null) { geoScale = new Vector3( Convert.ToSingle(property.Properties[4].Value), Convert.ToSingle(property.Properties[5].Value), Convert.ToSingle(property.Properties[6].Value) ); } m = Matrix4D.CreateTranslation(scalingPivot).Inverted() * Matrix4D.CreateScale(lclScaling) * Matrix4D.CreateTranslation(scalingPivot) * Matrix4D.CreateTranslation(scalingOffset) * Matrix4D.CreateTranslation(rotationPivot).Inverted() * Matrix4D.CreateFromQuaternion(postRotation) * Matrix4D.CreateFromQuaternion(lclRotation) * Matrix4D.CreateFromQuaternion(preRotation) * Matrix4D.CreateTranslation(rotationPivot) * Matrix4D.CreateTranslation(rotationOffset) * Matrix4D.CreateTranslation(lclTranslation); if (m != Matrix4D.Identity) { transforms.Add((long)element.Properties[0].Value, m); } } foreach (FBXElem element in objects.Children.Where(e => e.ID == "Geometry")) { bool bUVs = true; bool bNorms = true; bool bColours = true; bool bUseIndexNorm = false; bool bNeedsTriangulating = false; string geometryName = element.Properties[1].Value.ToString(); geometryName = geometryName.Substring(0, geometryName.IndexOf("::")); List <Vector3> verts = new List <Vector3>(); List <Vector3> norms = new List <Vector3>(); List <Vector2> uvs = new List <Vector2>(); List <Colour> colours = new List <Colour>(); double[] vertParts = (double[])element.Children.Find(e => e.ID == "Vertices").Properties[0].Value; for (int i = 0; i < vertParts.Length; i += 3) { verts.Add(new Vector3((float)vertParts[i + 0], (float)vertParts[i + 1], (float)vertParts[i + 2])); } SceneManager.Current.UpdateProgress($"Processed {element.Properties[1].Value}->Vertices"); FBXElem normElem = element.Children.Find(e => e.ID == "LayerElementNormal"); if (normElem != null) { double[] normParts = (double[])normElem.Children.Find(e => e.ID == "Normals").Properties[0].Value; for (int i = 0; i < normParts.Length; i += 3) { norms.Add(new Vector3((float)normParts[i + 0], (float)normParts[i + 1], (float)normParts[i + 2])); } bUseIndexNorm = (normElem.Children.Find(e => e.ID == "MappingInformationType").Properties[0].Value.ToString() == "ByVertice"); SceneManager.Current.UpdateProgress($"Processed {element.Properties[1].Value}->Normals"); } else { bNorms = false; } FBXElem colourElem = element.Children.Find(e => e.ID == "LayerElementColor"); if (colourElem != null) { double[] colourParts = (double[])colourElem.Children.Find(e => e.ID == "Colors").Properties[0].Value; FBXElem colourReferenceType = colourElem.Children.Find(e => e.ID == "ReferenceInformationType"); switch (colourReferenceType.Properties[0].Value.ToString()) { case "IndexToDirect": int[] colourIndicies = (int[])colourElem.Children.Find(e => e.ID == "ColorIndex").Properties[0].Value; for (int i = 0; i < colourIndicies.Length; i++) { int offset = colourIndicies[i] * 4; colours.Add(new Colour( (float)colourParts[offset + 0], (float)colourParts[offset + 1], (float)colourParts[offset + 2], (float)colourParts[offset + 3]) ); } break; case "Direct": bColours = false; break; default: throw new NotImplementedException($"Unsupported Colour Reference Type: {colourReferenceType.Properties[0].Value}"); } SceneManager.Current.UpdateProgress($"Processed {element.Properties[1].Value}->Colours"); } else { bColours = false; } FBXElem uvElem = element.Children.Find(e => e.ID == "LayerElementUV"); if (uvElem != null) { double[] uvParts = (double[])uvElem.Children.Find(e => e.ID == "UV").Properties[0].Value; FBXElem uvReferenceType = uvElem.Children.Find(e => e.ID == "ReferenceInformationType"); if (uvReferenceType.Properties[0].Value.ToString() == "IndexToDirect") { List <Vector2> luvs = new List <Vector2>(); for (int i = 0; i < uvParts.Length; i += 2) { luvs.Add(new Vector2((float)uvParts[i + 0], 1 - (float)uvParts[i + 1])); } int[] uvindicies = (int[])uvElem.Children.Find(e => e.ID == "UVIndex").Properties[0].Value; for (int i = 0; i < uvindicies.Length; i++) { if (uvindicies[i] == -1) { uvs.Add(Vector2.Zero); } else { uvs.Add(luvs[uvindicies[i]]); } } } else { for (int i = 0; i < uvParts.Length; i += 2) { uvs.Add(new Vector2((float)uvParts[i + 0], (float)uvParts[i + 1])); } } SceneManager.Current.UpdateProgress($"Processed {element.Properties[1].Value}->UVs"); } else { bUVs = false; } int[] indicies = (int[])element.Children.Find(e => e.ID == "PolygonVertexIndex").Properties[0].Value; List <FBXFace> faces = new List <FBXFace>(); FBXFace face = new FBXFace(); int j = 0; for (int i = 0; i < indicies.Length; i++) { bool bFace = false; int index = indicies[i]; if (index < 0) { bFace = true; index = (index * -1) - 1; } j++; face.AddVertex(verts[index], bNorms ? norms[bUseIndexNorm ? index : i] : Vector3.Zero, bUVs ? uvs[i] : Vector2.Zero, bColours ? colours[i] : Colour.White); if (bFace) { if (j > 3) { triangulationErrors.Add((long)element.Properties[0].Value, geometryName); bNeedsTriangulating = true; break; } faces.Add(face); face = new FBXFace(); j = 0; } } List <ModelMeshPart> parts = new List <ModelMeshPart>(); if (!bNeedsTriangulating) { SceneManager.Current.UpdateProgress($"Processed {element.Properties[1].Value}->Faces"); FBXElem elemMaterial = element.Children.Find(e => e.ID == "LayerElementMaterial"); if (elemMaterial != null) { int[] faceMaterials = (int[])elemMaterial.Children.Find(e => e.ID == "Materials").Properties[0].Value; for (int i = 0; i < faceMaterials.Length; i++) { faces[i].MaterialID = faceMaterials[i]; } SceneManager.Current.UpdateProgress($"Processed {element.Properties[1].Value}->Materials"); } IEnumerable <IGrouping <int, FBXFace> > materialGroups = faces.GroupBy(f => f.MaterialID); int processedFaceCount = 0, processedGroupCount = 0; foreach (IGrouping <int, FBXFace> materialGroup in materialGroups) { IEnumerable <IGrouping <int, FBXFace> > smoothingGroups = materialGroup.GroupBy(f => f.SmoothingGroup); foreach (IGrouping <int, FBXFace> smoothingGroup in smoothingGroups) { ModelMeshPart meshpart = new ModelMeshPart { PrimitiveType = Flummery.Core.PrimitiveType.Triangles }; processedFaceCount = 0; foreach (FBXFace groupface in smoothingGroup) { foreach (Vertex vert in groupface.Vertices) { meshpart.AddVertex(vert.Position, vert.Normal, vert.UV, vert.Colour); } processedFaceCount++; if (processedFaceCount % 250 == 0) { SceneManager.Current.UpdateProgress($"Processed {element.Properties[1].Value}->MeshPart[{processedGroupCount}]->Face[{processedFaceCount}]"); } } meshpart.Key = materialGroup.Key; parts.Add(meshpart); SceneManager.Current.UpdateProgress($"Processed {element.Properties[1].Value}->MeshPart"); processedGroupCount++; } } } components.Add((long)element.Properties[0].Value, parts); SceneManager.Current.UpdateProgress($"Processed {element.Properties[1].Value}"); } Dictionary <long, BoneType> nodeAttributes = new Dictionary <long, BoneType>(); Dictionary <long, object> nodeAttachments = new Dictionary <long, object>(); foreach (FBXElem nodeAttribute in objects.Children.Where(e => e.ID == "NodeAttribute")) { FBXElem typeFlags = nodeAttribute.Children.Find(e => e.ID == "TypeFlags"); if (typeFlags != null) { switch (typeFlags.Properties[0].Value.ToString().ToLower()) { case "light": LIGHT light = new LIGHT(); FBXElem lightType = nodeAttribute.Children.Find(c => c.ID == "Properties70").Children.GetProperty("LightType"); light.Type = (LIGHT.LightType)(lightType == null ? 0 : lightType.Properties[4].Value); nodeAttributes.Add((long)nodeAttribute.Properties[0].Value, BoneType.Light); nodeAttachments.Add((long)nodeAttribute.Properties[0].Value, light); break; default: // null node break; } } } string[] connectionOrder = new string[] { "System.Collections.Generic.List`1[Flummery.Core.ModelMeshPart]", "Flummery.Core.Texture", "Flummery.Core.Material", "Flummery.Core.ModelMesh" }; FBXElem connections = fbx.Elements.Find(e => e.ID == "Connections"); HashSet <long> loaded = new HashSet <long>(); foreach (string connectionType in connectionOrder) { IEnumerable <FBXElem> connectionsOfType = connections.Children.Where(c => components.ContainsKey((long)c.Properties[1].Value) && components[(long)c.Properties[1].Value].GetType().ToString() == connectionType); foreach (FBXElem connection in connectionsOfType) { long keyA = (long)connection.Properties[1].Value; long keyB = (long)connection.Properties[2].Value; Console.WriteLine("{0} is connected to {1} :: {2}", keyA, keyB, connectionType); switch (connectionType) { case "Flummery.Core.ModelMesh": int boneID; if (keyB == 0) { boneID = model.AddMesh((ModelMesh)components[keyA]); model.SetName(((ModelMesh)components[keyA]).Name, boneID); if (transforms.ContainsKey(keyA)) { model.SetTransform(transforms[keyA], boneID); } FBXElem attribute = connections.Children.FirstOrDefault(c => nodeAttributes.ContainsKey((long)c.Properties[1].Value) && (long)c.Properties[2].Value == keyA); if (attribute != null) { keyA = (long)attribute.Properties[1].Value; if (nodeAttributes.ContainsKey(keyA)) { model.Bones[boneID].Type = nodeAttributes[keyA]; } if (nodeAttachments.ContainsKey(keyA)) { model.Bones[boneID].Attachment = nodeAttachments[keyA]; } } } else { ModelMesh parent = model.FindMesh(keyB); if (parent != null) { boneID = model.AddMesh((ModelMesh)components[keyA], parent.Parent.Index); model.SetName(((ModelMesh)components[keyA]).Name, boneID); if (transforms.ContainsKey(keyA)) { model.SetTransform(transforms[keyA], boneID); } } else { if (!components.ContainsKey(keyB)) { Console.WriteLine("Components doesn't contain {0}", keyB); } else { Console.WriteLine("Couldn't find {0}", ((ModelMesh)components[keyB]).Name); } } } break; case "Flummery.Core.Texture": if (components.ContainsKey(keyB) && components[keyB].GetType().ToString() == "Flummery.Core.Material") { if (loaded.Add(keyA)) { ((Material)components[keyB]).Texture = (Texture)components[keyA]; //SceneManager.Current.Add((Material)components[keyB]); } } else { Console.WriteLine("{0} is of unknown type {1}", keyA, components[keyA].GetType().ToString()); Console.WriteLine("{0} is of unknown type {1}", keyB, components[keyB].GetType().ToString()); } break; case "System.Collections.Generic.List`1[Flummery.Core.ModelMeshPart]": if (components.ContainsKey(keyB) && components[keyB].GetType().ToString() == "Flummery.Core.ModelMesh") { if (triangulationErrors.ContainsKey(keyA)) { triangulationErrors[keyA] += " (geometry of " + ((ModelMesh)components[keyB]).Name + ")"; } foreach (ModelMeshPart part in (List <ModelMeshPart>)components[keyA]) { ((ModelMesh)components[keyB]).AddModelMeshPart(part); } } break; case "Flummery.Core.Material": if (components.ContainsKey(keyB) && components[keyB].GetType().ToString() == "Flummery.Core.ModelMesh") { List <FBXElem> materialLookup = connections.Children.Where(c => (long)c.Properties[2].Value == keyB).ToList(); for (int i = materialLookup.Count - 1; i > -1; i--) { if (!connectionsOfType.Any(c => (long)c.Properties[1].Value == (long)materialLookup[i].Properties[1].Value)) { materialLookup.RemoveAt(i); } } foreach (ModelMeshPart part in ((ModelMesh)components[keyB]).MeshParts) { if ((long)materialLookup[(int)part.Key].Properties[1].Value == keyA) { part.Material = (Material)components[keyA]; SceneManager.Current.Add(part.Material); } } } break; default: Console.WriteLine("{0} is of unknown type {1}", keyA, components[keyA].GetType().ToString()); if (components.ContainsKey(keyB)) { Console.WriteLine("{0} is of unknown type {1}", keyB, components[keyB].GetType().ToString()); } Console.WriteLine("==="); break; } } } if (triangulationErrors.Count > 0) { SceneManager.Current.UpdateProgress($"Failed to load {name}"); string error = $"File \"{name}\" has part{(triangulationErrors.Count > 1 ? "s" : "")} that need been triangulating! Please triangulate the following:"; foreach (KeyValuePair <long, string> kvp in triangulationErrors) { error += $"\r\n{kvp.Value}"; } SceneManager.Current.RaiseError(error); return(null); } else { SceneManager.Current.UpdateProgress($"Loaded {name}"); model.Santise(); //if (worldMatrix != Matrix4D.Identity) { ModelManipulator.Freeze(model, worldMatrix); } ModelManipulator.FlipAxis(model, Axis.Z, true); return(model); } }