public static QuickPolygon Create(string name, MeshType meshIndex, Vector3 position, Quaternion rotation, bool isUI) { GameObject polygon = new GameObject(); if (!string.IsNullOrEmpty(name)) { polygon.name = name; } else { polygon.name = meshIndex.ToString(); } polygon.transform.position = position; polygon.transform.rotation = rotation; #if UNITY_EDITOR if (!Application.isPlaying) { Undo.RegisterCreatedObjectUndo(polygon, "Shape Created"); Selection.activeObject = polygon; } #endif QuickPolygon newPolygon = polygon.AddComponent <QuickPolygon>(); newPolygon.ShapeMeshIndex = meshIndex; newPolygon.IsUI = isUI; newPolygon.Reset(); newPolygon.RecalculateMesh(); return(newPolygon); }
private Mesh LoadMesh(MeshType meshType) { string meshName = meshType.ToString() + "Mesh.mesh"; string pathMesh = PackagePath + "Meshes/" + meshName; return(AssetDatabase.LoadAssetAtPath <Mesh>(pathMesh)); }
private bool CopyMesh(MeshType type, CPUMesh mesh, string suffix) { bool madeNew = false; if (!meshFilters.ContainsKey(type)) { var newGameObject = new GameObject(); newGameObject.transform.parent = transform; newGameObject.transform.LocalReset(); newGameObject.name = type.ToString(); meshFilters[type] = newGameObject.AddComponent <MeshFilter>(); meshRenderers[type] = newGameObject.AddComponent <MeshRenderer>(); meshFilters[type].mesh = new Mesh(); meshFilters[type].mesh.name = type.ToString() + "_" + suffix; madeNew = true; } meshFilters[type].mesh.Clear(); mesh.CopyToMesh(meshFilters[type].mesh); return(madeNew); }
public BF2GeometryTemplate(string fileName, MeshType meshType) { if (meshType == MeshType.DebugSphereMesh) { return; } this.FileName = fileName + "." + meshType.ToString().Replace("MeshParticleMesh", "BundledMesh"); if (!File.Exists(this.FileName)) { //throw new NotImplementedException(); } this._Name = Path.GetFileNameWithoutExtension(this.FileName); }
/// <summary> /// Verify required members and start the MLMeshing API. /// </summary> void Start() { if (MeshParent == null) { Debug.LogError("Error: MLMeshingBehavior.MeshParent is not set, disabling script."); enabled = false; return; } if (MeshPrefab == null) { Debug.LogError("Error: MLMeshingBehavior.MeshPrefab is not set, disabling script."); enabled = false; return; } if ((MeshType == MeshingType.PointCloud && PointCloudMeshMaterial == null) || (MeshType == MeshingType.Triangles && TriangleMeshMaterial == null)) { Debug.LogErrorFormat("Error: MLMeshingBehavior failed because material member required for mesh type {0} is null, disbling script.", MeshType.ToString()); enabled = false; return; } #if PLATFORM_LUMIN MLMeshing.MeshSettings initSettings; if (UseDefaultSettings) { initSettings = MLMeshing.MeshSettings.Create(); SetSettings(ref initSettings); } else { initSettings = GetSettings(); } MLResult result = MLMeshing.Start(initSettings); if (!result.IsOk) { Debug.LogErrorFormat("Error: MLMeshingBehavior failed starting MLMeshing, disabling script. Reason: {0}", result); enabled = false; return; } #if UNITY_EDITOR UnityEditor.AssemblyReloadEvents.beforeAssemblyReload += StopAPIBeforeAssemblyReload; #endif #endif }
public static void LoadMap(string MapName = "") { try { XmlDocument XML = new XmlDocument(); XmlNode xmlNode; XmlNodeList xmlNodeList; XML.Load(MapsList[MapName]); Maps.Name = XML.DocumentElement.SelectSingleNode("Name").InnerText; Log.WriteLine("Loading map: \"{0}\"", MapName); if (XML.DocumentElement.SelectNodes("Description").Count > 0) { Maps.Description = XML.DocumentElement.SelectSingleNode("Description").InnerText; } #region PlayerStart if (XML.DocumentElement.SelectNodes("PlayerStart").Count > 0) { xmlNode = XML.DocumentElement.SelectSingleNode("PlayerStart"); if (xmlNode.SelectNodes("Position").Count > 0) { string[] Position = xmlNode.SelectSingleNode("Position").InnerText.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); Game.MainCamera.Position = new Vector3(float.Parse(Position[0]), float.Parse(Position[1]), float.Parse(Position[2])); PlayerStartPos = Game.MainCamera.Position; } if (xmlNode.SelectNodes("Rotation").Count > 0) { string[] Rotation = xmlNode.SelectSingleNode("Rotation").InnerText.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); float Yaw = float.Parse(Rotation[0]); float Pitch = float.Parse(Rotation[1]); Game.MainCamera.YawPitch = new Vector2( MathHelper.DegreesToRadians(-Yaw), MathHelper.DegreesToRadians(Pitch)); PlayerStartRot = new Vector2(Yaw, Pitch); } } #endregion #region Camera if (XML.DocumentElement.SelectNodes("Camera").Count > 0) { xmlNode = XML.DocumentElement.SelectSingleNode("Camera"); if (xmlNode.SelectNodes("Near").Count > 0) { Game.MainCamera.zNear = float.Parse(xmlNode.SelectSingleNode("Near").InnerText); } if (xmlNode.SelectNodes("Far").Count > 0) { Game.MainCamera.zFar = float.Parse(xmlNode.SelectSingleNode("Far").InnerText); } if (xmlNode.SelectNodes("FOV").Count > 0) { Game.MainCamera.FOV = float.Parse(xmlNode.SelectSingleNode("FOV").InnerText); } if (xmlNode.SelectNodes("MoveSpeed").Count > 0) { Game.MainCamera.MoveSpeed = float.Parse(xmlNode.SelectSingleNode("MoveSpeed").InnerText); } } #endregion #region SkyBox if (XML.DocumentElement.SelectNodes("Skybox").Count > 0) { string SkyBox = XML.DocumentElement.SelectSingleNode("Skybox").InnerText; if (SkyBox != null && SkyBox != String.Empty) { Game.SkyBox = new Mesh(); MeshPart MPart = MeshPart.MakeBox(Game.MainCamera.zFar * 2.0f / (float)Math.Sqrt(3.0), true); // Cube diagonal = side / sqrt(3) MPart.Material = Materials.Load(SkyBox); Game.SkyBox.Parts.Add(MPart); Game.SkyBox.CalcBoundingObjects(); } } #endregion #region Fog if (XML.DocumentElement.SelectNodes("Fog").Count > 0) { xmlNode = XML.DocumentElement.SelectSingleNode("Fog"); if (xmlNode.SelectNodes("Enabled").Count > 0) { Settings.Graphics.Fog.UseFogOnMap = Convert.ToBoolean(xmlNode.SelectSingleNode("Enabled").InnerText); } if (xmlNode.SelectNodes("Color").Count > 0) { try { string[] Color = xmlNode.SelectSingleNode("Color").InnerText.Split( new char[] { ' ', ';' }, StringSplitOptions.RemoveEmptyEntries); float R, G, B; switch (Color.Length) { case 1: R = float.Parse(Color[0]); Settings.Graphics.Fog.Color = new Vector3(R, R, R); break; case 3: R = float.Parse(Color[0]); G = float.Parse(Color[1]); B = float.Parse(Color[2]); Settings.Graphics.Fog.Color = new Vector3(R, G, B); break; default: break; } } catch { } } if (xmlNode.SelectNodes("DistanceMin").Count > 0) { Settings.Graphics.Fog.MinDistance = float.Parse(xmlNode.SelectSingleNode("DistanceMin").InnerText); } if (xmlNode.SelectNodes("DistanceMax").Count > 0) { Settings.Graphics.Fog.MaxDistance = float.Parse(xmlNode.SelectSingleNode("DistanceMax").InnerText); } } #endregion #region Models foreach (XmlNode xmlNodeModel in XML.DocumentElement.SelectNodes("Model")) { Model model = new Model(); model.Name = xmlNodeModel.SelectSingleNode("Name").InnerText; if (xmlNodeModel.SelectNodes("Visible").Count > 0) { model.Visible = Convert.ToBoolean(xmlNodeModel.SelectSingleNode("Visible").InnerText); } foreach (XmlNode xmlNodeMesh in xmlNodeModel.SelectNodes("Mesh")) { Mesh mesh = new Mesh(); string Name = xmlNodeMesh.SelectSingleNode("Name").InnerText; MeshType meshType = MeshType.Mesh; if (xmlNodeMesh.SelectNodes("Type").Count > 0) { string meshTypeStr = xmlNodeMesh.SelectSingleNode("Type").InnerText; try { meshType = (MeshType)Enum.Parse(typeof(MeshType), meshTypeStr, true); } catch { meshType = MeshType.Mesh; } } float SideA, SideB, SideC; MeshPart mp; bool FlipPolygons = false; switch (meshType) { case MeshType.Plain: mesh.Name = Name; mesh.MeshName = meshType.ToString(); SideA = float.Parse(xmlNodeMesh.SelectSingleNode("SideA").InnerText); SideB = float.Parse(xmlNodeMesh.SelectSingleNode("SideB").InnerText); mp = MeshPart.MakePlain(SideA, SideB); mesh.Parts.Add(mp); mesh.CalcBoundingObjects(); break; case MeshType.Box: mesh.Name = Name; mesh.MeshName = meshType.ToString(); SideA = float.Parse(xmlNodeMesh.SelectSingleNode("SideA").InnerText); SideB = float.Parse(xmlNodeMesh.SelectSingleNode("SideB").InnerText); SideC = float.Parse(xmlNodeMesh.SelectSingleNode("SideC").InnerText); if (xmlNodeMesh.SelectNodes("FlipPolygons").Count > 0) { FlipPolygons = Convert.ToBoolean(xmlNodeMesh.SelectSingleNode("FlipPolygons").InnerText); } mp = MeshPart.MakeBox(SideA, SideB, SideC, FlipPolygons); mesh.Parts.Add(mp); mesh.CalcBoundingObjects(); break; case MeshType.Mesh: default: string MeshName = xmlNodeMesh.SelectSingleNode("Mesh").InnerText; mesh = Meshes.Load(Name, MeshName); break; } string[] Pos = xmlNodeMesh.SelectSingleNode("Position").InnerText.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); mesh.Position = new Vector3(float.Parse(Pos[0]), float.Parse(Pos[1]), float.Parse(Pos[2])); string[] Rot = xmlNodeMesh.SelectSingleNode("Rotation").InnerText.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); mesh.Rotation = new Vector3(MathHelper.DegreesToRadians(float.Parse(Rot[0])), MathHelper.DegreesToRadians(float.Parse(Rot[1])), MathHelper.DegreesToRadians(float.Parse(Rot[2]))); string[] Scl = xmlNodeMesh.SelectSingleNode("Scale").InnerText.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); mesh.Scale = new Vector3(float.Parse(Scl[0]), float.Parse(Scl[1]), float.Parse(Scl[2])); XmlNodeList xmlMaterials = xmlNodeMesh.SelectSingleNode("Materials").SelectNodes("Material"); int Count = Math.Min(xmlMaterials.Count, mesh.Parts.Count); for (int i = 0; i < Count; i++) { mesh.Parts[i].Material = Materials.Load(xmlMaterials[i].InnerText); } if (Count < mesh.Parts.Count && Count > 0) { for (int i = Count; i < mesh.Parts.Count; i++) { mesh.Parts[i].Material = mesh.Parts[0].Material; } } model.Meshes.Add(mesh); } Models.MODELS.Add(model); } #endregion #region Light foreach (XmlNode xmlNodeLight in XML.DocumentElement.SelectNodes("Light")) { Light light = new Light(); light.Name = xmlNodeLight.SelectSingleNode("Name").InnerText; if (xmlNodeLight.SelectNodes("Enabled").Count > 0) { light.Enabled = Convert.ToBoolean(xmlNodeLight.SelectSingleNode("Enabled").InnerText); } LightType lightType = LightType.Point; if (xmlNodeLight.SelectNodes("Type").Count > 0) { string meshTypeStr = xmlNodeLight.SelectSingleNode("Type").InnerText; try { lightType = (LightType)Enum.Parse(typeof(LightType), meshTypeStr, true); } catch { lightType = LightType.Point; } light.Type = lightType; } #region Diffuse Light xmlNodeList = xmlNodeLight.SelectNodes("Diffuse"); if (xmlNodeList.Count > 0) { try { string[] Diffuse = xmlNodeList.Item(0).InnerText.Split( new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); float R, G, B; switch (Diffuse.Length) { case 1: R = float.Parse(Diffuse[0]); light.Diffuse = new Vector3(R, R, R); break; case 3: R = float.Parse(Diffuse[0]); G = float.Parse(Diffuse[1]); B = float.Parse(Diffuse[2]); light.Diffuse = new Vector3(R, G, B); break; } } catch { } } #endregion #region Specular Light xmlNodeList = xmlNodeLight.SelectNodes("Specular"); if (xmlNodeList.Count > 0) { string[] Specular = xmlNodeList.Item(0).InnerText.Split( new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); float R, G, B; switch (Specular.Length) { case 1: R = float.Parse(Specular[0]); light.Specular = new Vector3(R, R, R); break; case 3: R = float.Parse(Specular[0]); G = float.Parse(Specular[1]); B = float.Parse(Specular[2]); light.Specular = new Vector3(R, G, B); break; } } #endregion #region Position xmlNodeList = xmlNodeLight.SelectNodes("Position"); if (xmlNodeList.Count > 0) { string[] Pos = xmlNodeList.Item(0).InnerText.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); light.Position = new Vector3(float.Parse(Pos[0]), float.Parse(Pos[1]), float.Parse(Pos[2])); } #endregion #region Direction xmlNodeList = xmlNodeLight.SelectNodes("Direction"); if (xmlNodeList.Count > 0) { string[] Direct = xmlNodeList.Item(0).InnerText.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); light.Direction = Vector3.FromYawPitch( MathHelper.DegreesToRadians(float.Parse(Direct[0])), MathHelper.DegreesToRadians(float.Parse(Direct[1]))); } #endregion #region Attenuation xmlNodeList = xmlNodeLight.SelectNodes("Attenuation"); if (xmlNodeList.Count > 0) { string[] Atten = xmlNodeList.Item(0).InnerText.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); light.Attenuation = new Vector3(float.Parse(Atten[0]), float.Parse(Atten[1]), float.Parse(Atten[2])); } #endregion if (xmlNodeLight.SelectNodes("CutOFF").Count > 0) { light.CutOFF = float.Parse(xmlNodeLight.SelectSingleNode("CutOFF").InnerText); } if (xmlNodeLight.SelectNodes("Exponent").Count > 0) { light.Exponent = float.Parse(xmlNodeLight.SelectSingleNode("Exponent").InnerText); } Lights.LIGHTS.Add(light); } #endregion #region Sounds //xmlNodeList = XML.DocumentElement.SelectSingleNode("Sounds").SelectNodes("Sound"); //foreach (XmlNode xmlNodeSound in xmlNodeList) //{ // string SndName = xmlNodeSound.SelectSingleNode("Name").InnerText; // bool SndEnabled = Convert.ToBoolean(xmlNodeSound.SelectSingleNode("Enabled").InnerText); // string SndFile = xmlNodeSound.SelectSingleNode("File").InnerText; // int SndVolume = Convert.ToInt32(xmlNodeSound.SelectSingleNode("Volume").InnerText); // int SndRadius = Convert.ToInt32(xmlNodeSound.SelectSingleNode("Radius").InnerText); // string[] SndPos = xmlNodeSound.SelectSingleNode("Position").InnerText.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); // Vector3 SndPosition = new Vector3( // float.Parse(SndPos[0]), // float.Parse(SndPos[1]), // float.Parse(SndPos[2])); //} #endregion Log.WriteLineGreen("Done."); } catch { Log.WriteLineRed("Map \"{0}\" loading Error!\n\t", MapName); } }
/// <summary> /// For creating shape objects /// </summary> /// <param name="type">the name of the object you wish to create</param> public MeshClass(MeshType type) { if (type == MeshType.Cube) { objectMesh = Mesh.CreateBox(DeviceManager.LocalDevice, 1f, 1f, 1f); objectMesh.ComputeNormals(); objectMesh.Optimize(MeshOptimizeFlags.Compact); ApplyColor(Color.White); } else if (type == MeshType.Triangle) { var ShapeVertices = new CustomVertex.VertexPositionColor[] { new CustomVertex.VertexPositionColor() { Color = Color.White.ToArgb(), Position = new Vector3(-1f, 0f, 1f) }, new CustomVertex.VertexPositionColor() { Color = Color.White.ToArgb(), Position = new Vector3(1f, 0f, 1f) }, new CustomVertex.VertexPositionColor() { Color = Color.White.ToArgb(), Position = new Vector3(-1f, 0f, -1f) }, new CustomVertex.VertexPositionColor() { Color = Color.White.ToArgb(), Position = new Vector3(1f, 0f, -1f) }, new CustomVertex.VertexPositionColor() { Color = Color.White.ToArgb(), Position = new Vector3(0f, 1f, 0f) }, }; var ShapeIndices = new short[] { 0, 2, 1, // base 1, 2, 3, 0, 1, 4, // sides 1, 3, 4, 3, 2, 4, 2, 0, 4, }; objectMesh = new Mesh(DeviceManager.LocalDevice, ShapeIndices.Length, ShapeVertices.Length, MeshFlags.Managed, VertexFormat.Position | VertexFormat.Diffuse); objectMesh.LockVertexBuffer(LockFlags.None).WriteRange <CustomVertex.VertexPositionColor>(ShapeVertices); objectMesh.UnlockVertexBuffer(); objectMesh.LockIndexBuffer(LockFlags.None).WriteRange <short>(ShapeIndices); objectMesh.UnlockIndexBuffer(); Mesh other = objectMesh.Clone(DeviceManager.LocalDevice, MeshFlags.Managed, objectMesh.VertexFormat | VertexFormat.Normal | VertexFormat.Texture2); objectMesh.Dispose(); objectMesh = null; other.ComputeNormals(); objectMesh = other.Clone(DeviceManager.LocalDevice, MeshFlags.Managed, other.VertexFormat); other.Dispose(); objectMesh.Optimize(MeshOptimizeFlags.Compact); } ObjectPosition = Vector3.Zero; ObjectRotate = Vector3.Zero; ObjectScale = new Vector3(1, 1, 1); world = Matrix.Translation(ObjectPosition); Name = type.ToString(); IsShapeObject = true; }
public static Mesh Generate(MeshType type) { var mesh = new Mesh(); mesh.name = type.ToString(); switch (type) { case MeshType.Quad: { var vertices = new Vector3[4] { new Vector3(-0.5f, -0.5f, 0f), new Vector3(-0.5f, 0.5f, 0f), new Vector3(0.5f, 0.5f, 0f), new Vector3(0.5f, -0.5f, 0f), }; var indices = new int[6] { 0, 1, 2, 0, 2, 3 }; var uv = new Vector2[4] { new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1), }; mesh.vertices = vertices; mesh.SetIndices(indices, MeshTopology.Triangles, 0); mesh.uv = uv; } break; case MeshType.Cube: { var vertices = new Vector3[8] { new Vector3(-0.5f, -0.5f, -0.5f), new Vector3(0.5f, -0.5f, -0.5f), new Vector3(0.5f, 0.5f, -0.5f), new Vector3(-0.5f, 0.5f, -0.5f), new Vector3(-0.5f, -0.5f, 0.5f), new Vector3(0.5f, -0.5f, 0.5f), new Vector3(0.5f, 0.5f, 0.5f), new Vector3(-0.5f, 0.5f, 0.5f), }; var indices = new int[6] { 0, 1, 2, 0, 2, 3, // 1, 6, 2, // 1, 5, 6, }; var uv = new Vector2[4] { new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1), }; mesh.vertices = vertices; mesh.SetIndices(indices, MeshTopology.Triangles, 0); mesh.uv = uv; } break; case MeshType.Circle: { var vertices = new List <Vector3>(); var div = 16; foreach (var i in Enumerable.Range(0, div)) { var rad = Mathf.Lerp(0, 2f * Mathf.PI, i * 1.0f / div); vertices.Add(new Vector3(Mathf.Cos(rad), Mathf.Sin(rad), 0)); } } break; default: break; } mesh.RecalculateBounds(); mesh.RecalculateNormals(); mesh.RecalculateTangents(); return(mesh); }
public static string ToDisplayString(this MeshType type) { return(type.ToString().Replace("MeshType", "")); }
/// <summary> /// For creating shape objects /// </summary> /// <param name="type">the name of the object you wish to create</param> public MeshClass(MeshType type) { if (type == MeshType.Cube) { objectMesh = Mesh.CreateBox(DeviceManager.LocalDevice, 1f, 1f, 1f); objectMesh.ComputeNormals(); objectMesh.Optimize(MeshOptimizeFlags.Compact); ApplyColor(Color.White); } else if (type == MeshType.Triangle) { var ShapeVertices = new CustomVertex.VertexPositionColor[] { new CustomVertex.VertexPositionColor() { Color = Color.White.ToArgb(), Position = new Vector3(-1f, 0f, 1f) }, new CustomVertex.VertexPositionColor() { Color = Color.White.ToArgb(), Position = new Vector3(1f, 0f, 1f) }, new CustomVertex.VertexPositionColor() { Color = Color.White.ToArgb(), Position = new Vector3(-1f, 0f, -1f) }, new CustomVertex.VertexPositionColor() { Color = Color.White.ToArgb(), Position = new Vector3(1f, 0f, -1f) }, new CustomVertex.VertexPositionColor() { Color = Color.White.ToArgb(), Position = new Vector3(0f, 1f, 0f) }, }; var ShapeIndices = new short[] { 0, 2, 1, // base 1, 2, 3, 0, 1, 4, // sides 1, 3, 4, 3, 2, 4, 2, 0, 4, }; objectMesh = new Mesh(DeviceManager.LocalDevice, ShapeIndices.Length, ShapeVertices.Length, MeshFlags.Managed, VertexFormat.Position | VertexFormat.Diffuse); objectMesh.LockVertexBuffer(LockFlags.None).WriteRange<CustomVertex.VertexPositionColor>(ShapeVertices); objectMesh.UnlockVertexBuffer(); objectMesh.LockIndexBuffer(LockFlags.None).WriteRange<short>(ShapeIndices); objectMesh.UnlockIndexBuffer(); Mesh other = objectMesh.Clone(DeviceManager.LocalDevice, MeshFlags.Managed, objectMesh.VertexFormat | VertexFormat.Normal | VertexFormat.Texture2); objectMesh.Dispose(); objectMesh = null; other.ComputeNormals(); objectMesh = other.Clone(DeviceManager.LocalDevice, MeshFlags.Managed, other.VertexFormat); other.Dispose(); objectMesh.Optimize(MeshOptimizeFlags.Compact); } ObjectPosition = Vector3.Zero; ObjectRotate = Vector3.Zero; ObjectScale = new Vector3(1, 1, 1); world = Matrix.Translation(ObjectPosition); Name = type.ToString(); IsShapeObject = true; }