/// <summary> /// Creates a new model based upon a <see cref="XMesh"/>, using an external texture and a plain white material. /// </summary> /// <param name="mesh">The <see cref="XMesh"/> providing the mesh data.</param> /// <param name="materials">The materials to use for rendering the model.</param> /// <remarks>Calling <see cref="IDisposable.Dispose"/> will call <see cref="IReferenceCount.ReleaseReference"/> on <paramref name="mesh"/> and <paramref name="materials"/>.</remarks> public Model(XMesh mesh, params XMaterial[] materials) { #region Sanity checks if (mesh == null) { throw new ArgumentNullException(nameof(mesh)); } #endregion _asset = mesh; _asset.HoldReference(); // Get mesh from asset Mesh = mesh.Mesh; // Get separate materials Materials = materials ?? new XMaterial[0]; NumberSubsets = Materials.Length; foreach (var material in Materials) { material.HoldReference(); } _separateMaterials = true; // Get bounding bodies BoundingSphere = mesh.BoundingSphere; BoundingBox = mesh.BoundingBox; }
/// <summary> /// Creates a new editor presenter /// </summary> /// <param name="engine">The engine to use for rendering</param> /// <param name="universe">The universe to display</param> /// <param name="lighting">Shall lighting be used for rendering?</param> public EditorPresenter(Engine engine, Universe universe, bool lighting) : base(engine, universe) { #region Sanity checks if (engine == null) { throw new ArgumentNullException(nameof(engine)); } if (universe == null) { throw new ArgumentNullException(nameof(universe)); } #endregion Lighting = lighting; // Restore previous camera position (or default to center of terrain) var mainCamera = CreateCamera(universe.CurrentCamera); View = new View(Scene, mainCamera) { Name = "Editor", BackgroundColor = universe.FogColor }; // Floating axis-arrows for easier orientation var axisArrows = new FloatingModel(XMesh.Get(engine, "Engine/AxisArrows.x")) { Name = "AxisArrows", Alpha = 160, Position = new DoubleVector3(-16, -12, 40), Rotation = Quaternion.RotationYawPitchRoll(0, 0, 0) }; axisArrows.SetScale(0.03f); View.FloatingModels.Add(axisArrows); }
public override int GetHashCode() { int result = 0; if (SplatTexutre != null) { for (int i = 0; i < SplatTexutre.Length; i++) { result ^= SplatTexutre[i].GetHashCode() + 1; } } if (MaterialTexture != null) { for (int i = 0; i < MaterialTexture.Length; i++) { if (MaterialTexture[i] != null) { result ^= MaterialTexture[i].GetHashCode() + 1; } } } return(GetType().GetHashCode() ^ (XMesh != null ? XMesh.GetHashCode() : 1) ^ (Mesh != null ? Mesh.GetHashCode() : 1) ^ (Texture != null ? Texture.GetHashCode() : 1) ^ (SkinnedMesh != null ? SkinnedMesh.GetHashCode() : 1) ^ (BaseTexture != null ? BaseTexture.GetHashCode() : 1) ^ (SpecularTexture != null ? SpecularTexture.GetHashCode() : 1) ^ result); }
//--------------------// #region Dispose /// <inheritdoc/> protected override void OnDispose() { try { if (_ownMesh) { Mesh?.Dispose(); } if (_asset != null) { _asset.ReleaseReference(); _asset = null; } if (_separateMaterials) { foreach (var material in Materials) { material.ReleaseReference(); } } } finally { base.OnDispose(); } }
public Tractor() { count++; if (meshFixed == null) { meshFixed = new XMesh("data\\tractor_fixed.x"); } if (meshFrontWheels == null) { meshFrontWheels = new XMesh("data\\tractor_frontwheels.x"); } if (meshRearWheels == null) { meshRearWheels = new XMesh("data\\tractor_rearwheels.x"); } this.Mesh = meshFixed; this.Scale = new Microsoft.DirectX.Vector3(0.7f, 0.7f, 0.7f); frontWheels.Mesh = meshFrontWheels; frontWheels.Position = new Microsoft.DirectX.Vector3(0, -1.5503f, 2.3186f); rearWheels.Mesh = meshRearWheels; rearWheels.Position = new Microsoft.DirectX.Vector3(0, -0.8669f, -1.7969f); this.AddChild(frontWheels); this.AddChild(rearWheels); this.Position = new Vector3(X1, 0, Z1); this.OrientationMode = OrientationModeEnum.YXZ; }
private string AddFileToProject(string filename) { string openFile = Program.Instance.OpenFile; if (openFile != null) { string projectFolder = string.Concat(new FileInfo(openFile).DirectoryName, "\\"); string newFileName = new FileInfo(filename).Name; string copiedName = string.Concat(projectFolder, newFileName); if (!File.Exists(copiedName)) { File.Copy(filename, copiedName); if (filename.EndsWith(".x")) { XMesh mesh = new XMesh(filename); for (int i = 0; i < mesh.NumberTextures; i++) { TextureBase texture = mesh.GetTexture(i); if ((texture != null) && (!string.IsNullOrEmpty(texture.FileName))) { AddFileToProject(texture.FileName); } } mesh.Dispose(); } } return(copiedName); } else { return(null); } }
public Flag(Vector3 position) { count++; pole = new GameObject(); flag = new GameObject(); if (meshPole == null) { meshPole = new XMesh("data/flagpole.x"); } if (meshFlag == null) { meshFlag = new XMesh("data/flag.x"); flagShader = new ShaderBase("FlagShader", "data/flag.fx"); flagShader.SetVariable("matWorldViewProj", ShaderBase.ShaderParameters.WorldProjection); flagShader.SetVariable("matWorld", ShaderBase.ShaderParameters.World); flagShader.SetVariable("vecLightDir", new Vector3(-0.3529871f, 0.8714578f, -0.3405313f)); flagShader.SetTechnique("TFlag"); } //this.Position = new Microsoft.DirectX.Vector3((count % 2 == 0)?5f:-5f, 0, (count % 2 == 0)?50f:20f); this.Position = position; pole.Mesh = meshPole; pole.Scale = new Vector3(2, 2, 2); flag.Mesh = meshFlag; flag.Position = new Vector3(0, 2.3f, 0); flag.Shader = flagShader; this.AddChild(pole); this.AddChild(flag); this.Scale = new Microsoft.DirectX.Vector3(2f, 2f, 2f); if (!string.IsNullOrEmpty(oldTexture) && !string.IsNullOrEmpty(newTexture)) { meshFlag.ReplaceTexture(oldTexture, newTexture, true); flagShader.SetVariable("Texture", meshFlag.GetTexture(0)); } }
public override string ToString() { return(GetType().Name + "." + (XMesh != null ? XMesh.ToString() : "null") + (Mesh != null ? Mesh.ToString() : "null") + (Texture != null ? Texture.ToString() : "null") + World + IsBillboard + (SkinnedMesh != null ? SkinnedMesh.ToString() : "null")); }
protected void SetMeshFile(string filename) { if (Mesh != null) { Mesh.Dispose(); Mesh = null; } Mesh = new XMesh(filename, airplaneControl.AircraftParameters.FolderName); this.Name = Utility.GetFileNamePart(filename); }
/// <summary> /// Clean up. /// </summary> public override void Dispose() { count--; if (count == 0) { meshWindSock.Dispose(); meshWindSock = null; flagShader.Dispose(); flagShader = null; } }
public override int GetHashCode() { return(GetType().GetHashCode() ^ (Layout != null ? Layout.GetHashCode() : 1) ^ (MeshDescription != null ? MeshDescription.GetHashCode() : 1) ^ (MetaMesh != null ? MetaMesh.GetHashCode() : 1) ^ (Mesh != null ? Mesh.GetHashCode() : 1) ^ (XMesh != null ? XMesh.GetHashCode() : 1) ^ (MetaXMesh != null ? MetaXMesh.GetHashCode() : 1) ^ XMeshFlags.GetHashCode() ^ Pool.GetHashCode()); }
/// <summary> /// Create Direct3D device and swap chain /// </summary> protected void InitDevice() { device = D3DDevice.CreateDeviceAndSwapChain(directControl.Handle); swapChain = device.SwapChain; SetViews(); meshManager = new XMeshManager(device); mesh = meshManager.Open("Media\\Tiger\\tiger.x"); InitMatrices(); }
/// <summary> /// Clean up. /// </summary> public override void Dispose() { count--; if (count == 0) { meshFixed.Dispose(); meshFixed = null; meshTurbine.Dispose(); meshTurbine = null; meshBlades.Dispose(); meshBlades = null; } }
/// <summary> /// Clean up. /// </summary> public override void Dispose() { count--; if (count == 0) { meshFixed.Dispose(); meshFixed = null; meshFrontWheels.Dispose(); meshFrontWheels = null; meshRearWheels.Dispose(); meshRearWheels = null; } }
/// <summary> /// Create Direct3D device and swap chain /// </summary> public void InitDevice() { device = D3DDevice.CreateDeviceAndSwapChain(host.Handle); swapChain = device.SwapChain; SetViews(); meshManager = new XMeshManager(device); mesh = meshManager.Open("Media\\Tiger\\tiger.x"); InitMatrices(); needsResizing = false; }
protected void SetMeshFile(string filename) { if (Mesh != null) { Mesh.Dispose(); Mesh = null; } if (!string.IsNullOrEmpty(filename)) { Mesh = new XMesh(filename, airplaneControl.AircraftParameters.FolderName); XMesh xMesh = (XMesh)(this.Mesh); xMesh.ComputeBoundingBox(); } }
public CollisionMesh(GameObject gameObject, XMesh xmesh) { this.gameObject = gameObject; if (xmesh != null) { this.mesh = xmesh.SystemMesh; } else { this.mesh = (gameObject.Mesh as XMesh).SystemMesh; } // Register the collision mesh. CollisionManager.AddCollisionMesh(this); }
/// <inheritdoc/> public override void Initialize() { base.Initialize(); // Prepare painting brush meshes Scene.Positionables.Add(_terrainPaintingBrushCircle = new Model(XMesh.Get(Engine, "Engine/Circle.x")) { Visible = false }); Scene.Positionables.Add(_terrainPaintingBrushSquare = new Model(XMesh.Get(Engine, "Engine/Rectangle.x")) { Visible = false }); }
/// <summary> /// Clean up. /// </summary> public override void Dispose() { count--; if (count == 0) { meshPole.Dispose(); meshPole = null; meshFlag.Dispose(); meshFlag = null; flagShader.Dispose(); flagShader = null; } pole.Dispose(); flag.Dispose(); }
/// <summary> /// Default constructor. /// </summary> public ScareCrow() { cornMesh = new XMesh("data/corn.x"); cornfields.Add(new CornField(cornMesh, new Vector3(30, 0, 30))); cornfields.Add(new CornField(cornMesh, new Vector3(-30, 0, 30))); cornfields.Add(new CornField(cornMesh, new Vector3(-30, 0, -30))); birds = new Birds(100); birds.Random = false; birds.SetRandomTarget(); arrow = new GameObject(); arrow.Mesh = new XMesh("data\\arrow.x"); startTime = 0; }
/// <inheritdoc/> public override void Initialize() { base.Initialize(); if (_preCachedAssets == null) { // Preload selection highlighting meshes _preCachedAssets = new Asset[] { XMesh.Get(Engine, "Engine/Circle.x"), XMesh.Get(Engine, "Engine/Rectangle.x") }; foreach (var asset in _preCachedAssets) { asset.HoldReference(); } } _selectionsSync.Register <Entity, PositionableRenderable>(GetSelectionHighlighting, UpdateRepresentationShifted); _selectionsSync.Initialize(); }
public WindSock() { count++; if (meshWindSock == null) { meshWindSock = new XMesh("flag.x"); flagShader = new ShaderBase("FlagShader", "flag.fx"); flagShader.SetVariable("matWorldViewProj", ShaderBase.ShaderParameters.WorldProjection); flagShader.SetVariable("matWorld", ShaderBase.ShaderParameters.World); flagShader.SetTechnique("TVertexShaderOnly"); //texture = new TextureBase("EuropeFlag.jpg"); //flagShader.SetVariable("Tex0", texture); } this.Mesh = meshWindSock; this.Shader = flagShader; this.Scale = new Microsoft.DirectX.Vector3(1f, 1f, 1f); this.Position = new Microsoft.DirectX.Vector3(1, 1, 0); }
private void LoadMesh(string filename) { lock (meshLock) { if (mesh != null) { mesh.Dispose(); mesh = null; } worldMatrix = Matrix4x4F.Identity; XMesh meshT = meshManager.Open(filename); meshT.ShowWireFrame = cbWireframe.Checked; mesh = meshT; }; }
/// <summary> /// Adds the selection highlighting for a <see cref="EntityBase{TCoordinates,TTemplate}"/> /// </summary> /// <param name="entity">The <see cref="EntityBase{TCoordinates,TTemplate}"/> to add the selection highlighting for</param> private Model GetSelectionHighlighting(Entity entity) { if (entity.TemplateData.Collision == null) { return(null); } var selectionHighlight = new PerTypeDispatcher <Collision <Vector2>, Model>(ignoreMissing: true) { (Circle circle) => { // Create a circle around the entity based on the radius var hightlight = new Model(XMesh.Get(Engine, "Engine/Circle.x")); float scale = circle.Radius / 20 + 1; hightlight.PreTransform = Matrix.Scaling(scale, 1, scale); return(hightlight); }, (Box box) => { // Create a rectangle around the entity based on the box corners var highlight = new Model(XMesh.Get(Engine, "Engine/Rectangle.x")); // Determine the component-wise minimums and maxmimums and the absolute difference var min = new Vector2( Math.Min(box.Minimum.X, box.Maximum.X), Math.Min(box.Minimum.Y, box.Maximum.Y)); var max = new Vector2( Math.Max(box.Minimum.X, box.Maximum.X), Math.Max(box.Minimum.Y, box.Maximum.Y)); var diff = max - min; highlight.PreTransform = Matrix.Scaling(diff.X, 1, diff.Y) * Matrix.Translation(min.X, 0, -min.Y); return(highlight); } }.Dispatch(entity.TemplateData.Collision); if (selectionHighlight != null) { selectionHighlight.Name = entity.Name + " Selection"; } return(selectionHighlight); }
/// <inheritdoc/> protected override void RegisterRenderablesSync() { base.RegisterRenderablesSync(); RenderablesSync.Register( (Waypoint waypoint) => new Model(XMesh.Get(Engine, "Engine/Waypoint.x")) { Scale = new Vector3(100) }, UpdateRepresentation); RenderablesSync.Register( (Trigger trigger) => { var area = Model.Cylinder(Engine, XTexture.Get(Engine, "flag.png"), radiusBottom: trigger.Range, radiusTop: trigger.Range, length: 150); area.Rotation = Quaternion.RotationYawPitchRoll(0, (float)Math.PI / 2, 0); area.Alpha = 160; return(area); }, UpdateRepresentation); }
public AirplaneModel(IAirplaneControl airplaneControl) { this.airplaneControl = airplaneControl; if (!string.IsNullOrEmpty(airplaneControl.AircraftParameters.FixedMesh)) { this.Mesh = new XMesh(airplaneControl.AircraftParameters.FixedMesh, airplaneControl.AircraftParameters.FolderName); XMesh xMesh = (XMesh)(this.Mesh); xMesh.ComputeBoundingBox(); } this.Scale = new Vector3(airplaneControl.AircraftParameters.Scale, airplaneControl.AircraftParameters.Scale, airplaneControl.AircraftParameters.Scale); this.Position = new Vector3(0.0f, 10.0f, 0.0f); foreach (AircraftParameters.ControlSurface controlSurfaceDef in airplaneControl.AircraftParameters.ControlSurfaces) { ControlSurface surface = new ControlSurface(controlSurfaceDef, airplaneControl); AddChild(surface); } engineMinFreq = airplaneControl.AircraftParameters.EngineMinFrequency; engineMaxFreq = airplaneControl.AircraftParameters.EngineMaxFrequency; if (!string.IsNullOrEmpty(airplaneControl.AircraftParameters.EngineSound)) { engineSound = new SoundControllable(airplaneControl.AircraftParameters.EngineSound, airplaneControl.AircraftParameters.FolderName); } if ((airplaneControl.AircraftParameters.FlightModelType == AircraftParameters.FlightModelTypeEnum.Helicopter) && (airplaneControl.AircraftParameters.RotorSound != null)) { if (!string.IsNullOrEmpty(airplaneControl.AircraftParameters.RotorSound)) { rotorSound = new SoundControllable(airplaneControl.AircraftParameters.RotorSound, airplaneControl.AircraftParameters.FolderName); } } crashSound = new SoundControllable("data\\crash.wav"); if (!(airplaneControl is RecordedFlight)) { Birds.ScareCrow = this; } }
public void ApplyAds(string oldTexture, string newTexture) { try { foreach (SceneryObject o in sceneryObjects) { try { if (o.Mesh is XMesh) { XMesh xMesh = o.Mesh as XMesh; if (xMesh != null) { xMesh.ReplaceTexture(oldTexture, newTexture, true); } } } catch { } } Flag.ApplyAds(oldTexture, newTexture); #if EDITOR foreach (Gate o in gates) { if (o.Mesh is XMesh) { XMesh xMesh = o.Mesh as XMesh; if (xMesh != null) { xMesh.ReplaceTexture(oldTexture, newTexture, true); } } } #endif } catch { } }
private static XElement CreateElement(GameObject go, List <Material> mats) { XElement xe = new XElement(go.name); xe.Position = go.transform.localPosition.ToVec3(); xe.Euler = go.transform.localEulerAngles.ToVec3(); xe.Scale = go.transform.localScale.ToVec3(); MeshFilter mf = go.GetComponent <MeshFilter>(); if (mf != null) { Mesh m = mf.sharedMesh; Material[] ms = go.GetComponent <MeshRenderer>().sharedMaterials; for (int i = 0; i < m.subMeshCount; i++) { XMesh xm = new XMesh(); xm.Name = go.name + i; xm.Triangles = new List <int>(m.GetTriangles(i)); List <int> ct = xm.Triangles.Distinct().ToList(); xm.Vertexs = (from v in ct select m.vertices[v].ToVec3()).ToList(); xm.Normals = (from v in ct select m.normals[v].ToVec3()).ToList(); xm.UVs = (from v in ct select m.uv[v].ToXVec2()).ToList(); xm.Triangles = (from v in xm.Triangles select ct.IndexOf(v)).ToList(); xm.MaterialID = mats.IndexOf(ms[i]); if (xm.MaterialID < 0) { xm.MaterialID = mats.Count; mats.Add(ms[i]); } xe.Meshs.Add(xm); } } for (int i = 0; i < go.transform.childCount; i++) { xe.Children.Add(CreateElement(go.transform.GetChild(i).gameObject, mats)); } return(xe); }
public void OnPolymesh(PolymeshTopology node) { XMesh m = new XMesh(); m.Name = "Mesh" + XData.Objects.Last().Meshs.Count; Transform t = Transforms.Peek(); for (int i = 0; i < node.NumberOfFacets; i++) { PolymeshFacet f = node.GetFacet(i); m.Triangles.Add(f.V1); m.Triangles.Add(f.V2); m.Triangles.Add(f.V3); } for (int i = 0; i < node.NumberOfPoints; i++) { XYZ p = t.OfPoint(node.GetPoint(i)); m.Vertexs.Add(new XVec3(p.X, p.Y, p.Z)); } for (int i = 0; i < node.NumberOfNormals; i++) { XYZ n = t.OfVector(node.GetNormal(i)); m.Normals.Add(new XVec3(n.X, n.Y, n.Z)); } for (int i = 0; i < node.NumberOfUVs; i++) { UV u = node.GetUV(i); m.UVs.Add(new XVec2(u.U, u.V)); } m.MaterialID = matLink[matid]; XData.Objects.Last().Meshs.Add(m); string log = null; if (EnableLog) { log = "Mesh\n,Name:" + m.Name + "\nVertex:" + m.Vertexs.Count; } meshLog.Add(log); }
/// <summary> /// Creates a new model based upon a <see cref="XMesh"/>, using its internal material data if available. /// </summary> /// <param name="mesh">The <see cref="XMesh"/> providing the mesh data.</param> /// <remarks>Calling <see cref="IDisposable.Dispose"/> will not dispose the <paramref name="mesh"/>. This is handled by the <see cref="CacheManager"/>.</remarks> public Model(XMesh mesh) { #region Sanity checks if (mesh == null) { throw new ArgumentNullException(nameof(mesh)); } #endregion _asset = mesh; _asset.HoldReference(); // Get mesh from asset Mesh = mesh.Mesh; // Get materials from asset Materials = mesh.Materials; NumberSubsets = Materials.Length; // Get bounding bodies BoundingSphere = mesh.BoundingSphere; BoundingBox = mesh.BoundingBox; }