protected override void InitFromMeshData(Device device, GeometryGenerator.MeshData mesh) { var subset = new MeshGeometry.Subset { FaceCount = mesh.Indices.Count / 3, FaceStart = 0, VertexCount = mesh.Vertices.Count, VertexStart = 0 }; Subsets.Add(subset); var max = new Vector3(float.MinValue); var min = new Vector3(float.MaxValue); foreach (var vertex in mesh.Vertices) { max = Vector3.Maximize(max, vertex.Position); min = Vector3.Minimize(min, vertex.Position); } BoundingBox = new BoundingBox(min, max); Vertices.AddRange(mesh.Vertices.Select(v => new PosNormalTexTan(v.Position, v.Normal, v.TexC, v.TangentU)).ToList()); Indices.AddRange(mesh.Indices.Select(i => (short)i)); Materials.Add(new Material { Ambient = Color.Gray, Diffuse = Color.White, Specular = new Color4(16, 1, 1, 1) }); DiffuseMapSRV.Add(null); NormalMapSRV.Add(null); ModelMesh.SetSubsetTable(Subsets); ModelMesh.SetVertices(device, Vertices); ModelMesh.SetIndices(device, Indices); }
private void Update() { Indices.Clear(); LoadedInstanceIds.Clear(); LoadedGroupIds.Clear(); LoadedFileTypeIds.Clear(); foreach (DatabasePackedFile packed in PackageFiles) { Indices.AddRange(packed.Indices); } foreach (DatabaseIndex index in Indices) { if (!LoadedInstanceIds.Contains(index.InstanceId)) { LoadedInstanceIds.Add(index.InstanceId); } if (!LoadedGroupIds.Contains(index.GroupId)) { LoadedGroupIds.Add(index.GroupId); } if (!LoadedFileTypeIds.Contains(index.TypeId)) { LoadedFileTypeIds.Add(index.TypeId); } } Indices.Changed(); PackageFiles.Changed(); }
public void AddSprite(Texture2D texture, Point2 position, Rectangle sourceRectangle, TiledMapTileFlipFlags flipFlags) { Indices.AddRange(CreateTileIndices(Vertices.Count)); Debug.Assert(Indices.Count <= TiledMapHelper.MaximumIndicesPerModel); Vertices.AddRange(CreateVertices(texture, position, sourceRectangle, flipFlags)); Debug.Assert(Vertices.Count <= TiledMapHelper.MaximumVerticesPerModel); }
public CodeIndexExpression(CodeExpression targetObject, IEnumerable <CodeExpression> indices) { Ensure.That(nameof(targetObject)).IsNotNull(targetObject); Ensure.That(nameof(indices)).IsNotNull(indices); TargetObject = targetObject; Indices.AddRange(indices); }
public wCatmullRom() { Points.AddRange(new List <wPoint>() { StartPoint, StartControlPoint, EndControlPoint, EndPoint }); Indices.AddRange(new List <int>() { 0, 1, 2, 3 }); }
public wQuadraticBezier() { Points.AddRange(new List <wPoint>() { StartPoint, StartControlPoint, EndControlPoint, EndPoint }); Indices.AddRange(new List <int>() { 0, 1, 2, 3 }); }
public wLine(wPoint StartPoint, wVector Direction, double Length) { Points.AddRange(new List <wPoint>() { Start, End }); Indices.AddRange(new List <int>() { 0, 1 }); }
public wLine() { Points.AddRange(new List <wPoint>() { Start, End }); Indices.AddRange(new List <int>() { 0, 1 }); }
private void GenerateIndices() { List <VertexFormat> tempVertices = new List <VertexFormat>(); List <int> tempIndices = new List <int>(); foreach (VertexFormat v in Vertices) { //Search for existing vertex int i = 0; bool found = false; foreach (VertexFormat v2 in tempVertices) { if (VertexFormat.Compare(v, v2)) { //found found = true; break; } i++; } //In found join the normals if (found) { tempIndices.Add(i); VertexFormat v2 = tempVertices[i]; } else { i = tempVertices.Count; tempVertices.Add(v); tempIndices.Add(i); } //normal VertexFormat vTemp = tempVertices[i]; vTemp.Normal += v.Normal; tempVertices[i] = vTemp; } //Normalize all Vertices Vertices.Clear(); foreach (VertexFormat v in tempVertices) { v.Normal.Normalize(); Vertices.Add(v); } Vertices.AddRange(tempVertices); Indices.Clear(); Indices.AddRange(tempIndices); }
public wLine(double StartX, double StartY, double EndX, double EndY) { Start = new wPoint(StartX, StartY); End = new wPoint(EndX, EndY); Points.AddRange(new List <wPoint>() { Start, End }); Indices.AddRange(new List <int>() { 0, 1 }); Direction = new wVector(Start, End); }
public wLine(wPoint StartPoint, wPoint EndPoint) { Start = StartPoint; End = EndPoint; Points.AddRange(new List <wPoint>() { Start, End }); Indices.AddRange(new List <int>() { 0, 1 }); Direction = new wVector(Start, End); }
public wCatmullRom(wPoint PointStart, wPoint ControlPointStart, wPoint ControlPointEnd, wPoint PointEnd) { StartPoint = PointStart; EndPoint = PointEnd; StartControlPoint = ControlPointStart; EndControlPoint = ControlPointStart; Points.AddRange(new List <wPoint>() { StartPoint, StartControlPoint, EndControlPoint, EndPoint }); Indices.AddRange(new List <int>() { 0, 1, 2, 3 }); }
public wQuadraticBezier(wPoint PointStart, wPoint ControlPointStart, wPoint ControlPointEnd, wPoint PointEnd) { StartPoint = PointStart; EndPoint = PointEnd; StartControlPoint = ControlPointStart; EndControlPoint = ControlPointEnd; Points.AddRange(new List <wPoint>() { StartPoint, StartControlPoint, EndControlPoint, EndPoint }); Indices.AddRange(new List <int>() { 0, 1, 2, 3 }); }
public override void Tessellate() { // Create vertices. Vector3D normal = new Vector3D(0, 1, 0); for (int z = 0; z < _length; ++z) { for (int x = 0; x < _width; ++x) { AddVertex(new Point3D(x, 0, (_length - 1) - z), normal); // Invert z so that winding order is correct. } } // Create indices. for (int z = 0; z < _length - 1; ++z) { for (int x = 0; x < _width; ++x) { // Create vertex for degenerate triangle. if (x == 0 && z > 0) { AddIndex(((z + 0) * _width) + x); } AddIndex(((z + 0) * _width) + x); AddIndex(((z + 1) * _width) + x); // Create vertex for degenerate triangle. if (x == _width - 1 && z < _length - 2) { AddIndex(((z + 1) * _width) + x); } } } // It's easiest to define the plane in terms of a triangle strip, // and then convert it. Int32Collection newIndices = MeshUtility.ConvertTriangleStripToTriangleList(Indices); Indices.Clear(); Indices.AddRange(newIndices); }
public void CommitPrimitive(PrimitiveType mode, IEnumerable <int> indices = null) { var enumerable = indices?.Select(x => _primitiveIndices[x]) ?? _primitiveIndices; switch (mode) { case PrimitiveType.Triangles: Indices.AddRange(enumerable); break; case PrimitiveType.TriangleStrip: Indices.AddRange(GetTriangleStripEnumerable(enumerable)); break; case PrimitiveType.TriangleFan: Indices.AddRange(GetTriangleFanEnumerable(enumerable)); break; default: throw new NotImplementedException(); } }
private BasicModel(Device device, TextureManager11 textureManager, string filename, string texturePath, bool autoLoadTextures, bool flipUv, bool tex1By1) { var importer = new AssimpContext(); if (!importer.IsImportFormatSupported(Path.GetExtension(filename))) { throw new ArgumentException($"Model format {Path.GetExtension(filename)} is not supported. Cannot load {filename}.", nameof(filename)); } #if DEBUG var logStream = new ConsoleLogStream(); logStream.Attach(); #endif var postProcessFlags = PostProcessSteps.GenerateSmoothNormals | PostProcessSteps.CalculateTangentSpace; if (flipUv) { postProcessFlags |= PostProcessSteps.FlipUVs; } var model = importer.ImportFile(filename, postProcessFlags); var min = new Vector3(float.MaxValue); var max = new Vector3(float.MinValue); _meshCount = model.Meshes.Count; foreach (var mesh in model.Meshes) { var verts = new List <VertPosNormTexTan>(); var subset = new MeshSubset() { VertexCount = mesh.VertexCount, VertexStart = Vertices.Count, FaceStart = Indices.Count / 3, FaceCount = mesh.FaceCount }; Subsets.Add(subset); // bounding box corners for (var i = 0; i < mesh.VertexCount; i++) { var pos = mesh.HasVertices ? mesh.Vertices[i].ToVector3() : new Vector3(); min = MathF.Minimize(min, pos); max = MathF.Maximize(max, pos); var norm = mesh.HasNormals ? mesh.Normals[i] : new Vector3D(); var texC = mesh.HasTextureCoords(0) ? mesh.TextureCoordinateChannels[0][i] : (tex1By1 ? new Vector3D(1, 1, 0) : new Vector3D()); var tan = mesh.HasTangentBasis ? mesh.Tangents[i] : new Vector3D(); var v = new VertPosNormTexTan(pos, norm.ToVector3(), texC.ToVector2(), tan.ToVector3()); verts.Add(v); } Vertices.AddRange(verts); var indices = mesh.GetIndices().Select(i => i + subset.VertexStart).ToList(); Indices.AddRange(indices); var mat = model.Materials[mesh.MaterialIndex]; var material = mat.ToMaterial(); Materials.Add(material); if (autoLoadTextures) { TextureSlot diffuseSlot; mat.GetMaterialTexture(TextureType.Diffuse, 0, out diffuseSlot); var diffusePath = diffuseSlot.FilePath; if (Path.GetExtension(diffusePath) == ".tga") { // DirectX doesn't like to load tgas, so you will need to convert them to pngs yourself with an image editor diffusePath = diffusePath.Replace(".tga", ".png"); } var fullDiffusePath = diffusePath == null ? null : Path.Combine(texturePath, diffusePath); if (File.Exists(fullDiffusePath)) { DiffuseMapSRV.Add(textureManager.CreateTexture(fullDiffusePath)); } else { DiffuseMapSRV.Add(textureManager.CreateColor1By1(material.Diffuse.ToColor())); } TextureSlot normalSlot; mat.GetMaterialTexture(TextureType.Normals, 0, out normalSlot); var normalPath = normalSlot.FilePath; var fullNormalPath = normalPath == null ? null : Path.Combine(texturePath, normalPath); string textureName; if (File.Exists(fullNormalPath)) { textureName = fullNormalPath; } else { if (File.Exists(fullDiffusePath)) { var normalExt = Path.GetExtension(fullDiffusePath); normalPath = Path.GetFileNameWithoutExtension(diffusePath) + "_nmap" + normalExt; fullNormalPath = Path.Combine(texturePath, normalPath); if (File.Exists(fullNormalPath)) { textureName = fullNormalPath; } else { textureName = TextureManager11.TexDefaultNorm; } } else { textureName = TextureManager11.TexDefaultNorm; } } NormalMapSRV.Add(textureManager.CreateTexture(textureName)); } } BoundingBox = new BoundingBox(min, max); ModelMesh.SetSubsetTable(Subsets); ModelMesh.SetVertices(device, Vertices); ModelMesh.SetIndices(device, Indices); }
public CodeArrayIndexerExpression(ILInstruction inline, CodeExpression targetObject, params CodeExpression[] indices) : base(inline) { TargetObject = targetObject; Indices.AddRange(indices); }
public SkinnedModel(Device device, TextureManager texMgr, string filename, string texturePath, bool flipTexY = false) { var importer = new AssimpImporter(); #if DEBUG importer.AttachLogStream(new ConsoleLogStream()); importer.VerboseLoggingEnabled = true; #endif var model = importer.ImportFile(filename, PostProcessSteps.GenerateSmoothNormals | PostProcessSteps.CalculateTangentSpace); // Load animation data Animator = new SceneAnimator(); Animator.Init(model); // create our vertex-to-boneweights lookup var vertToBoneWeight = new Dictionary <uint, List <VertexWeight> >(); // create bounding box extents _min = new Vector3(float.MaxValue); _max = new Vector3(float.MinValue); foreach (var mesh in model.Meshes) { ExtractBoneWeightsFromMesh(mesh, vertToBoneWeight); var subset = new MeshGeometry.Subset { VertexCount = mesh.VertexCount, VertexStart = Vertices.Count, FaceStart = Indices.Count / 3, FaceCount = mesh.FaceCount }; Subsets.Add(subset); var verts = ExtractVertices(mesh, vertToBoneWeight, flipTexY); Vertices.AddRange(verts); // extract indices and shift them to the proper offset into the combined vertex buffer var indices = mesh.GetIndices().Select(i => (short)(i + (uint)subset.VertexStart)).ToList(); Indices.AddRange(indices); // extract materials var mat = model.Materials[mesh.MaterialIndex]; var material = mat.ToMaterial(); Materials.Add(material); // extract material textures var diffusePath = mat.GetTexture(TextureType.Diffuse, 0).FilePath; if (!string.IsNullOrEmpty(diffusePath)) { DiffuseMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, diffusePath))); } var normalPath = mat.GetTexture(TextureType.Normals, 0).FilePath; if (!string.IsNullOrEmpty(normalPath)) { NormalMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, normalPath))); } else { // for models created without a normal map baked, we'll check for a texture with the same // filename as the diffure texture, and _nmap suffixed // this lets us add our own normal maps easily var normalExt = Path.GetExtension(diffusePath); normalPath = Path.GetFileNameWithoutExtension(diffusePath) + "_nmap" + normalExt; if (File.Exists(Path.Combine(texturePath, normalPath))) { NormalMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, normalPath))); } } } BoundingBox = new BoundingBox(_min, _max); ModelMesh.SetSubsetTable(Subsets); ModelMesh.SetVertices(device, Vertices); ModelMesh.SetIndices(device, Indices); }
public BasicModel(Device device, TextureManager texMgr, string filename, string texturePath, bool flipUV = false) { var importer = new AssimpImporter(); if (!importer.IsImportFormatSupported(Path.GetExtension(filename))) { throw new ArgumentException("Model format " + Path.GetExtension(filename) + " is not supported! Cannot load {1}", "filename"); } #if DEBUG importer.AttachLogStream(new ConsoleLogStream()); importer.VerboseLoggingEnabled = true; #endif var postProcessFlags = PostProcessSteps.GenerateSmoothNormals | PostProcessSteps.CalculateTangentSpace; if (flipUV) { postProcessFlags |= PostProcessSteps.FlipUVs; } var model = importer.ImportFile(filename, postProcessFlags); var min = new Vector3(float.MaxValue); var max = new Vector3(float.MinValue); foreach (var mesh in model.Meshes) { var verts = new List <PosNormalTexTan>(); var subset = new MeshGeometry.Subset { VertexCount = mesh.VertexCount, VertexStart = Vertices.Count, FaceStart = Indices.Count / 3, FaceCount = mesh.FaceCount }; Subsets.Add(subset); // bounding box corners for (var i = 0; i < mesh.VertexCount; i++) { var pos = mesh.HasVertices ? mesh.Vertices[i].ToVector3() : new Vector3(); min = Vector3.Minimize(min, pos); max = Vector3.Maximize(max, pos); var norm = mesh.HasNormals ? mesh.Normals[i] : new Vector3D(); var texC = mesh.HasTextureCoords(0) ? mesh.GetTextureCoords(0)[i] : new Vector3D(); var tan = mesh.HasTangentBasis ? mesh.Tangents[i] : new Vector3D(); var v = new PosNormalTexTan(pos, norm.ToVector3(), texC.ToVector2(), tan.ToVector3()); verts.Add(v); } Vertices.AddRange(verts); var indices = mesh.GetIndices().Select(i => (short)(i + (uint)subset.VertexStart)).ToList(); Indices.AddRange(indices); var mat = model.Materials[mesh.MaterialIndex]; var material = mat.ToMaterial(); Materials.Add(material); var diffusePath = mat.GetTexture(TextureType.Diffuse, 0).FilePath; if (Path.GetExtension(diffusePath) == ".tga") { // DirectX doesn't like to load tgas, so you will need to convert them to pngs yourself with an image editor diffusePath = diffusePath.Replace(".tga", ".png"); } if (!string.IsNullOrEmpty(diffusePath)) { DiffuseMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, diffusePath))); } var normalPath = mat.GetTexture(TextureType.Normals, 0).FilePath; if (!string.IsNullOrEmpty(normalPath)) { NormalMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, normalPath))); } else { var normalExt = Path.GetExtension(diffusePath); normalPath = Path.GetFileNameWithoutExtension(diffusePath) + "_nmap" + normalExt; NormalMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, normalPath))); } } BoundingBox = new BoundingBox(min, max); ModelMesh.SetSubsetTable(Subsets); ModelMesh.SetVertices(device, Vertices); ModelMesh.SetIndices(device, Indices); }
public CodeArrayIndexerExpression(CodeExpression targetObject, params CodeExpression[] indices) { TargetObject = targetObject; Indices.AddRange(indices); }