static void Main() { System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-us"); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); if (!SharpDevice.IsDirectX11Supported()) { MessageBox.Show("DirectX11 feature level 11.0 is required to run Industrial Park. Maximum supported feature level is " + SharpDevice.GetSupportedFeatureLevel().ToString() + ". Please update your DirectX."); return; } MainForm = new MainForm(); if (!Directory.Exists(MainForm.userTemplatesFolder)) { Directory.CreateDirectory(MainForm.userTemplatesFolder); } ViewConfig = new ViewConfig(); AboutBox = new AboutBox(); UserTemplateManager = new UserTemplateManager(); EventSearch = new EventSearch(); AssetIDGenerator = new AssetIDGenerator(); Application.Run(MainForm); }
public void DrawPoints(SharpDevice Device, int count = int.MaxValue) { Device.DeviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.PointList; Device.DeviceContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(VertexBuffer, VertexSize, 0)); Device.DeviceContext.InputAssembler.SetIndexBuffer(IndexBuffer, Format.R32_UInt, 0); Device.DeviceContext.DrawIndexed(Math.Min(count, SubSets[0].IndexCount), 0, 0); }
private void AddPlane(SharpDevice device, PlaneSector_000A planeSection) { if (planeSection.leftSection is AtomicSector_0009 al) { AddAtomic(device, al); } else if (planeSection.leftSection is PlaneSector_000A pl) { AddPlane(device, pl); } else { throw new Exception(); } if (planeSection.rightSection is AtomicSector_0009 ar) { AddAtomic(device, ar); } else if (planeSection.rightSection is PlaneSector_000A pr) { AddPlane(device, pr); } else { throw new Exception(); } }
public void Draw(SharpDevice Device) { Device.DeviceContext.InputAssembler.PrimitiveTopology = primitiveTopology; Device.DeviceContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(VertexBuffer, VertexSize, 0)); Device.DeviceContext.InputAssembler.SetIndexBuffer(IndexBuffer, Format.R32_UInt, 0); Device.DeviceContext.DrawIndexed(IndexCount, 0, 0); }
private void AddPlane(SharpDevice device, PlaneSector_000A planeSection, List <string> materialList) { if (planeSection.leftSection is AtomicSector_0009 al) { AddAtomic(device, al, materialList); } else if (planeSection.leftSection is PlaneSector_000A pl) { AddPlane(device, pl, materialList); } else { throw new Exception(); } if (planeSection.rightSection is AtomicSector_0009 ar) { AddAtomic(device, ar, materialList); } else if (planeSection.rightSection is PlaneSector_000A pr) { AddPlane(device, pr, materialList); } else { throw new Exception(); } }
public void SetForRendering(SharpDevice device, RWSection[] rwChunkList, byte[] rwByteArray) { rwSectionArray = rwChunkList; if (rwByteArray == null) { rwSectionByteArray = ReadFileMethods.ExportRenderWareFile(rwSectionArray, 0x1400FFFF); if (rwSectionByteArray.Length > 450 * 1024) { System.Windows.Forms.MessageBox.Show(fileName + " is larger than 450 kb. I will still import it for you, but be warned that files too large might make the game crash."); } } else { rwSectionByteArray = rwByteArray; } meshList = new List <SharpMesh>(); vertexListG = new List <Vector3>(); triangleList = new List <Triangle>(); triangleListOffset = 0; foreach (RWSection rwSection in rwSectionArray) { if (rwSection is World_000B w) { vertexAmount = w.worldStruct.numVertices; triangleAmount = w.worldStruct.numTriangles; foreach (Material_0007 m in w.materialList.materialList) { if (m.texture != null) { MaterialList.Add(m.texture.diffuseTextureName.stringString); } else { MaterialList.Add(DefaultTexture); } } if (w.firstWorldChunk is AtomicSector_0009 a) { AddAtomic(device, a); } else if (w.firstWorldChunk is PlaneSector_000A p) { AddPlane(device, p); } } else if (rwSection is Clump_0010 c) { for (int g = 0; g < c.geometryList.geometryList.Count; g++) { AddGeometry(device, c.geometryList.geometryList[g], CreateMatrix(c.frameList, c.atomicList[g].atomicStruct.frameIndex)); } } } }
// indexed with subsets public static SharpMesh Create <VType>(SharpDevice device, VType[] vertices, int[] indices, List <SharpSubSet> SubSets, PrimitiveTopology topology = PrimitiveTopology.TriangleList) where VType : struct { return(new SharpMesh() { VertexBuffer = Buffer11.Create <VType>(device.Device, BindFlags.VertexBuffer, vertices), IndexBuffer = Buffer11.Create(device.Device, BindFlags.IndexBuffer, indices), VertexSize = Utilities.SizeOf <VType>(), SubSets = SubSets, primitiveTopology = topology, }); }
public void Draw(SharpDevice Device, int subset) { Device.DeviceContext.PixelShader.SetShaderResource(0, SubSets[subset].DiffuseMap); if (isTriStrip) { Device.DeviceContext.Draw(SubSets[subset].IndexCount, SubSets[subset].StartIndex); } else { Device.DeviceContext.DrawIndexed(SubSets[subset].IndexCount, SubSets[subset].StartIndex, 0); } }
// unindexed with subsets public static SharpMesh Create <VType>(SharpDevice device, VType[] vertices, List <SharpSubSet> SubSets, PrimitiveTopology topology = PrimitiveTopology.TriangleStrip) where VType : struct { return(new SharpMesh() { VertexBuffer = Buffer11.Create <VType>(device.Device, BindFlags.VertexBuffer, vertices), IndexBuffer = null, isTriStrip = true, VertexSize = Utilities.SizeOf <VType>(), SubSets = SubSets, IndexCount = 0, primitiveTopology = topology }); }
/// <summary> /// Constructor /// </summary> /// <param name="device">Device</param> /// <param name="filename">Path of shader file</param> /// <param name="description">Description structure</param> /// <param name="elements">Input Layout Elements</param> public SharpShader(SharpDevice device, string filename, SharpShaderDescription description, InputElement[] elements) { Device = device; // Compile Vertex and Pixel shaders var vertexShaderByteCode = ShaderBytecode.CompileFromFile(filename, description.VertexShaderFunction, "vs_5_0"); VertexShader = new VertexShader(Device.Device, vertexShaderByteCode); //create pixel shader if (!string.IsNullOrEmpty(description.PixelShaderFunction)) { var pixelShaderByteCode = ShaderBytecode.CompileFromFile(filename, description.PixelShaderFunction, "ps_5_0"); PixelShader = new PixelShader(Device.Device, pixelShaderByteCode); } if (!string.IsNullOrEmpty(description.GeometryShaderFunction)) { var geometryShaderByteCode = ShaderBytecode.CompileFromFile(filename, description.GeometryShaderFunction, "gs_5_0"); if (description.GeometrySO == null) { GeometryShader = new GeometryShader(Device.Device, geometryShaderByteCode); } else { int[] size = new int[] { description.GeometrySO.Select(e => e.ComponentCount * 4).Sum() }; GeometryShader = new GeometryShader(Device.Device, geometryShaderByteCode, description.GeometrySO, size, -1); } } if (!string.IsNullOrEmpty(description.DomainShaderFunction)) { var domainShaderByteCode = ShaderBytecode.CompileFromFile(filename, description.DomainShaderFunction, "ds_5_0"); DomainShader = new DomainShader(Device.Device, domainShaderByteCode); } if (!string.IsNullOrEmpty(description.HullShaderFunction)) { var hullShaderByteCode = ShaderBytecode.CompileFromFile(filename, description.HullShaderFunction, "hs_5_0"); HullShader = new HullShader(Device.Device, hullShaderByteCode); } var signature = ShaderSignature.GetInputSignature(vertexShaderByteCode); // Layout from VertexShader input signature Layout = new InputLayout(Device.Device, signature, elements); }
/// <summary> /// Load texture from file /// </summary> /// <param name="device">Device</param> /// <param name="filename">Filename</param> /// <returns>Shader Resource View</returns> public static ShaderResourceView LoadTextureFromFile(this SharpDevice device, string filename) { string ext = System.IO.Path.GetExtension(filename); if (ext.ToLower() == ".dds") { return(CreateTextureFromDDS(device.Device, device.DeviceContext, System.IO.File.ReadAllBytes(filename), out bool isCube)); } else if (ext.ToLower() == ".png") { return(CreateTextureFromBitmap(device.Device, filename)); } else { throw new Exception("Unsupported image format: " + filename); } }
public RenderWareModelFile(SharpDevice device, RWSection[] rwSectionArray) { meshList = new List <SharpMesh>(); vertexListG = new List <Vector3>(); triangleList = new List <Triangle>(); triangleListOffset = 0; List <string> materialList = new List <string>(); foreach (RWSection rwSection in rwSectionArray) { if (rwSection is World_000B w) { vertexAmount = w.worldStruct.numVertices; triangleAmount = w.worldStruct.numTriangles; foreach (Material_0007 m in w.materialList.materialList) { if (m.texture != null) { materialList.Add(m.texture.diffuseTextureName.stringString); } else { materialList.Add(DefaultTexture); } } if (w.firstWorldChunk is AtomicSector_0009 a) { AddAtomic(device, a, materialList); } else if (w.firstWorldChunk is PlaneSector_000A p) { AddPlane(device, p, materialList); } } else if (rwSection is Clump_0010 c) { for (int g = 0; g < c.geometryList.geometryList.Count; g++) { AddGeometry(device, c.geometryList.geometryList[g], CreateMatrix(c.frameList, c.atomicList[g].atomicStruct.frameIndex)); } } } }
public SharpRenderer(Control control) { try { device = new SharpDevice(control, false); } catch (Exception e) { MessageBox.Show("Error setting up DirectX11 renderer: " + e.Message); return; } LoadModels(); sharpFPS = new SharpFPS(); Camera.AspectRatio = (float)control.ClientSize.Width / control.ClientSize.Height; Camera.Reset(); ResetColors(); SetSharpShader(); LoadTexture(); ArchiveEditorFunctions.SetUpGizmos(); }
public static ShaderResourceView LoadTextureFromRenderWareNative(this SharpDevice device, TextureNativeStruct_0001 tnStruct) { Format format = Format.Unknown; if (tnStruct.compression == 0) { //if ((tnStruct.rasterFormatFlags & TextureRasterFormat.RASTER_C1555) != 0) // format = Format.B5G5R5A1_UNorm; //if ((tnStruct.rasterFormatFlags & TextureRasterFormat.RASTER_C4444) != 0) // format = Format.B4G4R4A4_UNorm; //else if ((tnStruct.rasterFormatFlags & TextureRasterFormat.RASTER_C555) != 0) // format = Format.B5G5R5A1_UNorm; if ((tnStruct.rasterFormatFlags & (TextureRasterFormat)0x0F00) == TextureRasterFormat.RASTER_C565) { format = Format.B5G6R5_UNorm; } if ((tnStruct.rasterFormatFlags & (TextureRasterFormat)0x0F00) == TextureRasterFormat.RASTER_C8888) { format = Format.B8G8R8A8_UNorm; } if ((tnStruct.rasterFormatFlags & (TextureRasterFormat)0x0F00) == TextureRasterFormat.RASTER_C888) { format = Format.B8G8R8A8_UNorm; } //else if ((tnStruct.rasterFormatFlags & TextureRasterFormat.RASTER_D16) != 0) // format = Format.D16_UNorm; //else if ((tnStruct.rasterFormatFlags & TextureRasterFormat.RASTER_D24) != 0) // format = Format.D24_UNorm_S8_UInt; //else if ((tnStruct.rasterFormatFlags & TextureRasterFormat.RASTER_D32) != 0) // format = Format.D32_Float; //else if ((tnStruct.rasterFormatFlags & TextureRasterFormat.RASTER_PAL4) != 0) // format = Format.P8; //else if ((tnStruct.rasterFormatFlags & TextureRasterFormat.RASTER_PAL8) != 0) // format = Format.P8; } else if (tnStruct.compression == 1) { format = Format.BC1_UNorm; } else { return(null); } if (format == Format.Unknown) { throw new Exception(tnStruct.textureName); } MipMapEntry[] mipMaps; if ((tnStruct.rasterFormatFlags & TextureRasterFormat.RASTER_PAL4) != 0 | (tnStruct.rasterFormatFlags & TextureRasterFormat.RASTER_PAL8) != 0) { mipMaps = ConvertFromPalette(tnStruct.mipMaps, tnStruct.mipMapCount, tnStruct.rasterFormatFlags, tnStruct.palette); if (tnStruct.platformType == 5) { format = Format.R8G8B8A8_UNorm; } } else { mipMaps = tnStruct.mipMaps; } DataRectangle[] dataRectangles = FillInitData(mipMaps, tnStruct.width, tnStruct.height, tnStruct.bitDepth, tnStruct.mipMapCount, format); Texture2D buffer = null; while (buffer == null) { try { buffer = new Texture2D(device.Device, new Texture2DDescription() { MipLevels = tnStruct.mipMapCount, Format = format, Width = tnStruct.width, Height = tnStruct.height, ArraySize = 1, BindFlags = BindFlags.ShaderResource, Usage = ResourceUsage.Default, SampleDescription = new SampleDescription(1, 0), OptionFlags = ResourceOptionFlags.None, }, dataRectangles); } catch { } } ShaderResourceView resourceView = new ShaderResourceView(device.Device, buffer); buffer.Dispose(); return(resourceView); }
private void AddNativeData(SharpDevice device, Extension_0003 extension, List <string> MaterialStream, Matrix transformMatrix) { isNativeData = true; NativeDataGC n = null; foreach (RWSection rw in extension.extensionSectionList) { if (rw is BinMeshPLG_050E binmesh) { if (binmesh.numMeshes == 0) { return; } } if (rw is NativeDataPLG_0510 native) { n = native.nativeDataStruct.nativeData; break; } } if (n == null) { throw new Exception(); } List <Vertex3> vertexList1 = new List <Vertex3>(); List <Vertex3> normalList = new List <Vertex3>(); List <RenderWareFile.Color> colorList = new List <RenderWareFile.Color>(); List <Vertex2> textCoordList = new List <Vertex2>(); foreach (Declaration d in n.declarations) { foreach (object o in d.entryList) { if (d.declarationType == Declarations.Vertex) { vertexList1.Add((Vertex3)o); } else if (d.declarationType == Declarations.Color) { colorList.Add((RenderWareFile.Color)o); } else if (d.declarationType == Declarations.TextCoord) { textCoordList.Add((Vertex2)o); } else if (d.declarationType == Declarations.Normal) { normalList.Add((Vertex3)o); } else { throw new Exception(); } } } List <VertexColoredTextured> vertexList = new List <VertexColoredTextured>(); List <int> indexList = new List <int>(); int k = 0; int previousAmount = 0; List <SharpSubSet> subSetList = new List <SharpSubSet>(); foreach (TriangleDeclaration td in n.triangleDeclarations) { foreach (TriangleList tl in td.TriangleListList) { foreach (int[] objectList in tl.entries) { Vector3 position = new Vector3(); SharpDX.Color color = new SharpDX.Color(255, 255, 255, 255); Vector2 textureCoordinate = new Vector2(); Vector3 normal = new Vector3(); for (int j = 0; j < objectList.Count(); j++) { if (n.declarations[j].declarationType == Declarations.Vertex) { position = (Vector3)Vector3.Transform( new Vector3( vertexList1[objectList[j]].X, vertexList1[objectList[j]].Y, vertexList1[objectList[j]].Z), transformMatrix); } else if (n.declarations[j].declarationType == Declarations.Color) { color = new SharpDX.Color(colorList[objectList[j]].R, colorList[objectList[j]].G, colorList[objectList[j]].B, colorList[objectList[j]].A); if (color.A == 0) { color = new SharpDX.Color(255, 255, 255, 255); } } else if (n.declarations[j].declarationType == Declarations.TextCoord) { textureCoordinate.X = textCoordList[objectList[j]].X; textureCoordinate.Y = textCoordList[objectList[j]].Y; } else if (n.declarations[j].declarationType == Declarations.Normal) { normal = new Vector3( normalList[objectList[j]].X, normalList[objectList[j]].Y, normalList[objectList[j]].Z); } } vertexList.Add(new VertexColoredTextured(position, textureCoordinate, color)); indexList.Add(k); k++; vertexListG.Add(position); } subSetList.Add(new SharpSubSet(previousAmount, vertexList.Count() - previousAmount, TextureManager.GetTextureFromDictionary(MaterialStream[td.MaterialIndex]), MaterialStream[td.MaterialIndex])); previousAmount = vertexList.Count(); } } if (vertexList.Count > 0) { for (int i = 2; i < indexList.Count; i++) { triangleList.Add(new Triangle(0, (ushort)(i + triangleListOffset - 2), (ushort)(i + triangleListOffset - 1), (ushort)(i + triangleListOffset))); } triangleListOffset += vertexList.Count; VertexColoredTextured[] vertices = vertexList.ToArray(); AddToMeshList(SharpMesh.Create(device, vertices, indexList.ToArray(), subSetList, SharpDX.Direct3D.PrimitiveTopology.TriangleStrip)); } else { AddToMeshList(null); } }
private void AddGeometry(SharpDevice device, Geometry_000F g, Matrix transformMatrix) { List <string> materialList = new List <string>(); foreach (Material_0007 m in g.materialList.materialList) { if (m.texture != null) { string textureName = m.texture.diffuseTextureName.stringString; materialList.Add(textureName); } else { materialList.Add(DefaultTexture); } } if ((g.geometryStruct.geometryFlags2 & GeometryFlags2.isNativeGeometry) != 0) { AddNativeData(device, g.geometryExtension, materialList, transformMatrix); return; } List <Vector3> vertexList1 = new List <Vector3>(); List <Vector3> normalList = new List <Vector3>(); List <Vector2> textCoordList = new List <Vector2>(); List <SharpDX.Color> colorList = new List <SharpDX.Color>(); if ((g.geometryStruct.geometryFlags & GeometryFlags.hasVertexPositions) != 0) { MorphTarget m = g.geometryStruct.morphTargets[0]; foreach (Vertex3 v in m.vertices) { Vector3 pos = (Vector3)Vector3.Transform(new Vector3(v.X, v.Y, v.Z), transformMatrix); vertexList1.Add(pos); vertexListG.Add(pos); } } if ((g.geometryStruct.geometryFlags & GeometryFlags.hasNormals) != 0) { for (int i = 0; i < vertexList1.Count; i++) { normalList.Add(new Vector3(g.geometryStruct.morphTargets[0].normals[i].X, g.geometryStruct.morphTargets[0].normals[i].Y, g.geometryStruct.morphTargets[0].normals[i].Z)); } } if ((g.geometryStruct.geometryFlags & GeometryFlags.hasVertexColors) != 0) { for (int i = 0; i < vertexList1.Count; i++) { RenderWareFile.Color c = g.geometryStruct.vertexColors[i]; colorList.Add(new SharpDX.Color(c.R, c.G, c.B, c.A)); } } else { for (int i = 0; i < vertexList1.Count; i++) { colorList.Add(new SharpDX.Color(1f, 1f, 1f, 1f)); } } if ((g.geometryStruct.geometryFlags & GeometryFlags.hasTextCoords) != 0) { for (int i = 0; i < vertexList1.Count; i++) { Vertex2 tc = g.geometryStruct.textCoords[i]; textCoordList.Add(new Vector2(tc.X, tc.Y)); } } else { for (int i = 0; i < vertexList1.Count; i++) { textCoordList.Add(new Vector2()); } } List <SharpSubSet> SubsetList = new List <SharpSubSet>(); List <int> indexList = new List <int>(); int previousIndexCount = 0; for (int i = 0; i < materialList.Count; i++) { foreach (Triangle t in g.geometryStruct.triangles) { if (t.materialIndex == i) { indexList.Add(t.vertex1); indexList.Add(t.vertex2); indexList.Add(t.vertex3); triangleList.Add(new Triangle(t.materialIndex, (ushort)(t.vertex1 + triangleListOffset), (ushort)(t.vertex2 + triangleListOffset), (ushort)(t.vertex3 + triangleListOffset))); } } if (indexList.Count - previousIndexCount > 0) { SubsetList.Add(new SharpSubSet(previousIndexCount, indexList.Count - previousIndexCount, TextureManager.GetTextureFromDictionary(materialList[i]), materialList[i])); } previousIndexCount = indexList.Count(); } triangleListOffset += vertexList1.Count; if (SubsetList.Count > 0) { VertexColoredTextured[] vertices = new VertexColoredTextured[vertexList1.Count]; for (int i = 0; i < vertices.Length; i++) { vertices[i] = new VertexColoredTextured(vertexList1[i], textCoordList[i], colorList[i]); } AddToMeshList(SharpMesh.Create(device, vertices, indexList.ToArray(), SubsetList)); } else { AddToMeshList(null); } }
private void AddAtomic(SharpDevice device, AtomicSector_0009 AtomicSector, List <string> MaterialList) { if (AtomicSector.atomicSectorStruct.isNativeData) { AddNativeData(device, AtomicSector.atomicSectorExtension, MaterialList, Matrix.Identity); return; } List <VertexColoredTextured> vertexList = new List <VertexColoredTextured>(); foreach (Vertex3 v in AtomicSector.atomicSectorStruct.vertexArray) { vertexList.Add(new VertexColoredTextured(new Vector3(v.X, v.Y, v.Z), new Vector2(), new SharpDX.Color())); vertexListG.Add(new Vector3(v.X, v.Y, v.Z)); } for (int i = 0; i < vertexList.Count; i++) { RenderWareFile.Color c = AtomicSector.atomicSectorStruct.colorArray[i]; VertexColoredTextured v = vertexList[i]; v.Color = new SharpDX.Color(c.R, c.G, c.B, c.A); vertexList[i] = v; } for (int i = 0; i < vertexList.Count; i++) { Vertex2 tc = AtomicSector.atomicSectorStruct.uvArray[i]; VertexColoredTextured v = vertexList[i]; v.TextureCoordinate = new Vector2(tc.X, tc.Y); vertexList[i] = v; } List <SharpSubSet> SubsetList = new List <SharpSubSet>(); List <int> indexList = new List <int>(); int previousIndexCount = 0; for (int i = 0; i < MaterialList.Count; i++) { for (int j = 0; j < AtomicSector.atomicSectorStruct.triangleArray.Length; j++) // each (Triangle t in AtomicSector.atomicStruct.triangleArray) { Triangle t = AtomicSector.atomicSectorStruct.triangleArray[j]; if (t.materialIndex == i) { indexList.Add(t.vertex1); indexList.Add(t.vertex2); indexList.Add(t.vertex3); triangleList.Add(new Triangle(t.materialIndex, (ushort)(t.vertex1 + triangleListOffset), (ushort)(t.vertex2 + triangleListOffset), (ushort)(t.vertex3 + triangleListOffset))); } } if (indexList.Count - previousIndexCount > 0) { SubsetList.Add(new SharpSubSet(previousIndexCount, indexList.Count - previousIndexCount, TextureManager.GetTextureFromDictionary(MaterialList[i]), MaterialList[i])); } previousIndexCount = indexList.Count(); } triangleListOffset += AtomicSector.atomicSectorStruct.vertexArray.Length; if (SubsetList.Count > 0) { AddToMeshList(SharpMesh.Create(device, vertexList.ToArray(), indexList.ToArray(), SubsetList)); } }